本文整理汇总了Python中networkx.network_simplex函数的典型用法代码示例。如果您正苦于以下问题:Python network_simplex函数的具体用法?Python network_simplex怎么用?Python network_simplex使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了network_simplex函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_negative_selfloops
def test_negative_selfloops(self):
"""Negative selfloops should cause an exception if uncapacitated and
always be saturated otherwise.
"""
G = nx.DiGraph()
G.add_edge(1, 1, weight=-1)
assert_raises(nx.NetworkXUnbounded, nx.network_simplex, G)
assert_raises(nx.NetworkXUnbounded, nx.capacity_scaling, G)
G[1][1]['capacity'] = 2
flowCost, H = nx.network_simplex(G)
assert_equal(flowCost, -2)
assert_equal(H, {1: {1: 2}})
flowCost, H = nx.capacity_scaling(G)
assert_equal(flowCost, -2)
assert_equal(H, {1: {1: 2}})
G = nx.MultiDiGraph()
G.add_edge(1, 1, 'x', weight=-1)
G.add_edge(1, 1, 'y', weight=1)
assert_raises(nx.NetworkXUnbounded, nx.network_simplex, G)
assert_raises(nx.NetworkXUnbounded, nx.capacity_scaling, G)
G[1][1]['x']['capacity'] = 2
flowCost, H = nx.network_simplex(G)
assert_equal(flowCost, -2)
assert_equal(H, {1: {1: {'x': 2, 'y': 0}}})
flowCost, H = nx.capacity_scaling(G)
assert_equal(flowCost, -2)
assert_equal(H, {1: {1: {'x': 2, 'y': 0}}})
示例2: test_digraph1
def test_digraph1(self):
# From Bradley, S. P., Hax, A. C. and Magnanti, T. L. Applied
# Mathematical Programming. Addison-Wesley, 1977.
G = nx.DiGraph()
G.add_node(1, demand=-20)
G.add_node(4, demand=5)
G.add_node(5, demand=15)
G.add_edges_from([(1, 2, {'capacity': 15, 'weight': 4}),
(1, 3, {'capacity': 8, 'weight': 4}),
(2, 3, {'weight': 2}),
(2, 4, {'capacity': 4, 'weight': 2}),
(2, 5, {'capacity': 10, 'weight': 6}),
(3, 4, {'capacity': 15, 'weight': 1}),
(3, 5, {'capacity': 5, 'weight': 3}),
(4, 5, {'weight': 2}),
(5, 3, {'capacity': 4, 'weight': 1})])
flowCost, H = nx.network_simplex(G)
soln = {1: {2: 12, 3: 8},
2: {3: 8, 4: 4, 5: 0},
3: {4: 11, 5: 5},
4: {5: 10},
5: {3: 0}}
assert_equal(flowCost, 150)
assert_equal(nx.min_cost_flow_cost(G), 150)
assert_equal(H, soln)
assert_equal(nx.min_cost_flow(G), soln)
assert_equal(nx.cost_of_flow(G, H), 150)
flowCost, H = nx.capacity_scaling(G)
assert_equal(flowCost, 150)
assert_equal(H, soln)
assert_equal(nx.cost_of_flow(G, H), 150)
示例3: test_digon
def test_digon(self):
"""Check if digons are handled properly. Taken from ticket
#618 by arv."""
nodes = [(1, {}),
(2, {'demand': -4}),
(3, {'demand': 4}),
]
edges = [(1, 2, {'capacity': 3, 'weight': 600000}),
(2, 1, {'capacity': 2, 'weight': 0}),
(2, 3, {'capacity': 5, 'weight': 714285}),
(3, 2, {'capacity': 2, 'weight': 0}),
]
G = nx.DiGraph(edges)
G.add_nodes_from(nodes)
flowCost, H = nx.network_simplex(G)
soln = {1: {2: 0},
2: {1: 0, 3: 4},
3: {2: 0}}
assert_equal(flowCost, 2857140)
assert_equal(nx.min_cost_flow_cost(G), 2857140)
assert_equal(H, soln)
assert_equal(nx.min_cost_flow(G), soln)
assert_equal(nx.cost_of_flow(G, H), 2857140)
flowCost, H = nx.capacity_scaling(G)
assert_equal(flowCost, 2857140)
assert_equal(H, soln)
assert_equal(nx.cost_of_flow(G, H), 2857140)
示例4: run_random_trial
def run_random_trial(self):
G = nx.MultiDiGraph()
nodes = range(1, 10)
edges = generate_edges(nodes, 50)
G.add_edges_from(edges)
source = random.choice(G.nodes())
while True:
target = random.choice(G.nodes())
if target != source:
break
amount = random.randint(1, 10)
G.node[source]['demand'] = -amount
G.node[target]['demand'] = amount
cost, flow_dict, exception = None, None, None
try:
cost, flow_dict = min_cost_flow(G)
except nx.NetworkXException as e:
exception = e
H = unmulti(G)
try:
cost2, flow_dict2 = nx.network_simplex(H)
except nx.NetworkXException as e:
self.assertEquals(type(e) != type(exception))
self.assertEquals(cost, None)
else:
self.assertEquals(cost, cost2)
self.assertEquals(exception, None)
示例5: test_zero_capacity_edges
def test_zero_capacity_edges(self):
"""Address issue raised in ticket #617 by arv."""
G = nx.DiGraph()
G.add_edges_from([(1, 2, {'capacity': 1, 'weight': 1}),
(1, 5, {'capacity': 1, 'weight': 1}),
(2, 3, {'capacity': 0, 'weight': 1}),
(2, 5, {'capacity': 1, 'weight': 1}),
(5, 3, {'capacity': 2, 'weight': 1}),
(5, 4, {'capacity': 0, 'weight': 1}),
(3, 4, {'capacity': 2, 'weight': 1})])
G.nodes[1]['demand'] = -1
G.nodes[2]['demand'] = -1
G.nodes[4]['demand'] = 2
flowCost, H = nx.network_simplex(G)
soln = {1: {2: 0, 5: 1},
2: {3: 0, 5: 1},
3: {4: 2},
4: {},
5: {3: 2, 4: 0}}
assert_equal(flowCost, 6)
assert_equal(nx.min_cost_flow_cost(G), 6)
assert_equal(H, soln)
assert_equal(nx.min_cost_flow(G), soln)
assert_equal(nx.cost_of_flow(G, H), 6)
flowCost, H = nx.capacity_scaling(G)
assert_equal(flowCost, 6)
assert_equal(H, soln)
assert_equal(nx.cost_of_flow(G, H), 6)
示例6: test_large
def test_large(self):
fname = os.path.join(os.path.dirname(__file__), 'netgen-2.gpickle.bz2')
G = nx.read_gpickle(fname)
flowCost, flowDict = nx.network_simplex(G)
assert_equal(6749969302, flowCost)
assert_equal(6749969302, nx.cost_of_flow(G, flowDict))
flowCost, flowDict = nx.capacity_scaling(G)
assert_equal(6749969302, flowCost)
assert_equal(6749969302, nx.cost_of_flow(G, flowDict))
示例7: test_multidigraph
def test_multidigraph(self):
"""Multidigraphs are acceptable."""
G = nx.MultiDiGraph()
G.add_weighted_edges_from([(1, 2, 1), (2, 3, 2)], weight='capacity')
flowCost, H = nx.network_simplex(G)
assert_equal(flowCost, 0)
assert_equal(H, {1: {2: {0: 0}}, 2: {3: {0: 0}}, 3: {}})
flowCost, H = nx.capacity_scaling(G)
assert_equal(flowCost, 0)
assert_equal(H, {1: {2: {0: 0}}, 2: {3: {0: 0}}, 3: {}})
示例8: processWiki
def processWiki(page, p=None, ignoreConditionalProbabilities=False):
if p == None: p = readDict("data/wpairs")
pe = readCounts("data/unigram-counts/" + page + ".en.tok.ucounts")
pf = readCounts("data/unigram-counts/" + page + ".fr.tok.ucounts")
G = nx.DiGraph()
# 1 is source (F), 2 is sink (E)
# 3 is F-null, 4 is E-null
G.add_node(1, {'demand': -1, 'name': '*SOURCE*'})
G.add_node(2, {'demand': 1, 'name': '*SINK*'})
G.add_node(3, {'name': '*F-NULL*'})
G.add_node(4, {'name': '*E-NULL*'})
G.add_edge(1, 3, {'capacity': 1, 'weight': 100000})
G.add_edge(4, 2, {'capacity': 1, 'weight': 100000})
nCnt = 5
fID = {}
for f in pf.iterkeys():
fID[f] = nCnt
G.add_node(nCnt, {'name': f, 'lang': 'f'})
G.add_edge(1, nCnt, {'capacity': pf[f], 'weight': 1})
G.add_edge(nCnt, 4, {'capacity': 1, 'weight': 100000})
nCnt += 1
eID = {}
for e in pe.iterkeys():
eID[e] = nCnt
G.add_node(nCnt, {'name': e, 'lang': 'e'})
G.add_edge(nCnt, 2, {'capacity': pe[e], 'weight': 1})
G.add_edge(3, nCnt, {'capacity': 1, 'weight': 100000})
nCnt += 1
for f in pf.iterkeys():
nf = fID[f]
if p.has_key(f):
for e in p[f].iterkeys():
if eID.has_key(e):
ne = eID[e]
cap = p[f][e]
if ignoreConditionalProbabilities: cap = 1
G.add_edge(nf, ne, {'capacity': cap, 'weight': 1})
flowCost,flowDict = nx.network_simplex(G)
return G,flowDict
示例9: match
def match(self, riders, ridees):
graph = nx.DiGraph()
for rider in riders:
graph.add_node(rider.household_id, demand = - rider.capacity)
for ridee in ridees:
graph.add_node(ridee.household_id, demand = 1)
for rider in riders:
for ridee in ridees:
distance = this._get_distance(rider.location, ridee.location)
graph.add_edge(rider.household_id, ridee.household_id, weight = distance, capacity = 1)
flowCost, flowDict = nx.network_simplex(graph)
return flowDict
示例10: nxMCF
def nxMCF(startNodes, endNodes, capacities, costs, supplies):
G = nx.DiGraph()
for n, s in enumerate(supplies):
G.add_node(n, demand=-s)
for edgei in range(len(startNodes)):
G.add_edge(startNodes[edgei],
endNodes[edgei],
weight=costs[edgei],
capacity=capacities[edgei])
cost, flow = nx.network_simplex(G, demand='demand', capacity='capacity', weight='weight')
totalFlow = 0
for k1,v1 in flow.items():
for k2,v2 in v1.items():
totalFlow += v2
return cost, totalFlow
示例11: test_transshipment
def test_transshipment(self):
G = nx.DiGraph()
G.add_node('a', demand=1)
G.add_node('b', demand=-2)
G.add_node('c', demand=-2)
G.add_node('d', demand=3)
G.add_node('e', demand=-4)
G.add_node('f', demand=-4)
G.add_node('g', demand=3)
G.add_node('h', demand=2)
G.add_node('r', demand=3)
G.add_edge('a', 'c', weight=3)
G.add_edge('r', 'a', weight=2)
G.add_edge('b', 'a', weight=9)
G.add_edge('r', 'c', weight=0)
G.add_edge('b', 'r', weight=-6)
G.add_edge('c', 'd', weight=5)
G.add_edge('e', 'r', weight=4)
G.add_edge('e', 'f', weight=3)
G.add_edge('h', 'b', weight=4)
G.add_edge('f', 'd', weight=7)
G.add_edge('f', 'h', weight=12)
G.add_edge('g', 'd', weight=12)
G.add_edge('f', 'g', weight=-1)
G.add_edge('h', 'g', weight=-10)
flowCost, H = nx.network_simplex(G)
soln = {'a': {'c': 0},
'b': {'a': 0, 'r': 2},
'c': {'d': 3},
'd': {},
'e': {'r': 3, 'f': 1},
'f': {'d': 0, 'g': 3, 'h': 2},
'g': {'d': 0},
'h': {'b': 0, 'g': 0},
'r': {'a': 1, 'c': 1}}
assert_equal(flowCost, 41)
assert_equal(nx.min_cost_flow_cost(G), 41)
assert_equal(H, soln)
assert_equal(nx.min_cost_flow(G), soln)
assert_equal(nx.cost_of_flow(G, H), 41)
flowCost, H = nx.capacity_scaling(G)
assert_equal(flowCost, 41)
assert_equal(nx.cost_of_flow(G, H), 41)
assert_equal(H, soln)
示例12: test_simple_digraph
def test_simple_digraph(self):
G = nx.DiGraph()
G.add_node('a', demand = -5)
G.add_node('d', demand = 5)
G.add_edge('a', 'b', weight = 3, capacity = 4)
G.add_edge('a', 'c', weight = 6, capacity = 10)
G.add_edge('b', 'd', weight = 1, capacity = 9)
G.add_edge('c', 'd', weight = 2, capacity = 5)
flowCost, H = nx.network_simplex(G)
soln = {'a': {'b': 4, 'c': 1},
'b': {'d': 4},
'c': {'d': 1},
'd': {}}
assert_equal(flowCost, 24)
assert_equal(nx.min_cost_flow_cost(G), 24)
assert_equal(H, soln)
assert_equal(nx.min_cost_flow(G), soln)
assert_equal(nx.cost_of_flow(G, H), 24)
示例13: test_bone_shaped
def test_bone_shaped(self):
# From #1283
G = nx.DiGraph()
G.add_node(0, demand=-4)
G.add_node(1, demand=2)
G.add_node(2, demand=2)
G.add_node(3, demand=4)
G.add_node(4, demand=-2)
G.add_node(5, demand=-2)
G.add_edge(0, 1, capacity=4)
G.add_edge(0, 2, capacity=4)
G.add_edge(4, 3, capacity=4)
G.add_edge(5, 3, capacity=4)
G.add_edge(0, 3, capacity=0)
flowCost, H = nx.network_simplex(G)
assert_equal(flowCost, 0)
assert_equal(
H, {0: {1: 2, 2: 2, 3: 0}, 1: {}, 2: {}, 3: {}, 4: {3: 2}, 5: {3: 2}})
flowCost, H = nx.capacity_scaling(G)
assert_equal(flowCost, 0)
assert_equal(
H, {0: {1: 2, 2: 2, 3: 0}, 1: {}, 2: {}, 3: {}, 4: {3: 2}, 5: {3: 2}})
示例14: range
nz_idx = pd.Series(np.nonzero(C_transfers.values)[0])
for i in range(abs(dif)):
C_transfers.iloc[nz_idx[i]] -= direct
for i in CG.nodes():
CG.node[i]['load'] = C_loads[i]
CG.node[i]['gen'] = C_gen[i]
CG.node[i]['trans'] = C_transfers[i]
for i in CG.nodes():
CG.node[i]['MW_net'] = CG.node[i]['gen'] - CG.node[i]['load'] - CG.node[i]['trans']
DG = CG.to_directed()
NS = nx.network_simplex(DG, demand='MW_net', weight='R', capacity='tot_MW_cap')
#### EVERYTHING BELOW DOESN'T WORK!!!
for i in G.nodes():
kv_list = [G.adj[i][j]['tot_kv'] for j in G.adj[i].keys() if isinstance(j, int)]
kv_max, kv_min = max(kv_list), min(kv_list)
G[i]['max_volt'] = kv_max
G[i]['min_volt'] = kv_min
#### GET GRID VOLTAGES FROM EIA FORM DATA
mwkv = pd.DataFrame(np.zeros(len(G.nodes())), index=G.nodes())
for x in ['load', 'gen', 'trans', 'min_volt', 'max_volt']:
mwkv_col = pd.DataFrame(np.vstack([tuple([i, G[i][x]]) for i in G.nodes() if x in G[i].keys()])).rename(columns={1 : x}).set_index(0)
mwkv = pd.concat([mwkv, mwkv_col], axis=1)
示例15:
G.add_node(1, demand=-2)
G.add_node(2, demand=-5)
# Adiciona nos de demanda
G.add_node(3, demand=1)
G.add_node(4, demand=3)
G.add_node(5, demand=3)
# Adiciona os arcos
G.add_edge(1, 3, weight=3)
G.add_edge(2, 3, weight=7)
# ###################################
G.add_edge(1, 4, weight=4)
G.add_edge(2, 4, weight=2)
# ###################################
G.add_edge(1, 5, weight=1)
G.add_edge(2, 5, weight=5)
flowCost, flowDict = nx.network_simplex(G)
print flowCost
# print flowDict
k,w = 0,0
mat = np.zeros([2,3])
cost = np.zeros([2,3])
total = 0
for i in flowDict:
#print "i:%d\n"%i
w = 0
for j in flowDict[i]:
# print flowDict[i][j],
mat[k,w] = flowDict[i][j]
cost[k,w] = G[i][j]["weight"]
w += 1
total += flowDict[i][j]*G[i][j]["weight"]