If Constraint
回答済みHelloo,
I have 2 binary variables h[i,j,t] and u[i,j,t] and I need to express this constraint in my model:
if for a given i : u[i,j,t]=1 then for all other i, h[i,j,t]=0,
the solution i found is :

But i dont know how to express it in Gurobipy,
thank you,
-
正式なコメント
In addition, please see our Knowledge Base article How do I model conditional statements in Gurobi?
-
Hi Rayan,
Your solution is correct. You can model this as follows:
m.addConstrs( sum( h[k,j,t] for k in I if k != i) <= M *( 1- u[i,j,t] ) for i in I for j in J for t in T )
Please choose the smallest possible value of M carefully. One value can be the sum of the upper bounds on the h variables.
Alternatively, you can add an indicator constraint, which does not require you to choose the value of M :
m.addConstrs( (u[i,j,t] ==1) >> (sum( h[k,j,t] for k in I if k != i) == 0 ) for i in I for j in J for t in T )
Best regards,Simran0 -
It perfectly worked !
Thanks a lot !!
0 -
Hello Simrane,
I want to express the same constraint, but this time i have 4 index for my 2 variables :
h[i,c,j,t] and u[i,c,j,t]
if for a given i and c : u[i,c,j,t]=1 then for all other i, and c : h[i,c,j,t]=0,
I tried this formulation, but it doesn't work :
model.addConstrs( (u[i,c,j,t] ==1) >> (gp.quicksum( h[k,d,j,t] for k in range(I) if k != i for d in range(C) if d != c) == 0 ) for i in range(I) for j in range(J) for t in range(T) for c in range(C))
0 -
Hi Rayan,
Your implementation should work fine if you want to add the constraint:
u[i,c,j,t]=1 implies h[k,d,j,t] = 0 for all h variables where k != i and d !=c.
However, if you want to add the constraint:
u[i,c,j,t]=1 implies h[k,d,j,t] = 0 for all h variables where k != i or d !=c,
you will need to modify your indicator constraint as follows:
m.addConstrs( (u[i,c,j,t] ==1) >> (gp.quicksum( h[k,d,j,t] for k in range(I) for d in range(C) if ( k != i or d != c) ) == 0 ) for i in range(I) for j in range(J) for t in range(T) for c in range(C))
Best regards,
Simran
0
サインインしてコメントを残してください。
コメント
5件のコメント