Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Gurobi objective #290

Merged
merged 4 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions mip/gurobi.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,10 @@ def get_objective(self) -> LinExpr:
st = GRBgetdblattrarray(self._model, attr, 0, self.num_cols(), obj)
if st != 0:
raise ParameterNotAvailable("Error getting objective function")
obj_expr = xsum(
obj[i] * self.model.vars[i]
for i in range(self.num_cols())
if abs(obj[i]) > 1e-20
)
obj_expr.const = self.get_objective_const()
sebheger marked this conversation as resolved.
Show resolved Hide resolved
obj_expr.sense = self.get_objective_sense
obj_expr = LinExpr(variables=[self.model.vars[i] for i in range(self.num_cols()) if abs(obj[i]) > 1e-20],
coeffs=[obj[i] for i in range(self.num_cols()) if abs(obj[i]) > 1e-20],
const=self.get_objective_const(),
sense=self.get_objective_sense())
return obj_expr

def get_objective_const(self) -> float:
Expand Down
1 change: 1 addition & 0 deletions test/dbft_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,4 @@ def test_dbft_mip(solver, pdata):
m.check_optimization_results()
if m.status == OptimizationStatus.OPTIMAL:
assert abs(m.objective_value - opt) <= TOL
assert abs(m.objective.x - m.objective_value) <= TOL
35 changes: 35 additions & 0 deletions test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1260,3 +1260,38 @@ def test_query_attributes_of_lin_expr(solver):
assert constr_expr.violation is None

m.optimize()

@pytest.mark.parametrize("solver", SOLVERS)
def test_objective(solver):
m = Model(solver_name=solver, sense=MAXIMIZE)
x = m.add_var(name="x", lb=0, ub=1)
y = m.add_var(name="y", lb=0, ub=1)

m.objective = x - y + 0.5
assert m.objective.x is None

# Make sure that we can access the objective and it's correct
assert len(m.objective.expr) == 2
assert m.objective.expr[x] == 1
assert m.objective.expr[y] == -1
assert m.objective.const == 0.5

status = m.optimize()
assert status == OptimizationStatus.OPTIMAL
assert m.objective_value == 1.5
assert m.objective_value == m.objective.x


# Test changing the objective
m.objective = x + y + 1.5
m.sense = MINIMIZE

assert len(m.objective.expr) == 2
assert m.objective.expr[x] == 1
assert m.objective.expr[y] == 1
assert m.objective.const == 1.5

status = m.optimize()
assert status == OptimizationStatus.OPTIMAL
assert m.objective_value == 1.5
assert m.objective_value == m.objective.x