-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticle_num_experiments_social.py
206 lines (180 loc) · 6.12 KB
/
article_num_experiments_social.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
"""
Created on 1/16/2022
Author: Joost Berkhout (VU, email: joost.berkhout@vu.nl)
Description: This script is used to do experiments for TSDP_optimization.py
regarding the sparse social network labeled as Moreno.
WARNING: The following values were used in TSDP_optimization.py for the
experiments
PRECISION = 10 ** (-6)
BINSEARCH_PRECISION = 10 ** (-8)
"""
from tabulate import tabulate
from TSDP_optimization import *
from constants import PRECISION
from utilities import (
store_results, find_cliques_from_adjacency_matrix,
calc_stationary_distribution, ergodic_project,
)
np.random.seed(0)
# user init
normType = 'inf'
verbose = False
numb_exp = 25
epsFR1SH = 10 ** (-8)
clique_mass_increase_factor = 1.1
timeLimit = 60 * 10
# technical init
results_dict = {}
# load email transition matrix
P = np.genfromtxt(
'Data\\Prepared data\\moreno_P_normalized_scc.csv',
delimiter=','
)
mu = calc_stationary_distribution(P)
n = len(mu)
# find cliques in order from large to small
cliques = find_cliques_from_adjacency_matrix(P)
cliques_length = np.array([len(clq) for clq in cliques])
cliques_idxs_large_to_small = np.argsort(cliques_length)[::-1]
cliques_to_consider = [cliques[i]
for i in cliques_idxs_large_to_small[:numb_exp]]
for idx, clique in enumerate(cliques_to_consider):
print(f'Start experiment with clique {idx} of length {len(clique)}')
# mu_goal where largest clique is made less popular
mu_goal = mu.copy()
mu_goal[clique] *= clique_mass_increase_factor
sum_clique = np.sum(mu_goal[clique])
outside_clique = [i for i in range(n) if i not in clique]
increase_factor_outside = (1 - sum_clique) / np.sum(mu_goal[outside_clique])
mu_goal[outside_clique] *= increase_factor_outside
assert np.abs(np.sum(mu_goal) - 1) <= PRECISION, "Sum of mu_goal is not 1!"
# check if mu and mu_goal are different
abs_diff = np.sum(np.abs(mu - mu_goal))
assert abs_diff > 0, "mu and mu_goal are not different!"
method_name = 'min_norm_rank_1()'
start_time = time.time()
DeltaSol = min_norm_rank_1(P, mu_goal, normType)
run_time = time.time() - start_time
store_results(
results_dict, method_name, P, DeltaSol, mu_goal, normType,
run_time, calc_norm=True
)
method_name = 'min_norm_rank_1_pert_pres_stoch()'
start_time = time.time()
alpha, soft_mu_goal = max_convex_softened_mu_goal(P, mu, mu_goal)
if alpha > 0:
DeltaSol = min_norm_rank_1_pert_pres_stoch(
P, mu, soft_mu_goal,
normtype=normType
)
else:
DeltaSol = np.zeros((n, n))
run_time = time.time() - start_time
store_results(
results_dict, method_name, P, DeltaSol, mu_goal, normType,
run_time
)
method_name = 'R1SH(2)'
start_time = time.time()
DeltaSol = RISH_K(
P, mu, mu_goal, normType=normType,
verbose=False, sequence='descending',
jumpDirectlyToGoalIfPossible=False,
considerDeltaHistory=True,
numbIntervals=2
)
run_time = time.time() - start_time
store_results(
results_dict, method_name, P, DeltaSol, mu_goal, normType,
run_time
)
method_name = 'R1SH(4)'
start_time = time.time()
DeltaSol = RISH_K(
P, mu, mu_goal, normType=normType,
verbose=False, sequence='descending',
jumpDirectlyToGoalIfPossible=False,
considerDeltaHistory=True,
numbIntervals=4
)
run_time = time.time() - start_time
store_results(
results_dict, method_name, P, DeltaSol, mu_goal, normType,
run_time
)
method_name = 'R1SH(8)'
start_time = time.time()
DeltaSol = RISH_K(
P, mu, mu_goal, normType=normType,
verbose=False, sequence='descending',
jumpDirectlyToGoalIfPossible=False,
considerDeltaHistory=True,
numbIntervals=8
)
run_time = time.time() - start_time
store_results(
results_dict, method_name, P, DeltaSol, mu_goal, normType,
run_time
)
method_name = 'R1SH(16)'
start_time = time.time()
DeltaSol = RISH_K(
P, mu, mu_goal, normType=normType,
verbose=False, sequence='descending',
jumpDirectlyToGoalIfPossible=False,
considerDeltaHistory=True,
numbIntervals=16
)
run_time = time.time() - start_time
store_results(
results_dict, method_name, P, DeltaSol, mu_goal, normType,
run_time
)
method_name = 'R1SH'
start_time = time.time()
DeltaSol = R1SH(
P, mu, mu_goal,
normType=normType, verbose=True,
sequence='descending',
jumpDirectlyToGoalIfPossible=False,
considerDeltaHistory=True,
trackBestSolutionAlongTheWay=True,
timeLimit=timeLimit
)
run_time = time.time() - start_time
store_results(
results_dict, method_name, P, DeltaSol, mu_goal, normType,
run_time
)
method_name = f'FR1SH({epsFR1SH})'
start_time = time.time()
DeltaSol = FR1SH(
P, mu, mu_goal, normType=normType,
verbose=True, phi=epsFR1SH,
sequence='descending',
jumpToGoalIfPossible=False,
considerDeltaHistory=True,
trackBestSolutionAlongTheWay=True,
timeLimit=timeLimit
)
run_time = time.time() - start_time
store_results(
results_dict, method_name, P, DeltaSol, mu_goal, normType,
run_time
)
method_name = 'Riesz projector'
start_time = time.time()
DeltaSol = DeltaSolErgProj = ergodic_project(mu_goal) - P
run_time = time.time() - start_time
store_results(
results_dict, method_name, P, DeltaSol, mu_goal, normType,
run_time
)
# print table of results
results_table = [[method] + [np.round(np.nanmean(results[k]), 4)
for k, v in results.items()]
for method, results in results_dict.items()]
column_labels = ['Method', f'mean(\|Delta\|_{normType})', 'Fraction feasible',
'mean run time', 'mean rank']
print(tabulate(results_table, column_labels))
print(tabulate(results_table, column_labels, tablefmt="latex"))