forked from marcovarrone/advanced-algorithms-goldberg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_triangulation.py
80 lines (64 loc) · 3.22 KB
/
test_triangulation.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
#!/usr/bin/env python -W ignore::DeprecationWarning
from numpy.random import randint
from max_flow_residuals import Goldberg
import graph_tool.all as gt
from generation.Triangulation import Triangulation
import pytest
def get_real_max_flow(graph, source, target):
cap = graph.ep.cap
res = gt.push_relabel_max_flow(graph, source, target, cap)
res.a = cap.a - res.a # the actual flow
return sum(res[e] for e in target.in_edges())
sizes = [10, 25, 50, 75, 100]
@pytest.mark.parametrize('n', sizes)
def test_max_flow_triangulation_simple_undirected(n):
seed_number = randint(1, 1000)
generator = Triangulation(n, type="simple", directed=False, seed_number=seed_number)
graph, source, target = generator.generate()
solver = Goldberg(graph)
max_flow = solver.get_max_flow(source, target)
generator = Triangulation(n, type="simple", directed=False, seed_number=seed_number)
graph, source, target = generator.generate()
res = gt.push_relabel_max_flow(graph, source, target, graph.ep.cap)
res.a = graph.ep.cap.a - res.a # the actual flow
gt_max_flow = sum(res[e] for e in target.in_edges())
assert max_flow == gt_max_flow
@pytest.mark.parametrize('n', sizes)
def test_max_flow_triangulation_delaunay_undirected(n):
seed_number = randint(1, 1000)
generator = Triangulation(n, type="delaunay", directed=False, seed_number=seed_number)
graph, source, target = generator.generate()
solver = Goldberg(graph)
max_flow = solver.get_max_flow(source, target)
generator = Triangulation(n, type="delaunay", directed=False, seed_number=seed_number)
graph, source, target = generator.generate()
res = gt.push_relabel_max_flow(graph, source, target, graph.ep.cap)
res.a = graph.ep.cap.a - res.a # the actual flow
gt_max_flow = sum(res[e] for e in target.in_edges())
assert max_flow == gt_max_flow
@pytest.mark.parametrize('n', sizes)
def test_max_flow_triangulation_simple_directed(n):
seed_number = randint(1, 1000)
generator = Triangulation(n, type="simple", directed=True, seed_number=seed_number)
graph, source, target = generator.generate()
solver = Goldberg(graph)
max_flow = solver.get_max_flow(source, target)
generator = Triangulation(n, type="simple", directed=True, seed_number=seed_number)
graph, source, target = generator.generate()
res = gt.push_relabel_max_flow(graph, source, target, graph.ep.cap)
res.a = graph.ep.cap.a - res.a # the actual flow
gt_max_flow = sum(res[e] for e in target.in_edges())
assert max_flow == gt_max_flow
@pytest.mark.parametrize('n', sizes)
def test_max_flow_triangulation_delaunay_directed(n):
seed_number = randint(1, 1000)
generator = Triangulation(n, type="delaunay", directed=True, seed_number=seed_number)
graph, source, target = generator.generate()
solver = Goldberg(graph)
max_flow = solver.get_max_flow(source, target)
generator = Triangulation(n, type="delaunay", directed=True, seed_number=seed_number)
graph, source, target = generator.generate()
res = gt.push_relabel_max_flow(graph, source, target, graph.ep.cap)
res.a = graph.ep.cap.a - res.a # the actual flow
gt_max_flow = sum(res[e] for e in target.in_edges())
assert max_flow == gt_max_flow