handling infinite supply in minimum cost flow problem

143 Views Asked by At

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:

enter image description here

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:

enter image description here

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!

1

There are 1 best solutions below

3
On

This feature is not implemented in networkx version 2.8 (might be implemented in the future).

One thing that comes to mind is that if you are working with a special case of 'infinite supply' nodes that each have a single connection (like in the post), then you can work out a solution by solving for all feasible supply values that make total demand = 0 and then iterate over all distributions of supply across such infinite supply nodes.

For example, if total demand for nodes with positive demand is 6, then the possible allocations across supply nodes are: (0,0,6), (0,1,5), (0,2,4), ..., (0,6,0), ...,(6,0,0). That is a lot of combinations to try, so it's not efficient, and depending on the specifics of the problem you can reduce the search space a bit more.