0

I need to model the following statement:

if $\sum\limits_{i=1}^N X_i=k$ then $Y=1$ else $Y=0$

$X_i$'s are binary variables

$k$ is an integer between $0$ and $N$

$Y$ is a binary variable

Thank you in advance.

callculus42
  • 30,550
Sou
  • 3

2 Answers2

0

In case $N$ is a constant, then add the following two inequalities to your MILP: $$NY \leq k $$ $$N-1+Y \geq k$$

As $Y$ is a binary variable, if $k\lt N$ then $Y$ must be $0$ to fulfill both inequalities. And when $k=N$ then $Y$ must be $1$ to fulfill both inequalities

Maksim
  • 1,706
0

To your question in the comments: $N$ positive constant, $k$ positive integer constant with $k\leq N$, $X$ decision variable with $0\leq X \leq N$.

Add 3 binary variables $r_1, r_2,r_3$ together with the following constraints: $$ \eqalign{ kr_1 & \leq X \\ X & \leq (1-r_1)(k-1)+r_1N \\ (k+1)(1-r_2) & \leq X\\ X & \leq kr_2+(1-r_2)N\\ 2r_3 & \leq r_1 +r_2 \\ r_1 +r_2 & \leq 1+ r_3 }$$

Then $r_3 = 1$ iff $X=k$, else $r_3 = 0$

Maksim
  • 1,706
  • Thank you Maksim again for your response. I noticed that in case X>k or X<k it works perfectly, but when X=k, $$r_1$$ could be 0/1 and $$r_2$$ could be 0/1 and if they both are not 1 and 1 $$r_3$$ should be 0 and it is false. – Sou Mar 27 '19 at 18:04
  • Pls check again. – Maksim Mar 27 '19 at 18:18
  • Yes, it works in all cases!! Thank you so much. If possible, I would like to know if you find these solution just by intuition (experience) or there is a method that helps (I am relatively new and sometimes I get stuck!) . Thkx again. – Sou Mar 27 '19 at 19:20