gurobipy.GurobiError: Constraint has no bool value (are you trying "lb <= expr <= ub"?)
AnsweredHi, I am new with Gurobi. I have faced some problems recently when programming my models. It would be grateful if get your professional advice.Thanks.
An error occurred after running the following code:
TS_i = model.addVars(n, vtype=GRB.INTEGER, name="TS_i")
TE_i = model.addVars(n, vtype=GRB.INTEGER, name="TE_i")
lambda_i_p = {}
for i in V:
for p in P:
lambda_i_p[i, p] = model.addVar(vtype=GRB.CONTINUOUS, name=f"lambda_i_p_{i}_{p}")
# Create constraints to represent the relationship between lambda_ip and TE_i, TS_i
for i in V:
for p in P:
if TS_i[i] <= p and p<= TE_i[i]:
model.addConstr(lambda_i_p[i, p]* (TE_i[i] - TS_i[i] + 1)== D_i[i] , name=f"constraint_lambda_ip_{i}_{p}")
Here is the details about the error:
Traceback (most recent call last):
File "D:\pythonproject\lib\site-packages\IPython\core\interactiveshell.py", line 3505, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-55c84a859894>", line 1, in <module>
runfile('D:\\PyCharm Pro 2023.2\\pythonProject\\9.5 test大规模+双目标.py', wdir='D:\\PyCharm Pro 2023.2\\pythonProject')
File "D:\PyCharm Pro 2023.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "D:\PyCharm Pro 2023.2\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "D:\PyCharm Pro 2023.2\pythonProject\9.5 test大规模+双目标.py", line 112, in <module>
if TS_i[i] <= p and p<= TE_i[i]:
File "src\gurobipy\tempconstr.pxi", line 57, in gurobipy.TempConstr.__bool__
gurobipy.GurobiError: Constraint has no bool value (are you trying "lb <= expr <= ub"?)
I think the problem should occur in this line of code.
if TS_i[i] <= p and p<= TE_i[i]:
because TS_i andTE_i here is thedecision variable , but I don’t know how to modify it.
-
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 Qiuyan,
It is not possible to use Gurobi variable objects in if-clauses because there is no value associated with them to be used in comparison. The reason for the error you see is well-explained in the article Constraint has no bool value (are you trying "lb <= expr <= ub"?). Please check the cause number 3).
I assume you would like to model the following constraint:
\[\mbox{if}~~ TS_i \leq p ~\mbox{and}~ TE_i \geq p \rightarrow \lambda_{ip} (TE_i - TS_i + 1) = D_i \]
To implement the above constraint, you need to use three binary auxiliary variables to model the relationships below:
- \(\mbox{if}~~ TS_i \leq p \rightarrow x_i = 1 \)
- \(\mbox{if}~~ TE_i \geq p \rightarrow y_i = 1 \)
- \(\mbox{if}~~ x_i = 1 ~\mbox{and}~ y_i= 1 \rightarrow z_i = 1 \)
- \(\mbox{if}~~ z_i = 1 \rightarrow \lambda_{ip} (TE_i - TS_i + 1) = D_i \)
You can use Gurobi Python APIs for the Indicator constraint to implement the above relationships.
Best regards,
Maliheh
1 -
Hello,Maliheh,Thanks for your advice,it's helpful.I tried to modeling the constraint using the method above.However, the value of variable: lambda_i_p[i, p] all become 0.And I check the code,I think the problem may still lie in this section of the code.Which part do you think the problem occurred?
# Define the lambda_i_p derived variables
lambda_i_p = {}
for i in V:
for p in P:
lambda_i_p[i, p] = model.addVar(vtype=GRB.CONTINUOUS, name=f"lambda_i_p_{i}_{p}")
# Add auxiliary variables x, y, and z
x = {}
y = {}
z = {}
for i in V:
for p in P:
x[i, p] = model.addVar(vtype=GRB.BINARY, name=f"x_{i}{p}")
y[i, p] = model.addVar(vtype=GRB.BINARY, name=f"y_{i}{p}")
z[i, p] = model.addVar(vtype=GRB.BINARY, name=f"z_{i}{p}")
# Add constraints: x[i]=1 if and only if TS_i[i] <= p
# Add constraints: y[i]=1 if and only if TE_i[i] >= p
# Add constraints: z[i]=1 if and only if x[i]=1 and y[i]=1
# Add constraint: lambda_i_p[i, p] * (TE_i[i] - TS_i[i] + 1) == D_i[i] when z[i]=1 holds
for i in V:
for p in P:
model.addConstr((x[i, p] == 1) >> (TS_i[i] <= p), name=f"constraint_x_{i}_{p}")
model.addConstr((y[i, p] == 1) >> (p <= TE_i[i]), name=f"constraint_y_{i}_{p}")
model.addConstr(z[i, p] == x[i, p] * y[i, p], name=f"constraint_z_{i}{p}")
model.addConstr(lambda_i_p[i, p] * (TE_i[i] - TS_i[i] + 1) == D_i[i] * z[i, p], name=f"constraint_lambda_ip_{i}_{p}")0 -
Do you get an error? Or do you mean that having all \(\lambda_{ip}\) assigned to 0 is not what you expect to see in the optimal solution?
Please note that you have modelled different relationships. In your implementation version below, if \(z_i = 0\), the expression \(\lambda_{ip}(TE_i - TS_i +1)\) must be 0 then. However, in the constraint \(\mbox{if}~ z_i = 1 ~ \rightarrow ~ \lambda_{ip} (TE_i - TS_i +1) = D_i\), we do not impose any constraint on the value of \(\lambda_{ip} (TE_i - TS_i +1)\) if \(z_i = 0\).
model.addConstr(lambda_i_p[i, p] * (TE_i[i] - TS_i[i] + 1) == D_i[i] * z[i, p], name=f"constraint_lambda_ip_{i}_{p}")
The same is true for all the other constraints you have implemented.
- The constraint \(\mbox{if}~~ TS_i \leq p \rightarrow x_i = 1 \) is different from the constraint \(\mbox{if}~~ x_i = 1 \rightarrow TS_i \leq p \). To model the former relationship using Gurobi Python API for indicator constraints, you can implement its equivalent logical contrapositive in the form \(\mbox{if}~~ x_i \neq 1 \rightarrow TS_i \nleq p \) which is \(\mbox{if}~~ x_i = 0 \rightarrow TS_i \geq p + \epsilon \).
- The constraint \(\mbox{if}~~ TE_i \geq p \rightarrow y_i = 1 \) is different from the constraint \(\mbox{if}~~ y _i = 1 \rightarrow TE_i \geq p\) (see above).
- The constraint \(\mbox{if}~~ x_i = 1 ~\mbox{and}~ y_i= 1 \rightarrow z_i = 1 \) is different from \(z_i = x_i y_i\). In the former version, we are not imposing any constraint on the value of \(z_i\) if, for example, \(x_i = 0\) and \(y_i = 1\).
Best regards,
Maliheh
0
Post is closed for comments.
Comments
4 comments