Watts-Strogatsモデルは結局以下の形で幕を下ろします。
---------------------------------------------------------
import numpy as np
def mk_WSmodel(N, k_half, p, matrix):
list=reorder(mk_extended_cycle(N, k_half, matrix=False))
change_index=np.random.choice(list.shape[0], np.int(list.shape[0]*p), replace=False)
for index in change_index:
match=match_list(list, index, N)
list[index, 1]=match[np.random.choice(match.shape[0], 1)]
if matrix:
list=list2matrix(list)
return list
def mk_extended_cycle(N, k_half, matrix):
list=np.zeros(2).reshape(1, 2)
for i in range(N):
j=1
while j<=k_half:
list=np.r_[list, np.array([i, i+j]).reshape(1, 2)]
j=j+1
list=list[1:, :]%N
if matrix:
list=list2matrix(list)
return list
def reorder(list):
for i in range(list.shape[0]):
if zero_one()==1:
temp=list[i, 0]
list[i, 0]=list[i, 1]
list[i, 1]=temp
return list
def match_list(list, index, N):
match=np.ones(N)
match[np.int(list[index, 0])]=0
for i in range(list.shape[0]):
if list[i, 0]==list[index, 0]:
match[np.int(list[i, 1])]=0
elif list[i, 1]==list[index, 0]:
match[np.int(list[i, 0])]=0
return np.arange(0, N, 1)[match==1]
def zero_one():
return np.random.choice(2, 1)
def list2matrix(list, N):
link=np.zeros(N*N).reshape(N, N)
for i in range(list.shape[0]):
link[np.int(list[i, 0]), np.int(list[i, 1])]=1
link[np.int(list[i, 1]), np.int(list[i, 0])]=1
return link
def cal_L(list, N):
mean_length=np.zeros(N)
for i in range(N):
mean_length[i]=np.sum(dykstra_neighborlist(list, N, i))/(N-1)
L=np.sum(mean_length)/N
return L
def cal_C(list, N):
triangle=np.zeros(N)
link=list2matrix(list, N)
for i in range(N):
neighbor_list=extract_neighborlist(list, i)
k=neighbor_list.shape[0]
if k>1:
for x in range(k):
y=x+1
while y<k:
if link[np.int(neighbor_list[np.int(x), 1]), np.int(neighbor_list[np.int(y), 1])]==1:
triangle[i]=triangle[i]+2.0
y=y+1
triangle[i]=triangle[i]/(k*(k-1))
C=np.float(np.sum(triangle))/N
return C
def dykstra_neighborlist(list, N, start):
end=np.zeros(N)+inf
end[start]=0
while np.sum(end)==inf:
candidate=np.zeros(N)+inf
for i in range(N):
if end[i]!=inf:
neighbor_list=extract_neighborlist(list, i)
for j in range(neighbor_list.shape[0]):
if end[np.int(neighbor_list[j, 1])]==inf:
length=end[i]+1
if candidate[np.int(neighbor_list[j, 1])]>length:
candidate[np.int(neighbor_list[j,1])]=length
candidate[end!=inf]=inf
end[np.argmin(candidate)]=np.min(candidate)
return end
def extract_neighborlist(list, i):
neighbor1=list[list[:, 0]==i]
neighbor2=list[list[:, 1]==i]
for j in range(neighbor2.shape[0]):
temp=neighbor2[j, 0]
neighbor2[j, 0]=neighbor2[j, 1]
neighbor2[j, 1]=temp
neighbor_list=np.r_[neighbor1, neighbor2]
return neighbor_list
0 件のコメント:
コメントを投稿