-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticle_num_experiments_sparse_small.py
228 lines (201 loc) · 6.84 KB
/
article_num_experiments_sparse_small.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
"""
Created on 7/18/2021
Author: Joost Berkhout (VU, email: joost.berkhout@vu.nl)
Description: This script is used to do the numerical experiments for a small
sparse instance to test the performance of the method compared to an LP
approach (which does not work for the larger sparse networks for road,
Moreno and email).
"""
import networkx
from tabulate import tabulate
from TSDP_optimization import *
from utilities import (
store_results, find_cliques_from_adjacency_matrix,
calc_stationary_distribution, ergodic_project,
)
"""
WARNING:
Choose the following constant values in TSDP_optimization.py:
PRECISION = 10 ** (-5)
BINSEARCH_PRECISION = 10 ** (-5)
"""
# user init
timeLimitLP = 5 * 60
normType = 'inf'
n = 100 # size random instances
numb_exp = 25
verbose = False
epsFR1SH = 10 ** (-8)
# technical init
results_dict = {}
for random_seed in range(numb_exp):
np.random.seed(random_seed)
print(f'Start experiment with seed {random_seed}')
# random preferential attachment network
G = networkx.generators.random_graphs.barabasi_albert_graph(
n, 5,
seed=random_seed
)
A = networkx.linalg.graphmatrix.adjacency_matrix(G).todense()
P = np.array(A / np.sum(A, axis=1))
mu = calc_stationary_distribution(P)
# Experiment with more difficult goal
# # mu_goal where largest clique is uniform
# mu_goal = mu.copy()
# cliques = find_cliques_from_adjacency_matrix(P)
# cliques_length = [len(clq) for clq in cliques]
# largest_clique_idx = np.argmax(cliques_length)
# clique_idxs = cliques[largest_clique_idx]
# sum_max = np.sum(mu_goal[clique_idxs])
# mu_goal[clique_idxs] = sum_max / len(clique_idxs)
# Experiment with an easier goal
# mu_goal where influence of largest clique reduced by 10%
mu_goal = mu.copy()
cliques = find_cliques_from_adjacency_matrix(P)
cliques_length = [len(clq) for clq in cliques]
largest_clique_idx = np.argmax(cliques_length)
clique_idxs = cliques[largest_clique_idx]
clique_mass_increase_factor = .9
mu_goal[clique_idxs] *= clique_mass_increase_factor
sum_clique = np.sum(mu_goal[clique_idxs])
outside_clique = [i for i in range(n) if i not in clique_idxs]
increase_factor_outside = (1 - sum_clique) / np.sum(mu_goal[outside_clique])
mu_goal[outside_clique] *= increase_factor_outside
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 = 'optimal'
# start_time = time.time()
# DeltaSol = goal_MC_optimization(
# P, mu_goal, normtype=normType,
# rankOne=False, ensureNonNeg=True,
# verbose=False, timeLimit=timeLimitLP
# )
# run_time = time.time() - start_time
# store_results(
# results_dict, method_name, P, DeltaSol, mu_goal, normType,
# run_time
# )
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=False,
sequence='descending',
jumpDirectlyToGoalIfPossible=False,
considerDeltaHistory=True,
trackBestSolutionAlongTheWay=True
)
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
)
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"))