# Enforcing discontinuous range for a variable

I want to enforce the following ranges for a set of variables, where the ranges for each variable are discontinuous. For example, I have two sets of ranges for each variable,

\begin{align}\sf LB_1 &\le \sf variable\,1 \le UB1\\\sf LB_2 &\le\sf variable\,1 \le UB2\end{align}

where $$\sf LB$$ and $$\sf UB$$ are the lower and upper bounds respectively. As an example, for a variable $$x$$, I have the following ranges

\begin{align}0&\le x \le 20\\31&\le x \le 59.\end{align}

Then, I can introduce a binary variable $$U$$ and write the following constraint:

$$U\times{\sf LB_1}+(1-U)\times{\sf LB_2} \le x \le U\times\sf{UB_1}+(1-U)\times {\sf UB_2}.$$

However, if I have three ranges, where $$x$$ can be 0 or $$2\le x\le 11$$ or $$45\le x\le 79$$, how do I write a constraint for three ranges for a single variable?

Introduce three binary variables $$u_i$$ and the following linear constraints: \begin{align} \sum_i u_i &= 1\\ 0 u_1 + 2 u_2 + 45 u_3 \le x &\le 0 u_1 + 11 u_2 + 79 u_3 \end{align} If you prefer, you can omit $$u_1$$ and replace the first constraint with $$\le$$: \begin{align} u_2 + u_3 &\le 1\\ 2 u_2 + 45 u_3 \le x &\le 11 u_2 + 79 u_3 \end{align}