From bbfd6e11bb4504626b0159c10c841c890a1001cd Mon Sep 17 00:00:00 2001 From: Robert Parker Date: Fri, 28 Jun 2024 20:54:12 -0600 Subject: [PATCH 1/4] raise descriptive runtimeerror instead of asserting --- pyomo/contrib/incidence_analysis/scc_solver.py | 9 ++++++++- .../tests/test_scc_solver.py | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pyomo/contrib/incidence_analysis/scc_solver.py b/pyomo/contrib/incidence_analysis/scc_solver.py index 378647c190c..ca501d88aa0 100644 --- a/pyomo/contrib/incidence_analysis/scc_solver.py +++ b/pyomo/contrib/incidence_analysis/scc_solver.py @@ -66,7 +66,14 @@ def generate_strongly_connected_components( ) ) - assert len(variables) == len(constraints) + if len(variables) != len(constraints): + nvar = len(variables) + ncon = len(constraints) + raise RuntimeError( + f"generate_strongly_connected_components only supports variables" + f" systems with the same numbers of variables and equality constraints." + f" Got {nvar} variables and {ncon} constraints." + ) if igraph is None: igraph = IncidenceGraphInterface() diff --git a/pyomo/contrib/incidence_analysis/tests/test_scc_solver.py b/pyomo/contrib/incidence_analysis/tests/test_scc_solver.py index b75f93e4a12..293d72ec1f0 100644 --- a/pyomo/contrib/incidence_analysis/tests/test_scc_solver.py +++ b/pyomo/contrib/incidence_analysis/tests/test_scc_solver.py @@ -501,5 +501,23 @@ def test_with_inequalities(self): self.assertEqual(m.x[3].value, 1.0) +@unittest.skipUnless(scipy_available, "SciPy is not available") +@unittest.skipUnless(networkx_available, "NetworkX is not available") +class TestExceptions(unittest.TestCase): + + def test_nonsquare_system(self): + m = pyo.ConcreteModel() + m.x = pyo.Var([1, 2], initialize=1) + m.eq = pyo.Constraint(expr=m.x[1] + m.x[2] == 1) + + msg = "Got 2 variables and 1 constraints" + with self.assertRaisesRegex(RuntimeError, msg): + list( + generate_strongly_connected_components( + constraints=[m.eq], variables=[m.x[1], m.x[2]] + ) + ) + + if __name__ == "__main__": unittest.main() From 74c4162a8807c917f1a53dc7d388445f911804f2 Mon Sep 17 00:00:00 2001 From: Robert Parker Date: Fri, 28 Jun 2024 20:57:28 -0600 Subject: [PATCH 2/4] add comment about assert --- pyomo/contrib/incidence_analysis/scc_solver.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pyomo/contrib/incidence_analysis/scc_solver.py b/pyomo/contrib/incidence_analysis/scc_solver.py index ca501d88aa0..508758cce1e 100644 --- a/pyomo/contrib/incidence_analysis/scc_solver.py +++ b/pyomo/contrib/incidence_analysis/scc_solver.py @@ -85,6 +85,8 @@ def generate_strongly_connected_components( subsets, include_fixed=include_fixed ): # TODO: How does len scale for reference-to-list? + # If this assert fails, it may be due to a bug in block_triangularize + # or generate_subsystem_block. assert len(block.vars) == len(block.cons) yield (block, inputs) From 5b594e71ec65646d4ec859c1101b53c3627e45e3 Mon Sep 17 00:00:00 2001 From: Robert Parker Date: Fri, 28 Jun 2024 20:58:33 -0600 Subject: [PATCH 3/4] remove whitespace --- pyomo/contrib/incidence_analysis/tests/test_scc_solver.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pyomo/contrib/incidence_analysis/tests/test_scc_solver.py b/pyomo/contrib/incidence_analysis/tests/test_scc_solver.py index 293d72ec1f0..ef4853d7e9a 100644 --- a/pyomo/contrib/incidence_analysis/tests/test_scc_solver.py +++ b/pyomo/contrib/incidence_analysis/tests/test_scc_solver.py @@ -504,7 +504,6 @@ def test_with_inequalities(self): @unittest.skipUnless(scipy_available, "SciPy is not available") @unittest.skipUnless(networkx_available, "NetworkX is not available") class TestExceptions(unittest.TestCase): - def test_nonsquare_system(self): m = pyo.ConcreteModel() m.x = pyo.Var([1, 2], initialize=1) From fe7941414d74b3991284355cc97fb48fc86179e2 Mon Sep 17 00:00:00 2001 From: Robert Parker Date: Wed, 10 Jul 2024 15:31:30 -0600 Subject: [PATCH 4/4] fix typo --- pyomo/contrib/incidence_analysis/scc_solver.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyomo/contrib/incidence_analysis/scc_solver.py b/pyomo/contrib/incidence_analysis/scc_solver.py index 508758cce1e..db201dccb0a 100644 --- a/pyomo/contrib/incidence_analysis/scc_solver.py +++ b/pyomo/contrib/incidence_analysis/scc_solver.py @@ -70,9 +70,9 @@ def generate_strongly_connected_components( nvar = len(variables) ncon = len(constraints) raise RuntimeError( - f"generate_strongly_connected_components only supports variables" - f" systems with the same numbers of variables and equality constraints." - f" Got {nvar} variables and {ncon} constraints." + "generate_strongly_connected_components only supports systems with the" + f" same numbers of variables and equality constraints. Got {nvar}" + f" variables and {ncon} constraints." ) if igraph is None: igraph = IncidenceGraphInterface()