-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevolution_models.py
More file actions
299 lines (267 loc) · 12.6 KB
/
evolution_models.py
File metadata and controls
299 lines (267 loc) · 12.6 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
import networkx as nx
import selection_probability
import math
import random
def B_A(graph, internal_link_factor, links_added_per_step, add_percentage, del_percentage, time_step):
# parameters initialization
network = graph
f = internal_link_factor
m = links_added_per_step
num_add = int(add_percentage * nx.number_of_nodes(network))
num_del = int(del_percentage * nx.number_of_nodes(network))
time = time_step
probability = []
probability_temp = []
total_probability = 0
nodes_pre_step = []
nodes_added_this_step = []
nodes_pair_without_edge = []
nodes_for_del = []
# calculate how many nodes should be added in this time step
'''if n >=math.log10(t) and n <= math.log10(t+1):
num_add = int (n * nx.number_of_nodes(network) / 100)
num_del = num_add
#print 'num_add',num_add
else:
num_add = int(math.log10(t) * nx.number_of_nodes(network) / 100)
num_del = int((n * (t-1) / (pow(10,n) - 1)) * nx.number_of_nodes(network) / 100)
#print 'num_add', num_add
#print 'num_del', num_del'''
# calculate the probability of each node to be linked according to whose degree
for node in nx.nodes_iter(network):
#print 'degree',network.degree(node)
nodes_pre_step.append(node)
probability_temp.append(network.degree(node))
total_probability += network.degree(node)
for prob in probability_temp:
probability.append(float(prob) / total_probability)
#print 'prob_temp',probability
#print 'prob',probability
# add num_add nodes to the network
i = 1
while i <= num_add:
nodes_added_this_step.append('%d' % time + '_' + '%d' % i)
i += 1
network.add_nodes_from(nodes_added_this_step)
# create m links for each node added in this time dtep according to the probability
#print 'nodes_pre_step',nodes_pre_step
for node_added in nodes_added_this_step:
selected_nodes = selection_probability.select(nodes_pre_step,probability,m)
#print 'selected_nodes', selected_nodes
for node in selected_nodes:
network.add_edge(node_added,node)
# add f % internal links according to the production of each pair of nodes' degrees
probability = []
probability_temp = []
total_probability = 0
for i, elei in enumerate(nx.nodes_iter(network)):
for j, elej in enumerate(nx.nodes_iter(network)):
if i >= j:
continue
if not network.has_edge(elei, elej):
nodes_pair_without_edge.append((elei,elej))
probability_temp.append(network.degree(elei) * network.degree(elej))
total_probability += network.degree(elei) * network.degree(elej)
for prob in probability:
probability.append(float(prob) / total_probability)
selected_pairs = selection_probability.select(nodes_pair_without_edge,probability,int(f*nx.number_of_nodes(network)))
for nodei,nodej in selected_pairs:
network.add_edge(nodei,nodej)
# delete num_del nodes according to whose degree
probability = []
probability_temp = []
total_probability = 0.0
for node in nx.nodes_iter(network):
nodes_for_del.append(node)
if network.degree(node) == 0:# if the degree is 0, let it to be 0.1, to avoid errors in probability calculation
node_degree = 0.1
else:
node_degree = network.degree(node)
probability_temp.append(1.0/node_degree)
total_probability += 1.0/node_degree
for prob in probability_temp:
probability.append(prob/total_probability)
selected_del = selection_probability.select(nodes_for_del,probability,num_del)
for node in selected_del:
network.remove_node(node)
#print 'del',node
def W_S(graph,beta,edges_rewired):
network = graph
edges_add = []
edges_removed = []
for edge in edges_rewired:
x = random.uniform(0, 1)
if x <= beta:
node_random_selected = random.randint(0, nx.number_of_nodes(network)-1)
#print 'node_random_selected',node_random_selected
#print network.nodes()[node_random_selected]
if node_random_selected != edge[0] and node_random_selected != edge[1] \
and (not ((edge[0], network.nodes()[node_random_selected]) in edges_add))\
and (not ((edge[0], network.nodes()[node_random_selected]) in network.edges())):
edges_add.append((edge[0],network.nodes()[node_random_selected]))
edges_removed.append(edge)
network.remove_edges_from(edges_removed)
network.add_edges_from(edges_add)
for i in edges_removed:
edges_rewired.remove(i)
for j in edges_add:
edges_rewired.append(j)
#print edges_rewired
def E_R(network,p,rate,time):
num_add = int(rate*nx.number_of_nodes(network))
nodes_added_this_step = []
i = 1
nodes_del = []
while True:
nodes_del = random.sample(network.nodes(), num_add)
if not('newcomer' in nodes_del):
break
network.remove_nodes_from(nodes_del)
while i <= num_add:
nodes_added_this_step.append('%d' % time + '_' + '%d' % i)
i += 1
network.add_nodes_from(nodes_added_this_step)
for i in nodes_added_this_step:
for j in nx.nodes_iter(network):
if j == 'newcomer':
continue
x = random.uniform(0, 1)
if x<=p:
network.add_edge(i,j)
def L_P(graph, threshold_add, threshold_del, time):
# parameters initialization
network = graph
#print nx.number_of_edges(network)
num_add = int(threshold_add * nx.number_of_edges(network)) # the number of egdes to be added
#num_del = int(threshold_del * nx.number_of_edges(network)) # the number of edges to be deleted
nodes_pair_with_edge = [] # the pairs of nodes with edges
nodes_pair_without_edge = [] # the pairs of nodes without edges
probability_add = [] # the probabilities of the pairs of nodes to be added
#probability_del = [] # the probabilities of the pairs of nodes to be deleted
u = 0 # node i
v = 0 # node j
score = 0 # the score of each pair of nodes in link prediction model
#total_score_with_edge = 0.0 # the sum of scores of pairs of nodes with edge
total_score_without_edge = 0.0 # the sum of scores of pairs of nodes without edge
# calculate the score of each pair of nodes
for i, elei in enumerate(nx.nodes_iter(network)):
for j, elej in enumerate(nx.nodes_iter(network)):
if i >= j:
continue
if not network.has_edge(elei, elej):
try:
pre = nx.adamic_adar_index(network, [(elei, elej)])
for u, v, s in pre:
score = s
except ZeroDivisionError:
score = 5
total_score_without_edge += score
nodes_pair_without_edge.append((u, v, score))
for a, b, c in nodes_pair_without_edge:
probability_add.append(c / total_score_without_edge) # calculate the probabilities of edges to be added
# select edges to be added according to probabilities
edges_add = selection_probability.select(nodes_pair_without_edge, probability_add, num_add)
for a, b, c in edges_add:
network.add_edge(a, b) # add selected edges
#print 'del', num_del,',', 'add', num_add,',','edges:', nx.number_of_edges(network)
#print 'del', edges_del
#print 'add', edges_add
def L_P_common_neighbors(graph, threshold_add, threshold_del, time):
# parameters initialization
network = graph
#print nx.number_of_edges(network)
num_add = int(threshold_add * nx.number_of_edges(network)) # the number of egdes to be added
#num_del = int(threshold_del * nx.number_of_edges(network)) # the number of edges to be deleted
nodes_pair_with_edge = [] # the pairs of nodes with edges
nodes_pair_without_edge = [] # the pairs of nodes without edges
probability_add = [] # the probabilities of the pairs of nodes to be added
#probability_del = [] # the probabilities of the pairs of nodes to be deleted
u = 0 # node i
v = 0 # node j
score = 0 # the score of each pair of nodes in link prediction model
#total_score_with_edge = 0.0 # the sum of scores of pairs of nodes with edge
total_score_without_edge = 0.0 # the sum of scores of pairs of nodes without edge
# calculate the score of each pair of nodes
for i, elei in enumerate(nx.nodes_iter(network)):
for j, elej in enumerate(nx.nodes_iter(network)):
if i >= j:
continue
if not network.has_edge(elei, elej):
try:
score = len(nx.common_neighbors(network, elei, elej))
except :
continue
total_score_without_edge += score
nodes_pair_without_edge.append((elei, elej, score))
for a, b, c in nodes_pair_without_edge:
probability_add.append(c / total_score_without_edge) # calculate the probabilities of edges to be added
# select edges to be added according to probabilities
edges_add = selection_probability.select(nodes_pair_without_edge, probability_add, num_add)
for a, b, c in edges_add:
network.add_edge(a, b) # add selected edges
#print 'del', num_del,',', 'add', num_add,',','edges:', nx.number_of_edges(network)
#print 'del', edges_del
#print 'add', edges_add
def L_P_jaccard_coefficient(graph, threshold_add, threshold_del, time):
# parameters initialization
network = graph
# print nx.number_of_edges(network)
num_add = int(threshold_add * nx.number_of_edges(network)) # the number of egdes to be added
# num_del = int(threshold_del * nx.number_of_edges(network)) # the number of edges to be deleted
nodes_pair_with_edge = [] # the pairs of nodes with edges
nodes_pair_without_edge = [] # the pairs of nodes without edges
probability_add = [] # the probabilities of the pairs of nodes to be added
# probability_del = [] # the probabilities of the pairs of nodes to be deleted
u = 0 # node i
v = 0 # node j
score = 0 # the score of each pair of nodes in link prediction model
# total_score_with_edge = 0.0 # the sum of scores of pairs of nodes with edge
total_score_without_edge = 0.0 # the sum of scores of pairs of nodes without edge
# calculate the score of each pair of nodes
for i, elei in enumerate(nx.nodes_iter(network)):
for j, elej in enumerate(nx.nodes_iter(network)):
if i >= j:
continue
if not network.has_edge(elei, elej):
try:
pre = nx.jaccard_coefficient(network, [(elei, elej)])
for u, v, s in pre:
score = s
except :
continue
total_score_without_edge += score
nodes_pair_without_edge.append((elei, elej, score))
for a, b, c in nodes_pair_without_edge:
probability_add.append(c / total_score_without_edge) # calculate the probabilities of edges to be added
# select edges to be added according to probabilities
edges_add = selection_probability.select(nodes_pair_without_edge, probability_add, num_add)
for a, b, c in edges_add:
network.add_edge(a, b) # add selected edges
# print 'del', num_del,',', 'add', num_add,',','edges:', nx.number_of_edges(network)
# print 'del', edges_del
# print 'add', edges_add
def N_W_S(graph,beta,nodes_add_rate):
network = graph
edges_add = []
num_of_nodes_add = int(nx.number_of_nodes(network)*nodes_add_rate)
num = 1
i=0
j=0
num = 1
while num <= num_of_nodes_add:
node_selected = random.randint(0, nx.number_of_nodes(network)-1)
node_1 = node_selected - 1
node_2 = node_selected + 1
node_3 = node_selected +2
for edge in nx.edges_iter(network):
x = random.uniform(0, 1)
if x < beta:
node_random_selected = random.randint(0, nx.number_of_nodes(network)-1)
#print 'node_random_selected',node_random_selected
#print network.nodes()[node_random_selected]
if node_random_selected != edge[0] and node_random_selected != edge[1] \
and (not ((edge[0], network.nodes()[node_random_selected]) in edges_add))\
and (not ((edge[0], network.nodes()[node_random_selected]) in network.edges())):
edges_add.append((edge[0],network.nodes()[node_random_selected]))
network.add_edges_from(edges_add)
#draw_network.draw(network)