境界条件を含んだ3次元ランダムウォークを書いたのですが、少し動きがおかしい。
python
1stage=[[[1. 1. 1.] 2 [1. 0. 1.] 3 [1. 1. 1.]] 4 5 [[1. 0. 1.] 6 [0. 0. 0.] 7 [1. 0. 1.]] 8 9 [[1. 1. 1.] 10 [1. 0. 1.] 11 [1. 1. 1.]]]
このstage上をランダムウォークさせるときに、0のところは通れないとします。
python
1import numpy as np 2import random 3import itertools 4n = 3 5itr = 5 6step = [i for i in range(0,itr)] 7r = [i for i in range(0,n)] 8walk = np.zeros([n,n,n],dtype="float") 9walk[0,0,0] = 1 10for t in step: 11 if t == 0: 12 pass 13 else: 14 number = random.randint(1,6) 15 next_walk = np.zeros([n,n,n],dtype="float") 16 for i in itertools.product(r,r,r): 17 x = i[0] 18 y = i[1] 19 z = i[2] 20 #周期境界条件 21 x1 = (x-1 + n) % n 22 x2 = (x+1) % n 23 y1 = (y-1 + n) % n 24 y2 = (y+1) % n 25 z1 = (z-1 + n) % n 26 z2 = (z+1) % n 27 if stage[i]==0: 28 continue 29 else: 30 if number == 1: #x軸正に移動 31 if stage[x1,y,z]==1: 32 next_walk[x,y,z]=np.copy(walk[x1,y,z]) 33 elif stage[x1,y,z]==0:#通れない場合(以下省略) 34 continue 35 36 elif number == 2: #y軸正に移動 37 if stage[x,y1,z]==1: 38 next_walk[x,y,z]=np.copy(walk[x,y1,z]) 39 elif stage[x,y1,z]==0: 40 continue 41 42 elif number == 3: #z軸正に移動 43 if stage[x,y,z1]==1: 44 next_walk[x,y,z]=np.copy(walk[x,y,z1]) 45 elif stage[x,y,z1]==0: 46 continue 47 48 elif number == 4: #x軸負に移動 49 if stage[x2,y,z]==1: 50 next_walk[x,y,z]=np.copy(walk[x2,y,z]) 51 elif stage[x2,y,z]==0: 52 continue 53 54 elif number == 5: #y軸負に移動 55 if stage[x,y2,z]==1: 56 next_walk[x,y,z]=np.copy(walk[x,y2,z]) 57 elif stage[x,y2,z]==0: 58 continue 59 60 elif number == 6: #z軸負に移動 61 if stage[x,y,z2]==1: 62 next_walk[x,y,z]=np.copy(walk[x,y,z2]) 63 elif stage[x,y,z2]==0: 64 continue 65 walk = np.copy(next_walk) 66 print(t,walk)
問題が出る場合の結果は以下になります。粒子がいる場所を1にした場合、途中で消える現象がおきてしまいます。
んーなぜなのでしょう、通れない場合は、t+1秒後の粒子の位置はt秒後の位置からうごかないとしています。
間違いやおかしい点に気づいた方、ご指摘のほどお願い致します。
(追記)
どうやら、たとえばnumber=1で、x+1に移動するときに0であった場合、動かないという指示を加えているのにも関わらず、x+1の0を採用してしまっているみたいです。
(追記2)
ちょっと書き直してみましたが、まだ少しおかしいです。
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:#stage[x,y,z]=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]) elif number==1 and stage[x1,y,z]==0 and stage[x,y,z]==1: next_walk[i]= walk[i] 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]) elif number==2 and stage[x,y1,z]==0 and stage[x,y,z]==1: next_walk[i]=walk[i] 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]) elif number==3 and stage[x,y,z1]==0 and stage[x,y,z]==1: next_walk[i]=walk[i] 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]) elif number == 4 and stage[x2,y,z]==0 and stage[x,y,z]==1: next_walk[i]=walk[i] 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]) elif number==5 and stage[x,y2,z]==0 and stage[x,y,z]==1: next_walk[i]=walk[i] 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]) elif number ==6 and stage[x,y,z2]==0 and stage[x,y,z]==1: next_walk[i]=walk[i] else: 上記の条件以外は処理しない continue walk = np.copy(next_walk) print(t,walk)
結果
0 [[[1 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]]] 1 6 1 [[[0 0 1] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]]] 2 5 2 [[[0 0 0] [0 0 0] [0 0 1]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]]] 3 1 3 [[[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 1]] [[0 0 0] [0 0 0] [0 0 0]]] 4 2 4 [[[0 0 0] [0 0 0] [0 0 0]] [[0 0 1] [0 0 0] [0 0 1]] [[0 0 0] [0 0 0] [0 0 0]]]
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/02 16:29
2018/11/02 16:52 編集
2018/11/02 16:57 編集
2018/11/02 17:01 編集
2018/11/02 17:00
2018/11/02 17:14
2018/11/03 03:06
2018/11/03 10:07
2018/11/03 10:16
2018/11/03 12:26
2018/11/03 12:40
2018/11/03 13:13