A recent question on a web forum, one I've seen asked elsewhere, was the following: in a mathematical programming model, how does one constrain a binary variable to take the value 1 if another variable takes a specific (predefined) value, and 0 otherwise? If the binary variable is x, the other variable is y, and the target value is m, then what we want is y=m⟹x=1y≠m⟹x=0.I hinted at the answer to this at the end of a previous post (Indicator Implies Relation), but perhaps a bit more detail is in order.
We will need y to be bounded, say L≤y≤U. The easy case is when y is integer valued, in which case y≠m means either y≤m−1 or y≥m+1. One approach (I don't claim it's the only one) is to introduce two new binary variables, z1 and z2. Essentially, z1 will be an indicator for y≤m−1, z2 will be an indicator for y≥m+1, and x remains an indicator for y=m. To do this, we add the constraints y≤(m−1)z1+mx+Uz2y≥Lz1+mx+(m+1)z2z1+x+z2=1.
Life gets trickier if y is continuous. For any given choice of values for the discrete variables, we need the projection of the feasible region onto the subspace of continuous variables to be closed, which rules out strict inequalities. In other words, we can't say y>m or y<m when x=0. The best we can do is pick a small positive parameter ϵ>0 and treat any value of y in (m−ϵ,m+ϵ) as if it were m. The resulting inequalities are as above, but with m±1 changed to m±ϵ: y≤(m−ϵ)z1+mx+Uz2y≥Lz1+mx+(m+ϵ)z2z1+x+z2=1.Note that this precludes y∈(m−ϵ,m)∪(m,m+ϵ), i.e., the only value between m−ϵ and m+ϵ that y can take is m.
As a practical matter, you should be careful not to pick ϵ too small. It's tempting to think in terms of the smallest positive double-precision number known to your compiler, but that could result in x=0 when theoretically y=m but actually the computed value of y contains a bit of rounding error.
Monday, February 2, 2015
4 comments:
Due to intermittent spamming, comments are being moderated. If this is your first time commenting on the blog, please read the Ground Rules for Comments. In particular, if you want to ask an operations research-related question not relevant to this post, consider asking it on Operations Research Stack Exchange.
Subscribe to:
Post Comments (Atom)
Hello, would this not fail when L=0 and m = 1 for a discrete case? In that case, z1 can be eliminated altogether and we are left with y <= x+Uz2 and y>= x+2z2
ReplyDeleteIf you want to eliminate z1 in this case, you have to change the final equation to x+z2≤1. If y=1, your second inequality forces z2=0 and the first inequality then forces x=1. If y=0, the second inequality forces x=0. If y≥2, the first inequality forces z2=1 and the third inequality (the former equation) forces x=0.
DeleteSo by making x+z2 <= 1, when y = 0, it forces bother x and z2 to be 0?
DeleteNo, the constraint y≥x+2z2 forces both x and z2 to be 0 when y=0.
Delete