# Link a binary variable to continuous variable in Java Gurobi

I have the following problem:

Depending on my continuous optimization variable $$S_m$$, I would like to introduce a binary variable $$x_m$$, which, depending on the value of $$S_m$$ (greater or less than 0) receives the value 0 or 1. The whole thing is to be repeated for 12 months. I have already created both variables, I just don't know how to link them, so that $$x_m$$ has no influence on $$S_m$$, and the value for $$x_m$$ depends purely on the value of $$S_m$$.

The easiest would probably be to model it as follows:

$$S_m \leq a + bx_m \\ S_m \geq ax_m$$

where $$a$$ is the switch and $$a+b$$ is the upper bound for variable $$S_m$$. In this case, if $$S_m< a$$, then $$x_m$$ has to be 0 (otherwise this is infeasible). Similarly, if $$S_m > a$$, then $$x_m$$ has to be 1. The key question here is what you want to happen if $$S_m = a$$, but that is something that depends on your business requirements.

To answer your questions, you can model these things using a simple linear constraint, see the Gurobi Java documentation here.

• what exactly do you mean by a is the switch. Or I have just a thinking error. My lower limit is -100, the upper limit is 150, but as I said, the question is whether S_m is greater than or less than zero, and thus whether x_m is 1 or 0, which I need in other constraints. Mar 5 at 14:16
• Ahh, ok. In that case you would have $a=0$ and $b=150$: $$S_m \leq 150x_m \\ S_m \geq -100 + 100x_m$$ You still have to decide what to do at $S_m=0$ though, because that behaviour will depend on the objective function. Mar 8 at 8:28

In addition to what @Richard mentioned:

1. You can consider $$a=0$$ and $$b=\epsilon$$ in @Richard's answer.
2. Here you can find how special functions in the constraint can be modeled using Gurobi. Yours is defining an indicator variable for $$S_m$$ to find out whether the value of your variable is positive or negative.

Edit: you can model it as follow:

$$S_m \leq 150x_m-\epsilon(1-x_m)$$ $$-100(1-x_m)+\epsilon x_m\leq S_m$$

A positive $$S_m$$ will force $$x_m=1$$ via the first constraint. A negative $$S_m$$ will make $$x_m=0$$ while the second constraint is active.

• what do you mean by consider 𝑏=𝜖 ? what does 𝜖 stands for? Mar 5 at 14:21
• @Handballer73 it is a very small positive constant value. Mar 5 at 14:22
• thanks a lot, i will try tomorrow Mar 5 at 15:06
• thanks again! I understand the equations. Only how do I implement this exactly in Gurobi (Java). I add the code in a new answer..maybe someone can have a look. Also, is there a java command for this very small constant or just enter a very small value? Mar 7 at 10:12
for (int m = 0; m < (int)this.Anzahl_Monate; m++)
{
constra = new GRBLinExpr();

• I think it would be worth pointing out that that 0.000001 was chosen as the tolerance to break the equality at $S_m=0$. Mar 8 at 8:31