diff --git a/.gitignore b/.gitignore index aaa535eb..8ff9fb50 100644 --- a/.gitignore +++ b/.gitignore @@ -38,7 +38,7 @@ benchmark/scripts/benchmarks-pypsa-eur/__pycache__ benchmark/scripts/leftovers/ # IDE -.idea/ +.idea # Claude .claude/settings.local.json diff --git a/doc/release_notes.rst b/doc/release_notes.rst index 3ede5437..014f51fd 100644 --- a/doc/release_notes.rst +++ b/doc/release_notes.rst @@ -29,6 +29,7 @@ Version 0.5.4 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 * Internally assign new data fields to `Variable` and `Constraint` with a multiindexed-safe routine. Before the assignment when using multi-indexed coordinates, an deprecation warning was raised. This is fixed now. diff --git a/linopy/model.py b/linopy/model.py index 01b3027b..82d255ca 100644 --- a/linopy/model.py +++ b/linopy/model.py @@ -684,7 +684,8 @@ def add_constraints( def add_objective( self, - expr: LinearExpression + expr: Variable + | LinearExpression | QuadraticExpression | Sequence[tuple[ConstantLike, VariableLike]], overwrite: bool = False, @@ -710,6 +711,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 self.objective.sense = sense diff --git a/linopy/solvers.py b/linopy/solvers.py index caf113f6..537dbd22 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")