Skip to content

Commit 9e5847a

Browse files
committed
fix test
1 parent 9e7eea5 commit 9e5847a

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

linopy/model.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
110110
# The objective contains a constant term
111111
if not model.allow_constant_objective:
112112
raise ConstantObjectiveError(
113-
"Objective function contains constant terms. Please use LinearExpression.drop_constants()/QuadraticExpression.drop_constants() or set Model.allow_constant_objective=True."
113+
"Objective function contains constant terms. Please use expr.drop_constants() or set model.allow_constant_objective=True."
114114
)
115115

116116
# Modify the model objective to drop the constant term
@@ -808,7 +808,7 @@ def add_objective(
808808
| Sequence[tuple[ConstantLike, VariableLike]],
809809
overwrite: bool = False,
810810
sense: str = "min",
811-
allow_constant: bool | None = None,
811+
allow_constant_objective: bool | None = None,
812812
) -> None:
813813
"""
814814
Add an objective function to the model.
@@ -819,7 +819,7 @@ def add_objective(
819819
Expression describing the objective function.
820820
overwrite : False, optional
821821
Whether to overwrite the existing objective. The default is False.
822-
allow_constant : bool, optional
822+
allow_constant_objective : bool, optional
823823
Set the `Model.allow_constant_objective` attribute. If True, the objective is allowed to contain a constant term.
824824
825825
Returns
@@ -837,12 +837,12 @@ def add_objective(
837837

838838
self.objective.expression = expr
839839
self.objective.sense = sense
840-
if allow_constant is not None:
841-
self.allow_constant_objective = allow_constant
840+
if allow_constant_objective is not None:
841+
self.allow_constant_objective = allow_constant_objective
842842

843843
if not self.allow_constant_objective and self.objective.has_constant:
844844
raise ConstantObjectiveError(
845-
"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.",
845+
"Objective function contains constant terms but this is not allowed as Model.allow_constant_objective=False. Either remove constants from the expression with expr.drop_constants() or pass allow_constant_objective=True.",
846846
)
847847

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

test/test_optimization.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ def test_model_resolve(
948948
# add another constraint after solve
949949
model.add_constraints(model.variables.y >= 3)
950950

951-
status, condition = model.solve(
951+
status, _ = model.solve(
952952
solver, io_api=io_api, explicit_coordinate_names=explicit_coordinate_names
953953
)
954954
assert status == "ok"
@@ -958,7 +958,14 @@ def test_model_resolve(
958958

959959
def test_model_with_constant_in_objective_feasible(model: Model) -> None:
960960
objective = model.objective.expression + 1
961-
model.add_objective(expr=objective, overwrite=True)
961+
962+
with pytest.raises(ConstantObjectiveError):
963+
model.add_objective(
964+
expr=objective, overwrite=True, allow_constant_objective=False
965+
)
966+
967+
model.add_objective(expr=objective, overwrite=True, allow_constant_objective=True)
968+
model.allow_constant_objective = False
962969

963970
with pytest.raises(ConstantObjectiveError):
964971
status, _ = model.solve(solver_name="highs")
@@ -974,11 +981,11 @@ def test_model_with_constant_in_objective_feasible(model: Model) -> None:
974981

975982
def test_model_with_constant_in_objective_infeasible(model: Model) -> None:
976983
objective = model.objective.expression + 1
977-
model.add_objective(expr=objective, overwrite=True)
984+
model.add_objective(expr=objective, overwrite=True, allow_constant_objective=True)
978985
model.add_constraints([(1, "x")], "<=", 0)
979986
model.add_constraints([(1, "y")], "<=", 0)
980987

981-
_, condition = model.solve(solver_name="highs", allow_constant=True)
988+
_, condition = model.solve(solver_name="highs")
982989

983990
assert condition == "infeasible"
984991
# Even though the problem was not solved, the constant term should still be accessible
@@ -987,7 +994,7 @@ def test_model_with_constant_in_objective_infeasible(model: Model) -> None:
987994

988995
def test_model_with_constant_in_objective_error(model: Model) -> None:
989996
objective = model.objective.expression + 1
990-
model.add_objective(expr=objective, overwrite=True, allow_constant=True)
997+
model.add_objective(expr=objective, overwrite=True, allow_constant_objective=True)
991998
model.add_constraints([(1, "x")], "<=", 0)
992999
model.add_constraints([(1, "y")], "<=", 0)
9931000

0 commit comments

Comments
 (0)