coin-or / python-mip

Python-MIP: collection of Python tools for the modeling and solution of Mixed-Integer Linear programs
Eclipse Public License 2.0
516 stars 90 forks source link

Wrong objective function/ Constant shift #256

Closed gdragotto closed 2 years ago

gdragotto commented 2 years ago

Hello,

I started to believe that I get the wrong objective function with Gurobi, although the optimal solution itself is correct.

I build an objective function inside a LinExpr that looks like

+ 2.8yBR_0-1 + 4.199999999999999yBR_0-2 + 4.199999999999999yBR_0-3 + 2.8yBR_1-0 + 2.8yBR_1-2 + 4.199999999999999yBR_1-3 + 7.0yBR_2-1 + 4.199999999999999yBR_2-3 + 7.0yBR_3-0 - 28.0 where yBR are my decision variables.

However, when I set the objective through model.objective = maximize(objective) with objective containing the above expression, the constant -28.0 gets translated in 28. Specifically, if I open the MIP file I see something like

Maximize [...] + 28Constant and in the variables Constant = 1

Further, if I query print(model.objective.const) I get the correct -28.0.

Expected behavior The package should optimize with the correct constant shift of -28

Desktop (please complete the following information):

EDIT Here's how I make it work:

const = objective.const 
objective -= const
model.objective = maximize(objective)
model.optimize()
realObjective = model.objective_value +const
sebheger commented 2 years ago

Hi @gdragotto,

thanks for reporting your issue.

On the one hand, from a quick look, it might be associated with #214. Is your problem only related to gurobi - so CBC works fine?

On the other hand, could you please provide a complete minimalistic python code example that could be run to see/debug your results? Thanks!

gdragotto commented 2 years ago

Hello,

just a follow up: fixed in #214