4
$\begingroup$

Let $x$ be an integer variable that takes the values $1$, $2$ or $3$. Let $y_1$ and $y_2$ be binary variables.

I want to express the two following logical constraints:

if $x=2$ then $y_1=1$ if $x=3$ then $y_2=1$

That's all. I have looked around here but usually the constraints are inequalities or continuous variables.

Edit:

I have come up with the following solutions:

$-1y_1=(x-1)(x-3)$
when $x=1 \rightarrow y_1=0$,
when $x=3 \rightarrow y_1=0$,
when $x=2 \rightarrow y_1=1$.

$2y_2=(x-1)(x-2)$
when $x=1 \rightarrow y_2=0$,
when $x=2 \rightarrow y_2=0$,
when $x=3 \rightarrow y_2=1$.

It breaks the linearity, but the constraints are in a Mixed Integer Nonlinear Programming problem.

Could be that a valid solution?

$\endgroup$
2
  • 2
    $\begingroup$ Your quadratic equality constraints are correct, but you are better off with linear constraints, even if you already have nonlinearity elsewhere in your model. $\endgroup$
    – RobPratt
    Jun 26 '20 at 20:24
  • 1
    $\begingroup$ Always go for linearizing equations if you can, and start thinking from adding a new binary variable. $\endgroup$ Jun 27 '20 at 1:30
7
$\begingroup$

Introduce binary variable $y_0$ and linear constraints: \begin{align} y_0 + y_1 + y_2 &= 1\\ 1y_0 + 2y_1 + 3y_2 &= x \end{align}

Equivalently, eliminating $y_0$: \begin{align} y_1 + y_2 &\le 1\\ 1 + y_1 + 2y_2 &= x \end{align}

$\endgroup$
6
  • $\begingroup$ That's it! Thank you very much. $\endgroup$
    – Miquel
    Jun 26 '20 at 20:33
  • $\begingroup$ Glad to help, and welcome to OR StackExchange, @Miquel. Please mark my answer as accepted. $\endgroup$
    – RobPratt
    Jun 26 '20 at 20:42
  • $\begingroup$ By the way, I guess there's no problem in putting together both restrictions in one single restriction, expressed as follows: $y_0 +y_1+y_2=1$ $1y_0+2y_1+3y_2=x$ $y_1+2y_2-x=-1$ $\endgroup$
    – Miquel
    Jun 28 '20 at 17:23
  • 1
    $\begingroup$ Those three equality constraints are linearly dependent, and there is no reason to include all three. For example, the second constraint is just the sum of the other two. $\endgroup$
    – RobPratt
    Jun 28 '20 at 19:36
  • 1
    $\begingroup$ You can remove any one of them but not two. $\endgroup$
    – RobPratt
    Jun 28 '20 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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