How to define if constraint with decision varibales?
回答済みHow do i express that "If w[d,0,j]==1 then p[d,j]==0, where w[d,i,j] is a decision variable"?
-
If \(w_{d,0,j}\) is a binary variable then you can use an indicator constraint.
The code
model.addGenConstrIndicator(w, True, p, GRB.EQUAL, 0)
would introduce the indicator constraint
\[\begin{align*}
w = 1 \rightarrow p = 0
\end{align*}\]meaning "if w = 1 then p = 0".
Is \(w\) a binary variable in your case?
0 -
Thanks for your answer. Yes, w is a binary variable and p is a continous variable. For p is also a decision variable, is the method to define still the same?
0 -
For p is also a decision variable, is the method to define still the same?
Yes, it is no problem that \(p\) is an optimization variable.
0 -
I got it! Thank you, Jaromił Najman!
0 -
Hello, Jaromił: I still have a question: when ∑i∈S (w[d,i,j]) = 0, p[d,j]=0. How do i define this situation?
I tried to follow the way you wrote above, but it proposed "TypeError: 0.0 is not a variable".
for d in range(1, 3):
for j in range(1,4):
expr = quicksum(w[d,i,j] for i in range(1,2) if i < j)
model.addGenConstrIndicator(expr, False, p[d, j], GRB.EQUAL, 0)
expr.clear()0 -
Hi Ariel,
Your approach does not work because the addGenConstrIndicator method accepts only a single variable as input for the binary indicator, i.e., a sum or any other expression is not allowed.
You will have to model your conditional by hand. To do that you can introduce a new binary variable \(b\) and a constant parameter \(M\) which equals the number of \(w\) variables in your sum. You can then model
\[\begin{align*}
\sum w \geq b\\
\sum w \leq M\cdot b\\
b = 0 \rightarrow p = 0
\end{align*}\]In the above \(b\) is forced to \(0\) when \(\sum w = 0\) and to \(1\) when \(\sum w > 0\). The last term can be introduced via the addGenConstrIndicator method because \(b\) is a single binary optimization variable.
Best regards,
Jaromił0
サインインしてコメントを残してください。
コメント
6件のコメント