Skip to content

Commit 039357c

Browse files
committed
Raise an error when simplex reaches max iterations
1 parent d0f9b6e commit 039357c

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

linear_programming/simplex.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,10 @@ def run_simplex(self) -> dict[Any, Any]:
302302
self.tableau = self.change_stage()
303303
else:
304304
self.tableau = self.pivot(row_idx, col_idx)
305-
return {}
305+
raise ValueError(
306+
f"Simplex did not converge within {Tableau.maxiter} iterations. "
307+
"The problem may be cycling or unbounded."
308+
)
306309

307310
def interpret_tableau(self) -> dict[str, float]:
308311
"""Given the final tableau, add the corresponding values of the basic

tests/test_simplex.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import numpy as np
2+
import pytest
3+
4+
from linear_programming.simplex import Tableau
5+
6+
7+
def test_run_simplex_raises_when_max_iterations_are_exhausted(monkeypatch):
8+
monkeypatch.setattr(Tableau, "maxiter", 0)
9+
tableau = Tableau(
10+
np.array([[-1.0, -1.0, 0.0, 0.0, 1.0], [1.0, 1.0, 1.0, 0.0, 2.0]]),
11+
2,
12+
0,
13+
)
14+
15+
with pytest.raises(
16+
ValueError,
17+
match=r"Simplex did not converge within 0 iterations\. "
18+
r"The problem may be cycling or unbounded\.",
19+
):
20+
tableau.run_simplex()

0 commit comments

Comments
 (0)