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:
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
addConstrsis similar to the previous one, although a bit more complex. We invoke the
summethod on a
tupledict, wrapped inside of a generator expression, to add one linear constraint for each commodity-node pair. In this instance, we call
sumtwice, and we use a generator expression that contains of a pair of
forloops, but the basic concepts remain the same.