@@ -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