gurobi indicator constraints multiple variables
回答済みHello,
I would like to add an indicator condition to my model. This is to sum over a variable and if the sum is greater than 0 set a binary variable to 1
My idea:
z and r are binary variables
routes[t][k] returns the corresponding indexes for the z variable
t and k are two superior for loops
mod.addConstr((gp.quicksum(z[i,t,k] for i in routes[t][k]) >= 1) >> (r[t,k] == 1))
-
Hi Leon,
This is discussed in the Knowledge Base article How do I model conditional statements in Gurobi?
In particular, you will not need an \(\epsilon > 0\), since your condition is a \(\geq\) and not a strict inequality.
Moreover, if your condition reads "if sum is greater or equal than 0 then set b = 1 else b = 0", then you don't need the additional indicator variables discussed in the linked Knowledge Base article, because \(b\) then already holds the value of your binary.
In your special case, you can use the constraint
\[\begin{align*}
\sum z \leq M \cdot r
\end{align*}\]where \(M\) is a big-M constant equal to the number of binary variables \(z\). This way, if \(\sum z \geq 1\) holds, then \(r\) has to be set to \(1\).. Note that in this case if \(\sum z = 0\), then \(r\) can be \(0\) or \(1\).
Best regards,
Jaromił1 -
Hi Jaromił,
Thanks for your help!
Best regards,
Leon0
サインインしてコメントを残してください。
コメント
2件のコメント