2

これは、numpy配列のみを使用したランダムウォーク問題のバージョンです。位置が500ステップにわたって再訪される時間を見つけるには、位置のソートされた配列をそのオフセットと比較し、0に近い時間を記録してから、オフセットを増やす必要があります。

これまでの私のコードは次のとおりです。問題は「while」ループにあり、位置が「zeroArray」の要素として再訪される最終的な回数を保存しようとしています。

実行すると、インデックスエラーが発生し、結果が記録されず、ループを停止するブール式が変更されたにもかかわらず、何度も繰り返されたカウンターが発生します。

編集:numpy配列で繰り返し位置を見つける方法:1)最終的な位置配列を昇順で並べ替えます。2)オフセットを増やしながらスライスを比較します。ただし、そのオフセットで0.001m以内の位置を見つけます。つまり、位置を隣接する位置と比較します(オフセット1)。あなたは隣人が2つのスペースであなたがたった2つのケースを見つけるかもしれないと計算する18のケースを見つけるかもしれません。そして、3つのスペースで0が見つかり、その時点で停止します。

import numpy as np
import random

MAX_STEP_SIZE = 0.90    # maximum size of a single step [m]
NUM_STEPS = 500         # number of steps in a random walk []
NUM_WALKS = 10        # number of random walks in a run []
TOLERANCE = 0.001    # separation of points considered the same [m]
STEP_TO_RECORD_1 = 100 # first step to record and analyze []
STEP_TO_RECORD_2 = 500 # 2nd step to record and analyze []
random.seed(12345)

#......................................................................distance
def distance(posA, posB) :
    """Distance between two positions"""
    return np.abs(posA - posB)


#...............................................................initialPosition
def initialPosition() :
    """Initial position of walker at the start of a random walk"""
    return 0.0

def genPositions(nSteps, maxStep) :
    """Return the new position after a random step between -maxStep and
    maxStep, given the previous position"""
    genArray1 = (maxStep - (-maxStep))*(np.random.random(nSteps+1)) + (-maxStep)
    genArray1[0]=initialPosition()    
    return np.cumsum(genArray1)

oneStep = np.zeros(NUM_WALKS)
fiveStep = np.zeros(NUM_WALKS)
zeroStep = np.zeros(NUM_WALKS)
walkArray = np.zeros(NUM_WALKS)
counter = 1
hitcounter = 0
zerocounter = 0
keepchecking = bool(1)

for ii in range(NUM_WALKS):
    position = (genPositions(NUM_STEPS, MAX_STEP_SIZE))   
    oneStep[ii] = position[100]
    fiveStep[ii] = position[-1]
    zeroArray = np.sort(position)
    while keepchecking == bool(1):
        zerocounter = 0
        for jj in range(len(zeroArray)):
            hitcounter = 0
            if distance(zeroArray[jj+counter], zeroArray[jj]) <= TOLERANCE:
               hitcounter +=1
            zerocounter += hitcounter
            counter +=1
            if hitcounter == 0:
                keepchecking = bool(0)
    zeroStep[ii] = zerocounter

助けてくれてありがとう、

4

1 に答える 1

2

次のように、位置ベクトルをそれ自体と直接比較できます。

deltas = np.abs(position[None, :] - position[:, None])

ここで、は stepと stepdeltas[i, j]の位置間の距離です。次のようにヒットを取得できます。ij

hits = deltas <= TOLERANCE

クローズ ポジションのペアを取得するには、次の手順を実行できます。

row, col = np.nonzero(hits)
idx = row < col # get rid of the diagonal and the upper triangular part
row = row[idx]
col = col[idx]

例として:

>>> position = genPositions(NUM_STEPS, MAX_STEP_SIZE)
>>> row, col = np.nonzero(np.abs(position[None, :] -
...                              position[:, None]) < TOLERANCE)
>>> idx = row < col
>>> row= row[idx]
>>> col = col[idx]
>>> row
array([ 35,  40, 112, 162, 165, 166, 180, 182, 200, 233, 234, 252, 253,
       320, 323, 325, 354, 355, 385, 432, 443, 451], dtype=int64)
>>> col
array([ 64,  78, 115, 240, 392, 246, 334, 430, 463, 366, 413, 401, 315,
       380, 365, 348, 438, 435, 401, 483, 473, 492], dtype=int64)

>>> for j in xrange(len(row)) :
...     print '{0}, {1} --> {2}, {3}'.format(row[j], col[j], position[row[j]],
...                                          position[col[j]]) 
... 
35, 64 --> 2.56179226445, 2.56275159205
40, 78 --> 2.97310455111, 2.97247314695
112, 115 --> 3.40413767436, 3.40420856824
...
432, 483 --> 10.2560778101, 10.2556475598
443, 473 --> 10.7463713139, 10.7460626764
451, 492 --> 12.3804383241, 12.3805940238
于 2013-03-21T00:27:36.487 に答える