GurobiError "Constraint has no bool value" is fixed by flipping the constraint.
AnsweredIn the following code: x is constant, and B is the optimization variable. I have the following constraint.
\[x<=sum(B)\]
This constraint is the source of the problem.
If I flip it as follows, the problem is solved:
\[sum(B)>=x\]
If you go to the for loop, I comment out the first formulation. The first formulation produces the error. I am just curious why the first formulation is not working.
import numpy as np
m=gp.Model()
cost=[-70,-60,-50,-40,-30,-20,-10,0]
cost=np.flipud(cost)
x=[1,0,0,1,1,1,0,0]
x=np.flipud(x)
B=m.addVars(len(x), vtype=GRB.BINARY)
obj=gp.quicksum(cost[i]*B[i] for i in range (len(x)))
m.setObjective(obj)
m.addConstr(gp.quicksum(B[i] for i in range (len(x)))<=1 )
m.addConstrs(B[i]<=x[i] for i in range (len(x)))
for i in range(len(x)):
#m.addConstr(x[i]<=gp.quicksum(B[jj] for jj in range (i+1)))
m.addConstr(gp.quicksum(B[jj] for jj in range (i+1))>=x[i])
m.optimize()
Thanks,
Hussein
0
-
Official comment
This post is more than two 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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Hussein,
This issue is discussed in the second paragraph of Constraint has no bool value (are you trying "lb <= expr <= ub"?).
A workaround is
m.addConstr(int(x[i]) <= gp.quicksum(B[jj] for jj in range (i+1)))
Best regards,
Jaromił1 -
Thank you so much! Jaromił Najman
Best,
Hussein
0
Post is closed for comments.
Comments
3 comments