-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheuristics.py
80 lines (71 loc) · 2.22 KB
/
heuristics.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
'''
Created on 25/lug/2013
@author: Davide Compagnin
'''
from decimal import Decimal
def nothing(_):
pass
def default(t):
return t.utilization()
def worst_fit(items, bins, capacity=Decimal(1), weight=default, misfit=nothing, empty_bin=list):
sets = [empty_bin() for _ in xrange(0, bins)]
sums = [Decimal(0) for _ in xrange(0, bins)]
for x in items:
c = weight(x)
# pick the bin where the item will leave the most space
# after placing it, aka the bin with the least sum
candidates = [s for s in sums if s + c <= capacity]
if candidates:
# fits somewhere
i = sums.index(min(candidates))
sets[i] += [x]
sums[i] += c
else:
misfit(x)
return sets
def best_fit(items, bins, capacity=Decimal(1), weight=default, misfit=nothing, empty_bin=list):
sets = [empty_bin() for _ in xrange(0, bins)]
sums = [Decimal(0) for _ in xrange(0, bins)]
for x in items:
c = weight(x)
# find the first bin that is sufficiently large
idxs = range(0, bins)
idxs.sort(key=lambda i: sums[i], reverse=True)
for i in idxs:
if sums[i] + c <= capacity:
sets[i] += [x]
sums[i] += c
break
else:
misfit(x)
return sets
def first_fit(items, bins, capacity=Decimal(1), weight=default, misfit=nothing,
empty_bin=list):
sets = [empty_bin() for _ in xrange(0, bins)]
sums = [Decimal(0) for _ in xrange(0, bins)]
for x in items:
c = weight(x)
for i in xrange(0, bins):
if sums[i] + c <= capacity:
sets[i] += [x]
sums[i] += c
break
else:
misfit(x)
return sets
def next_fit(items, bins, capacity=Decimal(1), weight=default, misfit=nothing,
empty_bin=list):
sets = [empty_bin() for _ in xrange(0, bins)]
cur = 0
s = Decimal(0)
for x in items:
c = weight(x)
while s + c > capacity:
s = Decimal(0)
cur += 1
if cur == bins:
misfit(x)
return sets
sets[cur] += [x]
s += c
return sets