メインコンテンツへスキップ

If Constraint

回答済み

コメント

5件のコメント

  • 正式なコメント
    Gwyneth Butera
    • Gurobi Staff

    In addition, please see our Knowledge Base article How do I model conditional statements in Gurobi?

  • Simranjit Kaur
    • Gurobi Staff

    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,
    Simran
    0
  • Rayan SAAD
    • Gurobi-versary
    • Investigator
    • Conversationalist

    It perfectly worked !

    Thanks a lot !!

    0
  • Rayan SAAD
    • Gurobi-versary
    • Investigator
    • Conversationalist

    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
  • Simranjit Kaur
    • Gurobi Staff

    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

サインインしてコメントを残してください。