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

Generate multidict using for loop in Gurobi

回答済み

コメント

2件のコメント

  • 正式なコメント
    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

    The multidict function generates multiple dictionaries out of a single larger dictionary data. You can construct the dictionaries via \(\texttt{for}\)-loops

    from gurobipy import *

    number_of_vehicles = 2
    vehicles_origin = [4, 6]
    vehicles_destination = [3, 0]
    total_time_vehicle = [35, 27]

    truck = [i for i in range(number_of_vehicles)]
    starting_node = {}
    destination_nodes = {}
    time = {}
    for i in truck:
    starting_node[i] = vehicles_origin[i]
    destination_nodes[i] = vehicles_destination[i]
    time[i] = total_time_vehicle[i]

    If you insist on using the multidict function, you can pre-construct the larger dictionary

    multi = {}
    for i in range(number_of_vehicles):
    l = [vehicles_origin[i],vehicles_destination[i],total_time_vehicle[i]]
    multi[i] = l

    truck, starting_node, destination_nodes, time = multidict(multi)

    Best regards,
    Jaromił

     

    0

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