使用多个需求/供应集时的最小成本流网络 (networkx)

问题描述

这是我第一次提交 stackoverflow,所以请让我知道我是否根据社区准则提出这个问题。

问题简而言之:有没有办法让 networkx 找到给定多个需求/供应集的最小成本流网络?

我目前正在研究一个网络优化问题,我正在使用 nx.min_cost_flow() 函数来计算给定节点供需设置的最小成本网络布局,以及某些边缘的最大容量。该代码有效,但现在我正试图找到一种方法,在给定多个供需集时找到最佳网络布局。我尝试了一种适用于最佳树的方法(遍历所有需求/供应集并在每个时间步中使用每个边的最大容量),但遗憾的是,当使用 nx.min_cost_flow() 作为时,这并没有提供最佳解决方案我希望模型也能够创建循环。

我在下面添加了相关代码,但它可能有点令人困惑,因为有许多元素涉及与我目前面临的问题无关的模型的其他部分。完整模型太大,无法分享

我曾尝试在网上寻找解决方案,但没有成功。希望你知道一个解决方案。提前致谢!

def full_existing(T,G,folder,rpc,routing): 
fullG = G.copy()
for (i,j) in fullG.edges():
    fullG[i][j]['capacity'] = 0
    for (i,j) in T.edges():
        if 'current' in T[i][j]:
            fullG[i][j]['weight'] = T[i][j]['weight']*rpc
f_demand = open(folder+'/demand.txt','r')
for X in f_demand:
    print('Demand set is called')
    Y = X.split(',')
    demand = {i: float(Y[i+1]) for i in range(len(Y)-1)}      
    G1 = fullG.copy()
    for i in fullG.nodes():
        if i in demand.keys():
            G1.nodes[i]['demand'] = demand[i]
        else:
            G1.nodes[i]['demand'] = 0
    for (i,j) in G1.edges():
        G1[i][j]['capacity'] = inf 
        for (i,j) in T.edges():
            if 'current' in T[i][j]:
                G1[i][j]['capacity'] = T[i][j]['current']
                G1[i][j]['weight'] = T[i][j]['weight']*rpc
            else:
                G1[i][j]['capacity'] = inf           
    G1 = simplex(G1)
    for i,j in G1.edges():
        fullG[i][j]['capacity'] = max(fullG[i][j]['capacity'],G1[i][j]['capacity'])
for (i,j) in T.edges():
    if 'current' in T[i][j]:
        fullG[i][j]['weight'] = T[i][j]['weight']     
nx.set_edge_attributes(fullG,nx.get_edge_attributes(T,'current'),'current')
return fullG

调用单纯形过程是因为输入图是无向的。

def simplex(G):
GX = G.copy()    
G1 = GX.to_directed()
ns = nx.min_cost_flow(G1)
for i,j in G1.edges():
    if ns[i][j] > 0 or ns[j][i] > 0:
        GX[i][j]['capacity'] = max(ns[i][j],ns[j][i])
    else:
        GX[i][j]['capacity'] = 0
return GX

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)