1
# -*- coding: utf-8 -*-
4
from nose.tools import assert_equal, assert_raises
6
class TestNetworkSimplex:
7
def test_simple_digraph(self):
9
G.add_node('a', demand = -5)
10
G.add_node('d', demand = 5)
11
G.add_edge('a', 'b', weight = 3, capacity = 4)
12
G.add_edge('a', 'c', weight = 6, capacity = 10)
13
G.add_edge('b', 'd', weight = 1, capacity = 9)
14
G.add_edge('c', 'd', weight = 2, capacity = 5)
15
flowCost, H = nx.network_simplex(G)
16
soln = {'a': {'b': 4, 'c': 1},
20
assert_equal(flowCost, 24)
21
assert_equal(nx.min_cost_flow_cost(G), 24)
23
assert_equal(nx.min_cost_flow(G), soln)
24
assert_equal(nx.cost_of_flow(G, H), 24)
26
def test_negcycle_infcap(self):
28
G.add_node('s', demand = -5)
29
G.add_node('t', demand = 5)
30
G.add_edge('s', 'a', weight = 1, capacity = 3)
31
G.add_edge('a', 'b', weight = 3)
32
G.add_edge('c', 'a', weight = -6)
33
G.add_edge('b', 'd', weight = 1)
34
G.add_edge('d', 'c', weight = -2)
35
G.add_edge('d', 't', weight = 1, capacity = 3)
36
assert_raises(nx.NetworkXUnbounded, nx.network_simplex, G)
38
def test_sum_demands_not_zero(self):
40
G.add_node('s', demand = -5)
41
G.add_node('t', demand = 4)
42
G.add_edge('s', 'a', weight = 1, capacity = 3)
43
G.add_edge('a', 'b', weight = 3)
44
G.add_edge('a', 'c', weight = -6)
45
G.add_edge('b', 'd', weight = 1)
46
G.add_edge('c', 'd', weight = -2)
47
G.add_edge('d', 't', weight = 1, capacity = 3)
48
assert_raises(nx.NetworkXUnfeasible, nx.network_simplex, G)
50
def test_no_flow_satisfying_demands(self):
52
G.add_node('s', demand = -5)
53
G.add_node('t', demand = 5)
54
G.add_edge('s', 'a', weight = 1, capacity = 3)
55
G.add_edge('a', 'b', weight = 3)
56
G.add_edge('a', 'c', weight = -6)
57
G.add_edge('b', 'd', weight = 1)
58
G.add_edge('c', 'd', weight = -2)
59
G.add_edge('d', 't', weight = 1, capacity = 3)
60
assert_raises(nx.NetworkXUnfeasible, nx.network_simplex, G)
62
def test_transshipment(self):
64
G.add_node('a', demand = 1)
65
G.add_node('b', demand = -2)
66
G.add_node('c', demand = -2)
67
G.add_node('d', demand = 3)
68
G.add_node('e', demand = -4)
69
G.add_node('f', demand = -4)
70
G.add_node('g', demand = 3)
71
G.add_node('h', demand = 2)
72
G.add_node('r', demand = 3)
73
G.add_edge('a', 'c', weight = 3)
74
G.add_edge('r', 'a', weight = 2)
75
G.add_edge('b', 'a', weight = 9)
76
G.add_edge('r', 'c', weight = 0)
77
G.add_edge('b', 'r', weight = -6)
78
G.add_edge('c', 'd', weight = 5)
79
G.add_edge('e', 'r', weight = 4)
80
G.add_edge('e', 'f', weight = 3)
81
G.add_edge('h', 'b', weight = 4)
82
G.add_edge('f', 'd', weight = 7)
83
G.add_edge('f', 'h', weight = 12)
84
G.add_edge('g', 'd', weight = 12)
85
G.add_edge('f', 'g', weight = -1)
86
G.add_edge('h', 'g', weight = -10)
87
flowCost, H = nx.network_simplex(G)
88
soln = {'a': {'c': 0},
89
'b': {'a': 0, 'r': 2},
92
'e': {'r': 3, 'f': 1},
93
'f': {'d': 0, 'g': 3, 'h': 2},
95
'h': {'b': 0, 'g': 0},
96
'r': {'a': 1, 'c': 1}}
97
assert_equal(flowCost, 41)
98
assert_equal(nx.min_cost_flow_cost(G), 41)
100
assert_equal(nx.min_cost_flow(G), soln)
101
assert_equal(nx.cost_of_flow(G, H), 41)
103
def test_max_flow_min_cost(self):
105
G.add_edge('s', 'a', bandwidth = 6)
106
G.add_edge('s', 'c', bandwidth = 10, cost = 10)
107
G.add_edge('a', 'b', cost = 6)
108
G.add_edge('b', 'd', bandwidth = 8, cost = 7)
109
G.add_edge('c', 'd', cost = 10)
110
G.add_edge('d', 't', bandwidth = 5, cost = 5)
111
soln = {'s': {'a': 5, 'c': 0},
117
flow = nx.max_flow_min_cost(G, 's', 't', capacity = 'bandwidth',
119
assert_equal(flow, soln)
120
assert_equal(nx.cost_of_flow(G, flow, weight = 'cost'), 90)
122
def test_digraph1(self):
123
# From Bradley, S. P., Hax, A. C. and Magnanti, T. L. Applied
124
# Mathematical Programming. Addison-Wesley, 1977.
126
G.add_node(1, demand = -20)
127
G.add_node(4, demand = 5)
128
G.add_node(5, demand = 15)
129
G.add_edges_from([(1, 2, {'capacity': 15, 'weight': 4}),
130
(1, 3, {'capacity': 8, 'weight': 4}),
131
(2, 3, {'weight': 2}),
132
(2, 4, {'capacity': 4, 'weight': 2}),
133
(2, 5, {'capacity': 10, 'weight': 6}),
134
(3, 4, {'capacity': 15, 'weight': 1}),
135
(3, 5, {'capacity': 5, 'weight': 3}),
136
(4, 5, {'weight': 2}),
137
(5, 3, {'capacity': 4, 'weight': 1})])
138
flowCost, H = nx.network_simplex(G)
139
soln = {1: {2: 12, 3: 8},
140
2: {3: 8, 4: 4, 5: 0},
144
assert_equal(flowCost, 150)
145
assert_equal(nx.min_cost_flow_cost(G), 150)
146
assert_equal(H, soln)
147
assert_equal(nx.min_cost_flow(G), soln)
148
assert_equal(nx.cost_of_flow(G, H), 150)