I got a typical problem about minimum cost flow problem. I'm given a dataset
# node : {pos, demand}
nodes_dict = {1: {'pos': (0, 0, 1), 'demand': 'NA'}, 2: {'pos': (0, 3, 1), 'demand': 'NA'}, 3: {'pos': (0, 6, 1), 'demand': 'NA'}, 4: {'pos': (4, 0, 1), 'demand': 1}, 5: {'pos': (4, 3, 1), 'demand': 2}, 6: {'pos': (4, 6, 1), 'demand': 3}, 7: {'pos': (10, 3, 1), 'demand': 0}}
# (node_start, node_end) : capacity
edges = {(1, 4): 2, (3, 6): 4, (2, 5): 1, (6, 5): 3, (6, 7): 0, (5, 7): 0, (4, 7): 0}
I got no problem modeling with NetworkX, except for a particular case where a node demand is 'NA' that implies node has infinite supply.
For example above, a graph looks like this with edge costs and no capacities considered:
My code for solving this example and try to find minimum cost flow goes like this:
def solve(N,E):
G = nx.DiGraph()
for k,v in N.items():
#node wants to send (negative demand) or receive (positive demand)
if v["demand"] == 'NA':
demand = 'Inf Supply'
else:
demand = int(v["demand"])
G.add_node(k, demand=demand)
for k,v in E.items():
index_node_1 = k[0]
index_node_2 = k[1]
#cost_edge is defined above and returns a integer
G.add_edge(k[0], k[1], weight=cost_edge( N[index_node_1]['pos'], N[index_node_2]['pos'] ))
return nx.min_cost_flow_cost(G)
I'm stuck on how should I treat infinite supply node. If I treat like demand = 0, graph goes like this:
But I got:
NetworkXUnfeasible: total node demand is not zero
And same with a very large int instead 0. I know that mass principle implies that total demand must sum 0, but in that case, I can't write a solution for infinite supply then.
Any idea about I'm not seeing? Thanks community!