メインコンテンツへスキップ

How to define if constraint with decision varibales?

回答済み

コメント

7件のコメント

  • 正式なコメント
    Simranjit Kaur
    • Gurobi Staff
    This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support.
  • Jaromił Najman
    • Gurobi Staff

    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
  • Ariel W
    • Gurobi-versary
    • Curious
    • Conversationalist

    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
  • Jaromił Najman
    • Gurobi Staff

    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
  • Ariel W
    • Gurobi-versary
    • Curious
    • Conversationalist

    I got it! Thank you, Jaromił Najman!

    0
  • Ariel W
    • Gurobi-versary
    • Curious
    • Conversationalist

    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
  • Jaromił Najman
    • Gurobi Staff

    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

投稿コメントは受け付けていません。