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?

  • $\begingroup$ 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. $\endgroup$
    – Kuifje
    Jul 21, 2021 at 13:39
  • $\begingroup$ d is a variable. I made a mistake in the Post. I will edit it sorry. $\endgroup$
    – Koli
    Jul 21, 2021 at 13:58
  • $\begingroup$ Does the "complex calculation" for $s$ involve other decision variables, or is it constant? $\endgroup$
    – RobPratt
    Jul 22, 2021 at 13:33

1 Answer 1


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$$

  • $\begingroup$ 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; $\endgroup$
    – Koli
    Jul 21, 2021 at 14:22
  • $\begingroup$ I updated my answer. You cannot avoid the ambiguous borders without introducing a small tolerance $\epsilon>0$. $\endgroup$
    – RobPratt
    Jul 21, 2021 at 16:11
  • $\begingroup$ Ok I tried changing the first constraint again and now GUROBI concluded that my program is infeasible $\endgroup$
    – Koli
    Jul 22, 2021 at 7:53
  • $\begingroup$ 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 $\endgroup$
    – Koli
    Jul 22, 2021 at 7:59
  • $\begingroup$ 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. $\endgroup$
    – RobPratt
    Jul 22, 2021 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.