-
Notifications
You must be signed in to change notification settings - Fork 2
/
simplex.py
237 lines (223 loc) · 6.87 KB
/
simplex.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
# encode=utf8
"""
## TODO
* Support box constraint
"""
from __future__ import print_function
import sys
import numpy as np
from base import Optimum
from lu.pf_update import PF
from lu.utils import copy_arr
def align_basis(x_b, basis, dim):
x0 = np.zeros(dim)
for i in range(len(basis)):
x0[basis[i]] = x_b[i]
return x0
def check_size(c, A, b, basis):
row, col = A.shape
if row != len(b) or col != len(c) or row != len(basis):
return False
else:
return True
def simplex_revised(c, A, b, basis, **argv):
"""
Revised simplex for Linear Programming
min c*x
s.t A*x = b,
x >= 0
Input:
c: object vector
A: equation constraint
b: equation constraint
basis: index of basis
eps: tolerance
max_iter: max number of iteration
Return:
success: Optimum
fail:
-1: illegal
-2: unbounded
-3: unsolved
"""
# argument
eps = argv.get("eps", 1e-16)
max_iter = argv.get("max_iter", 100)
debug = argv.get("debug", False)
ret_lu = argv.get("ret_lu", False)
is_neg = lambda x: x < -eps
is_pos = lambda x: x > eps
is_zero = lambda x: x <= eps and x >= -eps
# check problem
row, col = A.shape
if not check_size(c, A, b, basis):
info = "Size illegal c:%s A:%s,%s b:%s, basis:%s\n" % (len(c), row, col, len(b), len(basis))
sys.stderr.write(info)
return -1
# check primal feasible
if any(is_neg(i) for i in b):
sys.stderr.write("Basis illegal b:%s" % str(b))
return -1
basis = list(basis)
pf = PF()
pf.factor(A.take(basis, axis=1))
cir = 50
# basis variable
x_b = np.zeros(row)
lmbd = np.zeros(row)
z0 = 0
# iteration
for itr in range(max_iter):
nonbasis = [i for i in range(col) if i not in basis]
c_b = c[basis]
D = A.take(nonbasis, axis=1)
c_d = c[nonbasis]
# solve system B
copy_arr(b, x_b)
x_b = pf.ftrans(x_b)
copy_arr(c_b, lmbd)
lmbd = pf.btrans(lmbd)
r_d = c_d - lmbd.dot(D)
z0 = np.dot(x_b, c_b)
if debug:
print("\nIteration %d" % itr)
print("z\t%s" % z0)
print("basis\t%s" % str(basis))
print("x_b\t%s" % str(x_b))
print("lambda\t%s" % str(lmbd))
print("r_d\t%s" % str(r_d))
# check reduced cost
neg_ind = [i for i in range(len(r_d)) if is_neg(r_d[i])]
if len(neg_ind) == 0:
sys.stderr.write("Problem solved\n")
x_opt = align_basis(x_b, basis, col)
opt = Optimum(z_opt=z0, x_opt=x_opt, lmbd_opt=lmbd, basis=basis, x_basis=x_b, num_iter=itr)
if ret_lu:
opt.lu_factor = pf.lu_factor
return opt
ind_new = nonbasis[neg_ind[0]]
# pivot
a_q = A.take(ind_new, axis=1)
y_q = np.copy(a_q)
y_q = pf.ftrans(y_q)
pos_ind = [i for i in range(len(y_q)) if is_pos(y_q[i])]
if len(pos_ind) == 0:
sys.stderr.write("Problem unbounded\n")
return -2
ratio = [x_b[i] / y_q[i] for i in pos_ind]
min_ind = np.argmin(ratio)
out = pos_ind[min_ind]
ind_out = basis[out]
basis[out] = ind_new
if debug:
print("y_q\t%s" % str(y_q))
print("basis in %s out %s" % (ind_new, ind_out))
if (itr + 1) % cir == 0:
pf.factor(A.take(basis, axis=1))
else:
pf.update(y_q, out)
sys.stderr.write("Iteration exceed %s\n" % max_iter)
sys.stderr.write("Current optimum %s\n" % z0)
return -3
def simplex_dual(c, A, b, basis, **argv):
"""
Dual simplex for Linear Programming
min c*x
s.t A*x = b,
x >= 0
Input:
c: object vector
A: equation constraint
b: equation constraint
basis: index of basis
eps: tolerance
max_iter: max number of iteration
Return:
success: Optimum
fail:
-1: illegal
-2: unbounded
-3: unsolved
"""
# init
eps = argv.get("eps", 1e-16)
max_iter = argv.get("max_iter", 100)
debug = argv.get("debug", False)
ret_lu = argv.get("ret_lu", False)
is_neg = lambda x: x < -eps
is_pos = lambda x: x > eps
is_zero = lambda x: x <= eps and x >= -eps
# check problem
row, col = A.shape
if not check_size(c, A, b, basis):
info = "Size illegal c:%s A:%s,%s b:%s, basis:%s\n" % (len(c), row, col, len(b), len(basis))
sys.stderr.write(info)
return -1
basis = list(basis)
pf = PF()
pf.factor(A.take(basis, axis=1))
cir = 50
# basis variable
x_b = np.zeros(row)
lmbd = np.zeros(row)
z0 = 0
# iteration
for itr in range(max_iter):
nonbasis = [i for i in range(col) if i not in basis]
c_b = c[basis]
D = A.take(nonbasis, axis=1)
c_d = c[nonbasis]
# solve system B
copy_arr(b, x_b)
x_b = pf.ftrans(x_b)
copy_arr(c_b, lmbd)
lmbd = pf.btrans(lmbd)
r_d = c_d - lmbd.dot(D)
z0 = np.dot(x_b, c_b)
# check dual feasible
if any(is_neg(i) for i in r_d):
sys.stderr.write("Dual infeasible r_d:%s\n" % str(r_d))
return -1
if debug:
print("\nIteration %d" % itr)
print("z\t%s" % z0)
print("basis\t%s" % str(basis))
print("x_b\t%s" % str(x_b))
print("lambda\t%s" % str(lmbd))
print("r_d\t%s" % str(r_d))
# check x_b
neg_ind = [i for i in range(len(x_b)) if is_neg(x_b[i])]
if len(neg_ind) == 0:
sys.stderr.write("Problem solved\n")
x_opt = align_basis(x_b, basis, col)
opt = Optimum(z_opt=z0, x_opt=x_opt, lmbd_opt=lmbd, basis=basis, x_basis=x_b, num_iter=itr)
if ret_lu:
opt.lu_factor = pf.lu_factor
return opt
out = neg_ind[0]
ind_out = basis[out]
# pivot
e_q = np.zeros(row)
e_q[out] = 1
u_q = pf.btrans(e_q)
y_q = D.T.dot(u_q)
y_neg = [i for i in range(len(y_q)) if is_neg(y_q[i])]
if len(y_neg) == 0:
sys.stderr.write("Problem unbounded\n")
return -2
ratio = [r_d[i] / -y_q[i] for i in y_neg]
min_ind = np.argmin(ratio)
ind_new = nonbasis[y_neg[min_ind]]
basis[out] = ind_new
if (itr + 1) % cir == 0:
pf.factor(A.take(basis, axis=1))
else:
aqf = np.copy(A.take(ind_new, axis=1))
aqf = pf.ftrans(aqf)
pf.update(aqf, out)
if debug:
print("y_q\t%s" % str(y_q))
print("basis in %s out %s" % (ind_new, ind_out))
sys.stderr.write("Iteration exceed %s\n" % max_iter)
sys.stderr.write("Current optimum %s\n" % z0)
return -3