-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_fjgraph.py
237 lines (186 loc) · 6.69 KB
/
test_fjgraph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# coding: utf-8
import unittest
import fjgraph
import networkx
from collections import Counter
class IncidenceGraphTest(unittest.TestCase):
def setUp(self):
self.G = networkx.MultiGraph()
self.G.add_edge(0, 1, capacity=1)
self.G.add_edge(0, 1, capacity=1)
self.G.add_edge(1, 4, capacity=1)
self.G.add_edge(1, 4, capacity=1)
self.G.add_edge(4, 3, capacity=1)
self.G.add_edge(3, 2, capacity=1)
self.G.add_edge(2, 0, capacity=1)
self.G.add_edge(1, 3, capacity=1)
self.G.add_edge(1, 2, capacity=1)
self.G.add_edge(1, 1, capacity=1)
def test_calc_check_values(self):
incidence_graph = fjgraph.IncidenceGraph(self.G)
n = self.G.number_of_nodes()
m = self.G.number_of_edges()
cvalues = incidence_graph.calc_check_values([0] * n)
self.assertEqual(cvalues, [0] * m)
class VertexCoverSolverTest(unittest.TestCase):
def setUp(self):
self.solver = fjgraph.VertexCoverSolver()
def test_chain_graph(self):
G = networkx.Graph()
G.add_edge(0, 1)
G.add_edge(1, 2)
lp_solution = self.solver.lp_solve(G)
self.assertEqual(lp_solution.opt_value(), 1.0)
self.assertEqual(lp_solution.values_dict(),
{0: 0.0, 1: 1.0, 2: 0.0})
ip_solution = self.solver.ip_solve(G)
self.assertEqual(ip_solution.opt_value(), 1.0)
self.assertEqual(lp_solution.values_dict(),
{0: 0.0, 1: 1.0, 2: 0.0})
def test_perfect_graph(self):
G = networkx.complete_graph(4)
lp_solution = self.solver.lp_solve(G)
self.assertEqual(lp_solution.opt_value(), 2.0)
self.assertEqual(lp_solution.values_dict(),
{0: 0.5, 1: 0.5, 2: 0.5, 3: 0.5})
ip_solution = self.solver.ip_solve(G)
self.assertEqual(ip_solution.opt_value(), 3.0)
self.assertEqual(ip_solution.values_dict(),
{0: 1.0, 1: 1.0, 2: 1.0, 3: 0.0})
def test_self_loop(self):
G = networkx.MultiGraph()
G.add_edge(0, 0)
lp_solution = self.solver.lp_solve(G)
self.assertEqual(lp_solution.opt_value(), 0.5)
self.assertEqual(lp_solution.values_dict(),
{0: 0.5})
ip_solution = self.solver.ip_solve(G)
self.assertEqual(ip_solution.opt_value(), 1.0)
self.assertEqual(ip_solution.values_dict(),
{0: 1.0})
class ThreeWayCutSetDistCalculatorTest(unittest.TestCase):
"ThreeWayCutSetDistCalculatorクラスのテスト"
@classmethod
def setUpClass(self):
self.calc = fjgraph.ThreeWayCutSetDistCalculator()
def test_detailed_cutset_dist(self):
"詳細カットセット分布を計算する"
calc = self.calc
G = networkx.MultiGraph()
G.add_edge(0, 1)
G.add_edge(1, 2)
G.add_edge(2, 0)
ret = calc.detailed_cutset_dist(G)
self.assertEqual(ret[(1, 1, 1, 3)], 6)
# (j, k, l, w) num_of_patterns
# ----------------------------
# (0, 0, 3, 0) 1
# (0, 1, 2, 2) 3
# (0, 2, 1, 2) 3
# (0, 3, 0, 0) 1
# (1, 0, 2, 2) 3
# (1, 1, 1, 3) 6
# (1, 2, 0, 2) 3
# (2, 0, 1, 2) 3
# (2, 1, 0, 2) 3
# (3, 0, 0, 0) 1
def test_all_cutset(self):
"すべての3分割カットセットを求める"
calc = self.calc
G = networkx.MultiGraph()
G.add_edge(0, 1)
G.add_edge(1, 2)
G.add_edge(2, 0)
ret = calc.all_cutset(G)
# 3分割カットセットは1パターンのみ
self.assertEqual(len(ret), 1)
edges = frozenset(G.edges())
self.assertTrue(edges in ret)
def test_cutset_dist(self):
"3分割カットセット重み分布を求める"
calc = self.calc
G = networkx.MultiGraph()
G.add_edge(0, 1)
G.add_edge(1, 2)
G.add_edge(2, 0)
ret = calc.cutset_dist(G)
self.assertEqual(ret[0], 0)
self.assertEqual(ret[1], 0)
self.assertEqual(ret[2], 0)
self.assertEqual(ret[3], 1)
class ErdosRenyiGraphEnsembleTest(unittest.TestCase):
def test_generate_graph(self):
n = 10
p = 0.8
ensemble = fjgraph.ErdosRenyiGraphEnsemble(
num_of_nodes=n,
edge_prob=p
)
self.assertEqual(ensemble.num_of_nodes(), n)
G = ensemble.generate_graph()
self.assertTrue(isinstance(G, networkx.Graph))
self.assertTrue(not isinstance(G, networkx.MultiGraph))
class NMGraphEnsembleTest(unittest.TestCase):
def test_generate_graph(self):
n = 10
m = 10
ensemble = fjgraph.NMGraphEnsemble(
num_of_nodes=n,
num_of_edges=m
)
self.assertEqual(ensemble.num_of_nodes(), n)
self.assertEqual(ensemble.num_of_edges(), m)
G = ensemble.generate_graph()
self.assertTrue(isinstance(G, networkx.Graph))
self.assertTrue(not isinstance(G, networkx.MultiGraph))
self.assertEqual(G.number_of_nodes(), n)
self.assertEqual(G.number_of_edges(), m)
class MinCutCalculatorTest(unittest.TestCase):
@classmethod
def setUpClass(self):
self.calc = fjgraph.MinCutCalculator()
def test_mincut(self):
G = networkx.MultiGraph()
G.add_edge(0,1, weight = 1)
G.add_edge(0,1, weight = 1)
G.add_edge(1,4, weight = 1)
G.add_edge(1,4, weight = 1)
G.add_edge(4,3, weight = 1)
G.add_edge(3,2, weight = 1)
G.add_edge(2,0, weight = 1)
G.add_edge(1,3, weight = 1)
G.add_edge(1,2, weight = 1)
G.add_edge(1,1, weight = 1)
mincut = self.calc.global_mincut(G)
self.assertEqual(mincut, 3)
st_mincut = self.calc.st_mincut(G, 0, 1)
self.assertEqual(st_mincut, 3)
class CutSetDistCalculatorTest(unittest.TestCase):
@classmethod
def setUpClass(self):
self.calc = fjgraph.CutSetDistCalculator()
G = networkx.MultiGraph()
G.add_edge(0, 1)
G.add_edge(1, 2)
G.add_edge(2, 0)
self.G = G
def test_detailed_global_cutset_dist(self):
calc = self.calc
G = self.G
dist = calc.detailed_global_cutset_dist(G)
ok_dist = Counter({
(0, 0): 1,
(3, 0): 1,
(1, 2): 3,
(2, 2): 3,
})
self.assertEqual(dist, ok_dist)
def test_detailed_st_cutset_dist(self):
calc = self.calc
G = self.G
dist = calc.detailed_st_cutset_dist(G, 0, 1)
ok_dist = Counter({
(1, 2): 2,
(2, 2): 2,
})
self.assertEqual(dist, ok_dist)