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

Multiple "or" statements within a constraint

回答済み

コメント

5件のコメント

  • 正式なコメント
    Simranjit Kaur
    • Gurobi Staff
    This post is more than three 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 why not try our AI Gurobot?.
  • Jaromił Najman
    • Gurobi Staff

    Hi Yaren,

    The Python \(\texttt{or}\) operator does not work with Gurobi's Python syntax. You could use the addGenConstrOr function. However, it requires all input variables to be binary making it quite hard to use directly in your case.

    I guess that you are trying to model that at most one of the 4 sum terms is equal to \(\texttt{f_yi}\), is this correct? For this, you could introduce additional binary variables \(p_{s,i,y}\) which equals 1 if service is provided by provider \(s\) and \(0\) otherwise. You can then introduce the indicator constraints for each \(s\)

    (p[s,i,y] = 1) -> (sum(x[t,s,i,y] for t in range(...) == f_yi[i,y])

    To make sure that no-one is receiving service from multiple providers, you can add

    sum(p[s,i,y] for s in S) <= 1 # limit number of providers

    In the case that \(\texttt{f_yi}\) is a scalar, you can use the equality constraint

    sum(x[t,s,i,y] for t in range(...) == f_yi[i,y]*p[s,i,y]

    instead of the indicator constraint. This additionally forces all \(\texttt{x[t,s,i,y]}\) values to 0.

    Best regards,
    Jaromił

    0
  • Yaren Bilge Kaya
    • Gurobi-versary
    • First Comment
    • First Question

    Hello Jaromił, 

    Thank you so much for your response, I really appreciate it! I'm fairly new to Gurobi, wasn't aware that the "or" statement wasn't working the way I wanted and thanks for pointing it out. And you're right, addGenConstrOr function doesn't work for my case.

    I think the only problem I'm facing right now is that: p_s,i,y is a variable object and if I'm not mistaken, p_s,i,y couldn't be evaluated to a value in the "if" statement. Therefore unfortunately, I don't know how to write the following statement you recommended. 

    (p[s,i,y] = 1) -> (sum(x[t,s,i,y] for t in range(...) == f_yi[i,y])

    And I'm receiving this following error:

    Best regards,

    Yaren

    0
  • Jaromił Najman
    • Gurobi Staff

    Hi Yaren,

    Sorry for not being more clear from the beginning. You are correct and variable objects cannot be used in \(\texttt{if}\)-statements. You can model indicator constraints via the addGenConstrIndicator function as

    model.addGenConstrIndicator(p[s,i,y], True, sum(x[t,s,i,y] for t in range(...) == f_yi[i,y], GRB.EQUAL, 1.0)

    or directly via addConstr

    model.addConstr((p[s,i,y] == 1) >> (sum(x[t,s,i,y] for t in range(...) == f_yi[i,y]))

    Best regards,
    Jaromił

    0
  • Yaren Bilge Kaya
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you so much Jaromił, this was really helpful!

    Best,

    Yaren

    0

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