Skip to main content

Conditional stament in Gurobi

Answered

Comments

3 comments

  • Maliheh Aramon
    • Gurobi Staff Gurobi Staff

    Please check the article How do I model conditional statements in Gurobi? which formulates a constraint very similar to the one you are interested in. 

    Best regards,

    Maliheh

     

    0
  • Nick Fryganiotis
    • Gurobi-versary
    • Curious
    • First Comment

    Thanks for the comment. I have seen the article you mention. The problem is that in my own case, the variable $z_s(t)$ is upper and lower bounded. I am not quite sure if that means that i need two variables $b_1$, and $b_2$, one for each bound respectively, and then a new variable $\hat_{b} = b_1 \cdot b_2$. Is there a simpler way? 

    Thank you in advance,

    Nick

    0
  • Maliheh Aramon
    • Gurobi Staff Gurobi Staff
    • If the continuous variable \(z_s(t)\) is defined with lower bound 0, you do not need any additional binary variable.
    • If the variable \(z_s(t)\) can take negative values as well, you then need two binary variables:

    \[\mbox{if}~ z_s(t) < 0 \rightarrow b_0=0, ~\mbox{and if}~ z_s(t) \geq 0 \rightarrow b_0=1\]

    \[\mbox{if}~ z_s(t) < ht_s \rightarrow b_1=1, ~\mbox{and if}~ z_s(t) \geq ht_s \rightarrow b_1=0\]

    You then need to define the binary variable \(b_2\) to equal 1 if both \(b_0\) and \(b_1\) are equal to 1. You can rely on the API for the simple general constraint And(). Finally you can model

    \[\mbox{if}~ b_2=1 \rightarrow sr_s(t)=x_s(t), ~\mbox{and if}~ b_2= 0 \rightarrow sr_s(t)=0\]

    Best regards,

    Maliheh

    0

Please sign in to leave a comment.