境界条件を含めたランダムウォーク
以下のステージ上(3次元)で乱数を生成させながら、ランダムウォークさせるときに、stageを条件文として以下のようにコードを書きましたが、うまく動くときと動かない時があります。自分としては完璧だと思ったのですが、なぜ計算されないのか頭を悩ませています。
stage=[[[1. 1. 1.]
[1. 0. 1.]
[1. 1. 1.]]
[[1. 0. 1.]
[0. 0. 0.]
[1. 0. 1.]]
[[1. 1. 1.]
[1. 0. 1.]
[1. 1. 1.]]]
メイン計算
import numpy as np
import random
import itertools
stage = np.array([[[1,1,1],
[1,0,1],
[1,1,1]],
[[1,0,1],
[0,0,0],
[1,0,1]],
[[1,1,1],
[1,0,1],
[1,1,1]]],dtype=np.uint8)
n = 3
itr = 5
step = [i for i in range(0,itr)]
r_list = [i for i in range(0,n)]
walk = np.zeros([n,n,n],dtype=np.uint8)
walk[0,0,0] = 1
#for k in range(0,1):
for t in step:
if t == 0:
pass
else:
number = random.randint(1,6)
next_walk = np.zeros([n,n,n],dtype=np.uint8)
print(t,number)
for i in itertools.product(r_list,r_list,r_list):
x = i[0]
y = i[1]
z = i[2]
#Boundary condition
x1 = (x-1 + n) % n
x2 = (x+1) % n
y1 = (y-1 + n) % n
y2 = (y+1) % n
z1 = (z-1 + n) % n
z2 = (z+1) % n
if stage[i]== 0:
continue
elif number == 1 and stage[x1,y,z]==1 and stage[x,y,z]==1:
next_walk[x,y,z]= np.copy(walk[x1,y,z])#*(1/6))
elif number==1 and stage[x1,y,z]==0 and stage[x,y,z]==1:
next_walk[i]= walk[i]#(1/6)**t
elif number == 2 and stage[x,y1,z]==1 and stage[x,y,z]==1:
next_walk[x,y,z]= np.copy(walk[x,y1,z])#*(1/6))
elif number==2 and stage[x,y1,z]==0 and stage[x,y,z]==1:
next_walk[i]=walk[i]#(1/6)**t
elif number == 3 and stage[x,y,z1]==1 and stage[x,y,z]==1:
next_walk[x,y,z]= np.copy(walk[x,y,z1])#*(1/6))
elif number==3 and stage[x,y,z1]==0 and stage[x,y,z]==1:
next_walk[i]=walk[i]#(1/6)**t
elif number == 4 and stage[x2,y,z]==1 and stage[x,y,z]==1:
next_walk[x,y,z]= np.copy(walk[x2,y,z])#*(1/6))
elif number == 4 and stage[x2,y,z]==0 and stage[x,y,z]==1:
next_walk[i]=walk[i]#(1/6)**t
elif number == 5 and stage[x,y2,z]==1 and stage[x,y,z]==1:
next_walk[x,y,z]= np.copy(walk[x,y2,z])#*(1/6))
elif number==5 and stage[x,y2,z]==0 and stage[x,y,z]==1:
next_walk[i]=walk[i]#(1/6)**t
elif number == 6 and stage[x,y,z2]==1 and stage[x,y,z]==1:
next_walk[x,y,z]=np.copy(walk[x,y,z2])#*(1/6))
elif number ==6 and stage[x,y,z2]==0 and stage[x,y,z]==1:
next_walk[i]=walk[i]#(1/6)**t
else: #stage[i]==0:
continue
#next_walk[x,y,z]=(1/6)**t
walk = np.copy(next_walk)
print(t,walk)
怪しい点、アドバイス等宜しくお願い致します。