Conditional quadratic constraints
回答済みHow would we formulate the following logical expression in Gurobi:

The if-else logic can be solved by the following expressions (one has to know how to approximate BigM parameter well):

Now we have the binary variable x(m) which is equal to 1 if:

and is equal to 0 in the else condition.
How can we use this binary variable x(m) to activate the expression (3) when x(m) = 1:

and to activate the expression (4) when x(m) = 0:


Thank you!
-
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\):
- \[b = \begin{cases} 1 & \text{if} \; \; x \geq y \notag \\ 0 & \text{if}\;\; x < y \end{cases}\]
- \[\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 -
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 -
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 -
Hi Maliheh,
thanks for your solution using auxiliary variables, it solved the problem!
Kind regards,
Luka
0
サインインしてコメントを残してください。
コメント
4件のコメント