2
$\begingroup$

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

$\endgroup$
1
1
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ what do you mean by consider 𝑏=𝜖 ? what does 𝜖 stands for? $\endgroup$ – Handballer73 Mar 5 at 14:21
  • $\begingroup$ @Handballer73 it is a very small positive constant value. $\endgroup$ – Oguz Toragay Mar 5 at 14:22
  • 1
    $\begingroup$ thanks a lot, i will try tomorrow $\endgroup$ – Handballer73 Mar 5 at 15:06
  • $\begingroup$ 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? $\endgroup$ – Handballer73 Mar 7 at 10:12
1
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$ – Handballer73 Mar 5 at 14:16
  • $\begingroup$ 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. $\endgroup$ – Richard Mar 8 at 8:28
1
$\begingroup$
for (int m = 0; m < (int)this.Anzahl_Monate; m++)
    {
        constra = new GRBLinExpr();
        constra.addTerm(150, x_m[m]);
        constra.addTerm(0.000001, x_m[m]);
        constra.addConstant(-0.000001);
        
        model.addConstr(constra, GRB.GREATER_EQUAL, S_m[m], "a");
        
        constrb = new GRBLinExpr();
        constrb.addTerm(100, x_m[m]);
        constrb.addConstant(-100);
        constrb.addTerm(0.000001, x_m[m]);
    
        model.addConstr(constrb, GRB.LESS_EQUAL, S_m[m], "b" + m);
         
    }
$\endgroup$
1
  • $\begingroup$ 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$. $\endgroup$ – Richard Mar 8 at 8:31

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.