当总节点需求不为零时,使用networkx的python上的传输问题

2024-10-02 22:26:04 发布

您现在位置:Python中文网/ 问答频道 /正文

graph representation

共有6个节点,分别具有正向和负向需求。我需要计算出最好的变体,以满足尽可能多的消费者。我正在尝试使用networkx python库解决此问题:

import networkx as nx

G = nx.DiGraph()

G.add_node("1", demand=15)
G.add_node("2", demand=25)
G.add_node("3", demand=60)
G.add_node("4", demand=-55)
G.add_node("5", demand=-35)
G.add_node("6", demand=-40)

G.add_edge("1", "2", weight=2)
G.add_edge("1", "3", weight=1)
G.add_edge("1", "6", weight=3)

G.add_edge("2", "6", weight=4)
G.add_edge("2", "3", weight=5)

G.add_edge("3", "4", weight=3)

G.add_edge("4", "5", weight=6)

G.add_edge("5", "6", weight=2)

flowCost = nx.min_cost_flow_cost(G)
print(flowCost)

但据我所知,问题在于我对消极和积极要求的总结不是零,我得到了错误:

networkx.exception.NetworkXUnfeasible: total node demand is not zero

那么这个问题有什么解决办法吗?也许使用不同的图书馆更好


Tags: importnetworkxaddnode节点as消费者变体