Documentation

Flow conservation constraints

The next set of constraints are the flow conservation constraints. They require that, for each commodity and node, the sum of the flow into the node plus the quantity of external inflow at that node must be equal to the sum of the flow out of the node:


# Flow-conservation constraints
m.addConstrs(
    (flow.sum(h, '*', j) + inflow[h, j] == flow.sum(h, j, '*')
        for h in commodities for j in nodes), "node")

This call to addConstrs is similar to the previous one, although a bit more complex. We invoke the sum method on a tupledict, wrapped inside of a generator expression, to add one linear constraint for each commodity-node pair. In this instance, we call sum twice, and we use a generator expression that contains of a pair of for loops, but the basic concepts remain the same.