Building a multi-dimensional array of variables

The next step in our example (after creating an empty Model object) is to add variables to the model. The variables are stored in a dictionary flow:

m = Model('netflow')

flow = {}
for h in commodities:
  for i,j in arcs:
    flow[h,i,j] = m.addVar(ub=capacity[i,j], obj=cost[h,i,j],
                           name='flow_%s_%s_%s' % (h, i, j))
m.update()
The flow variable is triply subscripted: by commodity, source node, and destination node. Note that the dictionary only contains variables for source, destination pairs that are present in arcs.

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.

Search