# 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 tried hat and it still didn't calculate the b values correctly. Just for clarification do you mean my constraint should be: 0b1+a1b2+a2b3 - d <= 0 or 0b1+a1b2+a2b3 <= 0? And also the interval borders would be problematic. I would need: b1 -> 0 <= d <= a1; b2 -> a1 < d <= a2; b3 -> a2 < d <= a3; – Koli Jul 21 at 14:22
• I updated my answer. You cannot avoid the ambiguous borders without introducing a small tolerance $\epsilon>0$. – RobPratt Jul 21 at 16:11
• Ok I tried changing the first constraint again and now GUROBI concluded that my program is infeasible – Koli Jul 22 at 7:53
• Ok, how would the constraints change if I wanted to simplify my situation: Let's say I want s = ... if d <= a_1 and s=0 if d > a1 I'm sorry if my questions are dumb but I am really new to this topic – Koli Jul 22 at 7:59
• 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