Mixed linear programming if...then

215 Views Asked by At

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.

2

There are 2 best solutions below

9
On BEST ANSWER

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

3
On

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$