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 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.