I am trying to create a binary variable ($z$) within a linear model which 'switches' on only when another variable ($d$) becomes greater than zero.

I have had some success using the following two constraints (where $M_l$ and $M_u$ are lower and upper bounds of $d$):

$$zM_u \ge d$$

$$\left(z-1\right)M_l \le d$$

This seems to work for values less than zero ($z$ gets set to zero) and for values greater than zero ($z$ gets set to one). However it doesn't properly constrain $z$ when $d$ is zero, arbitrarily setting it to either 0 or 1.

Is there some way I can change (or replace) these inequalities to force the constraint I want?



With a numerical solver, you will have issues around zero no matter how you model it. Solvers have tolerances and strict inequalities are not possible. Sure, you can crank up the tolerances, but you end up with the same problem in the end anyway. You models are correct in theory (except around $0$)

Sometimes it can help to use margins and have three cases, negative, problematic around zero and positive. Hence, instead of working with one $0/1$ variables, you introduce three of them, with the constraint that they sum up to $1$. The first is activated when you are significantly negative, the second when you are in a small neighbourhood around zero, and the third when you are sufficiently positive. You will then have a clear separation of the negative and positive case (assuming the region around zero is significantly large w.r.t solver feasibility tolerances) and your variable $d$ would correspond to the third binary being activated. What you should do when the second indicator is activated depends on the application.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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