# MIP binary decision value under a condition if else

In MIP, I have a constraint that is validated only if the demand $$d_i\leq Q$$: C1 : $$d_i \leq x_i \leq Q$$ where $$x_i \geq 0$$. I tried to introduce big M with binary variables as follows: $$My \leq x_i \leq Q - d_i$$ Where $$M=|Q-\min{(d_i)}|$$ The problem where $$d_i > Q$$ then $$x_i$$ is equal to zero but if $$d_i \leq Q$$, $$x_i$$ should be between $$d_i$$ and $$Q$$. Any help please to how formulate this constraint. Thank you

I'm going to drop the subscript $$i$$ to save a little typing, and I will assume that both $$d$$ and $$Q$$ are nonnegative (which was not actually stated). I will also assume that $$Q$$ is a constant. $$d$$ can be either a constant or a variable. Given a valid upper bound $$M$$ (presumably $$\ge Q)$$ for $$d,$$ we introduce a binary variable $$y$$ together with the constraints $$d \le Qy + M(1-y)\quad (1)$$ $$d \ge Q(1-y)\quad (2)$$ $$0 \le x \le Qy \quad (3)$$ and $$x \ge d-M(1-y). \quad (4)$$ If $$y=1,$$ then (1) and (2) imply $$0 \le d \le Q$$ and (3) and (4) imply $$d \le x \le Q.$$ If $$y=0,$$ then (1) and (2) imply $$Q \le d \le M$$ and (3) and (4) imply $$x = 0.$$
Note that $$d = Q$$ can occur with either value of $$y,$$ meaning $$d=Q$$ and $$x=0$$ is feasible (with $$y=0$$). Depending on the objective function and the rest of the model, this many not be a problem. If it is, your best recourse is to change (2) to $$d \ge (Q+\epsilon)(1-y)\quad (2')$$for some small positive value of $$\epsilon.$$ This will ensure that $$y=0$$ (and thus $$x=0)$$ occurs only when $$d$$ is strictly greater than $$Q.$$ Unfortunately, it will also make any solution with $$Q < d < Q+\epsilon$$ infeasible.