Building a multi-dimensional array of variables

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.