-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_bipartite_match.py
188 lines (138 loc) · 7.57 KB
/
test_bipartite_match.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
import torch
from bipartite_match import ind_counts_to_longs, get_matched_indices
from collections import defaultdict
from pytest import approx
def test_ind_counts_to_longs():
arrival_counts = torch.tensor([2,3,2])
result = ind_counts_to_longs(arrival_counts)
result_set = set(result.numpy().tolist())
assert result_set == set([0,0,1,1,1,2,2])
from bipartite_match import CurrentElems, toy_e_weights_type
from compute_matching import weight_matrix
def unambiguous_matching():
currpool = CurrentElems([[torch.tensor(2), 0, 5], [torch.tensor(1), 0, 5], [torch.tensor(2), 0, 5]],
[[torch.tensor(0), 0, 5]])
e_weights_type = toy_e_weights_type()
e_weights = weight_matrix(currpool.lhs, currpool.rhs, e_weights_type)
correct_matching = torch.tensor([[0.0],
[1.0],
[0.0]])
return currpool, e_weights_type, e_weights, correct_matching
# testing index getting
def test_get_matched_indices_1():
currpool, e_weights_type, e_weights, correct_matching = unambiguous_matching()
lhs_inds, rhs_inds, _ = get_matched_indices(correct_matching, e_weights)
assert set(lhs_inds) == set([1])
assert set(rhs_inds) == set([0])
def test_empty_match():
currpool, e_weights_type, e_weights, correct_matching = unambiguous_matching()
lhs_inds, rhs_inds, _ = get_matched_indices(torch.zeros_like(correct_matching), e_weights)
assert set(lhs_inds) == set([])
assert set(rhs_inds) == set([])
def test_screwy_match():
currpool, e_weights_type, e_weights, correct_matching = unambiguous_matching()
correct_matching[0,0] = 0.1
correct_matching[1,0] = 0.9
lhs_inds, rhs_inds, _ = get_matched_indices(correct_matching, e_weights)
assert set(lhs_inds) == set([1])
assert set(rhs_inds) == set([0])
def test_match_value():
currpool, e_weights_type, e_weights, correct_matching = unambiguous_matching()
lhs_inds, rhs_inds, true_loss = get_matched_indices(correct_matching, e_weights)
assert true_loss == 3.0
def test_empty_match_value():
currpool, e_weights_type, e_weights, correct_matching = unambiguous_matching()
lhs_inds, rhs_inds, true_loss = get_matched_indices(torch.zeros_like(correct_matching), e_weights)
assert true_loss == 0.0
def test_screwy_match_value():
currpool, e_weights_type, e_weights, correct_matching = unambiguous_matching()
correct_matching[0,0] = 0.1
correct_matching[1,0] = 0.9
lhs_inds, rhs_inds, true_loss = get_matched_indices(correct_matching, e_weights)
assert true_loss == 3.0
from bipartite_match import History, history_to_arrival_dict, arrivals_only, step_simulation
def test_history_to_arrival_dict():
hist = History([[torch.tensor(1), 0, 2], [torch.tensor(2), 0, 5], [torch.tensor(2), 2, 5]],
[[torch.tensor(1), 0, 1], [torch.tensor(1), 1, 5]])
desired_dict_lhs = defaultdict(list)
desired_dict_lhs[0] = [[torch.tensor(1), 0, 2], [torch.tensor(2), 0, 5]]
desired_dict_lhs[2] = [[torch.tensor(2), 2, 5]]
desired_dict_rhs = defaultdict(list)
desired_dict_rhs[0] = [[torch.tensor(1), 0, 1]]
desired_dict_rhs[1] = [[torch.tensor(1), 1, 5]]
assert history_to_arrival_dict(hist.lhs) == desired_dict_lhs
assert history_to_arrival_dict(hist.rhs) == desired_dict_rhs
def test_arrivals_only():
hist = History([[torch.tensor(1), 0, 2], [torch.tensor(2), 0, 5], [torch.tensor(2), 2, 5]],
[[torch.tensor(1), 0, 1], [torch.tensor(1), 1, 5]])
desired_dict_lhs = defaultdict(list)
desired_dict_lhs[0] = [[torch.tensor(1), 0, 2], [torch.tensor(2), 0, 5]]
desired_dict_lhs[2] = [[torch.tensor(2), 2, 5]]
desired_dict_rhs = defaultdict(list)
desired_dict_rhs[0] = [[torch.tensor(1), 0, 1]]
desired_dict_rhs[1] = [[torch.tensor(1), 1, 5]]
currpool = CurrentElems([],[])
newpool = arrivals_only(currpool, desired_dict_lhs, desired_dict_rhs, 0)
target_pool = CurrentElems([[torch.tensor(1), 0, 2], [torch.tensor(2), 0, 5]],[[torch.tensor(1), 0, 1]])
assert newpool.lhs == target_pool.lhs
assert newpool.rhs == target_pool.rhs
def test_step_simulation_noarrival():
desired_dict_lhs = defaultdict(list)
desired_dict_rhs = defaultdict(list)
currpool = CurrentElems([[torch.tensor(1), 0, 2], [torch.tensor(2), 0, 5]], [[torch.tensor(1), 0, 1]])
match_edges = torch.tensor([[0.0],
[1.0]])
e_weights_type = toy_e_weights_type()
e_weights = weight_matrix(currpool.lhs, currpool.rhs, e_weights_type)
result_pool, total_loss = step_simulation(currpool, match_edges, e_weights, desired_dict_lhs, desired_dict_rhs, 1)
assert approx(total_loss, 0.1)
assert result_pool.lhs == [[torch.tensor(1), 0, 2]]
assert result_pool.rhs == []
def test_step_simulation_nomatch():
desired_dict_lhs = defaultdict(list)
desired_dict_rhs = defaultdict(list)
currpool = CurrentElems([[torch.tensor(1), 0, 2], [torch.tensor(2), 0, 5]], [[torch.tensor(1), 0, 1]])
match_edges = torch.tensor([[0.0],
[0.0]])
e_weights_type = toy_e_weights_type()
e_weights = weight_matrix(currpool.lhs, currpool.rhs, e_weights_type)
result_pool, total_loss = step_simulation(currpool, match_edges, e_weights, desired_dict_lhs, desired_dict_rhs, 1)
assert approx(total_loss, 0.0)
assert result_pool.lhs == [[torch.tensor(1), 0, 2], [torch.tensor(2), 0, 5]]
assert result_pool.rhs == []
def test_step_simulation():
hist = History([[torch.tensor(1), 0, 2], [torch.tensor(2), 0, 5], [torch.tensor(2), 2, 5]],
[[torch.tensor(1), 0, 1], [torch.tensor(1), 1, 5]])
desired_dict_lhs = defaultdict(list)
desired_dict_lhs[0] = [[torch.tensor(1), 0, 2], [torch.tensor(2), 0, 5]]
desired_dict_lhs[2] = [[torch.tensor(2), 2, 5]]
desired_dict_rhs = defaultdict(list)
desired_dict_rhs[0] = [[torch.tensor(1), 0, 1]]
desired_dict_rhs[1] = [[torch.tensor(1), 1, 5]]
currpool = CurrentElems([[torch.tensor(1), 0, 2], [torch.tensor(2), 0, 5]],[[torch.tensor(1), 0, 1]])
match_edges = torch.tensor([[0.0],
[1.0]])
e_weights_type = toy_e_weights_type()
e_weights = weight_matrix(currpool.lhs, currpool.rhs, e_weights_type)
result_pool, total_loss = step_simulation(currpool, match_edges, e_weights, desired_dict_lhs, desired_dict_rhs, 1)
assert approx(total_loss, 0.1)
assert result_pool.lhs == [[torch.tensor(1), 0, 2]]
assert result_pool.rhs == [[torch.tensor(1), 1, 5]]
from compute_matching import compute_matching
def test_e2e():
hist = History([[torch.tensor(1), 0, 2], [torch.tensor(2), 0, 5], [torch.tensor(2), 2, 5]],
[[torch.tensor(1), 0, 1], [torch.tensor(1), 1, 5]])
desired_dict_lhs = defaultdict(list)
desired_dict_lhs[0] = [[torch.tensor(1), 0, 2], [torch.tensor(2), 0, 5]]
desired_dict_lhs[2] = [[torch.tensor(2), 2, 5]]
desired_dict_rhs = defaultdict(list)
desired_dict_rhs[0] = [[torch.tensor(1), 0, 1]]
desired_dict_rhs[1] = [[torch.tensor(1), 1, 5]]
currpool = CurrentElems([[torch.tensor(1), 0, 2], [torch.tensor(2), 0, 5]],[[torch.tensor(1), 0, 1]])
e_weights_type = toy_e_weights_type()
potentials = torch.tensor([0.0,0.0,0.0,0.0,0.0])
match_edges, e_weights_full = compute_matching(currpool, potentials, e_weights_type)
result_pool, total_loss = step_simulation(currpool, match_edges, e_weights_full, desired_dict_lhs, desired_dict_rhs, 1)
assert approx(total_loss, 0.1)
assert result_pool.lhs == [[torch.tensor(2), 0, 5]]
assert result_pool.rhs == [[torch.tensor(1), 1, 5]]