I'm trying to linearize a constraint of this form: if a == b then c = 1 where a and b are positive integers and c is a binary variable. I'm looking for a solution like this one https://math.stackexchange.com/questions/2792360/how-to-linearize-if-then-constraint which doesn't work in this case. Thanks to anyone who can help me.
1 Answer
The implication
a = b => c = 1
(a,b: integer variables, c: a binary variable)
can be re-stated as:
c = 0 => a >= b + 1
or
a <= b - 1
(Using that a,b are integers). An "or" needs an extra binary variable. So we can write:
a >= b + 1 - M δ - M c
a <= b - 1 + M (1-δ) + M c
δ ∈ {0,1}
Here M is a large enough constant (to be chosen with care).
2 Comments
TMBailey
Nice.
pyomo seems to want the variables in constraints to all appear on the same side of the inequality, but that's easily done.Erwin Kalvelagen
No, Pyomo does not require that.
c = 1 if a==b else 0? Or even simpler,c = int(a==b)