-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsack.py
More file actions
executable file
·39 lines (29 loc) · 1014 Bytes
/
Knapsack.py
File metadata and controls
executable file
·39 lines (29 loc) · 1014 Bytes
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
from Item import items_weight, items_List
import random
#from constants import tightnessFactor
class Knapsack:
def __init__(self, bagNumber: int, capacity = 3):
self.capacity = capacity
self.bagNumber = bagNumber
def setCapacity(self, tightnessFactor: float): #NAO ESTA SENDO USADA PARA OR LIBRARY
capacity = 0
for i in items_List:
capacity += i.weight[self.bagNumber]
capacity *= tightnessFactor
self.capacity = capacity
def firstGreedyChoice(cap: float):
bag = Knapsack(0)
weightSum = 0
i = 0
maxSize = len(items_List)
choice = []
while weightSum < sum(items_weight[0]):
weightSum += items_weight[0][i]
#bag.setCapacity(tightnessFactor) #SETA CAPACIDADE
if weightSum > bag.capacity:
break
choice.append(1)
i += 1
while maxSize > len(choice):
choice.append(0)
return items_weight[0][:i], items_List[:i], choice