Skip to main content

Conditional quadratic constraints

Answered

Comments

4 comments

  • Maliheh Aramon
    • Gurobi Staff

    Hi Luka, 

    Using less-crowded and simpler notations, it seems that you are interested in modelling:

    \[\pi = \begin{cases} f(x, y) & \text{if} \; \; x \geq y \notag \\ g(x, y) & \text{if}\;\; x < y \end{cases}\]

    You can model this in two steps by defining an auxiliary binary variable \(b\):

    1. \[b = \begin{cases} 1 & \text{if} \; \; x \geq y \notag \\ 0 & \text{if}\;\; x < y \end{cases}\]
    2. \[\pi = \begin{cases} f(x, y) & \text{if} \; \; b = 1 \notag \\ g(x, y) & \text{if}\;\; b=0\end{cases}\]

    The expression in 2) matches the signature of the Gurobi Python API for the indicator constraints, i.e., the Model.addGenConstrIndicator() method, because the indicator variable \(b\) is binary. However, the expression in 1) does not.

    To model the expression in 1) using the Gurobi Python API for the general constraints, you can model its contrapositive instead which is logically equivalent to expression 1).

    \[\begin{cases} x \ngeq y & \text{if} \; \; b \neq 1  \notag \\  x \nless y& \text{if}\;\;  b \neq 0 \end{cases} \rightarrow \begin{cases} x < y & \text{if} \; \; b = 0  \notag \\  x \geq y& \text{if}\;\;  b= 1 \end{cases}\]  

    Best regards,

    Maliheh

    0
  • Luka Budin
    • Gurobi-versary
    • First Comment
    • First Question

    Hi Maliheh,

    If I model the expression 2. using the Model.addGenConstrIndicator() method I get the following error in Python: "TypeError: must be a real number, not gurobipy.QuadExpr".

    I have a quadratic constraint that has to be evaluated when b = 1 and another quadratic constraint when b = 0. Can't model these types of constraints with Model.addGenConstrIndicator() method.

    Are you maybe familiar with some other approach?

     

    Kind regards,

    Luka

    0
  • Maliheh Aramon
    • Gurobi Staff

    Yes, the Gurobi API for indicator constraints only supports the linear constraints directly. I guess your quadratic expressions of interest are the LHS of equations (3) and (4) in your original post. 

    You can again take advantage of auxiliary variables. You can define an auxiliary variable \(z_{(m)}\) where:

    \[z_m = \big(E^{p, VT}_{(m)} + E^{p, NT}_{(m)}\big) \times \pi_{(m)}\]

    You should be then able to use the indicator API to model the following:

    \[z_m = 0.9 \times \big(E^{p, VT}_{(m)} \times \lambda^{VT} + E^{p, NT}_{(m)} \times \lambda^{NT} \big), \; \; \; \text{if}\;\; b = 1\]

    The same idea can be used for the case where \(b=0\). You would need to define another set of auxiliary variables. 

    Best regards,

    Maliheh

    0
  • Luka Budin
    • Gurobi-versary
    • First Comment
    • First Question

    Hi Maliheh,

    thanks for your solution using auxiliary variables, it solved the problem!

     

    Kind regards,

    Luka

    0

Please sign in to leave a comment.