-
Notifications
You must be signed in to change notification settings - Fork 0
/
traceback-efficiency.py
174 lines (137 loc) · 5.33 KB
/
traceback-efficiency.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
import pathlib, os, pickle
import networkx as nx # type: ignore
G = None
carriers = []
shortest_paths = None
adopters = {}
robocallers = {}
call_space = []
cache_file = pathlib.Path.cwd().joinpath('phone-net-0.pkl')
def set_cache():
data = (G, carriers, shortest_paths, call_space)
with open(cache_file, 'wb') as file:
pickle.dump(data, file)
def load_cache():
global G, carriers, shortest_paths, call_space
if not os.path.exists(cache_file):
return False
with open(cache_file, 'rb') as file:
G, carriers, shortest_paths, call_space = pickle.load(file)
return True
def generate_phone_network(N, m):
global G, carriers
if G and carriers:
return
print('Generating phone network')
G = nx.barabasi_albert_graph(N, m)
degree = G.degree()
carriers = sorted(degree, key=lambda x: x[1], reverse=True)
def all_pairs_johnson():
global shortest_paths
if shortest_paths:
return
print('Running Johnson algorithm')
shortest_paths = nx.johnson(G)
def get_call_path(source, target):
return shortest_paths[source][target]
def set_adopters(num_adopters = 5):
global adopters
adopters = {carrier[0]: True for carrier in carriers[:num_adopters]}
def set_robocallers(num_robocallers = 5):
global robocallers
# robocallers, unlike adopters should be last num_robocallers in carriers
robocallers = {carrier[0]: True for carrier in carriers[-num_robocallers:]}
def is_adopter(node):
return adopters.get(node, False)
def gen_call_space():
global call_space
if (len(call_space) > 0):
return call_space
print('Generating call space')
call_space, visited = [], {}
for (src, _) in carriers:
for (dst, _) in carriers:
if src == dst:
continue
if (src, dst) in visited or (dst, src) in visited:
continue
visited[(src, dst)] = True
call_space.append((src, dst))
def send_to_jager(prev, curr, next):
return [(prev, curr), (curr, next)]
def do_contribution(call_path):
records = []
n = len(call_path)
for index, carrier in enumerate(call_path):
if is_adopter(carrier):
prev = next = None
if index > 0:
prev = call_path[index - 1]
if index < n - 1:
next = call_path[index + 1]
records.append(send_to_jager(prev=prev, curr=carrier, next=next))
return records
def analyze_records(retrieved, call_path):
# print('call_path:', call_path)
# print('retrieved:', retrieved)
constructed_path = []
for record in retrieved:
for (src, dst) in record:
if src and src not in constructed_path:
constructed_path.append(src)
if dst and dst not in constructed_path:
constructed_path.append(dst)
if (len(constructed_path) == 0):
return (False, False)
# print('Constructed Path:', constructed_path)
originator_found = constructed_path[0] == call_path[0]
call_path_constructed = call_path == constructed_path
# print('Original Path:', '->'.join([str(i) for i in call_path]))
# print('Constructed_path:', '->'.join([str(i) for i in constructed_path]))
# print('Originator_found:', originator_found)
# print('Call path constructed', call_path_constructed)
return (originator_found, call_path_constructed)
def set_market_shares(total_subscribers):
counts = {}
total_degree = sum([degree for (_, degree) in carriers])
for (carrier, degree) in carriers:
counts[carrier] = int(degree / total_degree * total_subscribers)
return counts
def main():
N, m = 7000, 2
total_subscribers = 10 * pow(10, 6)
if not load_cache():
generate_phone_network(N, m)
all_pairs_johnson()
gen_call_space()
set_cache()
runs = 3
calls = call_space
num_robocallers = int(0.1 * N)
set_robocallers(num_robocallers=num_robocallers)
population = set_market_shares(total_subscribers)
for run in range(runs):
num_adopters = int(0.1 * N * (run + 1))
set_adopters(num_adopters=num_adopters)
total_calls, successful_tracebacks, full_path_found = 0, 0, 0
for (src, dst) in calls:
# only process calls originating from bottom 1% of carriers
if not robocallers.get(src, False):
continue
call_path = get_call_path(src, dst)
retrieved = do_contribution(call_path)
(has_origin, has_fullpath) = analyze_records(
retrieved=retrieved,
call_path=call_path
)
total_calls += population[dst]
successful_tracebacks += population[dst] if has_origin else 0
full_path_found += population[dst] if has_fullpath else 0
trace_percentage = round((successful_tracebacks / total_calls) * 100, 2)
path_percentage = round((full_path_found / total_calls) * 100, 2)
print(f'\n================= {round(num_adopters/N * 100, 2)}% Participation =================')
print(f'Number of adopters: {num_adopters}/{N}')
print(f'Successful Tracebacks: {successful_tracebacks}/{total_calls} ({trace_percentage}%)')
print(f'Full Path Recovery: {full_path_found}/{total_calls} ({path_percentage}%)')
if __name__ == '__main__':
main()