# How do I formulate constraints that check if a parameter is between certain values, using binary variables?

I have $$3$$ parameters $$a_1,a_2,a_3$$ and a variable $$d$$ and $$3$$ binary variables $$b_1,b_2,b_3$$ and a "result" variable $$s$$. How do I model constraints so that:

• If $$d$$ is between $$0$$ and $$a_1$$, then $$s=1$$
• If $$d$$ is between $$a_1$$ and $$a_2$$, then $$s =$$ ... complex calculation ...
• If $$d$$ is between $$a_2$$ and $$a_3$$, then $$s = 0$$

I have found a formulation that is as follows: \begin{align} a_1b_1 + a_2b_2 - d \le 0 \\ -a_1b_1 - a_2b_2 - a_3b_3 + d \le 0 \\ b_1 + b_2 + b_3 = 1 \end{align}

But if use this formulation in my model, the b-variables get calculated incorrectly. Does anyone see the reason why and/or has a better idea?

• If $d$ and $a_i$ are parameters, then you have no modeling to do, the values are known a priori so you can deduce $s$ while pre processing. – Kuifje Jul 21 at 13:39
• d is a variable. I made a mistake in the Post. I will edit it sorry. – Koli Jul 21 at 13:58
• Does the "complex calculation" for $s$ involve other decision variables, or is it constant? – RobPratt Jul 22 at 13:33

It looks like your first constraint should instead be $$0b_1 + a_1 b_2 + a_2 b_3 - d \le 0$$ With this change, the logical implications are \begin{align} b_1 = 1 &\implies 0 \le d \le a_1 \\ b_2 = 1 &\implies a_1 \le d \le a_2 \\ b_3 = 1 &\implies a_2 \le d \le a_3 \end{align}
To avoid ambiguous borders, introduce a small tolerance $$\epsilon>0$$ and impose instead $$0 b_1 + (a_1+\epsilon) b_2 + (a_2+\epsilon) b_3 - d \le 0$$
• I updated my answer. You cannot avoid the ambiguous borders without introducing a small tolerance $\epsilon>0$. – RobPratt Jul 21 at 16:11
• I suspect that the infeasibility arises from your constraints that link $s$ to $b$. Try solving without those first to see if the problem is then feasible. – RobPratt Jul 22 at 13:01