While generating random numbers on the stage below (3D), when random walking, I wrote the following code using stage as a conditional statement, but sometimes it works well and sometimes it doesn't.I thought it was perfect for me, but I'm struggling to figure out why it's not calculated.
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.]]]
Main Calculation
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
# forkin range(0,1):
Fort 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 intertools.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)
Thank you for your advice and questions.
python python3 numpy random
Here's how to solve yourself (if you're wrong, I'd appreciate it if you could let me know):
import random
import numpy as np
n = 27
itr = 10
step = [i for i in range(0,itr+1)]
r_list = [i for i in range (0, n) ]
walk = np.zeros ([n, n, n], dtype = np.uint8)
walk [0,0,0] = 1
prob = [ ]
time = [ ]
Fort in step:
if t == 0:
pass
else:
number = random.randint(1,6)
next_walk=np.zeros ([n,n,n], dtype=np.uint8)
for i intertools.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
else:
if walk[i] == 1:
if number == 1:
if stage [x2,y,z] == 1:
next_walk[x2,y,z] = walk[i]
else:
next_walk[x,y,z] = walk[i]
elif number == 2:
if stage [x,y2,z] == 1:
next_walk[x,y2,z] = walk[i]
else:
next_walk[x,y,z] = walk[i]
elif number==3:
if stage [x,y,z2] == 1:
next_walk[x,y,z2] = walk[i]
else:
next_walk[x,y,z] = walk[i]
elif number == 4:
if stage [x1,y,z] == 1:
next_walk[x1,y,z] = walk[i]
else:
next_walk[x,y,z] = walk[i]
elif number == 5:
if stage [x,y1,z] == 1:
next_walk[x,y1,z] = walk[i]
else:
next_walk[x,y,z] = walk[i]
elif number == 6:
if stage [x,y,z1] == 1:
next_walk[x,y,z1] = walk[i]
else:
next_walk[x,y,z] = walk[i]
else:
continue
© 2024 OneMinuteCode. All rights reserved.