-
Notifications
You must be signed in to change notification settings - Fork 0
/
elimination_experiments.py
445 lines (333 loc) · 14.5 KB
/
elimination_experiments.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 25 10:07:57 2018
@author: joshua
"""
import time
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import community # python-louvain
import elimination as elim
import aggregation as agg
import graph as gp
def experiment_changing_density():
""" Comparing calc_stationary_dist with general_elimination_pi for various
densities.
"""
elim_times = []
linalg_times = []
for i in [0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]:
sims1 = []
sims2 = []
for _ in range(0, 20):
P = elim.rand_stoch_matrix(200, i)
T = elim.rand_trans_times(200)
order = [100]*2
start_time = time.time()
elim.calc_stationary_dist(P, T)
sims1.append(time.time() - start_time)
print("--- %s seconds ---" % (time.time() - start_time))
start_time = time.time()
elim.general_elimination_pi(P, T, order)
sims2.append(time.time() - start_time)
print("--- %s seconds ---" % (time.time() - start_time))
linalg_times.append(np.mean(sims1))
elim_times.append(np.mean(sims2))
#fig = plt.figure()
plt.plot([0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0], linalg_times, 'r')
plt.plot([0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0], elim_times, 'b')
plt.xlabel('density')
plt.ylabel('computation time')
#fig.savefig('experiment_changing_density.jpg')
plt.show()
return(elim_times, linalg_times)
def experiment_elimination_more_at_a_time():
""" Plots three graphs showing the computation time for performing
elimination on random graphs of size 100, 1000, and 3000, elimination i
number of nodes at a time. This is figure 3 in dissertation.
"""
n100_times = []
n1000_times = []
n3000_times = []
for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 35, 40, 45, 50]:
order = [i]*int(100/float(i))
sims = []
for _ in range(0, 10):
P = elim.rand_stoch_matrix(100, 0.1)
T = elim.rand_trans_times(100)
start_time = time.time()
elim.general_elimination_pi(P, T, order)
sims.append(time.time() - start_time)
print("--- %s seconds ---" % (time.time() - start_time))
n100_times.append(np.mean(sims))
for i in [1, 5, 10, 20, 30, 50, 70, 100, 200, 300, 400, 500]:
order = [i]*int(1000/float(i))
sims = []
for _ in range(0, 2):
P = elim.rand_stoch_matrix(1000, 0.01)
T = elim.rand_trans_times(1000)
start_time = time.time()
elim.general_elimination_pi(P, T, order)
sims.append(time.time() - start_time)
print("--- %s seconds ---" % (time.time() - start_time))
n1000_times.append(np.mean(sims))
for i in [50, 150, 300, 600, 1000, 1500]:
order = [i]*int(3000/float(i))
sims = []
for _ in range(0, 2):
P = elim.rand_stoch_matrix(3000, 0.01)
T = elim.rand_trans_times(3000)
start_time = time.time()
elim.general_elimination_pi(P, T, order)
sims.append(time.time() - start_time)
print("--- %s seconds ---" % (time.time() - start_time))
n3000_times.append(np.mean(sims))
#fig = plt.figure()
plt.plot([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 35, 40, 45, 50], n100_times, 'r')
plt.xlabel('number eliminated per iteration')
plt.ylabel('computation time')
#fig.savefig('experiment_elimination_more_at_a_time_100.jpg')
plt.show()
#fig = plt.figure()
plt.plot([1, 5, 10, 20, 30, 50, 70, 100, 200, 300, 400, 500], n1000_times, 'b')
plt.xlabel('number eliminated per iteration')
plt.ylabel('computation time')
#fig.savefig('experiment_elimination_more_at_a_time_1000.jpg')
plt.show()
#fig = plt.figure()
plt.plot([50, 150, 300, 600, 1000, 1500], n3000_times, 'g')
plt.xlabel('number eliminated per iteration')
plt.ylabel('computation time')
#fig.savefig('experiment_elimination_more_at_a_time_3000.jpg')
plt.show()
def heuristic_1_Perm(P, T):
""" Returns a permutation that once applied to (P,T) means that elimination
is performed eliminating first nodes who having highest transition rate
out.
"""
N = P.shape[0]
T_diag = np.identity(N)
for i in range(0, N):
T_diag[i, i] = T[i, 0]
T_diag = sp.sparse.csr_matrix(T_diag)
rates = T_diag*P
sum_rates = sum(rates.transpose()).todense()
permutation = np.argsort(-sum_rates)
return permutation.tolist()[0]
def heuristic_1(P, T):
""" Returns the stationary distribution calculated by eliminating according
to heuristic 1.
"""
permutation = heuristic_1_Perm(P, T)
(perm_P, perm_T) = agg.permute_P_and_T(P, T, permutation)
order_to_eliminate = [2]*100
start_time = time.time()
stationary = elim.general_elimination_pi(perm_P, perm_T, order_to_eliminate)
print("--- %s seconds ---" % (time.time() - start_time))
#note, this returns the stationary distribution permuted by 'permutation'
return stationary
def experiment_using_heuristic_1():
""" Returns plots comparing the computation speed elimination according to
heuristic 1 versus eliminating randomly. Markov processes are
non-hierarchical Erdos-Renyi type.
"""
random_times = []
sorted_order_times = []
for N in [30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360,
390, 420, 450, 480, 510, 540, 570, 600]:
sims1 = []
sims2 = []
order = [int(N/10)]*10
for _ in range(0, 12):
while True:
try:
P = elim.rand_stoch_matrix(N, 0.1)
T = elim.rand_trans_times(N)
start_time = time.time()
elim.general_elimination_pi(P, T, order)
sims1.append(time.time() - start_time)
print("--- %s seconds ---" % (time.time() - start_time))
permutation = heuristic_1_Perm(P, T)
(perm_P, perm_T) = agg.permute_P_and_T(P, T, permutation)
start_time = time.time()
elim.general_elimination_pi(perm_P, perm_T, order)
sims2.append(time.time() - start_time)
print("--- %s seconds ---" % (time.time() - start_time))
break
except IndexError:
pass # Try again
random_times.append(np.mean(sims1))
sorted_order_times.append(np.mean(sims2))
#fig = plt.figure()
plt.plot([30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360,
390, 420, 450, 480, 510, 540, 570, 600], random_times, 'r')
plt.plot([30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360,
390, 420, 450, 480, 510, 540, 570, 600], sorted_order_times, 'b')
plt.xlabel('number of nodes')
plt.ylabel('computation time')
#fig.savefig('experiment_using_heuristic_1.jpg')
plt.show()
def heuristic_2_Perm(P, T):
""" Returns a permutation that once applied to (P,T) means that elimination
is performed eliminating first nodes who having highest transition rate
into them.
"""
N = P.shape[0]
T_diag = np.identity(N)
for i in range(0, N):
T_diag[i, i] = T[i, 0]
T_diag = sp.sparse.csr_matrix(T_diag)
rates = T_diag*P
sum_rates = sum(rates).todense()
permutation = np.argsort(-sum_rates)
return permutation.tolist()[0]
def heuristic_2(P, T):
""" Returns the stationary distribution calculated by eliminating according
to heuristic 2.
"""
permutation = heuristic_2_Perm(P, T)
(perm_P, perm_T) = agg.permute_P_and_T(P, T, permutation)
order_to_eliminate = [2]*100
start_time = time.time()
stationary = elim.general_elimination_pi(perm_P, perm_T, order_to_eliminate)
print("--- %s seconds ---" % (time.time() - start_time))
#note, this returns the stationary distribution permuted by 'permutation'
return stationary
def experiment_using_heuristic_2():
""" Returns plots comparing the computation speed elimination according to
heuristic 2 versus eliminating randomly. Markov processes are
non-hierarchical Erdos-Renyi type.
"""
random_times = []
sorted_order_times = []
random_times = []
sorted_order_times = []
for N in [30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360,
390, 420, 450, 480, 510, 540, 570, 600]:
sims1 = []
sims2 = []
order = [int(N/10)]*10
for _ in range(0, 12):
while True:
try:
P = elim.rand_stoch_matrix(N, 0.1)
T = elim.rand_trans_times(N)
####
start_time = time.time()
elim.general_elimination_pi(P, T, order)
sims1.append(time.time() - start_time)
print("--- %s seconds ---" % (time.time() - start_time))
####
permutation = heuristic_2_Perm(P, T)
(perm_P, perm_T) = agg.permute_P_and_T(P, T, permutation)
start_time = time.time()
elim.general_elimination_pi(perm_P, perm_T, order)
sims2.append(time.time() - start_time)
print("--- %s seconds ---" % (time.time() - start_time))
break
except IndexError:
pass # Try again
random_times.append(np.mean(sims1))
sorted_order_times.append(np.mean(sims2))
#fig = plt.figure()
plt.plot([30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360,
390, 420, 450, 480, 510, 540, 570, 600], random_times, 'r')
plt.plot([30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360,
390, 420, 450, 480, 510, 540, 570, 600], sorted_order_times, 'b')
plt.xlabel('number of nodes')
plt.ylabel('computation time')
#fig.savefig('experiment_using_heuristic_2.jpg')
plt.show()
def get_permutation_and_order_from_partition(partition):
""" Returns the permutation required to eliminate in the order specified by
partition, which is the output of the community.best_partition function.
"""
number_of_communities = len(set(partition.values()))
permutation = []
order = []
for i in range(number_of_communities):
ith_community_nodes = [node for node in partition if partition[node] == i]
permutation = permutation + ith_community_nodes
order.append(len(ith_community_nodes))
return(permutation, order)
def experiment_using_louvain():
""" Plots graphs comparing the runtime of elimination when eliminating
according to a heirarchy found using Louvain compared with random
elimination.
"""
random_times = []
sorted_order_times = []
for N in [30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360,
390, 420, 450, 480, 510, 540, 570, 600]:
sims1 = []
sims2 = []
for _ in range(0, 12):
while True:
try:
P = elim.rand_stoch_matrix(N, 0.1)
T = elim.rand_trans_times(N)
N = P.shape[0]
T_diag = np.identity(N)
for i in range(0, N):
T_diag[i, i] = T[i, 0]
T_diag = sp.sparse.csr_matrix(T_diag)
rates = T_diag*P
A = rates + rates.transpose()
graph = gp.matrices_to_graph(A, T)
graph = graph.to_undirected()
partition = community.best_partition(graph, weight='transition probability')
(permutation, order) = get_permutation_and_order_from_partition(partition)
(perm_P, perm_T) = agg.permute_P_and_T(P, T, permutation)
start_time = time.time()
elim.general_elimination_pi(perm_P, perm_T, order)
sims2.append(time.time() - start_time)
print("--- %s seconds ---" % (time.time() - start_time))
start_time = time.time()
elim.general_elimination_pi(P, T, order)
sims1.append(time.time() - start_time)
print("--- %s seconds ---" % (time.time() - start_time))
break
except IndexError:
pass # Try again
random_times.append(np.mean(sims1))
sorted_order_times.append(np.mean(sims2))
#fig = plt.figure()
plt.plot([30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360,
390, 420, 450, 480, 510, 540, 570, 600], random_times, 'r')
plt.plot([30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360,
390, 420, 450, 480, 510, 540, 570, 600], sorted_order_times, 'b')
plt.xlabel('number of nodes')
plt.ylabel('computation time')
#fig.savefig('experiment_using_louvain.jpg')
plt.show()
def experiment_heuristics():
""" Simply checks performance for the heuristics.
"""
P = elim.rand_stoch_matrix(200, 0.1)
T = elim.rand_trans_times(200)
order = [2]*100
start_time = time.time()
elim.calc_stationary_dist(P, T)
print("--- %s seconds ---" % (time.time() - start_time))
start_time = time.time()
elim.general_elimination_pi(P, T, order)
print("--- %s seconds ---" % (time.time() - start_time))
start_time = time.time()
elim.elimination_pi(P, T)
print("--- %s seconds ---" % (time.time() - start_time))
start_time = time.time()
heuristic_1(P, T)
print("--- %s seconds ---" % (time.time() - start_time))
start_time = time.time()
heuristic_2(P, T)
print("--- %s seconds ---" % (time.time() - start_time))
if __name__ == "__main__":
print("Running elimination experiments")
experiment_changing_density()
experiment_elimination_more_at_a_time()
experiment_using_heuristic_1()
experiment_using_heuristic_2()
experiment_using_louvain()
experiment_heuristics()