From cb6d79d419c804fcd4c095984944c77a726098f3 Mon Sep 17 00:00:00 2001 From: Robbie Muir Date: Thu, 1 May 2025 10:49:50 +0200 Subject: [PATCH 1/6] Add useful error message for missing highspy. Allow variable objective --- .gitignore | 3 +++ linopy/model.py | 5 ++++- linopy/solvers.py | 5 +++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 603fe6ed..3afe0fd0 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,6 @@ benchmark/notebooks/.ipynb_checkpoints benchmark/scripts/__pycache__ benchmark/scripts/benchmarks-pypsa-eur/__pycache__ benchmark/scripts/leftovers/ + +# IDE +.idea diff --git a/linopy/model.py b/linopy/model.py index 99fae982..f5c6dff4 100644 --- a/linopy/model.py +++ b/linopy/model.py @@ -683,7 +683,8 @@ def add_constraints( def add_objective( self, - expr: LinearExpression + expr: Variable + | LinearExpression | QuadraticExpression | Sequence[tuple[ConstantLike, VariableLike]], overwrite: bool = False, @@ -709,6 +710,8 @@ def add_objective( "Objective already defined." " Set `overwrite` to True to force overwriting." ) + if isinstance(expr, Variable): + expr = expr * 1.0 # Convert to expression self.objective.expression = expr # type: ignore[assignment] self.objective.sense = sense diff --git a/linopy/solvers.py b/linopy/solvers.py index 235dbee3..9da3751a 100644 --- a/linopy/solvers.py +++ b/linopy/solvers.py @@ -455,6 +455,11 @@ def solve_problem_from_file( status.legacy_status = first_line # Use HiGHS to parse the problem file and find the set of variable names, needed to parse solution + if "highs" not in available_solvers: + raise ModuleNotFoundError( + f"highspy is not installed. Please install it to use {self.solver_name} solver." + ) + h = highspy.Highs() h.readModel(path_to_string(problem_fn)) variables = {v.name for v in h.getVariables()} From 345dcefd84fe95e0833d32f1952b18df7918c780 Mon Sep 17 00:00:00 2001 From: Robbie Muir Date: Thu, 1 May 2025 10:52:30 +0200 Subject: [PATCH 2/6] updated release note --- doc/release_notes.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/release_notes.rst b/doc/release_notes.rst index cbe20019..f0442b71 100644 --- a/doc/release_notes.rst +++ b/doc/release_notes.rst @@ -12,6 +12,7 @@ Upcoming Version gap tolerance. * Improve the mapping of termination conditions for the SCIP solver * Treat GLPK's `integer undefined` status as not-OK +* Allow the objective to be set by a variable instead of an expression Version 0.5.3 -------------- From 174ec99fba0d8b48302de81f0e190c7a010fabcc Mon Sep 17 00:00:00 2001 From: Robbie Muir Date: Fri, 2 May 2025 20:24:37 +0200 Subject: [PATCH 3/6] added test --- test/test_objective.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/test_objective.py b/test/test_objective.py index d0831d25..82d15b3d 100644 --- a/test/test_objective.py +++ b/test/test_objective.py @@ -26,6 +26,14 @@ def quadratic_objective() -> Objective: return m.objective +def test_set_objective_from_variable() -> None: + m = Model() + v = m.add_variables(coords=[[1, 2, 3]]) + m.add_objective(v) + assert isinstance(m.objective, Objective) + assert isinstance(m.objective.expression, LinearExpression) + + def test_model(linear_objective: Objective, quadratic_objective: Objective) -> None: assert isinstance(linear_objective.model, Model) assert isinstance(quadratic_objective.model, Model) From b7f6b0d9fa2c5b8f33d1a67e4f49b8a749368569 Mon Sep 17 00:00:00 2001 From: Robbie Muir Date: Fri, 2 May 2025 21:49:21 +0200 Subject: [PATCH 4/6] Added test for missing highspy --- linopy/solvers.py | 2 +- test/test_solvers.py | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/linopy/solvers.py b/linopy/solvers.py index 9da3751a..5e7a8b10 100644 --- a/linopy/solvers.py +++ b/linopy/solvers.py @@ -457,7 +457,7 @@ def solve_problem_from_file( # Use HiGHS to parse the problem file and find the set of variable names, needed to parse solution if "highs" not in available_solvers: raise ModuleNotFoundError( - f"highspy is not installed. Please install it to use {self.solver_name} solver." + f"highspy is not installed. Please install it to use {self.solver_name.name} solver." ) h = highspy.Highs() diff --git a/test/test_solvers.py b/test/test_solvers.py index b29e6720..cf023837 100644 --- a/test/test_solvers.py +++ b/test/test_solvers.py @@ -9,7 +9,7 @@ import pytest -from linopy import solvers +from linopy import Model, solvers free_mps_problem = """NAME sample_mip ROWS @@ -64,3 +64,19 @@ def test_free_mps_solution_parsing(solver: str, tmp_path: Path) -> None: assert result.status.is_ok assert result.solution.objective == 30.0 + + +def test_highs_missing(monkeypatch): + # Mock the value of "available_solvers" to exclude "highs" + monkeypatch.setattr("linopy.solvers.available_solvers", ["cbc"]) + + model = Model() + x = model.add_variables(lower=0.0, name="x") + model.add_constraints(x >= 0.0) + model.add_objective(x, sense="min") + + with pytest.raises( + ModuleNotFoundError, + match="highspy is not installed. Please install it to use CBC solve", + ): + model.solve(solver_name="cbc") From 0d89a06171df5b4972c98e488cf3cfe4a8321f03 Mon Sep 17 00:00:00 2001 From: Robbie Muir Date: Fri, 2 May 2025 22:36:35 +0200 Subject: [PATCH 5/6] typing --- test/test_solvers.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test_solvers.py b/test/test_solvers.py index cf023837..3ed9150a 100644 --- a/test/test_solvers.py +++ b/test/test_solvers.py @@ -8,6 +8,7 @@ from pathlib import Path import pytest +from _pytest.monkeypatch import MonkeyPatch from linopy import Model, solvers @@ -66,7 +67,7 @@ def test_free_mps_solution_parsing(solver: str, tmp_path: Path) -> None: assert result.solution.objective == 30.0 -def test_highs_missing(monkeypatch): +def test_highs_missing(monkeypatch: MonkeyPatch) -> None: # Mock the value of "available_solvers" to exclude "highs" monkeypatch.setattr("linopy.solvers.available_solvers", ["cbc"]) From 8723dd58ffbe50f213c43c5784eb32827439691f Mon Sep 17 00:00:00 2001 From: Robbie Muir Date: Fri, 2 May 2025 23:17:04 +0200 Subject: [PATCH 6/6] fixed monkeypatch test --- test/test_solvers.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/test_solvers.py b/test/test_solvers.py index 3ed9150a..87942ffa 100644 --- a/test/test_solvers.py +++ b/test/test_solvers.py @@ -5,12 +5,13 @@ @author: sid """ +import importlib from pathlib import Path import pytest from _pytest.monkeypatch import MonkeyPatch -from linopy import Model, solvers +from linopy import solvers free_mps_problem = """NAME sample_mip ROWS @@ -68,9 +69,14 @@ def test_free_mps_solution_parsing(solver: str, tmp_path: Path) -> None: def test_highs_missing(monkeypatch: MonkeyPatch) -> None: - # Mock the value of "available_solvers" to exclude "highs" monkeypatch.setattr("linopy.solvers.available_solvers", ["cbc"]) + import linopy.model + + importlib.reload(linopy.model) + + from linopy.model import Model + model = Model() x = model.add_variables(lower=0.0, name="x") model.add_constraints(x >= 0.0)