-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_budget_problem.py
139 lines (119 loc) · 3.8 KB
/
test_budget_problem.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
import networkx as nx
import cPickle as pkl
import string
import unittest
from nose.tools import assert_equal, assert_true
from interactions import InteractionsUtil as IU
from test_util import make_path
from budget_problem import charikar_algo, transitive_closure, \
binary_search_using_charikar
R = 'root'
A, B, C, D, E, F, G = string.lowercase[:7]
EPS = 0.0001
class CharikarAlgoTest(unittest.TestCase):
def setUp(self):
# the 'bunch' example in Charikar's paper
g = nx.DiGraph()
g.add_path([R, A, B, C, D, E])
g.add_edges_from([(A, C), (A, D), (A, E)])
for n in g.nodes_iter():
g.node[n]['r'] = 1
for n in [B, C, D, E]:
g[A][n]['c'] = 10
for s, t in zip([B, C, D], [C, D, E]):
g[s][t]['c'] = EPS
g[R][A]['c'] = EPS
self.g1 = g
def test_transitive_closure(self):
new_g, sp_table = transitive_closure(self.g1)
assert_equal(10 + EPS, new_g[R][B]['c'])
assert_equal(EPS * 2, new_g[B][D]['c'])
assert_equal(EPS * 3, new_g[B][E]['c'])
assert_equal(10, new_g[A][D]['c'])
assert_equal([R, A, C],
sp_table[R][C])
def check_level(self, level, edges, k=5, root=R, X=[A, B, C, D, E]):
actual = charikar_algo(self.g1, root,
X,
k, level)
assert_equal(sorted(edges),
sorted(actual.edges())
)
def test_return_empty(self):
actual = charikar_algo(self.g1, R,
[A, B, C, D, E],
100, 1)
assert_equal([],
actual.edges()
)
def test_level_1(self):
self.check_level(
1,
[(R, A), (A, B), (A, C), (A, D), (A, E)]
)
def check_level_1_another(self):
self.check_level(1,
edges=[(C, D), (D, E)],
k=1,
root=C,
X=[B, E]
)
def test_level_2(self):
self.check_level(
2,
[(R, A), (A, B), (B, C), (C, D), (D, E)]
)
def test_level_2_extra_level(self):
self.check_level(
level=2,
edges=[(A, B)],
X=[B, E],
root=A,
k=1
)
def test_level_100(self):
self.check_level(
100,
[(R, A), (A, B), (B, C), (C, D), (D, E)]
)
def check_binary_search(self, B, edges, level=2):
t = binary_search_using_charikar(
self.g1, R,
B=B,
level=3
)
assert_equal(
sorted(edges),
sorted(t.edges())
)
def test_bs_zero_budget(self):
self.check_binary_search(
B=0,
edges=[]
)
def test_bs_infinite_budget(self):
self.check_binary_search(
B=100,
edges=[(R, A), (A, B), (B, C), (C, D), (D, E)]
)
def test_bs_just_enough_budget(self):
self.check_binary_search(
B=10 + 4 * EPS,
edges=[(R, A), (A, B), (B, C), (C, D), (D, E)]
)
def test_bs_ensure_result_is_tree(self):
params = pkl.load(
open(make_path('test/data/quota_test_cases/params.pkl'))
)[0]
root = params['roots'][0]
preprune_secs = params['preprune_secs']
mg = IU.get_topic_meta_graph_from_synthetic(
make_path('test/data/quota_test_cases/interactions.json'),
preprune_secs
)
dag = IU.get_rooted_subgraph_within_timespan(
mg, root, preprune_secs
)
t = charikar_algo(dag, root, dag.nodes(),
k=20, level=2)
assert_true(nx.is_arborescence(t))