Skip to content

Commit a7522d6

Browse files
committed
fix tests
1 parent f51b340 commit a7522d6

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

linopy/model.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,15 @@ def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
116116
constant = self.objective.expression.const
117117
model.objective.expression = self.objective.expression.drop_constant()
118118
args = (model, *args[1:]) # type: ignore
119-
result = func(*args, **kwargs)
120119

121-
# Re-add the constant term
120+
try:
121+
result = func(*args, **kwargs)
122+
except Exception as e:
123+
# Even if there is an exception, make sure the model returns to it's original state
124+
model.objective.expression = model.objective.expression + constant
125+
raise e
126+
127+
# Re-add the constant term to return the model objective to the original expression
122128
model.objective.expression = model.objective.expression + constant
123129
if model.objective.value is not None:
124130
model.objective.set_value(model.objective.value + constant)

test/test_model.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ def test_objective() -> None:
8282
assert m.objectiverange.min() == 2
8383
assert m.objectiverange.max() == 2
8484

85-
# test objective with constant which is not supported
86-
with pytest.raises(ValueError):
87-
m.objective = m.objective + 3
85+
# test objective with constant which is supported
86+
m.objective = m.objective + 3
8887

8988

9089
def test_remove_variable() -> None:

test/test_objective.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,4 @@ def test_repr(linear_objective: Objective, quadratic_objective: Objective) -> No
192192
def test_objective_constant() -> None:
193193
m = Model()
194194
linear_expr = LinearExpression(None, m) + 1
195-
with pytest.raises(ValueError):
196-
m.objective = Objective(linear_expr, m)
195+
m.objective = Objective(linear_expr, m)

test/test_optimization.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ def test_model_resolve(
956956
assert np.isclose(model.objective.value or 0, 5.25)
957957

958958

959-
def test_model_with_constant_in_objective(model: Model) -> None:
959+
def test_model_with_constant_in_objective_feasible(model: Model) -> None:
960960
objective = model.objective.expression + 1
961961
model.add_objective(expr=objective, overwrite=True)
962962

@@ -969,6 +969,35 @@ def test_model_with_constant_in_objective(model: Model) -> None:
969969
assert model.objective.expression.solution == 4.3
970970

971971

972+
def test_model_with_constant_in_objective_infeasible(model: Model) -> None:
973+
objective = model.objective.expression + 1
974+
model.add_objective(expr=objective, overwrite=True)
975+
model.add_constraints([(1, "x")], "<=", 0)
976+
model.add_constraints([(1, "y")], "<=", 0)
977+
978+
with pytest.warns(ConstantInObjectiveWarning):
979+
status, condition = model.solve(solver_name="highs")
980+
981+
assert condition == "infeasible"
982+
# Even though the problem was not solved, the constant term should still be accessible
983+
assert model.objective.expression.const == 1
984+
985+
986+
def test_model_with_constant_in_objective_error(model: Model) -> None:
987+
objective = model.objective.expression + 1
988+
model.add_objective(expr=objective, overwrite=True)
989+
model.add_constraints([(1, "x")], "<=", 0)
990+
model.add_constraints([(1, "y")], "<=", 0)
991+
992+
try:
993+
status, condition = model.solve(solver_name="apples")
994+
except AssertionError:
995+
pass
996+
997+
# Even if something goes wrong, the model objective should return to the correct state
998+
assert model.objective.expression.const == 1
999+
1000+
9721001
@pytest.mark.parametrize(
9731002
"solver,io_api,explicit_coordinate_names", [p for p in params if "direct" not in p]
9741003
)

0 commit comments

Comments
 (0)