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

Calculation of a variable with another variable (VRP)

回答済み

コメント

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 try Gurobot, our chatbot interface offering instant, expert-level support.
  • Jaromił Najman
    • Gurobi Staff

    Hi Lars,

    The KB article How do I model conditional statements in Gurobi? describes one possible solution to your issue.

    You would have to define 2 new binary variables \(b_{1,i},b_{2,i}\) and model the relationships \(\texttt{if et[i] <= t[i] then b1[i] = 1 else b1[i] = 0}\) and \(\texttt{if t[i] <= lt[i] then b2[i] = 1 else b2[i] = 0}\).
    You can then use Gurobi's addGenConstrAnd method to model \(\texttt{if b1[i]=1 and b2[i]=1 then u[i] = 1 else u[i] = 0}\).

    Best regards, 
    Jaromił

     

    0
  • Lars Sauerteig
    • Gurobi-versary
    • First Comment
    • First Question

    Hi Jaromił,

    thank you for your answer. I tried implementing your idea based on the article you linked like this:

    mdl.addConstrs((t[i] >= et[i] + 0.0001 - M * (1 - ul[i])) for i in N)
    mdl.addConstrs((t[i] <= et[i] + M * ul[i]) for i in N)
    mdl.addConstrs((lt[i] >= t[i] + 0.0001 - M * (1 - uu[i])) for i in N)
    mdl.addConstrs((lt[i] <= t[i] + M * uu[i]) for i in N)
    mdl.addGenConstrAnd((u[i], [uu[i], ul[i]], "andconstr") for i in N)

    When I try to run the model, I get the error:
    TypeError: addGenConstrAnd() takes at least 3 positional arguments (2 given)

    What is the problem with the code?

     

    Best regards,

    Lars

    0
  • Jaromił Najman
    • Gurobi Staff

    Hi Lars,

    You have to respect the syntax of the addGenConstrAnd method. It adds only 1 constraint at a time. Thus, your code should look similar to

    for i in N:
     mdl.addGenConstrAnd(u[i], [uu[i], ul[i]], "andconstr")

    Alternatively, you can use the addConstrs overload and write

    mdl.addConstrs(u[i] == and_([uu[i], ul[i]]) for i in N, "addconstr")

    Best regards, 
    Jaromił

    0
  • Lars Sauerteig
    • Gurobi-versary
    • First Comment
    • First Question

    Hi Jaromił,

    thank your for your help! The code is now working the way I want it to work.

    Best regards,
    Lars

    0

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