-
Notifications
You must be signed in to change notification settings - Fork 3
/
branch and bound.py
294 lines (257 loc) · 9.14 KB
/
branch and bound.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# Branch and Bound for Minimization IP, simplex from question 1 used from file simplex.py
import numpy as np
import copy
import sys
import math
from simplex import *
INPUT_PATH = "./Q2/"
OUTPUT_PATH = "./Q2/"
ip_file = INPUT_PATH + "input.txt"
op_file = OUTPUT_PATH + "output.txt"
def powerset(s):
x = len(s)
res = []
for i in range(1 << x):
res.append([s[j] for j in range(x) if (i & (1 << j))])
return res
# taking A as input, converting to cut set formulation of TSP and then to canonical form for two-phased simplex
def initialize_bb(ip_file):
inp = open(ip_file, "r")
inp.readline()
line = inp.readline()
initial_basis = []
variables = []
# reading matrix
num_col_A = 0
num_row_A = 0
num_artificial = 0
num_slack = 0
c = []
i = 0
num_nodes = 0
while(line.find("end A") == -1):
rowA = [float(i) for i in line.split(" ")]
num_nodes = len(rowA)
for j in range(len(rowA)):
c.append(rowA[j])
variables.append("x"+str(i)+str(j))
i += 1
line = inp.readline()
A = []
b = []
# sum of xij for ith node = 2, so we take >=2 and <=2
start = 0
for i in range(0, num_nodes):
rowA = [0.0] * len(variables)
for j in range(start, start+num_nodes):
rowA[j] = 1.0
A.append(rowA)
b.append(2.0)
num_slack+=1
for itr in range(0, len(A)):
A[itr].append(0.0)
A[-1][-1] = 1.0
variables.append("S" + str(num_slack))
initial_basis.append("S" + str(num_slack))
start += num_nodes
start = 0
for i in range(0, num_nodes):
rowA = [0.0] * len(variables)
for j in range(start, start+num_nodes):
rowA[j] = 1.0
A.append(rowA)
b.append(2.0)
num_slack+=1
num_artificial+=1
for i in range(0, len(A)):
A[i].append(0.0)
A[i].append(0.0)
# for surplus variable
A[-1][-1] = 1.0
A[-1][-2] = -1.0
variables.append("S" + str(num_slack))
variables.append("A" + str(num_artificial))
initial_basis.append("A" + str(num_artificial))
start += num_nodes
# xij <= 1
for i in range(0, num_nodes):
for j in range(0, num_nodes):
rowA = [0.0] * len(variables)
rowA[i*num_nodes+j] = 1.0
b.append(1.0)
A.append(rowA)
num_slack+=1
for itr in range(0, len(A)):
A[itr].append(0.0)
A[-1][-1] = 1.0
variables.append("S" + str(num_slack))
initial_basis.append("S" + str(num_slack))
# xii = 0, so we take >=0, <=0
for i in range(0, num_nodes):
rowA = [0.0] * len(variables)
rowA[i*num_nodes+i] = 1.0
b.append(0.0)
A.append(rowA)
num_slack+=1
for itr in range(0, len(A)):
A[itr].append(0.0)
A[-1][-1] = 1.0
variables.append("S" + str(num_slack))
initial_basis.append("S" + str(num_slack))
for i in range(0, num_nodes):
rowA = [0.0] * len(variables)
rowA[i*num_nodes+i] = 1.0
b.append(0.0)
A.append(rowA)
num_slack+=1
num_artificial+=1
for i in range(0, len(A)):
A[i].append(0.0)
A[i].append(0.0)
# for surplus variable
A[-1][-1] = 1.0
A[-1][-2] = -1.0
variables.append("S" + str(num_slack))
variables.append("A" + str(num_artificial))
initial_basis.append("A" + str(num_artificial))
# for all subsets, sum of x from S to N\S is >=2
subsets = powerset(range(0, num_nodes))
for sub in subsets:
if len(sub) != 0 and len(sub) != num_nodes:
not_sub = []
for val in range(0, num_nodes):
if val not in sub:
not_sub.append(val)
rowA = [0.0] * len(variables)
for i in sub:
for j in not_sub:
rowA[i*num_nodes+j] = 1.0
b.append(2.0)
A.append(rowA)
num_slack+=1
num_artificial+=1
for i in range(0, len(A)):
A[i].append(0.0)
A[i].append(0.0)
# for surplus variable
A[-1][-1] = 1.0
A[-1][-2] = -1.0
variables.append("S" + str(num_slack))
variables.append("A" + str(num_artificial))
initial_basis.append("A" + str(num_artificial))
num_col_A = len(A[0])
num_row_A = len(A)
# putting artificial variables at the end of tableau
k = len(variables) - 1
for j in range(0, len(A[0])):
while variables[k].find("A") != -1:
k-=1
if variables[j].find("A") != -1 and j < k and k >= 0:
for i in range(0, len(A)):
temp = A[i][j]
A[i][j] = A[i][k]
A[i][k] = temp
temp = variables[j]
variables[j] = variables[k]
variables[k] = temp
# print(variables)
return A, num_row_A, num_col_A, b, c, num_slack, num_artificial, initial_basis, variables
def print_bb_results(message, opt_val, opt_vect, num_x, num_cuts):
if message != "Optimal":
print(message)
print(num_cuts)
return
print(-round(opt_val, 6))
x = []
ind = 1
for key in opt_vect:
x.append(int(opt_vect[key]))
if ind >= num_x:
break
ind+=1
print(*x)
# print(num_cuts)
if __name__ == "__main__":
if len(sys.argv)>1:
ip_file = INPUT_PATH + sys.argv[1]
op_file = OUTPUT_PATH + sys.argv[1]
# get initial values
ini_A, num_row_A, num_col_A, ini_b, ini_c, ini_num_slack, ini_num_artificial, ini_B, ini_vars = initialize_bb(ip_file)
# print(A, b, c)
# print(num_row_A, num_col_A)
# print(num_slack, num_artificial)
# print(B, vars)
flag = 1
num_splits = 0
message, opt_val, opt_val_vector, basic_variables, x_B, c_j, A_matrix = lp_solve(copy.deepcopy(ini_A), copy.deepcopy(ini_b), copy.deepcopy(ini_c), copy.deepcopy(ini_B), copy.deepcopy(ini_vars), ini_num_artificial, ini_num_slack)
# print(message, opt_val, opt_val_vector, basic_variables, x_B, c_j, A_matrix)
# print()
# print(message, opt_val_vector, basic_variables, A_matrix, x_B, c_j)
while flag:
# check if decision vars are fractional
if message == "Optimal":
frac_var = ""
for key in opt_val_vector:
if opt_val_vector[key] != int(opt_val_vector[key]) and key.find("x") != -1:
frac_var = key
break
if frac_var == "":
flag = 0
sys.stdout = open(op_file, 'w')
print_bb_results(message, opt_val, opt_val_vector, num_col_A, num_splits)
sys.stdout.close()
break
else:
sys.stdout = open(op_file, 'w')
print_bb_results(message, opt_val, opt_val_vector, num_col_A, num_splits)
sys.stdout.close()
flag = 0
# updating A, b, c, B, vars, num_artificial, num_slack
num_splits+=1
ini_num_slack+=1
bs = copy.deepcopy(basic_variables)
xb = copy.deepcopy(x_B)
a = copy.deepcopy(A_matrix)
vars = copy.deepcopy(ini_vars)
basic_variables = copy.deepcopy(ini_B)
frac_key_ind = 0
x_B_left = ini_b
x_B_right = ini_b
frac_eq_ind = bs.index(frac_var)
arti_flag = 0
# making the split according to the variable value
for ind in range(0, len(c_j)):
if vars[ind] == frac_var:
frac_key_ind = ind
ini_num_slack += 1
vars.append("Sb"+str(num_splits))
basic_variables.append("Sb"+str(num_splits))
arti_flag = 1
ini_num_artificial += 1
vars.append("Ab"+str(num_splits))
basic_variables.append("Ab"+str(num_splits))
x_B_left.append(math.floor(xb[frac_eq_ind]))
x_B_right.append(math.ceil(xb[frac_eq_ind]))
break
c_j.append(0.0)
num_c = 0
A_matrix = ini_A
for i in range(0, len(A_matrix)):
A_matrix[i].append(0.0)
if arti_flag == 1:
A_matrix[i].append(0.0)
num_c = len(A_matrix[i])
new_constr = [0.0] * num_c
# ind = 0
new_constr[frac_key_ind] = 1.0
new_constr[-1] = 1.0
if arti_flag == 1:
new_constr[-2] = -1.0
A_matrix.append(new_constr)
# print(num_splits)
# print("new constr: ", new_constr)
message, opt_val, opt_val_vector, basic_variables, x_B, c_j, A_matrix = lp_solve(A_matrix, x_B_left, ini_c, basic_variables, vars, ini_num_artificial, ini_num_slack)
if message == "Infeasible":
message, opt_val, opt_val_vector, basic_variables, x_B, c_j, A_matrix = lp_solve(A_matrix, x_B_right, ini_c, basic_variables, vars, ini_num_artificial, ini_num_slack)
# print(message, opt_val, opt_val_vector, basic_variables, x_B, c_j, A_matrix)
# print()