diff --git a/doc/release-notes.md b/doc/release-notes.md index 67b1bdcb..a1c36d9c 100644 --- a/doc/release-notes.md +++ b/doc/release-notes.md @@ -150,6 +150,7 @@ linopy 1.0 makes the strict arithmetic convention the only behaviour and puts de - `to_netcdf` handles timezone-aware datetimes in indexes, coordinates and `Model.parameters`. ([\#1021](https://github.com/PyPSA/linopy/pull/1021)) - Object-dtype parameters, e.g. strings with `NaN` holes, round-trip through netcdf with their holes. Mixed-type object arrays raise a `TypeError` on write. +- MPS export raises for indicator constraints instead of silently dropping them. ([\#1186](https://github.com/PyPSA/linopy/issues/1186)) *Solvers* diff --git a/linopy/io.py b/linopy/io.py index 139683a8..a57aa57e 100644 --- a/linopy/io.py +++ b/linopy/io.py @@ -848,6 +848,11 @@ def matrices_to_file(M: Matrices, fn: Path, names: Printers, sos: list[SosSet]) raise RuntimeError( "Package highspy not installed. It is required to write matrices to file." ) + if M.indicator_b.size: + raise NotImplementedError( + "Indicator constraints cannot be written with HiGHS, which " + "linopy uses for MPS files. Use io_api='lp' or io_api='direct'." + ) h = solvers.Highs._build_solver_model(M, names=names, sos=sos) h.writeModel(str(fn)) diff --git a/test/test_indicator_constraints.py b/test/test_indicator_constraints.py index 7ab33d89..0900eb18 100644 --- a/test/test_indicator_constraints.py +++ b/test/test_indicator_constraints.py @@ -7,7 +7,8 @@ import pytest import linopy -from linopy import Constraint, Model, available_solvers, licensed_solvers +from linopy import Constraint, Model, SolverFeature, available_solvers, licensed_solvers +from linopy.solver_capabilities import solver_supports from linopy.variables import Variable requires_gurobi = pytest.mark.skipif( @@ -335,6 +336,15 @@ def test_indicator_only( assert "s.t." in content assert "= 1 ->" in content + def test_mps_raises( + self, mbx: tuple[Model, Variable, Variable], tmp_path: Path + ) -> None: + """MPS export refuses indicators instead of dropping them.""" + m, b, x = mbx + m.add_indicator_constraints(b, 1, x, "<=", 5, name="ic0") + with pytest.raises(NotImplementedError, match="Indicator constraints"): + m.to_file(tmp_path / "test.mps") + class TestSolve: """Solving models with indicator constraints.""" @@ -387,6 +397,24 @@ def test_gurobi_multiple(self) -> None: assert m.objective.value is not None assert np.isclose(m.objective.value, 5, atol=1e-6) + @pytest.mark.parametrize( + "solver", + [ + s + for s in licensed_solvers + if solver_supports(s, SolverFeature.INDICATOR_CONSTRAINTS) + ], + ) + def test_mps_solve_raises( + self, mbx: tuple[Model, Variable, Variable], solver: str + ) -> None: + """Solving via MPS raises instead of ignoring the indicator.""" + m, b, x = mbx + m.add_indicator_constraints(b, 1, x, "<=", 5, name="ic0") + m.add_objective(x, sense="max") + with pytest.raises(NotImplementedError, match="Indicator constraints"): + m.solve(solver_name=solver, io_api="mps") + def test_unsupported_solver_raises( self, mbx: tuple[Model, Variable, Variable] ) -> None: