2

我有以下情况。在离散域 0、1、...、L 上有 M 个独立的随机游走者。我们对 N 个相同的域执行此操作。这会产生一个矩阵X,其中X[i, j]是 walkeri在 domain 上的位置j。为了进行随机步骤,我在 matrix 中添加了一个形状相同且具有随机 +1 和 -1 的矩阵X。然后我处理边缘。这很好用。

但是,我想将此模型扩展为具有不能相互穿过的固体粒子。这在 2 个案例中显示。

  1. 一个粒子在 position i,第二个在 position i+1。第一个粒子向右移动,而第二个粒子向左移动。
  2. 一个粒子在 position i,第二个在 position i+2。第一个粒子向右移动,而第二个粒子向左移动。

如果我独立完成所有步骤,我可以手动检查每个步骤,看看它是否合法。然而,这是一个糟糕的O(M^2N)表现。是否有更有效的方法来检测哪些矩阵元素对X[i,j], X[k, j]导致两个粒子相互穿过,最好以矢量化方式?这样,我可以让模拟跳过这些步骤。

4

1 回答 1

0

我想你必须使用某种形式的循环来实现这一点,但也许这会有所帮助:

import numpy as np
import matplotlib.pyplot as plt

L = 50
N = 30
W = 20
n_steps = 1000

# Initialize all walkers on the left side
wn0 = np.arange(W)[:, np.newaxis].repeat(N, axis=1)

# Set up the plot
fig, ax = plt.subplots()
worlds = np.zeros((N, L))
worlds[np.arange(N)[np.newaxis, :], wn0] = np.arange(W)[:, np.newaxis]
h = ax.imshow(worlds, cmap='gray_r')  # cmap='tab20')
ax.set_xlabel('Distance in 1D World')
ax.set_ylabel('Ensemble of Worlds')

for _ in range(n_steps):

    r = np.where(np.random.random(wn0.shape) < 0.5, 1, -1)

    wn1 = wn0 + r
    wn1 = np.clip(wn1, 0, L-1)

    # Case 1
    rest_mat = np.zeros_like(wn0, dtype=bool)
    for i in range(W):
        for j in range(i+1, W):
            rest_mat[[[i], [j]], np.logical_and(wn0[i] == wn1[j], wn1[i] == wn0[j])] = True
    wn1[rest_mat] = wn0[rest_mat]

    # Case 2, go from 0->W and than from W->0 to make sure all duplicates are gone
    for i in np.hstack((range(W), range(W)[-2::-1])):
        temp = (wn1[i] == wn1).sum(axis=0) > 1
        wn1[i, temp] = wn0[i, temp]

    # for wn1_j in wn1.T:  Check if there are collisions
    #     assert len(np.unique(wn1_j)) == W

    wn0 = wn1

    worlds = np.zeros((N, L))
    worlds[np.arange(N)[np.newaxis, :], wn1] = np.arange(W)[:, np.newaxis]
    h.set_data(worlds)
    plt.pause(0.1)

随机游走 GIF

于 2020-09-25T21:57:39.227 回答