-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapSack.py
More file actions
96 lines (73 loc) · 2.42 KB
/
KnapSack.py
File metadata and controls
96 lines (73 loc) · 2.42 KB
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
import random
class KnapSack:
def __init__(self, filename):
with open(filename, "r") as f:
lines = f.readlines()
self.nbObject = int(lines[0])
self.profits = lines[1].split()
self.weights = lines[2].split()
self.weightMax = lines[3]
self.solution = self.generateRandomSolution()
def fitness(self, sol=''):
"""
Return an evaluation of the current solution
"""
w = self.w(sol.split("-") if sol != '' else self.solution)
z = self.z(sol.split("-") if sol != '' else self.solution)
if w <= int(self.weightMax):
return z
else:
return float(z - self.penality() * (w - int(self.weightMax)))
def w(self, solution):
"""
Return the current weigh of the solution
"""
res = 0
for i in range(0, len(self.weights)):
res += int(self.weights[i]) * int(solution[i])
return res
def z(self, solution):
"""
Return the current profit of the solution
"""
res = 0
for i in range(0, len(self.profits)):
res += int(self.profits[i]) * int(solution[i])
return res
def penality(self):
"""
Return the higher ratio Profit / weight of the solution
"""
penMax = 0.0
for i in range(0, int(self.nbObject)):
penTmp = float(int(self.profits[i]) / int(self.weights[i]))
if penTmp > penMax:
penMax = penTmp
return penMax
def generateRandomSolution(self):
"""
Return a random solution
"""
bag = [0] * self.nbObject
for i in range(0, int(self.nbObject)):
bag[i] = int(bool(random.getrandbits(1)))
return bag
def getRandomNeighbor(self):
"""
Return a random neighbor of the current solution
"""
rdm1 = rdm2 = 0
while rdm1 == rdm2:
rdm1 = random.randint(0, int(self.nbObject) - 1)
rdm2 = random.randint(0, int(self.nbObject) - 1)
tmp = self.solution[rdm1]
self.solution[rdm1] = self.solution[rdm2]
self.solution[rdm2] = tmp
def toString(self):
"""
Return the solution with the OR-API format
"""
res = ''
for i in range(0, int(self.nbObject)):
res += str(self.solution[i]) + '-'
return res[:-1]