How to solve IF ELSE in MIP for inequality check

82 Views Asked by At

Assume I have two variables X and Y, I want to introduce a new variable Z with the following mapping:

IF X NOT EQUAL TO Y THEN Z = 1 ELSE Z = 0

Here, X and Y are both non-negative variables.

Thanks in advance.

1

There are 1 best solutions below

1
On BEST ANSWER

Suppose $X\in [0,M]$ and $Y\in [0,M]$, and introduce two additional binary variables (besides $Z$), $U$ and $V$, together with linear constraints: \begin{align} U + Z + V &= 1 \tag1\\ -MU + 0Z + 1V \le X-Y &\le -1U + 0Z + MV \tag2 \end{align} Constraint $(1)$ forces exactly one of $U$, $Z$, and $V$ to be $1$. Constraint $(2)$ enforces \begin{align} U=1 &\implies -M \le X-Y \le -1 &&(X<Y) \\ Z=1 &\implies 0 \le X-Y \le 0 &&(X=Y)\\ W=1 &\implies 1 \le X-Y \le M &&(X>Y)\\ \end{align}