Skip to main content

how can I retrieve the values for a single variable? The variable is something like d(b,t).

Answered

Comments

3 comments

  • Eli Towle
    Gurobi Staff Gurobi Staff

    Hi Yiwen,

    The variable l is a tupledict that maps a set of indices to Var objects. The values() method of a tupledict returns a list of the corresponding Var objects. Finally, the X attribute of a Var object stores the variable's value at the current solution.

    So, after optimizing, you can iterate through each variable in the tupledict and retrieve the variable's solution value. For example:

    for v in l.values():
    print("{}: {}".format(v.varName, v.X))

    You could also use a dictionary comprehension to construct a dictionary that maps the variable indices to the corresponding solution values. This can be done by iterating over the tupledict's key-value pairs:

    vals = { k : v.X for k,v in l.items() }

    I hope this helps!

    1
  • Yiwen Zhou
    Gurobi-versary
    First Comment
    First Question

    Thanks a lot Eli! :-)

    0
  • Mai Elgazzar

    how can I get the result in one list? 

    as my case is:

    for v in   A.values(): 
        z= (v.X)
        print ((z))

     

    my result is:

    0.0
    361.0
    709.0
    0.0

     

    while I need to have them in one list to be able to do further operations on them

     

    0

Please sign in to leave a comment.