-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBnP.py
186 lines (137 loc) · 6.51 KB
/
BnP.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import numpy as np
import gurobipy as gp
from gurobipy import GRB
from models import SubProblem, MasterProblem
from itertools import combinations
from utils import get_min_dist, copy_model, copy_models, PriorityQueue
def branch_n_price(n, demands, capacity, distances, MasterProb):
queue = PriorityQueue()
MasterProb.RelaxOptimize()
obj_val = MasterProb.relax_modelo.ObjVal
queue.insert(obj_val,MasterProb)
best_int_obj = 1e3
best_relax_obj = 1e3
nodes_explored = 0
best_model = None
while not queue.isEmpty():
obj_val, MP_branch = queue.delete()
nodes_explored += 1
MP_branch.RelaxOptimize()
solution = MP_branch.getSolution()
duals = MP_branch.getDuals()
branch_cost = MP_branch.getCosts()
branch_routes = MP_branch.modelo.getA().toarray()
sol_is_int = all([float(round(s,4)).is_integer() for s in solution ])
# sol_is_int = all([False if i > 0.3 and np.abs(i - 1.0) > 0.3 else True for i in solution ])
if obj_val < best_int_obj and sol_is_int:
print(f"Best Integer Obj: {obj_val}")
print(f"Nodes explored: {nodes_explored}")
best_int_obj = obj_val
# print(f"best sol: {solution}")
best_model = copy_model(branch_cost, branch_routes, MP_branch)
if obj_val < best_relax_obj:
print(f"Best Relaxed Obj: {obj_val}")
print(f"Nodes explored: {nodes_explored}")
best_relax_obj = obj_val
# --- # --- # Column generation # --- # --- #
new_MP = column_generation(n, demands, capacity, distances, duals, MP_branch)
if new_MP != None :
new_MP.RelaxOptimize()
branch_cost = new_MP.getCosts()
branch_routes = new_MP.modelo.getA().toarray()
if new_MP.relax_modelo.ObjVal <= best_relax_obj:
queue.insert(new_MP.relax_modelo.ObjVal, copy_model(branch_cost, branch_routes, new_MP))
else:
# --- # If stopped col generation then branch if solution is not integer # --- #
if not sol_is_int:
# print("#--#--#--# Not integer solution ........Branching")
queue = branch(
branch_cost, branch_routes, n, demands, capacity, distances, duals, solution, MP_branch, queue, best_relax_obj
)
else:
# print(f"best sol: {solution}")
best_model = MP_branch
return best_model
def branch(branch_cost, branch_routes, n, demands, capacity, distances, duals, solution_to_branch, MP_to_copy, queue, best_inc_obj):
frac_ixs = []
for ix, val in enumerate(solution_to_branch):
if val > 0.0 and val < 1.0:
frac_ixs.append(ix)
A_mp = MP_to_copy.modelo.getA().toarray()
locations_index = list(MP_to_copy.locations_index)
for comb in combinations(frac_ixs, 2):
SP_1 = SubProblem(n, demands, capacity, distances, duals)
SP_2 = SubProblem(n, demands, capacity, distances, duals)
SP_1.build_model()
SP_2.build_model()
s1_and_s2 = [
True
if (
A_mp[i-1, comb[0]] == 1
and A_mp[i-1, comb[1]] == 1
)
else False
for i in range(len(MP_to_copy.locations_index))
]
s1_not_s2 = [
True
if (A_mp[i-1, comb[0]] == 1 and A_mp[i-1, comb[1]] == 0)
else False
for i in range(len(MP_to_copy.locations_index))
]
for i in locations_index:
locations_prime = [x for x in locations_index if x != i]
for j in locations_prime:
if (s1_and_s2[i - 1] and s1_not_s2[j - 1]):
# SP_1.modelo.addConstr(SP_1.y[i - 1] + SP_1.y[j - 1] == 2)
# SP_2.modelo.addConstr(SP_2.y[i - 1] + SP_2.y[j - 1] == 1)
SP_1.modelo.addConstr(SP_1.y[i - 1] == 1)
SP_1.modelo.addConstr(SP_1.y[j - 1] == 1)
SP_2.modelo.addConstr(SP_2.y[i - 1] == 1)
SP_2.modelo.addConstr(SP_2.y[j - 1] == 0)
MP_1, MP_2 = copy_models(branch_cost, branch_routes, MP_to_copy)
SP_1.modelo.update()
SP_1.optimize()
if SP_1.modelo.Status == 2:
newAssing = [SP_1.y[i].x for i in SP_1.y] # new Assingment
obj = get_min_dist(newAssing, distances) # Cost of new route
if obj + SP_1.modelo.ObjVal < 0.0:
newColumn = gp.Column(newAssing, MP_1.modelo.getConstrs())
MP_1.modelo.addVar(vtype=GRB.BINARY, obj=obj, column=newColumn)
MP_1.modelo.update()
MP_1.RelaxOptimize()
mp1_cost = MP_1.getCosts()
mp1_routes = MP_1.modelo.getA().toarray()
if MP_1.relax_modelo.ObjVal <= best_inc_obj:
queue.insert(MP_1.relax_modelo.ObjVal, copy_model(mp1_cost, mp1_routes, MP_1))
SP_2.modelo.update()
SP_2.optimize()
if SP_2.modelo.Status == 2:
newAssing = [SP_2.y[i].x for i in SP_2.y] # new Assingment
obj = get_min_dist(newAssing, distances) # Cost of new route
if obj + SP_2.modelo.ObjVal < 0.0:
newColumn = gp.Column(newAssing, MP_2.modelo.getConstrs())
MP_2.modelo.addVar(vtype=GRB.BINARY, obj=obj, column=newColumn)
MP_2.modelo.update()
MP_2.RelaxOptimize()
mp2_cost = MP_2.getCosts()
mp2_routes = MP_2.modelo.getA().toarray()
if MP_2.relax_modelo.ObjVal <= best_inc_obj:
queue.insert(MP_2.relax_modelo.ObjVal, copy_model(mp2_cost, mp2_routes, MP_2))
return queue
def column_generation(n, demands, capacity, distances, duals, MP_branch):
SP_branch = SubProblem(n, demands, capacity, distances, duals)
SP_branch.build_model()
SP_branch.optimize()
new_MP = None
newAssing = [SP_branch.y[i].x for i in SP_branch.y] # new route
obj = get_min_dist(newAssing, distances) # Cost of new route
if obj + SP_branch.modelo.ObjVal < 0.0:
newColumn = gp.Column(newAssing, MP_branch.modelo.getConstrs())
MP_branch.modelo.addVar(vtype=GRB.BINARY, obj=obj, column=newColumn)
MP_branch.modelo.update()
MP_branch.RelaxOptimize()
best_cost = MP_branch.getCosts()
routes = MP_branch.modelo.getA().toarray()
new_MP = copy_model(best_cost, routes, MP_branch)
return new_MP