Skip to main content

Optimization Probelm not ADDINF CONSTRAINTS

Answered

Comments

5 comments

  • Marika Karbstein
    • Gurobi Staff

    Could you please elaborate on what you think is not working?

    You can write the LP file of the model that Gurobi solves before calling optimize() with model.write("Test.lp") and then check Test.lp for the correlation constraints.

    0
  • Srinivasan Subramaniyan
    • Gurobi-versary
    • First Question
    • First Comment

    Hello Marika,

    I have incorporated the correlation constraint into my model; however, it does not yield accurate results.

    For instance, when the bound is set to -0.25, no tasks should be grouped in any bins as they do not meet the correlation condition. Nevertheless, the solver continues to binpack without considering this condition. This discrepancy was confirmed through the print results employed in the end.

    0
  • Marika Karbstein
    • Gurobi Staff

    I cannot see that a computed solution violates a constraint of the model. 
    I ran your code with -0.25, the first correlation constraint is

     correlation_constraint[0,1]: - 0.25 y[1] + [
       0.37250842430155 x[0,0] * x[1,0] + 0.37250842430155 x[0,1] * x[1,1]
       + 0.37250842430155 x[0,2] * x[1,2] + 0.37250842430155 x[0,3] * x[1,3]
       + 0.37250842430155 x[0,4] * x[1,4] + 0.37250842430155 x[0,5] * x[1,5]
       + 0.37250842430155 x[0,6] * x[1,6] + 0.37250842430155 x[0,7] * x[1,7] ]
       <= -0.25

    In the solution, y[1]=1 and all product terms are 0. Hence the constraint is satisfied.
    I also see no violations for the other constraints.

    You probably need to redefine the constraints such that they map the conditions you have in mind.

    0
  • Srinivasan Subramaniyan
    • Gurobi-versary
    • First Question
    • First Comment

    Hi Marika,

    The constraint i have in mind is  
    to only binpack tasks if they have a -ve correlation that is np.corrcoef(items[i][3], items[j][3])[0, 1] < bound if they have to be in the same bin k! This constraint does not seem to work sometimes.

    #model.addConstrs((gp.quicksum(np.corrcoef(items[i][3], items[j][3])[0, 1] * x[i, k] * x[j, k] for k in range(UB)) <= bound for i in range(n) for j in range(n) if i != j), name='correlation_constraint') 

    0
  • Marika Karbstein
    • Gurobi Staff

    Hi Srinivasan,

    I am not sure whether I understand you correctly. But why not add the constraints similar to the check you are doing at the end:

    for i in range(n):
      for j in range(i+1,n):
          coefficient, _ = pearsonr(items[i][3], items[j][3])
          if coefficient > bound:
                model.addConstrs(x[i, k] + x[j, k] <= 1 for k in range(UB))
    It does not allow packing two items in the same bin if the coefficient is above the bound.
     
    Cheers,
    Marika
    0

Please sign in to leave a comment.