-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path441_p2.py
127 lines (110 loc) · 3.18 KB
/
441_p2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 13 20:15:12 2017
@author: Zoey Lee
"""
import random
import time
"""
name # weight # value
#####################
A # 45 # 3
B # 40 # 5
C # 50 # 8
D # 90 # 10
MaxWeight = 100
genome = (tot (0/1 0/1 0/1 0/1))
"""
def genome(weights, values, MutationPct, MaxWeight):
res = []
cp = (MutationPct[1])[:]
V=0
W=0
for i in range(len(cp)):
cp[i] *= weights[i]
for j in range(len(cp)):
if cp[j]!=0:
V += values[j]
W += weights[j]
if W > MaxWeight:
V = 0
res.append([V,MutationPct[1]])
return res[len(MutationPct[1])-1]
def newPct(possible, PopulationSize, weights, values, MaxWeight):
ret = []
ret.append(possible)
for i in range(PopulationSize):
child = crossover(ret[random.randint(0,len(ret)-1)][1],ret[random.randint(0,len(ret)-1)][1])
pick = random.randint(0,1)
pick2 = random.randint(0,1)
if pick==1:
ret.append(genome(weights, values, [0,mutate(child[0])], MaxWeight))
else:
ret.append(genome(weights, values, [0,child[0]], MaxWeight))
if pick2==1:
ret.append(genome(weights, values, [0,mutate(child[1])], MaxWeight))
else:
ret.append(genome(weights, values, [0,child[1]], MaxWeight))
return largest(ret)
def largest(gn):
lar = [-10000,0]
for i in range(len(gn)):
if gn[i][0]>lar[0]:
lar = gn[i]
return lar
def delSmallHelper(gn):
small = 0
for i in range(len(gn)):
if gn[i][0]<gn[small][0]:
small = i
gn.pop(small)
return gn
def delSmall(gn, maxSize):
while len(gn)>=maxSize:
gn = delSmallHelper(gn)
return gn
def crossover(g1, g2):
randpoint = random.randint(0,len(g1)-1)
child1 = g1[:randpoint] + g2[randpoint:]
child2 = g2[:randpoint] + g1[randpoint:]
return [child1,child2]
def mutate(gn):
randpoint = random.randint(0,len(gn)-1)
gn[randpoint] = 0 if gn[randpoint]==1 else 1
return gn
def knapsack():
"""
PopulationSize = 3
NumIterations = 5
MaxWeight = 100
values = [3,5,8,10]
weights = [45,40,50,90]
"""
PopulationSize = 50
NumIterations = 50
MaxWeight = 6
weights = [1,3,2,3,2,3,3]
values = [2,100,5,3,50,16,60]
print("\nGiven item: ", end="")
for i in range(len(values)):
print("[",values[i],end=", ")
print(weights[i],end=" ]")
if i!=len(values)-1:
print(", ", end="")
print("\nKnapsack maximum weight: ", MaxWeight)
MutationPct = [0,[random.randint(0,1) for i in range(len(weights))]]
gn = genome(weights, values, MutationPct, MaxWeight)
MutationPct = gn[:]
ret=[]
i=0
while i < NumIterations:
i += 1
MutationPct = newPct(gn, PopulationSize, weights, values, MaxWeight)
ret.append(MutationPct)
ret = delSmall(ret, PopulationSize)
gn = genome(weights, values, MutationPct, MaxWeight)
return largest(ret)
start = time.time()
print("\nBest combination: ",knapsack())
end = time.time()
print("Time spent: %.2fsec" %float(end-start), end='\n\n')