# Linearizing constraint with continuous and boolean variables

I have two continuous variables $$A$$, $$B$$ and two binary variables $$x$$, $$y$$.

Condition: if $$A = B \wedge x = 1 \wedge y=1$$ then $$z = 1$$ else $$z = 0$$ from

My current attempt is:

\begin{align} A-B + \delta+x+y &\leq 2 + M \cdot k_1\\ B - A - \delta-x-y &\leq - 2 + M \cdot(1 - k_1)\\ B-A + \delta+x+y &\leq 2 + M \cdot k_2\\ A - B - \delta-x-y &\leq -2 + M \cdot(1 - k_2)\\ k_1 + k_2 - 1 &\leq z \\k_1 &\geq z \\k_2 &\geq z \end{align}

where $$k_1, k_2$$ are boolean variables, but I am not getting the expected result.

• I haven't checked your formulation, but what value of M did you use, and how did you choose that value? – Mark L. Stone Jan 31 at 19:37
• I am choosing $M = 1000$ which is higher than the upper bound of the problem – ooo Jan 31 at 19:42
• Perhaps you should spell out exactly in what way(s) the result is different than you expected. – Mark L. Stone Jan 31 at 20:19
• Is $\delta$ a constant or variable? Is it nonnegative, strictly positive or what? – prubin Jan 31 at 21:29
• The last two constraints are intended to enforce $z=1\implies (k_1=1 \land k_2=1)$, but it is better to instead have $z\le k_1$ and $z\le k_2$, with no big-M. – RobPratt Jan 31 at 21:36

You want to model $$z=1 \iff (A=B\land x=1 \land y=1).$$
To enforce $$z=1 \implies (A=B\land x=1 \land y=1)$$: \begin{align} -M(1-z) \le A - B &\le M(1-z)\\ z &\le x\\ z &\le y \end{align}
To enforce the converse $$(A=B\land x=1 \land y=1) \implies z=1$$, equivalently, $$A>B\lor A: \begin{align} B-A+\delta &\le M_1 (1-w_1)\\ A-B+\delta &\le M_2 (1-w_2)\\ w_1+w_2+(1-x)+(1-y) + z&\ge 1\\ w_1,w_2&\in\{0,1\} \end{align}