Update from Gurobi 7.5.2 to 9.1.0 - GurobiError: "Constraint has no bool value"
AnsweredHello everyone,
I currently updated my Gurobi version from 7.5.2 to 9.1.0 (Academic License/OS: Windows). In the 7.5.2 version, I was running a MIP model using inequality constraints among others.
My inequality constraints are currently in the form:
model.addConstr(int(48) <= quicksum((x[i,j,1]*a+x[i,j,2]*b+x[i,j,3]*c) for (i,j) in d), name='Lower')
model.addConstr(quicksum((x[i,j,1]*a+x[i,j,2]*b+x[i,j,3]*c) for (i,j) in d) <= int(51), name='Upper')
My objective function is in the form (in a simplified version):
model.setObjective(quicksum(x[i,j,0] for (i,j) in d if x[i,j,0]==0), GRB.MAXIMIZE)
, and everything was working fine.
When I moved to the 9.1.0 version, the following error occurred:
"GurobiError: Constraint has no bool value (are you trying "lb <= expr <= ub"?)"
But the error occurs in the "model.setObjective" line of the code, not in the inequality constraints lines. When I remove "if x[i,j,0]==0" statement in the "setObjective" formula above, model works, but obviously returns wrong results.
I have already followed the instructions of Eli considering two-sided constraints in the following post:
Is there an alternative way to tackle this issue, because I guess I have to adjust my objective function?
I apologize for not posting a reproducible example or explanations about the optimization variables or the constants, however, I think the error occurs due to the manner I pose equality in my objective function.
Thank you in advance
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Building an expression with
quicksum(x[i,j,0] for (i,j) in d if x[i,j,0] == 0)
should not have worked in previous versions of Gurobi. So it's a good thing you see an error in newer versions of Gurobi. If Gurobi 7.5.2 optimized the model properly when you used this syntax, then you were lucky. The coefficients for variables in the objective function and constraints must be deterministically set prior to solving the model, and therefore cannot change based on the values assumed by those variables.
There are almost always modeling tricks you can use to implement this kind of logic in your model. You might find the Model.addGenConstrIndicator() method (or its overloaded form) helpful. This method allows you to model certain logical implications using binary variables. E.g., given a binary variable \( x \) and another variable \( y \), you could add a constraint like
$$x = 0 \implies y = 2.$$
That said, what is your objective function trying to do? If you only sum over the \( x \) variables that are equal to \( 0 \), you always end up with \( 0 \).
1 -
Hi Eli,
Thanks for your immediate response! Finally, it worked without using the Model.addGenConstrIndicator(), I just customized my linear constraints (actually the quicksum of the x[i,j,k] for k in range(...)).
I think the case is that with the current Gurobi version, the "GurobiError: Constraint has no bool value" error occurs with an "if" statement inside the objective function as you highlighted.
Thanks for your help
Best regards,
Loukas
0
Post is closed for comments.
Comments
3 comments