From 847da03276438dea4c20fe2d418d197ba97c11e6 Mon Sep 17 00:00:00 2001 From: nataliemes Date: Wed, 7 Oct 2026 22:57:12 +0400 Subject: [PATCH 1/2] Update how runlemke() failure is checked --- src/lemke/bimatrix.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lemke/bimatrix.py b/src/lemke/bimatrix.py index 3fe447b..c71e387 100644 --- a/src/lemke/bimatrix.py +++ b/src/lemke/bimatrix.py @@ -341,7 +341,7 @@ def runLH(self, droppedlabel): # tabl.runlemke(verbose=True, lexstats=True, z0=gz0) result = lemke.runlemke(lcp=lcp) - if result is None: + if not result.success: raise RuntimeError("runlemke() failed to find a solution unexpectedly.") equilibrium = result.z[:-2] @@ -418,7 +418,7 @@ def runtrace(self, xprior, yprior): lcp.d = np.hstack((Ay, xB, [1, 1])) result = lemke.runlemke(lcp=lcp) - if result is None: + if not result.success: raise RuntimeError("runlemke() failed to find a solution unexpectedly.") equilibrium = result.z[:-2] From c3673e1071792e6f80b2b2e83567def10ef53f5b Mon Sep 17 00:00:00 2001 From: nataliemes Date: Wed, 7 Oct 2026 22:57:39 +0400 Subject: [PATCH 2/2] Add unit tests --- tests/test_bimatrix_units.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/test_bimatrix_units.py b/tests/test_bimatrix_units.py index d17c4fb..0dba553 100644 --- a/tests/test_bimatrix_units.py +++ b/tests/test_bimatrix_units.py @@ -1,3 +1,4 @@ +import re import textwrap from fractions import Fraction @@ -15,6 +16,7 @@ trace_uniform_cmd, uniform, ) +from lemke.lemke import LcpResult from lemke.randomstart import MAX_ACCURACY from lemke.utils import MAXDECIMALS @@ -185,6 +187,38 @@ def test_rejects_non_positive_num_priors(small_game_file, bad_priors): G.trace_random_priors(bad_priors) +# --- RUNLEMKE FAILURE ------------------------------------------------- +@pytest.fixture +def lemke_failure(monkeypatch): + failed_result = LcpResult( + success=False, + num_pivots=0, + basis=frozenset(), + z0=Fraction(-1), + z=(), + w=(), + ray_entering_variable=None, + ) + monkeypatch.setattr( + "lemke.lemke.runlemke", + lambda **kwargs: failed_result, + ) + + +def test_runLH_throws_on_lemke_failure(lemke_failure, small_game_file): + G = bimatrix.from_file(small_game_file) + + with pytest.raises(RuntimeError, match=re.escape("runlemke() failed")): + G.runLH(1) + + +def test_runtrace_throws_on_lemke_failure(lemke_failure, small_game_file): + G = bimatrix.from_file(small_game_file) + + with pytest.raises(RuntimeError, match=re.escape("runlemke() failed")): + G.runtrace([0.5, 0.5], [0.4, 0.6]) + + # --- HELPER FUNCTIONS -------------------------------------------------------- @pytest.mark.parametrize( "input, expected",