Some problem about adding indicator constraints
回答済みI want to add constraints as below:
if dep[i][j] == 1 then z[0, j] < z[0, i] 0<i<4,i<j<4
I write these codes :
m.addConstrs((dep[i][j] == 1) >> (z[0, j] < z[0, i]) for i in range(tab_cnt - 1) for j in range(i + 1, tab_cnt))
Something wrong arise:
Traceback (most recent call last):
File "/Users/hyliu/Documents/workspace/mapping/test.py", line 30, in <module>
m.addConstrs((dep[i][j] == 1) >> (z[0, j] < z[0, i]) for i in range(tab_cnt - 1) for j in range(i + 1, tab_cnt))
File "model.pxi", line 3336, in gurobipy.Model.addConstrs
File "/Users/hyliu/Documents/workspace/mapping/test.py", line 30, in <genexpr>
m.addConstrs((dep[i][j] == 1) >> (z[0, j] < z[0, i]) for i in range(tab_cnt - 1) for j in range(i + 1, tab_cnt))
TypeError: '<' not supported between instances of 'Var' and 'Var'
Here is the definition of my variables:
tab_cnt = 4
dep = [
[0, 1, 1, 1],
[0, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 0, 0]
]
x = m.addVars(4, 4, vtype=GRB.BINARY, name='x')
y = m.addVars(1, 4, vtype=GRB.BINARY, name='y')
z = m.addVars(1, 4, vtype=GRB.INTEGER, name='z')
Thanks a lot!
1
-
正式なコメント
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?. -
Hi Hongyan,
First, note that < (strict inequality) is not supported for constraints. Perhaps you mean to use <= ( \( \leq \) )?
Second, indicator constraints should be used when you want to say something like "if a binary variable is equal to 1, then this constraint should be enforced." In this case, dep isn't a variable in the model, but rather a list of values. If you only want to add constraints corresponding to the nonzero values in dep, you could instead include an if condition in your loop:
m.addConstrs((z[0, j] <= z[0, i]) for i in range(tab_cnt - 1) for j in range(i + 1, tab_cnt) if dep[i][j] == 1)
I hope this helps. Thanks!
Eli
0 -
Thank you very much for your help, it really works.
0
投稿コメントは受け付けていません。
コメント
3件のコメント