Not equal binary variables in a constraint
AnsweredGood morning,
I want to model an allocation problem in which items are allocated to bins. Items have a characteristic (for example, color) so I need a constraint that ensure that If two items have different colors, they have to be allocated to different bins.
m.addConstrs( (a[i,b] != a[ii,b] for b in bins for i in items for ii in items if( i!=ii and Color[i]!=Color[ii])), "c1" )
a is the binary variable whose value is 1 if task i is allocated to bin b and 0 otherwise.
The problem is that not equal is not permitted in a constraint. I found this post but I don't get the point.
Thanks in advance.
-
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,
Matthias0 -
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 -
Thank you both very much!! I'm trying to implement it.
Best, Ana.
0
Please sign in to leave a comment.
Comments
3 comments