Skip to main content

how to get the variable values by indices

Answered

Comments

2 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

    The best way to do this is to add the variables to the model using Model.addVars(). The return value is a tupledict that maps the variable indices to the Var objects, just like you want. For example:

    K = [(1, 2),
         (2, 1),
         (16, 10),
         (18, 24),
         (24, 18)]

    # x is a tupledict mapping tuples in K to Var objects
    # Access Var objects with x[1, 2], x[2, 1], etc.
    x = model.addVars(K, name='x')

    model.optimize()

    print(x[1, 2].X)
    0

Post is closed for comments.