Building a multi-dimensional array of variables

The next step in our example (after creating an empty Model object) is to add decision variables to the model. The variables are created using addVars, and are returned in a tupledict which we'll call flow:

# Create optimization model
m = gp.Model('netflow')

# Create variables
flow = m.addVars(commodities, arcs, obj=cost, name="flow")

The first, positional arguments to addVars give the index set. In this case, we'll be indexing flow by commodities and arcs. In other words, flow[c,i,j] will capture the flow of commodity c from node i to node j. Note that flow only contains variables for source, destination pairs that are present in arcs. Note also that flow[c,i,j] is a continuous decision variable; variables are assumed to be continuous unless you state otherwise.

Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.