Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*

Expand Down
5 changes: 5 additions & 0 deletions linopy/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
30 changes: 29 additions & 1 deletion test/test_indicator_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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:
Expand Down
Loading