Modelling a range constraint based on a value

467 Views Asked by At

I'm trying to model a constraint based on a range. I have a lp variable $x_1$ with range $[0,60]$, and a binary variable $y_1$.

$y_1$ must be $1$ iff $x_1$ belongs to the range $[25,35]$. How do I add this constraint? I've tried this : $$x_1>25y_1, x_1 < 35+ 25(1-y_1).$$ While this ensures that $y_1$ is zero if $x_1$ is not in the required range, it does not ensure $y_1$ is $1$ otherwise. I could try to maximize $y_1$, but was hoping that there would be a way without having to depend on the solver for setting $y_1$ to $1$.

1

There are 1 best solutions below

2
On BEST ANSWER

Introduce two additional binary variables: $y_2=1$ iff $x_1 \ge 25$, and $y_3=1$ iff $x_1 \le 35$. Enforce these definitions with the following constraints: $$\begin{align} x_1 - 25 & \le My_2 \\ 25 - x_1 & \le M(1-y_2) \\ 35 - x_1 & \le My_3 \\ x_1 - 35 & \le M(1-y_3) \end{align}$$ where $M$ is a large constant.

Now require $y_1=1$ iff $y_2=y_3=1$: $$\begin{align} y_1 & \ge y_2 + y_3 - 1 \\ y_1 & \le y_2 \\ y_1 & \le y_3 \end{align}$$

Note that these constraints might fail at the boundaries, if $x_1=25$ or $35$. Hopefully this is OK. If not, this answer on OR.SE might help with the equality cases.