Arc capacity constraints

Arc capacity constraints

We begin with a straightforward set of constraints. The sum of the flow variables on an arc must be less than or equal to the capacity of that arc:

for i,j in arcs:
  m.addConstr(quicksum(flow[h,i,j] for h in commodities) <= capacity[i,j],
              'cap_%s_%s' % (i, j))
Note that we use list comprehension to build a list of all variables associated with an arc (i,j):
flow[h,i,j] for h in commodities
(To be precise, as we've used it here, this is actually called a generator expression in Python, but it is similar enough to list comprehension that you can safely ignore the difference for the purpose of understanding this example). The result is passed into the quicksum function to create a Gurobi linear expression that captures the sum of all of these variables. The Gurobi quicksum function is an alternative to the Python sum function that is much faster for building large expressions.