Skip to main content

Is it possible to retrieve variables as tuplelist outside the model definition scope in gurobipy?

Answered

Comments

1 comment

  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    Hi Runqiu,

    You can store the data structure containing the variables as a new member of the model object:

    def model_create_and_solve():
    m = gp.Model()
    # add vars...
    x = m.addVars(station_station_indices, vtype=GRB.BINARY, name="visit_truck")
    # ...more vars...
    # add constraints...
    # add objectives...
    m.optimize()
    # store interesting variables for later inspection
    m._x = x
    return m

    def result_post_process(m):
    # process to output the results based on values of the variables
    x = m._x
    ...

    Alternatively, you could also return the variables separately with the model. Keeping a reference in the model itself is probably easier, though.

    I hope that helps.

    Cheers,
    Matthias

    0

Please sign in to leave a comment.