Skip to main content

Not equal binary variables in a constraint

Answered

Comments

3 comments

  • Matthias Miltenberger
    • Gurobi Staff

    Hi Ana,

    If you have binary variables that need to be different, you can model that quite easily:

    a = m.addVar(vtype=GRB.BINARY, name="a")
    b = m.addVar(vtype=GRB.BINARY, name="b")
    m.addConstr(a + b == 1)

    I hope that helps!

    Cheers,
    Matthias

    0
  • Riley Clement
    • Gurobi Staff

    Hi Ana,

    I will also add to Matthias' answer by noting that I think your logical conditions that you want to model will not succeed for your problem.

    Say that Color[0] = blue, Color[1] = red, Color[2] = green, and for a particular bin b, we have a[0,b] =1, so that item 0 is put in bin b.

    From the logic you are trying to model, we must have a[1,b] = 0 and a[2,b] = 0 since they are not blue.  But then this violates a constraint which says that "item 1 and 2 have different colors so then a[1,b] != a[2,b]".

    Using Matthias' example a slight adjustment to use

    m.addConstr(a + b <= 1)

    will model the logic I think you need.  And if you have n variables a, b, c, ... which correspond to the one bin, but all different colors then you can use a stronger constraint of

    m.addConstr(a + b + c + .... <= 1)

    since only once of them can be chosen.

    - Riley

    0
  • Ana Guasque Ortega
    • Gurobi-versary
    • Investigator
    • Conversationalist

    Thank you both very much!! I'm trying to implement it. 

    Best, Ana.

    0

Please sign in to leave a comment.