3
$\begingroup$

I have 6 binary variables $a_i$ for $i$ from 0 to 5. I would like to model the next if-else statement using only MILP constraints

  if $(a_0+a_1+a_2)\mod 2=1$ then $(a_3+a_4+a_5) \mod 2 = 0$

I tried constraint (a_3+a_4+a_5) mod 2 = (a_0+a_1+a_2)mod 2+1, but this constraint is also taking the case when $(a_0+a_1+a_2)\mod 2 = 0$. And I need to take in consideration only the case $(a_0+a_1+a_2)\mod 2 = 1$.

$\endgroup$

1 Answer 1

5
$\begingroup$

One way to do this is to introduce three auxiliary variables: $x$ and $y$ are non-negative integers and $z$ is binary. The binary variable $z$ should equal 1 if and only if $a_0+a_1+a_2 \mbox{ mod } 2=1 $.

Then you may add the following constraints \begin{align} &a_0+a_2+a_2=2x+z\\ &a_3+a_4+a_5=2y+(1-z)\\ &x,y\in\mathbb{N}_0,\ z\in\{0,1\} \end{align} The first constraint says that $a_0+a_1+a_2 \mbox{ mod } 2=1 $ if and only if $z=1$ and the other constraint says tha $a_3+a_4+a_5 \mbox{ mod } 2=0 $ if and only if $z=1$.


Edit: Based on the comment by @RobPratt my first answer was a bit too restrictive. In stead of adding one binary variable $z$, introduce two binary variables $z_1$ and $z_2$ and add the following constraints \begin{align} &a_0+a_2+a_2=2x+z_1\\ &a_3+a_4+a_5=2y+z_2\\ &z_1+z_2\leq 1\\ &x,y\in\mathbb{N}_0,\ z_i\in\{0,1\}, i=1,2 \end{align}

$\endgroup$
2
  • 2
    $\begingroup$ What you proposed is a bit too strong, but you can repair it by replacing the $z$ In the first constraint with $z_1$, replacing the $1-z$ In the second constraint with $z_2$, and imposing $z_1 +z_2\le 1$. $\endgroup$
    – RobPratt
    Nov 3, 2021 at 12:33
  • $\begingroup$ Good point, I will add the edit $\endgroup$
    – Sune
    Nov 3, 2021 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.