-
Notifications
You must be signed in to change notification settings - Fork 0
/
BestFit.py
93 lines (93 loc) · 2.32 KB
/
BestFit.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
from testParameters import *
class Item:
def __init__(self):
self.weight = 0
self.mark = False
def getWeight(self):
return self.weight
def getMark(self):
return self.mark
def setWeight(self, weight):
self.weight = weight
def setTrue(self):
self.mark = True
def setFalse(self):
self.mark = False
def __repr__(self):
return str(self)
def __str__(self):
return "item weight: " + str(self.weight)
class Bin:
def __init__(self):
#Set B of items in the bin and cap is the capacity of the bin
self.B = []
self.cap = bin_size
def getCapacity(self):
return self.cap
def getB(self):
return self.B
#packing function returns true or false to
def pack(self, item3):
if (self.cap - item3.getWeight()) < 0:
print ("item cannot be packed\n")
else:
self.B.append(item3.getWeight())
self.cap = self.cap - item3.getWeight()
def __str__(self):
strp = "Items in Bin: "
for item5 in self.B:
strp = strp + str(item5) + ", "
return strp
def __repr__(self):
return str(self)
class BinPacking:
def __init__(self, S, BinList):
#Set S of items to be packed in Set of Bins BinList
self.S = S
self.BinList = [Bin()]
def getBinList(self):
return self.BinList
def getS(self):
return self.S
def descending(self):
intar = []
for item in self.S:
intar.append(item.getWeight())
intar.sort(reverse = True)
self.S = []
for int in intar:
item = Item()
item.setWeight(int)
self.S.append(item)
def __str__(self):
strp = "Weight of each bin: "
for bin2 in self.BinList:
strp = strp + str(bin_size - bin2.getCapacity()) + ", "
return strp
def __repr__(self):
return str(self)
class BestFit (BinPacking):
def packItems(self):
binl = self.getBinList()
for item in self.S:
bestfit = binl[0]
for bin in binl:
if item.getWeight() <= bin.getCapacity() and (bestfit.getCapacity() > bin.getCapacity() or bestfit.getCapacity() < item.getWeight()):
bestfit = bin
if bestfit.getCapacity() < item.getWeight():
bestfit = Bin()
binl.append(bestfit)
bestfit.pack(item)
def returnBF(self):
boxContent=[]
count = 0
binl = self.getBinList()
for bin in binl:
newBoxContent=[]
for item in bin.getB():
# ici le contenu du bins
newBoxContent.append(item)
boxContent.append(newBoxContent)
count = count + 1
NB_bins = count
return NB_bins,boxContent