I'm writing a simple model to investigate the branching of rivers using random walks. The walkers move across a 2D lattice and if they come into contact with the cluster (the river channel) they stop walking. The code works and produces the expected results of a branching river, but I've only used it for a small lattice. I want to run the model for a much larger lattice and for more iterations but it just seems too slow to do at the moment.
I think it is the way that I check if a walker is in the cluster. If a walker is next to the cluster it has a certain probability of sticking and stop walking.
I'm wondering if there is a more efficient way of doing this to speed up the model.
This is the loop for the model:
while iteration_number < iteration_total:
Walker.step_all() # move all walkers
lattice = np.zeros((L, L), dtype='uint8')
#add the walkers and source to the lattice
lattice[tuple(np.array([walker.xy for walker in walkers]).T)] = 1
lattice[source] = 2
# cluster analysis, set those in cluster to stop walking
cluster, cluster_num = nd.label(lattice)
for walker in (walker for walker in walkers if walker.walking == True):
if (walker.xy == np.argwhere(np.isin(cluster, cluster[lattice == 2]))).all(axis=1).any():
#decision returns a true or false value based on the probability
walker.walking = decision(probability)
#Track the number of iterations
iteration_number += 1
print('Iteration:' + str(int(iteration_number)), end='
')
Any help would be appreciated.
question from:
https://stackoverflow.com/questions/66049217/random-walkers-model-running-too-slow 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…