usr bin env python3 import math import sys usr bin env python3 import

  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
#! /usr/bin/env python3
import math
import sys
# ! /usr/bin/env python3
import numpy as np
class Simplex:
def __init__(self, A, b, c, B_idx, N_idx=None):
self.A = np.matrix(A)
self.b = np.matrix(b)
self.c = np.matrix(c)
self.B_idx = B_idx
if N_idx is None:
self.N_idx = [i for i in range(self.A.shape[1]) if i not in self.B_idx]
else:
self.N_idx = N_idx
def getB(self):
return self.A[:, self.B_idx]
def getCb(self):
return self.c[:, self.B_idx]
def getCn(self):
return self.c[:, self.N_idx]
def getN(self):
return self.A[:, self.N_idx]
def calcZvar(self):
InvB = np.linalg.inv(self.getB())
N = self.getN()
Cb = self.getCb()
Cb_InvB = Cb @ InvB
Cb_InvB_b = Cb_InvB @ self.b.T
Cb_InvB_N = Cb_InvB @ N
bracadddinget_term = Cb_InvB_N - self.getCn()
return Cb_InvB_b, -1 * bracadddinget_term
def getVector(self):
X = self.computeX()
res = [0.0] * self.c.shape[1]
index = 0
for j in self.B_idx:
res[j] = X[index, 0]
index += 1
return res
def isOptimal(self, z):
return all([x <= 0 for x in np.nditer(z)])
def enteringVariable(self, z):
# pri('CHOOSE ENTERING')nt
max_ascent = -1 * np.inf
index = -1
it = np.nditer(z.T, flags=['f_index'])
while not it.finished:
# print(it[0])
if it[0] > max_ascent:
max_ascent = it[0]
index = it.index
it.iternext()
if index != -1:
InvB = np.linalg.inv(self.getB())
val = InvB @ self.A[:, self.N_idx[index]]
return index, val
else:
return None, None
def computeX(self):
InvB = np.linalg.inv(self.getB())
InvB_b = InvB @ self.b.T
return InvB_b
def exitingVariable(self, entering_pos, ev):
X = self.computeX()
# print('CHOOSE EXITING')
min_value = np.inf
index = -1
for idx, cr in enumerate(np.nditer([X, ev])):
x, y = cr[0], cr[1]
# print(x, y)
if y <= 0:
continue
ratio = x / y
if ratio < min_value:
min_value = ratio
index = idx
# print('ratio={}'.format(min_value))
if index != -1:
return index
else:
return None, None
def do(self, verbose=False):
while True:
z, z_var = self.calcZvar()
# print(z, z_var)
if self.isOptimal(z_var):
if verbose:
print("solution")
return z, self.getVector(), self.B_idx
entering_pos, val = self.enteringVariable(z_var)
if entering_pos is None:
# print('Entering None')
# print(self.getVector())
return None, None, None
exiting_pos = self.exitingVariable(entering_pos, val)
if exiting_pos is None:
# print('Exiting None')
# print(self.getVector())
return None, None, None
self.B_idx[exiting_pos] = self.N_idx[entering_pos]
self.N_idx = [i for i in range(self.A.shape[1]) if i not in self.B_idx]
def calc_simplex_max(orig_A, orig_b, orig_c):
A = []
for l in orig_A:
A.append(l.copy())
b = orig_b.copy()
c = [0.0] * len(A[0])
base = [i + len(A[0]) for i in range(len(b))]
for i, row in enumerate(A):
for j in range(len(b)):
if i == j:
row.append(1.0)
else:
row.append(0.0)
for _ in range(len(b)):
c.append(-1.0)
# print(A)
# print(b)
# print(c)
# print(base)
simplex = Simplex(A, b, c, base)
z, res, base = simplex.do()
if z is None:
return None, None
if any([x >= len(orig_c) for x in base]):
return None, None
A = []
for l in orig_A:
A.append(l.copy())
b = orig_b.copy()
c = orig_c.copy()
# c = simplex.c
# notbase = [x for x in notbase if x < len(orig_c)]
# print(A)
# print(b)
# print(c)
# print(base)
# print(notbase)
simplex = Simplex(A, b, c, base)
z, res, _ = simplex.do()
return z, res
def isAlmostEqual(x, y, epsilon=10 ** (-8)):
return abs(x - y) <= epsilon
def isAlmostEqualLists(x, y, epsilon=10 ** (-8)):
return all(isAlmostEqual(x_i, y_i, epsilon) for (x_i, y_i) in zip(x, y))
def isIntList(list):
rounded_list = [round(x) for x in list]
return isAlmostEqualLists(list, rounded_list)
def createBranch(x, index, A, b):
A1 = [condition + [0.] for condition in A]
new_A1 = [0.] * len(A1[0])
new_A1[index] = 1.
new_A1[-1] = 1.
A1.append(new_A1)
b1 = b.copy()
b1.append(math.floor(x[index]))
# print('Created: {} {}'.format(A1, b1))
A2 = [condition + [0.] for condition in A]
new_A2 = [0.] * len(A1[0])
# new_A2[index] = -1.
new_A2[index] = 1.
new_A2[-1] = -1.
A2.append(new_A2)
b2 = b.copy()
b2.append(math.floor(x[index]) + 1.)
# print('Created: {} {}'.format(A2, b2))
return A1, b1, A2, b2
def process(A, b, c, q, xs):
# print('')
# print(A)
# print(b)
# print(c)
try:
f_val, x_val = calc_simplex_max(A, b, c)
except IndexError:
print(len(A[0]), len(b), len(c))
raise IndexError
# print(f_val, x_val)
if f_val is None:
return
if isIntList(x_val):
q.append((f_val, x_val, A, b))
xs.append((f_val, x_val))
else:
q.append((f_val, x_val, A, b))
def branchsAndBounds(A, b, c, iterations_max=30):
k = 0
f_val, x_val = calc_simplex_max(A, b, c)
if f_val is None:
return None, None, None
q = [(f_val, x_val.copy(), A, b)]
xs = []
if isIntList(x_val):
return f_val, x_val, k
k = 0
while q:
# print("Q len = {}".format(len(q)))
x_values = [x[1] for x in q]
f_values = [x[0] for x in q]
max_f_value_index = f_values.index(max(f_values))
_, new_x, c_A, c_b = q.pop(max_f_value_index)
frac_parts = [i for i, x in enumerate(new_x) if divmod(x, 1)[1] > 0]
if not frac_parts:
continue
index = frac_parts[0]
A1, b1, A2, b2 = createBranch(new_x, index, c_A, c_b)
process(A1, b1, c + [0.0] * (len(A1[0]) - len(A[0])), q, xs)
process(A2, b2, c + [0.0] * (len(A2[0]) - len(A[0])), q, xs)
if k > iterations_max:
break
k += 1
# sys.stdin.readline()
f_values = [x[0] for x in xs]
print(f_values)
max_val = max(f_values)
max_x = xs[f_values.index(max(f_values))][1]
return max_val, max_x, k
def main():
# A = [[1, 3, 4, -1], [2, 5, 1, 1]]
# b = [23, 14]
# c = [4, 2, -1, 0]
A = [[1.0, 3.0, 4.0, 0.0], [2.0, 5.0, 7.0, 1.0]]
b = [28.0, 64.0]
c = [3.0, 4.0, -10.0, 0.0]
print(branchsAndBounds(A, b, c))
if __name__ == '__main__':
main()