diff --git a/linear_programming/simplex.py b/linear_programming/simplex.py index a8affe1b72d2..6762d4449506 100644 --- a/linear_programming/simplex.py +++ b/linear_programming/simplex.py @@ -302,7 +302,10 @@ def run_simplex(self) -> dict[Any, Any]: self.tableau = self.change_stage() else: self.tableau = self.pivot(row_idx, col_idx) - return {} + raise ValueError( + f"Simplex did not converge within {Tableau.maxiter} iterations. " + "The problem may be cycling or unbounded." + ) def interpret_tableau(self) -> dict[str, float]: """Given the final tableau, add the corresponding values of the basic diff --git a/tests/test_simplex.py b/tests/test_simplex.py new file mode 100644 index 000000000000..ca812e5d3634 --- /dev/null +++ b/tests/test_simplex.py @@ -0,0 +1,20 @@ +import numpy as np +import pytest + +from linear_programming.simplex import Tableau + + +def test_run_simplex_raises_when_max_iterations_are_exhausted(monkeypatch): + monkeypatch.setattr(Tableau, "maxiter", 0) + tableau = Tableau( + np.array([[-1.0, -1.0, 0.0, 0.0, 1.0], [1.0, 1.0, 1.0, 0.0, 2.0]]), + 2, + 0, + ) + + with pytest.raises( + ValueError, + match=r"Simplex did not converge within 0 iterations\. " + r"The problem may be cycling or unbounded\.", + ): + tableau.run_simplex()