Error: must be real number, not MLinExpr while using model.addGenConstrIndicator
回答済みHi
I was trying to solve a problem where I have a constraint like below:
if b_B(i,j)==1, then B[i,j]-A[i,j]>=.001So i write my code in python as below:
for i in range (0,state):
for j in range (0,epoch-1):
m.addGenConstrIndicator(b_B[i,j], 1,B[i,j]-A[i,j]>=.001)
But while running this piece of code, it's showing an error. And the error type says
"TypeError: must be real number, not MLinExpr".
Here b_B, A &B all are MVar. [2-D matrix]. Also, b_B is binary.
-
正式なコメント
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?. -
The error is showing you that the last argument is a matrix linear expression rather than a linear expression. This makes sense since you are using matrix variable objects, MVar, in the expression. In fact, Model.addGenConstrIndicator() can't use matrix variable objects directly. Really, both the binary variable and LHS arguments cannot be matrix variable objects.
Luckily, Gurobi has a function to retrieve Var objects from MVar objects. According to the documentation, the function MVar.tolist(),
Returns the variables associated with this matrix variable as a list of individual Var objects.
Therefore, you can create variable lists for each MVar in your indicator constraint:
b_B_list=b_B.tolist()
A_list=A.tolist()
B_list=B.tolist()Then, you can update your indicator constraint definition to
model.addGenConstrIndicator(b_B_list[i][j], 1, B_list[i][j]-A_list[i][j]>=.001)
1 -
Thank you so so much!
0
投稿コメントは受け付けていません。
コメント
3件のコメント