Skip to content

Commit 85ca2a1

Browse files
committed
FIX: linprog_simplex: Fix bug in Phase 1
Fix #725
1 parent 63b80db commit 85ca2a1

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

quantecon/optimize/linprog_simplex.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,13 +452,31 @@ def solve_phase_1(tableau, basis, max_iter=10**6, piv_options=PivOptions()):
452452
See `solve_tableau`.
453453
454454
"""
455+
L = tableau.shape[0] - 1
456+
nm = tableau.shape[1] - (L+1) # n + m
457+
455458
success, status, num_iter_1 = \
456459
solve_tableau(tableau, basis, max_iter, skip_aux=False,
457460
piv_options=piv_options)
458461

459-
if success and tableau[-1, -1] > piv_options.fea_tol: # Infeasible
462+
if not success: # max_iter exceeded
463+
return success, status, num_iter_1
464+
if tableau[-1, -1] > piv_options.fea_tol: # Infeasible
460465
success = False
461466
status = 2
467+
return success, status, num_iter_1
468+
469+
# Check artifial variables have been eliminated
470+
tol_piv = piv_options.tol_piv
471+
for i in range(L):
472+
if basis[i] >= nm: # Artifial variable not eliminated
473+
for j in range(nm):
474+
if tableau[i, j] < -tol_piv or \
475+
tableau[i, j] > tol_piv: # Treated nonzero
476+
_pivoting(tableau, j, i)
477+
basis[i] = j
478+
num_iter_1 += 1
479+
break
462480

463481
return success, status, num_iter_1
464482

quantecon/optimize/tests/test_linprog_simplex.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,15 @@ def test_bug_8662(self):
239239
desired_fun = -36.0000000000
240240
res = linprog_simplex(c, A_ub=A_ub, b_ub=b_ub)
241241
_assert_success(res, c, b_ub=b_ub, desired_fun=desired_fun)
242+
243+
244+
class TestLinprogSimplex:
245+
def test_phase_1_bug_725(self):
246+
# Identified a bug in Phase 1
247+
# https://github.com/QuantEcon/QuantEcon.py/issues/725
248+
c = np.array([-4.09555556, 4.59044444])
249+
A_ub = np.array([[1, 0.1], [-1, -0.1], [1, 1]])
250+
b_ub = np.array([9.1, -0.1, 0.1])
251+
desired_x = [0.1, 0]
252+
res = linprog_simplex(c, A_ub=A_ub, b_ub=b_ub)
253+
_assert_success(res, c, b_ub=b_ub, desired_x=desired_x)

0 commit comments

Comments
 (0)