Skip to content

Commit 9e7eea5

Browse files
committed
minor changes
1 parent 14497bf commit 9e7eea5

File tree

6 files changed

+16
-19
lines changed

6 files changed

+16
-19
lines changed

linopy/expressions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,8 @@ def const(self, value: DataArray) -> None:
726726
self._data = assign_multiindex_safe(self.data, const=value)
727727

728728
@property
729-
def has_constant(self) -> DataArray:
730-
return self.const.any()
729+
def has_constant(self) -> bool:
730+
return bool(self.const.any())
731731

732732
# create a dummy for a mask, which can be implemented later
733733
@property

linopy/model.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from pathlib import Path
1515
from tempfile import NamedTemporaryFile, gettempdir
1616
from typing import Any, Literal, ParamSpec, TypeVar, overload
17-
from warnings import warn
1817

1918
import numpy as np
2019
import pandas as pd
@@ -83,9 +82,6 @@
8382
R = TypeVar("R")
8483

8584

86-
class ConstantInObjectiveWarning(UserWarning): ...
87-
88-
8985
class ConstantObjectiveError(Exception): ...
9086

9187

@@ -812,6 +808,7 @@ def add_objective(
812808
| Sequence[tuple[ConstantLike, VariableLike]],
813809
overwrite: bool = False,
814810
sense: str = "min",
811+
allow_constant: bool | None = None,
815812
) -> None:
816813
"""
817814
Add an objective function to the model.
@@ -822,6 +819,8 @@ def add_objective(
822819
Expression describing the objective function.
823820
overwrite : False, optional
824821
Whether to overwrite the existing objective. The default is False.
822+
allow_constant : bool, optional
823+
Set the `Model.allow_constant_objective` attribute. If True, the objective is allowed to contain a constant term.
825824
826825
Returns
827826
-------
@@ -838,10 +837,12 @@ def add_objective(
838837

839838
self.objective.expression = expr
840839
self.objective.sense = sense
840+
if allow_constant is not None:
841+
self.allow_constant_objective = allow_constant
842+
841843
if not self.allow_constant_objective and self.objective.has_constant:
842-
warn(
844+
raise ConstantObjectiveError(
843845
"Objective function contains constant terms but this is not allowed as Model.allow_constant_objective=False, running solve will result in an error. Please either remove constants from the expression with expr.drop_constants() or set Model.allow_constant_objective=True.",
844-
ConstantInObjectiveWarning,
845846
)
846847

847848
def remove_variables(self, name: str) -> None:

linopy/objective.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def has_constant(self) -> bool:
196196
"""
197197
Returns whether the objective has a constant term.
198198
"""
199-
return bool(self.expression.has_constant)
199+
return self.expression.has_constant
200200

201201
@property
202202
def model(self) -> Model:

test/test_linear_expression.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ def test_drop_constant(x: Variable) -> None:
12421242
assert all(expr_2.const.values == 0.0), (
12431243
f"Expected constant 0.0, got {expr_2.const.values}"
12441244
)
1245-
assert not bool(expr_2.has_constant)
1245+
assert not expr_2.has_constant
12461246

12471247

12481248
def test_simplify_basic(x: Variable) -> None:

test/test_optimization.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from linopy import GREATER_EQUAL, LESS_EQUAL, Model, solvers
2121
from linopy.common import to_path
2222
from linopy.expressions import LinearExpression
23-
from linopy.model import ConstantInObjectiveWarning, ConstantObjectiveError
23+
from linopy.model import ConstantObjectiveError
2424
from linopy.solver_capabilities import (
2525
SolverFeature,
2626
get_available_solvers_with_feature,
@@ -958,9 +958,7 @@ def test_model_resolve(
958958

959959
def test_model_with_constant_in_objective_feasible(model: Model) -> None:
960960
objective = model.objective.expression + 1
961-
962-
with pytest.warns(ConstantInObjectiveWarning):
963-
model.add_objective(expr=objective, overwrite=True)
961+
model.add_objective(expr=objective, overwrite=True)
964962

965963
with pytest.raises(ConstantObjectiveError):
966964
status, _ = model.solve(solver_name="highs")
@@ -980,8 +978,7 @@ def test_model_with_constant_in_objective_infeasible(model: Model) -> None:
980978
model.add_constraints([(1, "x")], "<=", 0)
981979
model.add_constraints([(1, "y")], "<=", 0)
982980

983-
model.allow_constant_objective = True
984-
_, condition = model.solve(solver_name="highs")
981+
_, condition = model.solve(solver_name="highs", allow_constant=True)
985982

986983
assert condition == "infeasible"
987984
# Even though the problem was not solved, the constant term should still be accessible
@@ -990,8 +987,7 @@ def test_model_with_constant_in_objective_infeasible(model: Model) -> None:
990987

991988
def test_model_with_constant_in_objective_error(model: Model) -> None:
992989
objective = model.objective.expression + 1
993-
model.allow_constant_objective = True
994-
model.add_objective(expr=objective, overwrite=True)
990+
model.add_objective(expr=objective, overwrite=True, allow_constant=True)
995991
model.add_constraints([(1, "x")], "<=", 0)
996992
model.add_constraints([(1, "y")], "<=", 0)
997993

test/test_quadratic_expression.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def test_drop_constant(x: Variable) -> None:
323323
assert all(expr_2.const.values == 0.0), (
324324
f"Expected constant 0.0, got {expr_2.const.values}"
325325
)
326-
assert not bool(expr_2.has_constant)
326+
assert not expr_2.has_constant
327327

328328

329329
def test_quadratic_expression_to_matrix(model: Model, x: Variable, y: Variable) -> None:

0 commit comments

Comments
 (0)