-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbranch_and_bound.py
More file actions
58 lines (46 loc) · 1.52 KB
/
branch_and_bound.py
File metadata and controls
58 lines (46 loc) · 1.52 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
from hill_climbing import *
import math
TIPOS = [ (1,3), (4,6), (5,7) ]
def estimate(state, types, best_type, max_size):
available_space = max_size - stateSize(state, types)
qtd_items = available_space / best_type[1]
qtd_items = math.ceil(qtd_items)
aux = qtd_items*best_type[0]
estimative = stateValue(state, types) + aux
return estimative
def get_ratio(type):
ratio = type[0] / type[1]
return ratio
def branch_and_bound(types, max_size):
queue = []
initial = hillClimb(TIPOS, 19)
queue.append([0 for i in types])
state = []
best_type = max(types, key=lambda x: get_ratio(x))
#print(best_type)
while len(queue) > 0:
validStates = []
state = queue.pop(0)
expandedStates = expandState(state)
validStates = list(
filter(
lambda state: stateIsValid(state, types, max_size),
expandedStates,
)
)
if not validStates:
return queue.pop()
bestStates = list(
filter(
lambda x:(estimate(x,types,best_type, max_size) > stateValue(initial, types)),
validStates,
)
)
# print(bestStates)
# print(queue)
queue += bestStates
queue.sort(key=lambda x: stateValue(x, types))
if __name__ == "__main__":
result = branch_and_bound(TIPOS, 19)
print(result)
print("Custo da solução: "+str(stateSize(result, TIPOS))+", Valor da solução: "+str(stateValue(result, TIPOS)))