Skip to content

Commit c8618b8

Browse files
authored
Fix Gurobi objective (#290)
* Fixed mistake in gurobi objective getter * Apply black * Only use add_const
1 parent a8b9faf commit c8618b8

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

mip/gurobi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,8 @@ def get_objective(self) -> LinExpr:
540540
for i in range(self.num_cols())
541541
if abs(obj[i]) > 1e-20
542542
)
543-
obj_expr.const = self.get_objective_const()
544-
obj_expr.sense = self.get_objective_sense
543+
obj_expr.add_const(self.get_objective_const())
544+
obj_expr.sense = self.get_objective_sense()
545545
return obj_expr
546546

547547
def get_objective_const(self) -> float:

test/dbft_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,4 @@ def test_dbft_mip(solver, pdata):
495495
m.check_optimization_results()
496496
if m.status == OptimizationStatus.OPTIMAL:
497497
assert abs(m.objective_value - opt) <= TOL
498+
assert abs(m.objective.x - m.objective_value) <= TOL

test/test_model.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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)
12671303
def 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

Comments
 (0)