@@ -1262,6 +1262,42 @@ def test_query_attributes_of_lin_expr(solver):
12621262
12631263 m .optimize ()
12641264
1265+ @pytest .mark .parametrize ("solver" , SOLVERS )
1266+ def test_objective (solver ):
1267+ m = Model (solver_name = solver , sense = MAXIMIZE )
1268+ x = m .add_var (name = "x" , lb = 0 , ub = 1 )
1269+ y = m .add_var (name = "y" , lb = 0 , ub = 1 )
1270+
1271+ m .objective = x - y + 0.5
1272+ assert m .objective .x is None
1273+ #TODO: assert m.objective.sense == MAXIMIZE
1274+
1275+ # Make sure that we can access the objective and it's correct
1276+ assert len (m .objective .expr ) == 2
1277+ assert m .objective .expr [x ] == 1
1278+ assert m .objective .expr [y ] == - 1
1279+ assert m .objective .const == 0.5
1280+
1281+ status = m .optimize ()
1282+ assert status == OptimizationStatus .OPTIMAL
1283+ assert m .objective_value == 1.5
1284+ assert m .objective_value == m .objective .x
1285+
1286+
1287+ # Test changing the objective
1288+ m .objective = x + y + 1.5
1289+ m .sense = MINIMIZE
1290+ # TODO: assert m.objective.sense == MINIMIZE
1291+
1292+ assert len (m .objective .expr ) == 2
1293+ assert m .objective .expr [x ] == 1
1294+ assert m .objective .expr [y ] == 1
1295+ assert m .objective .const == 1.5
1296+
1297+ status = m .optimize ()
1298+ assert status == OptimizationStatus .OPTIMAL
1299+ assert m .objective_value == 1.5
1300+ assert m .objective_value == m .objective .x
12651301
12661302@pytest .mark .parametrize ("solver" , SOLVERS )
12671303def test_remove (solver ):
@@ -1277,4 +1313,4 @@ def test_remove(solver):
12771313 m .remove ([None ])
12781314
12791315 m .remove (constr )
1280- # TODO: Test the removal of variables (currently failing, see https://github.com/coin-or/python-mip/pull/288#discussion_r919215654)
1316+ # TODO: Test the removal of variables (currently failing, see https://github.com/coin-or/python-mip/pull/288#discussion_r919215654)
0 commit comments