Reproducing assignment with min cost flow in networkx - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Reproducing assignment with min cost flow in networkx (/thread-19113.html) |
Reproducing assignment with min cost flow in networkx - mntfr - Jun-13-2019 Hi so I have seen the ortools webpage ortools where it states that you can solve the assignment problem with the min cost flow algorithm, however when I try to reproduce this process with networkx I get an error. Can somebody point me to what am I doing wrong? workersList = ['s1', 's2', 's3', 's4'] tasksList = ['c1', 'c2', 'c3', 'c4'] costs = [[90, 76, 75, 70], [35, 85, 55, 65], [125, 95, 90, 105], [45, 110, 95, 115]] import networkx as nx G = nx.DiGraph() G.add_node('Start', demand = len(workersList)) G.add_node('End', demand = -len(workersList)) G.add_nodes_from(workersList) G.add_nodes_from(tasksList) for worker in workersList: G.add_edge('Start', work, weight=0, capacity=1) for task in tasksList: G.add_edge(task, 'End', weigth=0, capacity=1) for i, worker in enumerate(workersList): for j, task in enumerate(tasksList): if costs[i][j] != 'NA': G.add_edge(worker, task, weight=costs[i][j], capacity=1) flowDict = nx.min_cost_flow(G) print(flowDict) |