forked from malizad/Spatial_Networks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpSW.py~
314 lines (259 loc) · 8.05 KB
/
SpSW.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
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
309
310
311
# coding: utf-8
# In[1]:
import matplotlib
import pylab as pl
import random as rd
import numpy as np
import networkx as nx
# In[2]:
def init(n,density):
global agents, pop
width = int(round((n/density)**0.5))
height = int(round((n/density)**0.5))
config = np.zeros([height, width])
pop = 0 #actual population size
for x in range(height):
for y in range(width):
if rd.random() < density:
config[x,y] = 1
pop += 1
# Populating the agents matrix with a random sequence of agents
seq = [x for x in range(pop)]
rd.shuffle(seq)
agents = np.zeros([height, width])
for r in range(height):
for t in range(width):
if agents[r,t] == 0:
agents[r,t] = -1
z = 0
for i in range(height):
for j in range(width):
if config[i,j] == 1:
agents[i,j]=seq[z]
z += 1
# In[3]:
def SpatialSW2(p,k,a):
global u,v,j,w,G,targets,nodes
G = nx.Graph()
nodes = list(range(pop)) # nodes are labeled 0 to n-1
# connect each node to k/2 neighbors
for j in range(1, k // 2+1):
targets = nodes[j:] + nodes[0:j] # first j nodes are now last in list
G.add_edges_from(zip(nodes,targets))
# rewire edges from each node
# loop over all nodes in order (label) and neighbors in order (distance)
# no self loops or multiple edges allowed
for j in range(1, k // 2+1): # outer loop is neighbors
targets = nodes[j:] + nodes[0:j] # first j nodes are now last in list
# inner loop in node order
for u,v in zip(nodes,targets):
if rd.random() < p:
chosen = False
while chosen == False:
w = rd.choice(nodes)
while w == u or G.has_edge(u, w): # Enforce no self-loops or multiple edges
w = rd.choice(nodes)
u_coord = np.where(agents == u)
w_coord = np.where(agents == w)
d = (float(u_coord[0] - w_coord[0])**2 + (float(u_coord[1] - w_coord[1])**2))**0.5
q = 0.5 * (d)**(-a)
if rd.random() < q:
G.remove_edge(u,v)
G.add_edge(u,w)
chosen = True
return G
# In[4]:
def SpSW(k,a):
global u,v,j,w,G,targets,nodes
G = nx.Graph()
nodes = list(range(pop)) # nodes are labeled 0 to n-1
# connect each node to k/2 neighbors
for j in range(1, k // 2+1):
targets = nodes[j:] + nodes[0:j] # first j nodes are now last in list
G.add_edges_from(zip(nodes,targets))
# rewire edges from each node
# loop over all nodes in order (label) and neighbors in order (distance)
# no self loops or multiple edges allowed
for j in range(1, k // 2+1): # outer loop is neighbors
targets = nodes[j:] + nodes[0:j] # first j nodes are now last in list
# inner loop in node order
for u,v in zip(nodes,targets):
u_coord = np.where(agents == u)
v_coord = np.where(agents == v)
d = (float(u_coord[0] - v_coord[0])**2 + (float(u_coord[1] - v_coord[1])**2))**0.5
p = (d)**(-a)
if rd.random() < p:
w = rd.choice(nodes)
while w == u or G.has_edge(u, w): # Enforce no self-loops or multiple edges
w = rd.choice(nodes)
G.remove_edge(u,v)
G.add_edge(u,w)
return G
# In[4]:
clust = list()
path = list()
ass = list()
dens = list()
mean_deg = list()
std_deg = list()
min_deg = list()
max_deg = list()
trans = list()
n=1000
k=20
density=[0.3,0.5,0.8]
alpha=[1,1.5,2]
for d in density:
for a in alpha:
for i in range(25):
init(n,d)
network = SpSW(k,a)
clust.append(nx.average_clustering(network))
path.append(nx.average_shortest_path_length(network))
ass.append(nx.degree_pearson_correlation_coefficient(network))
no_edges = len(nx.edges(network))
all_edges = float((pop*(pop-1))/2)
dens.append(no_edges/all_edges)
deg = nx.degree(network).values()
mean_deg.append(np.mean(deg))
std_deg.append(np.std(deg))
min_deg.append(np.min(deg))
max_deg.append(np.max(deg))
trans.append(nx.transitivity(network))
np.savetxt("clust.txt",clust,delimiter="\t")
np.savetxt("path.txt",path,delimiter='\t')
np.savetxt("ass.txt",ass,delimiter='\t')
np.savetxt("dens.txt",dens,delimiter='\t')
np.savetxt("mean_deg.txt",mean_deg,delimiter="\t")
np.savetxt("std_deg.txt",std_deg,delimiter='\t')
np.savetxt("min_deg.txt",min_deg,delimiter='\t')
np.savetxt("max_deg.txt",max_deg,delimiter='\t')
np.savetxt("trans.txt",trans,delimiter='\t')
# In[68]:
CC = np.mean(clust)
print 'Clustering Coefficient = ', CC
APL = np.mean(path)
print 'Average Path Length = ', APL
AS = np.mean(ass)
print 'Assortativity = ', AS
MD = np.mean(dens)
print 'Density = ', MD
MDe = np.mean(mean_deg)
print 'Mean Degree = ', MDe
Tr = np.mean(trans)
print 'Transitivity = ', Tr
MaxD = np.mean(max_deg)
print 'Max Degree = ', MaxD
MinD = np.mean(min_deg)
print 'Min Degree = ', MinD
StdD = np.mean(std_deg)
print 'Std Degree = ', StdD
# In[6]:
n=1000
k=20
pr = [0.01,0.05,0.1]
WS_d = list()
WS_hist_keys = list()
for p in pr:
network = nx.watts_strogatz_graph(n,k,p)
deg = nx.degree(network).values()
WS_d.append(deg)
WS_bn = np.arange(16,25,1)
lWS_bn = len(WS_bn)
WS_bnR = WS_bn[:lWS_bn-1]
WS_hist = histogram(deg,bins=WS_bn)
WS_hist_keys.append(WS_hist[0])
# In[7]:
n=1000
density=0.8
k=20
alpha=[1,1.5,2]
d_hist_keys = list()
d_hist_values = list()
deg_list = list()
for a in alpha:
init(n,density)
network = SpSW(k,a)
deg = nx.degree(network).values()
deg_list.append(deg)
bn = np.arange(16,25,1)
lbn = len(bn)
bbn = bn[:lbn-1]
d_hist = histogram(deg,bins=bn)
d_hist_keys.append(d_hist[0])
# In[8]:
plot(WS_bnR,WS_hist_keys[0],'k-s',label='WS p=0.01')
plot(WS_bnR,WS_hist_keys[1],'k-o',label='WS p=0.05')
plot(WS_bnR,WS_hist_keys[2],'k-^',label='WS p=0.1')
plot(bbn,d_hist_keys[0],'r',label='SWS a=1')
plot(bbn,d_hist_keys[1],'b--',label='SWS a=1.5')
plot(bbn,d_hist_keys[2],'g-*',label='SWS a=2')
#yscale('log')
xlabel('k',fontsize=18)
ylabel('Frequency of k',fontsize=18)
legend(loc='upper right')
# In[33]:
k_list=[10, 11, 12, 15, 17, 20, 25, 30]
freq_WS1=list()
for k in k_list:
n = 0
for i in range(999):
if WS_d[0][i] >= k:
n += 1
freq_WS1.append(n / float(1000))
# In[34]:
k_list=[10, 11, 12, 15, 17, 20, 25, 30]
freq_WS2=list()
for k in k_list:
n = 0
for i in range(999):
if WS_d[1][i] >= k:
n += 1
freq_WS2.append(n / float(1000))
# In[35]:
k_list=[10, 11, 12, 15, 17, 20, 25, 30]
freq_WS3=list()
for k in k_list:
n = 0
for i in range(999):
if WS_d[2][i] >= k:
n += 1
freq_WS3.append(n / float(1000))
# In[36]:
k_list=[10, 11, 12, 15, 17, 20, 25, 30]
freq_SWS1=list()
for k in k_list:
n = 0
for i in range(len(deg_list[0])):
if deg_list[0][i] >= k:
n += 1
freq_SWS1.append(n / float(len(deg_list[0])))
# In[37]:
k_list=[10, 11, 12, 15, 17, 20, 25, 30]
freq_SWS2=list()
for k in k_list:
n = 0
for i in range(len(deg_list[1])):
if deg_list[1][i] >= k:
n += 1
freq_SWS2.append(n / float(len(deg_list[1])))
# In[38]:
k_list=[10, 11, 12, 15, 17, 20, 25, 30]
freq_SWS3=list()
for k in k_list:
n = 0
for i in range(len(deg_list[2])):
if deg_list[2][i] >= k:
n += 1
freq_SWS3.append(n / float(len(deg_list[2])))
# In[40]:
plt.plot(k_list,freq_WS1,'k',label='WS p=0.01')
plt.plot(k_list,freq_WS2,'k-o',label='WS p=0.02')
plt.plot(k_list,freq_WS3,'k-s',label='WS p=0.05')
plt.plot(k_list,freq_SWS1,'r-+',label='SWS a=1')
plt.plot(k_list,freq_SWS2,'b--',label='SWS a=1.5')
plt.plot(k_list,freq_SWS3,'g-^',label='SWS a=2')
plt.xlabel('k',fontsize=18)
plt.ylabel('Pr(X >= k)',fontsize=18)
legend(loc='upper right')
# In[ ]: