-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknapsack.py
98 lines (81 loc) · 3.24 KB
/
knapsack.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
import numpy as np
class Knapsack01Problem:
"""This class encapsulates the Knapsack 0-1 Problem from RosettaCode.org
"""
def __init__(self):
# initialize instance variables:
self.items = []
self.maxCapacity = 0
# initialize the data:
self.__initData()
def __len__(self):
"""
:return: the total number of items defined in the problem
"""
return len(self.items)
def __initData(self):
"""initializes the RosettaCode.org knapsack 0-1 problem data
"""
self.items = [
("map", 9, 150),
("compass", 13, 35),
("water", 153, 200),
("sandwich", 50, 160),
("glucose", 15, 60),
("tin", 68, 45),
("banana", 27, 60),
("apple", 39, 40),
("cheese", 23, 30),
("beer", 52, 10),
("suntan cream", 11, 70),
("camera", 32, 30),
("t-shirt", 24, 15),
("trousers", 48, 10),
("umbrella", 73, 40),
("waterproof trousers", 42, 70),
("waterproof overclothes", 43, 75),
("note-case", 22, 80),
("sunglasses", 7, 20),
("towel", 18, 12),
("socks", 4, 50),
("book", 30, 10)
]
self.maxCapacity = 400
def getValue(self, zeroOneList):
"""
Calculates the value of the selected items in the list, while ignoring items that will cause the accumulating weight to exceed the maximum weight
:param zeroOneList: a list of 0/1 values corresponding to the list of the problem's items. '1' means that item was selected.
:return: the calculated value
"""
totalWeight = totalValue = 0
for i in range(len(zeroOneList)):
item, weight, value = self.items[i]
if totalWeight + weight <= self.maxCapacity:
totalWeight += zeroOneList[i] * weight
totalValue += zeroOneList[i] * value
return totalValue
def printItems(self, zeroOneList):
"""
Prints the selected items in the list, while ignoring items that will cause the accumulating weight to exceed the maximum weight
:param zeroOneList: a list of 0/1 values corresponding to the list of the problem's items. '1' means that item was selected.
"""
totalWeight = totalValue = 0
for i in range(len(zeroOneList)):
item, weight, value = self.items[i]
if totalWeight + weight <= self.maxCapacity:
if zeroOneList[i] > 0:
totalWeight += weight
totalValue += value
print("- Adding {}: weight = {}, value = {}, accumulated weight = {}, accumulated value = {}".format(item, weight, value, totalWeight, totalValue))
print("- Total weight = {}, Total value = {}".format(totalWeight, totalValue))
# testing the class:
def main():
# create a problem instance:
knapsack = Knapsack01Problem()
# creaete a random solution and evaluate it:
randomSolution = np.random.randint(2, size=len(knapsack))
print("Random Solution = ")
print(randomSolution)
knapsack.printItems(randomSolution)
if __name__ == "__main__":
main()