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

Defining a set dependent on an other set

ユーザーの入力を待っています。

コメント

6件のコメント

  • 正式なコメント
    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.
  • Jonasz Staszek
    • Community Moderator
    • Gurobi-versary
    • Thought Leader
    • First Question

    Hi Tiril,

    you would need two indices per variable for that - one for the commodity and the other one for the route. You implement it just like any other variable:

    import gurobipy as gp

    m = gp.Model()

    R = dict()
    R[1] = [0, 1, 2, 3]
    R[2] = [0,1,2,3,4,5,6]

    K = [1, 2]
    x = gp.tupledict()
    for k in K:
    for r in R[k]:
    x[k, r] = m.addVar(vtype="B", name=f"x_{k}_{r}")

    Best regards
    Jonasz

    0
  • Tiril Amundsen
    • Gurobi-versary
    • First Comment
    • First Question

    Hi, thank you for the answer! But when running only the above I get the error message "KeyError: 0". It seems to be for the "for r in R[k].

    0
  • Jonasz Staszek
    • Community Moderator
    • Gurobi-versary
    • Thought Leader
    • First Question

    You're right, I just corrected the error.

    Best regards
    Jonasz

    0
  • Tiril Amundsen
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you so much. Another question arised when implementing the complete model. I have a table of variables that is dependent on both commodity, routes and nodes. When adding constraints I have to sum over these sets, but I'm not sure how to model the matrices. Until now I have defined them for each commodity, like this: M1[r,n], M2[r,n], but I'm not sure how I can also sum over k in commodities.  

    0
  • Jonasz Staszek
    • Community Moderator
    • Gurobi-versary
    • Thought Leader
    • First Question

    I'm not sure I understood your challenge.

    If M1 and M2 were corresponding to various nodes, you could instead have them as on dict with a 3-tuples as keys, for example, the following way

    # for illustration only, not replicable
    M = dict()
    for n in nodes:
    for r in routes:
    for k in commodities:
    M[n, r, k] = ...

    If that's not what you have trouble with, I'd need a better clarification, perhaps with a code example.

    Best regards
    Jonasz

    0

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