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

how to get time value from multidict to my objective function?

回答済み

コメント

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 why not try our AI Gurobot?.
  • Eli Towle
    • Gurobi Staff

    The \( \texttt{time} \) returned by multidict() is a \( \texttt{dict} \), not a \( \texttt{list} \):

    >>> time
    {('A', 'B'): 123, ('A', 'C'): 134, ('B', 'D'): 345, ('C', 'D'): 234, ('C', 'E'): 453, ('E', 'F'): 345, ('E', 'B'): 643, ('F', 'C'): 332}

    When you pass \( \texttt{time} \) to Model.addVars(), Gurobi uses the dictionary keys as indices. This isn't desirable here, since \( \texttt{time.keys() == location} \).

    Can you post the full list of variable indices that you want to use? You won't be able to use the \( \texttt{time} \) values as indices, since \( 345 \) is used twice and would result in duplicate variable keys:

    >>> time.values()
    [123, 134, 345, 234, 453, 345, 643, 332]

    Also, the code for the objective function doesn't quite make sense. You should not set the objective function in a \( \texttt{for} \) loop, since you can only have one objective function. Maybe the \( \texttt{for k in set_truck} \) should be in the quicksum() function?

    0
  • John Raphy Karippery
    • Gurobi-versary
    • Investigator
    • Collaborator

    Thank you for your reply.

    value of time is just random

    Variable :


    \(x_{i,j,t}^k\) Binary variable=1, if truck  k  traverses edge  e_{ij}  at time t


    Does that mean the code will be something like this?

    mdl.modelSense = GRB.MINIMIZE
    mdl.setObjective(quicksum(x[i,j,k,t]* cost[i,j] for i,j in location for t in time for k in set_truck))




    0
  • Eli Towle
    • Gurobi Staff

    I don't see a reason to use multidict() to generate a dictionary that maps locations to times. Such a dictionary makes sense for costs, but not times.

    I suspect you want something like the following, assuming your time indices are \( \{0, \ldots, 4\} \):

    trucks = ["trucks1", "trucks2", "trucks3"]
    times = list(range(5))
    cost = {("A", "B"): 100,
            ("A", "C"): 70,
            ("B", "D"): 82,
            ("C", "D"): 95,
            ("C", "E"): 100,
            ("E", "F"): 77,
            ("E", "B"): 85,
            ("F", "C"): 50}
    locations = cost.keys()

    # ...

    x = mdl.addVars(locations, trucks, times, vtype=GRB.BINARY, name="x")

    This creates \( 120 \) variables (\( 8 \) locations \( \times \) \( 3 \) trucks \( \times \) \( 5 \) times) with the following indices:

    (A, B, trucks1, 0)
    (A, B, trucks1, 1)
    (A, B, trucks1, 2)
    (A, B, trucks1, 3)
    (A, B, trucks1, 4)
    (A, B, trucks2, 0)
    (A, B, trucks2, 1)
    (A, B, trucks2, 2)
    (A, B, trucks2, 3)
    (A, B, trucks2, 4)
    (A, B, trucks3, 0)
    (A, B, trucks3, 1)
    (A, B, trucks3, 2)
    (A, B, trucks3, 3)
    (A, B, trucks3, 4)
    (A, C, trucks1, 0)
    (A, C, trucks1, 1)
    ...
    (F, C, trucks2, 4)
    (F, C, trucks3, 0)
    (F, C, trucks3, 1)
    (F, C, trucks3, 2)
    (F, C, trucks3, 3)
    (F, C, trucks3, 4)

    Is this what you're trying to do?

    Yes, your modified objective function makes more sense.

    0
  • John Raphy Karippery
    • Gurobi-versary
    • Investigator
    • Collaborator

    Still I have syntax error with my objective function why?


    set_time =[123, 134, 345, 234, 453, 346, 643, 332]
    mdl.modelSense = GRB.MINIMIZE
    mdl.setObjective(quicksum(x[i,j,k,t]* cost[i,j] for i,j in location for t in set_time for k in set_truck))  
    0
  • Eli Towle
    • Gurobi Staff

    The objective function and syntax discussion is continued in a separate community post.

    0

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