Skip to main content

How to store a solution from gurobi optimizer in a list in python ?

Answered

Comments

7 comments

  • Official comment
    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

    Do you want the list to contain the indices of the \( \texttt{assign} \) variables for which the corresponding \( \texttt{assign} \) variable takes a value of 1 at the optimal solution? Assuming you defined your \( \texttt{assign} \) variables like

    assign = m.addVars(...)

    you can do this with a list comprehension:

    new_list = [k for k, v in assign.items() if v.X > 0.5] 
    1
  • Anis Boudieb
    • Gurobi-versary
    • First Comment
    • First Question

    Hi Eli,

    I used the list comprehension as you said and my combinations have been stored. Thank you for you help.

     

     

     

     

    0
  • Anis Boudieb
    • Gurobi-versary
    • First Comment
    • First Question

    Hi Eli,

    I have another question please. How I store the costs related to each variable. 

    By using multidict(), I am attributing costs to each variable as:

    combinations, ms = multidict({k: v for k, v in zip(list3, Costs)})
    x = m.addVars(combinations, name='assign')
    when I print ms I have this:

    ('287','128'): 2 ('292','129'): 3 ('271','130'): 1 ('272','131']: 0
    etc....
    After running the model I want to store the delays assigned. How is it possible ?

    0
  • Eli Towle
    • Gurobi Staff

    Could you clarify what the delays are in your code, and what format you're trying to store them in?

    0
  • Anis Boudieb
    • Gurobi-versary
    • First Comment
    • First Question

    I have two lists:

    Costs = [2, 3, 1, 0...]
    list3 = [('287','128'), ('292','129'), ('271','130'), ('272','131')...]

    I am using this code below to assign the cost to each combination. (I have more than thousands combination and associated cost, I just wrote few of them in this example)

    combinations, ms = multidict({k: v for k, v in zip(list3, Costs)})

    If I print ms I will have:

    {('287','128'): 2, ('292','129'): 3, ('271','130'): 1, ('272','131'): 0...}

    When I run my model I have this result   :

    assign[287,128]:1.0
    assign[292,129]:1.0
    assign[271,130]:1.0
    assign[272,131]:1.0

    But it does not show the costs associated with every variable assigned after the optimisation, and I would like to store the cost in a new list as :

    new_cost = [2, 3, 1, 0] 

    0
  • Eli Towle
    • Gurobi Staff

    The construction is basically the same, with the extra step of mapping the keys to the appropriate costs using the \( \texttt{ms} \) dictionary:

    cost_list = [ms[k] for k, v in x.items() if v.X > 0.5]

    Also, I don't see a reason to use multidict here, since you are just constructing a single dictionary. You could instead write

    ms = {k: v for k, v in zip(list3, Costs)}

    to obtain the same \( \texttt{ms} \) dictionary. Then, you can use the \( \texttt{list3} \) list instead of the \( \texttt{combinations} \) list, since these should be the same.

    1

Post is closed for comments.