# A is the incidence matrix. # c is a vector of edge capacities. flows = Variable(E-2) source = Variable() sink = Variable() p = Problem(Maximize(source), [A*hstack([flows,source,sink]) == 0, 0 <= flows, flows <= c]) class Edge(object): """ An undirected, capacity limited edge. """ def __init__(self, capacity): self.capacity = capacity self.flow = Variable() # Connects two nodes via the edge. def connect(self, in_node, out_node): in_node.edge_flows.append(-self.flow) out_node.edge_flows.append(self.flow) # Returns the edge's internal constraints. def constraints(self): return [abs(self.flow) <= self.capacity] class Node(object): """ A node with accumulation. """ def __init__(self, accumulation=0): self.accumulation = accumulation self.edge_flows = [] # Returns the node's internal constraints. def constraints(self): return [sum(f for f in self.edge_flows) == self.accumulation] constraints = [] for obj in nodes + edges: constraints += obj.constraints() prob = Problem(Maximize(sink.accumulation), constraints)