forked from Danfoa/attacks_on_complex_networks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiments.py
223 lines (186 loc) · 13.2 KB
/
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
import warnings
import numpy as np
from network_attacks import instantaneous_attack, incremental_attack, incremental_random_failure, \
instantaneous_random_failure
from utils.data_saver import save_results
from utils.network_generation import get_poisson_net, get_power_law_net, get_gnutella
from utils.visualization import plot_clustering_distribution, plot_metric_distribution, save_network_tracking
warnings.filterwarnings("ignore", category=UserWarning)
def instantaneous_attack_powerlaw(exp_num_nodes, exp_removal_ratios, exp_ks, is_random_attack):
for n_nodes in exp_num_nodes:
for k in exp_ks:
net = get_power_law_net(n_nodes, k, verbose=False)
if not is_random_attack:
min_path, max_path, cluster_size_ratios = instantaneous_attack(net=net,
removal_rates=exp_removal_ratios,
verbose=True)
file_name = "powerlaw-inst-attack-k=%.2f-n_nodes=%d" % (k, n_nodes)
title = "Instantaneous Attack - Powerlaw nodes=%d k=%.2f " % (n_nodes, k)
else:
min_path, max_path, cluster_size_ratios = instantaneous_random_failure(net=net,
removal_rates=exp_removal_ratios,
verbose=True)
file_name = "powerlaw-inst-failure-k=%.2f-n_nodes=%d" % (k, n_nodes)
title = "Instantaneous Failure - Powerlaw nodes=%d k=%.2f " % (n_nodes, k)
save_results(min_path, max_path, cluster_size_ratios, file_name)
plot_metric_distribution([min_path, max_path],
exp_removal_ratios,
y_label="path length",
labels=[r'$d_{min}$', r'$d_{max}$'],
title=title,
filename=file_name + '_distr.png')
clusters_info = [x[0] for x in cluster_size_ratios]
plot_clustering_distribution(clusters_info,
exp_removal_ratios,
y_label="clusterings",
labels=[r'$S$', r'$\langle s \rangle$'],
title=title,
filename=file_name + '_clust.png')
def instantaneous_attack_poisson(exp_num_nodes, exp_removal_ratios, exp_mus, is_random_attack):
for n_nodes in exp_num_nodes:
for mu in exp_mus:
net = get_poisson_net(n_nodes=n_nodes, mu=mu, verbose=True)
if not is_random_attack:
min_path, max_path, cluster_size_ratios = instantaneous_attack(net=net,
removal_rates=exp_removal_ratios,
verbose=True)
file_name = "poisson-inst-attack-mu=%.2f-n_nodes=%d" % (mu, n_nodes)
title = "Instantaneous Attack - Poisson nodes=%d mu=%.2f " % (n_nodes, mu)
else:
min_path, max_path, cluster_size_ratios = instantaneous_random_failure(net=net,
removal_rates=exp_removal_ratios,
verbose=True)
file_name = "poisson-inst-failure-mu=%.2f-n_nodes=%d" % (mu, n_nodes)
title = "Instantaneous Failure - Poisson nodes=%d mu=%.2f " % (n_nodes, mu)
save_results(min_path, max_path, cluster_size_ratios, file_name)
plot_metric_distribution([min_path, max_path],
exp_removal_ratios,
y_label="path length",
labels=[r'$d_{min}$', r'$d_{max}$'],
title=title,
filename=file_name + '_distr.png')
clusters_info = [x[0] for x in cluster_size_ratios]
plot_clustering_distribution(clusters_info,
exp_removal_ratios,
y_label="clusterings",
labels=[r'$S$', r'$\langle s \rangle$'],
title=title,
filename=file_name + '_clust.png')
def incremental_attack_poisson(exp_removal_rate, exp_max_rate, exp_num_nodes, exp_mus, is_random_attack, track_net=1):
for n_nodes in exp_num_nodes:
for mu in exp_mus:
net = get_poisson_net(n_nodes=n_nodes, mu=mu, verbose=False)
if not is_random_attack:
file_name = "poisson-incr-attack-mu=%.2f-n_nodes=%d" % (mu, n_nodes)
title = "Incremental Attack - Poisson nodes=%d mu=%.2f " % (n_nodes, mu)
min_path, max_path, cluster_size_ratios, network_tracking = incremental_attack(net=net,
removal_rate=exp_removal_rate,
max_rate=exp_max_rate,
verbose=True,
track_net_num=track_net)
else:
min_path, max_path, cluster_size_ratios, network_tracking = incremental_random_failure(net=net,
removal_rate=exp_removal_rate,
max_rate=exp_max_rate,
track_net_num=track_net)
file_name = "poisson-incr-failure-mu=%.2f-n_nodes=%d" % (mu, n_nodes)
title = "Incremental Failure - Poisson nodes=%d mu=%.2f " % (n_nodes, mu)
save_results(min_path, max_path, cluster_size_ratios, file_name)
save_network_tracking(network_tracking, title, file_name)
steps = int(exp_max_rate / exp_removal_rate)
plot_metric_distribution([min_path, max_path],
np.cumsum([exp_removal_rate] * steps),
y_label="path length",
labels=[r'$d_{min}$', r'$d_{max}$'],
title=title,
filename=file_name + '_distr.png')
clusters_info = [x[0] for x in cluster_size_ratios]
plot_clustering_distribution(clusters_info,
np.cumsum([exp_removal_rate] * steps),
y_label="clusterings",
labels=[r'$S$', r'$\langle s \rangle$'],
title=title,
filename=file_name + '_clust.png')
def incremental_attack_powerlaw(exp_removal_rate, exp_max_rate, exp_num_nodes, exp_ks, is_random_attack, track_net=1):
for n_nodes in exp_num_nodes:
for k in exp_ks:
net = get_power_law_net(n_nodes, k, verbose=False)
if not is_random_attack:
min_path, max_path, cluster_size_ratios, network_tracking = incremental_attack(net=net,
removal_rate=exp_removal_rate,
max_rate=exp_max_rate,
track_net_num=track_net)
file_name = "powerlaw-incr-attack-k=%.2f-n_nodes=%d" % (k, n_nodes)
title = "Incremental Attack - Powerlaw nodes=%d k=%.2f " % (n_nodes, k)
else:
min_path, max_path, cluster_size_ratios, network_tracking = incremental_random_failure(net=net,
removal_rate=exp_removal_rate,
max_rate=exp_max_rate,
track_net_num=track_net)
file_name = "powerlaw-incr-failure-k=%.2f-n_nodes=%d" % (k, n_nodes)
title = "Incremental Failure - Powerlaw nodes=%d k=%.2f " % (n_nodes, k)
save_results(min_path, max_path, cluster_size_ratios, file_name)
save_network_tracking(network_tracking, title, file_name)
steps = int(exp_max_rate / exp_removal_rate)
plot_metric_distribution([min_path, max_path],
np.cumsum([exp_removal_rate] * steps),
y_label="path length",
labels=[r'$d_{min}$', r'$d_{max}$'],
title=title,
filename=file_name + '_distr.png')
clusters_info = [x[0] for x in cluster_size_ratios]
plot_clustering_distribution(clusters_info,
np.cumsum([exp_removal_rate] * steps),
y_label="clusterings",
labels=[r'$S$', r'$\langle s \rangle$'],
title=title,
filename=file_name + '_clust.png')
def incremental_attack_(net, file_name, title, exp_removal_rate, exp_max_rate, is_random_attack, track_net=1):
if not is_random_attack:
min_path, max_path, cluster_size_ratios, network_tracking = incremental_attack(net=net,
removal_rate=exp_removal_rate,
max_rate=exp_max_rate,
verbose=True,
track_net_num=track_net)
else:
min_path, max_path, cluster_size_ratios, network_tracking = incremental_random_failure(net=net,
removal_rate=exp_removal_rate,
max_rate=exp_max_rate,
track_net_num=track_net)
save_results(min_path, max_path, cluster_size_ratios, file_name)
save_network_tracking(network_tracking, title, file_name)
steps = int(exp_max_rate / exp_removal_rate)
plot_metric_distribution([min_path, max_path],
np.cumsum([exp_removal_rate] * steps),
y_label="path length",
labels=[r'$d_{min}$', r'$d_{max}$'],
title=title,
filename=file_name + '_distr.png')
clusters_info = [x[0] for x in cluster_size_ratios]
plot_clustering_distribution(clusters_info,
np.cumsum([exp_removal_rate] * steps),
y_label="clusterings",
labels=[r'$S$', r'$\langle s \rangle$'],
title=title,
filename=file_name + '_clust.png')
if __name__ == "__main__":
exp_removal_rate = 0.0025
exp_removal_ratios = np.linspace(0.0, 0.5, 10)
exp_max_rate = 0.05
exp_num_nodes = [10000] # Test the attacks with different sizes of networks
exp_mus = [4]
exp_ks = [2.6]
for is_random_attack in [False]:
# Poisson
incremental_attack_poisson(exp_removal_rate, exp_max_rate, exp_num_nodes, exp_mus, is_random_attack,
track_net=7)
# instantaneous_attack_poisson(exp_num_nodes, exp_removal_ratios, exp_mus, is_random_attack)
# Scale Free
incremental_attack_powerlaw(exp_removal_rate, exp_max_rate, exp_num_nodes, exp_ks, is_random_attack,
track_net=7)
# instantaneous_attack_powerlaw(exp_num_nodes, exp_removal_ratios, exp_ks, is_random_attack)
# GNutella
net = get_gnutella()
file_name = title = "GNutella"
# oregon.degree_distribution(net)
# incremental_attack_(net, file_name, title, exp_removal_rate, exp_max_rate, is_random_attack, track_net=6)