-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunNetworkFragilityClean.py
More file actions
310 lines (255 loc) · 9.47 KB
/
RunNetworkFragilityClean.py
File metadata and controls
310 lines (255 loc) · 9.47 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 21 12:20:07 2022
@author: jeremie
"""
from NetworkFragilityClean import fragile_net
import numpy as np
import networkx as nx
import copy as COPY
def run_sparse_fn(G,Delta,NetworkType=''):
Output = {} #There is going to be a lot of output so get ready!
NestedOutput = {}
NestedOutput2 = {}
NestedOutput3 = {}
FN = fragile_net()
FN.add_graph(G)
CG = COPY.deepcopy(G)
#Start the first method...
Method = 'MinDegree'
print('Starting ' + Method + ' Now!')
Message = 'Starting: ' + NetworkType + ' Runs Now!'
print(Message)
fracs = []
removals = []
fracwithoutrewire = []
min_frac = 1
numRemovals = 1
Glist = [nx.adjacency_matrix(G)]
Glist2 = [nx.adjacency_matrix(G)]
while min_frac>Delta:
print('Number of Removals: ',numRemovals)
removals.append(numRemovals)
#print('This is the list of removals: ', removals)
Gnew,RL = FN.sparse_gg_removal(1,AttackStrategy=Method)
AAANew = nx.adjacency_matrix(Gnew)
Glist.append(AAANew)
#Anew1 = COPY.deepcopy(Anew)
Size,frac = FN.size_LCC(Gnew,return_fraction=True)
fracwithoutrewire.append(frac)
print(frac)
SUM = 1
while SUM>0:
Gnew2 = FN.sparse_rwtf_greedy()
SUM = FN.size_LCC(Gnew2)-FN.size_LCC(Gnew)
Size,frac = FN.size_LCC(Gnew2,return_fraction=True)
min_frac = frac
print(frac)
fracs.append(frac)
Glist2.append(nx.adjacency_matrix(Gnew2))
numRemovals = numRemovals+1
#Reset Graph to previous one without rewiring...
FN.add_graph_new(None)
FN.add_graph(nx.Graph(Glist[len(Glist)-1]))
#print("This is removals: ", removals)
NestedOutput['Removals'] = np.array(removals)
NestedOutput['Fractions'] = np.array(fracs)
NestedOutput['FinalAdjacency'] = Gnew2
NestedOutput['FinalNumRemoved'] = numRemovals-1
NestedOutput['FractionsWithoutRewire'] = np.array(fracwithoutrewire)
NestedOutput['GraphsWORW'] = Glist
NestedOutput['Graphs'] = Glist2
Output[Method] = NestedOutput
#################################################################
#################################################################
Method = 'EdgeBetweenness'
print('Starting ' + Method + ' Now!')
#Reset for calculational purposes
FN.add_graph_new(None)
FN.add_graph(CG)
Message = 'Starting: ' + Method + ' Runs Now!'
print(Message)
fracs = []
removals = []
fracwithoutrewire = []
min_frac = 1
numRemovals = 1
Glist = [nx.adjacency_matrix(CG)]
Glist2 = [nx.adjacency_matrix(CG)]
while min_frac>Delta:
print('Number of Removals: ',numRemovals)
removals.append(numRemovals)
Gnew,RL = FN.sparse_gg_removal(1,Method)
AAANew = nx.adjacency_matrix(Gnew)
Glist.append(AAANew)
#Glist.append(Gnew)
Size,frac = FN.size_LCC(Gnew,return_fraction=True)
fracwithoutrewire.append(frac)
SUM=1
while SUM>0:
Gnew2 = FN.sparse_rwtf_greedy()
SUM = FN.size_LCC(Gnew2)-FN.size_LCC(Gnew)
Size,frac = FN.size_LCC(Gnew2,return_fraction=True)
min_frac = frac
fracs.append(frac)
Glist2.append(nx.adjacency_matrix(Gnew2))
numRemovals = numRemovals+1
#Reset Graph to previous one without rewiring...
FN.add_graph_new(None)
FN.add_graph(nx.Graph(Glist[len(Glist)-1]))
NestedOutput2['Removals'] = np.array(removals)
NestedOutput2['Fractions'] = np.array(fracs)
NestedOutput2['FinalAdjacency'] = Gnew2
NestedOutput2['FinalNumRemoved'] = numRemovals-1
NestedOutput2['FractionsWithoutRewire'] = np.array(fracwithoutrewire)
NestedOutput2['GraphsWORW'] = Glist
NestedOutput2['Graphs'] = Glist2
Output[Method] = NestedOutput2
Method = 'EdgeSum'
print('Starting ' + Method + ' Now!')
#Reset for calculational purposes
FN.add_graph_new(None)
FN.add_graph(CG)
Message = 'Starting: ' + Method + ' Runs Now!'
print(Message)
fracs = []
removals = []
fracwithoutrewire = []
min_frac = 1
numRemovals = 1
Glist = [nx.adjacency_matrix(CG)]
Glist2 = [nx.adjacency_matrix(CG)]
while min_frac>Delta:
print('Number of Removals: ',numRemovals)
removals.append(numRemovals)
Gnew,RL = FN.sparse_gg_removal(1,Method)
AAANew = nx.adjacency_matrix(Gnew)
Glist.append(AAANew)
#Glist.append(Gnew)
Size,frac = FN.size_LCC(Gnew,return_fraction=True)
fracwithoutrewire.append(frac)
SUM=1
while SUM>0:
Gnew2 = FN.sparse_rwtf_greedy()
SUM = FN.size_LCC(Gnew2)-FN.size_LCC(Gnew)
Size,frac = FN.size_LCC(Gnew2,return_fraction=True)
min_frac = frac
fracs.append(frac)
Glist2.append(nx.adjacency_matrix(Gnew2))
numRemovals = numRemovals+1
#Reset Graph to previous one without rewiring...
FN.add_graph_new(None)
FN.add_graph(nx.Graph(Glist[len(Glist)-1]))
NestedOutput3['Removals'] = np.array(removals)
NestedOutput3['Fractions'] = np.array(fracs)
NestedOutput3['FinalAdjacency'] = Gnew2
NestedOutput3['FinalNumRemoved'] = numRemovals-1
NestedOutput3['FractionsWithoutRewire'] = np.array(fracwithoutrewire)
NestedOutput3['GraphsWORW'] = Glist
NestedOutput3['Graphs'] = Glist2
Output[Method] = NestedOutput3
return Output
def run_sparse_worw(G,Delta,NetworkType=''):
Output = {} #There is going to be a lot of output so get ready!
NestedOutput = {}
NestedOutput2 = {}
NestedOutput3 = {}
FN = fragile_net()
GG = COPY.deepcopy(G)
FN.add_graph(GG)
#Start the first method...
Method = 'MinDegree'
print('Starting ' + Method + ' Now!')
Message = 'Starting: ' + NetworkType + ' Runs Now!'
print(Message)
fracs = []
removals = []
fracwithoutrewire = []
Glist = [nx.adjacency_matrix(G)]
min_frac = 1
numRemovals = 1
while min_frac>Delta:
print('Number of Removals: ',numRemovals)
removals.append(numRemovals)
Gnew,L = FN.sparse_gg_removal(1,AttackStrategy=Method)
AAANew = nx.adjacency_matrix(Gnew)
Glist.append(AAANew)
Size,frac = FN.size_LCC(Gnew,return_fraction=True)
print(frac)
fracwithoutrewire.append(frac)
#FN.add_graph(Gnew)
min_frac = frac
numRemovals = numRemovals+1
#print("This is removals: ", removals)
NestedOutput['Removals'] = np.array(removals)
NestedOutput['Fractions'] = np.array(fracs)
NestedOutput['FinalAdjacency'] = Gnew
NestedOutput['FinalNumRemoved'] = numRemovals-1
NestedOutput['FractionsWithoutRewire'] = np.array(fracwithoutrewire)
NestedOutput['Graphs'] = Glist
Output[Method] = NestedOutput
#################################################################
#################################################################
Method = 'EdgeBetweenness'
print('Starting ' + Method + ' Now!')
#Reset for calculational purposes
FN.add_graph_new(None)
Message = 'Starting: ' + NetworkType + ' Runs Now!'
print(Message)
fracs = []
removals = []
fracwithoutrewire = []
min_frac = 1
Glist = [nx.adjacency_matrix(G)]
numRemovals = 1
while min_frac>Delta:
print('Number of Removals: ',numRemovals)
removals.append(numRemovals)
Gnew,L = FN.sparse_gg_removal(1,AttackStrategy=Method)
AAANew = nx.adjacency_matrix(Gnew)
Glist.append(AAANew)
Size,frac = FN.size_LCC(Gnew,return_fraction=True)
print(frac)
fracwithoutrewire.append(frac)
min_frac = frac
numRemovals = numRemovals+1
NestedOutput2['Removals'] = np.array(removals)
NestedOutput2['Fractions'] = np.array(fracs)
NestedOutput2['FinalAdjacency'] = Gnew
NestedOutput2['FinalNumRemoved'] = numRemovals-1
NestedOutput2['FractionsWithoutRewire'] = np.array(fracwithoutrewire)
NestedOutput2['Graphs'] = Glist
Output[Method] = NestedOutput2
#################################################################
Method = 'EdgeSum'
print('Starting ' + Method + ' Now!')
#Reset for calculational purposes
FN.add_graph_new(None)
Message = 'Starting: ' + NetworkType + ' Runs Now!'
print(Message)
fracs = []
removals = []
fracwithoutrewire = []
min_frac = 1
Glist = [nx.adjacency_matrix(G)]
numRemovals = 1
while min_frac>Delta:
print('Number of Removals: ',numRemovals)
removals.append(numRemovals)
Gnew,L = FN.sparse_gg_removal(1,AttackStrategy=Method)
AAANew = nx.adjacency_matrix(Gnew)
Glist.append(AAANew)
Size,frac = FN.size_LCC(Gnew,return_fraction=True)
print(frac)
fracwithoutrewire.append(frac)
min_frac = frac
numRemovals = numRemovals+1
NestedOutput3['Removals'] = np.array(removals)
NestedOutput3['Fractions'] = np.array(fracs)
NestedOutput3['FinalAdjacency'] = Gnew
NestedOutput3['FinalNumRemoved'] = numRemovals-1
NestedOutput3['FractionsWithoutRewire'] = np.array(fracwithoutrewire)
NestedOutput3['Graphs'] = Glist
Output[Method] = NestedOutput3
return Output