Skip to main content

Questions about conditional statements in gurobi

Answered

Comments

3 comments

  • Maliheh Aramon
    • Gurobi Staff

    Hi Kewei,

    The answer to your question is yes, assuming the function \(YSAL_t\) has a close-form and is not a black-box function. 

    Gurobi has an API for indicator constraints but the indicator variable should be binary. To implement conditional statements in the form you defined, you would need to rely on auxiliary binary variables. 

    • Assuming variable \(m\) is an integer variable, define the binary variable \(b^m_i\) being equal to 1 if \(m = m_i\) and 0, otherwise. 
    • Assuming variable \(w\) is an integer variable, define the binary variable \(b^w_j\) being equal to 1 if \(w = w_j\) and 0, otherwise. 
    • Ensure variables \(m\) and \(w\) take exactly one value via constraints \(\sum_i b^m_i = 1\) and \(\sum_j b^w_j = 1\).
    • To implement the condition in the form \[\mbox{if}~ m = m_2 ~\mbox{and}~ w = w_5, \mbox{then}~ RD_t = 0.00015 * YSAL_t^{0.708}\], you can instead implement \[\mbox{if}~ b^m_2 = 1 ~\mbox{and}~ b^w_5 = 1, \mbox{then}~ RD_t = 0.00015 * YSAL_t^{0.708}\] To do so, you would need to define an additional binary variable \(z_{m2w5} = \mbox{and}(b^m_2, b^w_5)\) and implement it using the Model.addGenConstrAnd() method. You would then have \[\mbox{if}~ z_{m2w5} = 1, \mbox{then}~ RD_t = 0.00015 * YSAL_t^{0.708}\] which can be implemented using the Model.addGenConstrIndicator() method. Similar steps should be repeated to implement all twelve conditions. 

    Best regards,

    Maliheh

    0
  • Kewei Fan
    • Gurobi-versary
    • First Question
    • Conversationalist

    Thank you very much for your answer, I have solved my problem, but there is a new problem, that is, gurobi seems to be unable to deal with this power function problem, which code should I use to implement this operation? At present, I write RD_t[t] == 0.00131 * YSAL_t[t]**0.624 according to the code habit of python, and will report an error "unsupported operand type(s) for ** or pow(): 'Var' and 'float'”

     

    0
  • Maliheh Aramon
    • Gurobi Staff

    Your recent question was already answered in your other post. We would appreciate it if you avoid cross-posting the same question. 

    Best regards,

    Maliheh

     

    0

Please sign in to leave a comment.