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/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 -------------- 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..5e7a8b10 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.name} solver." + ) + h = highspy.Highs() h.readModel(path_to_string(problem_fn)) variables = {v.name for v in h.getVariables()} 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) diff --git a/test/test_solvers.py b/test/test_solvers.py index b29e6720..87942ffa 100644 --- a/test/test_solvers.py +++ b/test/test_solvers.py @@ -5,9 +5,11 @@ @author: sid """ +import importlib from pathlib import Path import pytest +from _pytest.monkeypatch import MonkeyPatch from linopy import solvers @@ -64,3 +66,24 @@ 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: MonkeyPatch) -> None: + 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) + 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")