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.

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