如何尽可能有效地进行一维离散碰撞检测?

问题描述

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

但是,我想扩展此模型,使其具有不能互相穿过的固体颗粒。这在2种情况下显示。

  1. 一个粒子位于位置i,第二个粒子位于位置i+1。第一个粒子向右移动,第二个粒子向左移动。
  2. 一个粒子位于位置i,第二个粒子位于位置i+2。第一个粒子向右移动,第二个粒子向左移动。

如果我独立执行所有步骤,则可以手动检查每个步骤以查看是否合法。但是,这会降低O(M^2N)的性能。有没有更有效的方法来检测哪个矩阵元素对X[i,j],X[k,j]导致两个粒子相互穿过,最好以矢量方式穿过?这样,我可以使仿真跳过这些步骤。

解决方法

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

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,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)

Random Walk GIF

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...