3

私は単純なモンテカルロシミュレーションスクリプトに取り組んでいます。これは後でより大きなプロジェクトに拡張します。スクリプトは、グリッド内のポイントAからポイントBに移動しようとする基本的なクローラーです。点Aの座標は(1,1)(これは左上隅)であり、点Bの座標は(n、n)(これは右下隅、nはグリッドのサイズです)です。

クローラーが動き始めると、4つのオプションがあり、左、右、上、または下に移動できます(斜めの動きは許可されません)。これらの4つのオプションのいずれかが以下を満たしている場合:

  1. 新しいポイントは、まだnxnグリッドの境界内にある必要があります
  2. 新しいポイントは以前に訪問するべきではありません

新しいポイントは、残りの有効なオプションの中からランダムに選択されます(Pythonが乱数を選択するためにメルセンヌツイスターアルゴリズムを使用していることを私が知っている限り)。

シミュレーションを1,000,000回実行したいのですが(以下のコードは100回のみ実行されます)、各反復は次のいずれかで終了する必要があります。

  1. クローラーが動かなくなった(移動の有効なオプションがない)
  2. クローラーはグリッド上の最終目的地(n、n)に到達します。

アルゴリズムを正しく実装したと思いましたが、明らかに何かがおかしいです。シミュレーションを何度実行しても(100または1,000,000)、クローラーが最後まで到達したイベントは1つしか成功せず、残りの試行(99または999,999)は失敗します。

私が見逃している単純なものがあるに違いないが、何らかの理由でそれを見ることができない。何か案は?

本当にありがとう!

編集:テキストのいくつかのタイプミスが修正されました。

import random

i = 1 # initial coordinate top left corner
j = 1 # initial coordinate top left corner
k = 0 # counter for number of simulations
n = 3 # Grid size

foundRoute = 0 # counter for number of cases where the final point is reached
gotStuck = 0 # counter for number of cases where no valid options found
coordList = [[i, j]]

while k < 100:
    while True:
        validOptions = []

        opt1 = [i - 1, j]
        opt2 = [i, j + 1]
        opt3 = [i + 1, j]
        opt4 = [i, j - 1]

        # Check 4 possible options out of bound and re-visited coordinates are
        # discarded:

        if opt1[0] != 0 and opt1[0] <= n and opt1[1] != 0 and opt1[1] <= n:
            if not opt1 in coordList:
                validOptions.append(opt1)

        if opt2[0] != 0 and opt2[0] <= n and opt2[1] != 0 and opt2[1] <= n:
            if not opt2 in coordList:
                validOptions.append(opt2)

        if opt3[0] != 0 and opt3[0] <= n and opt3[1] != 0 and opt3[1] <= n:
            if not opt3 in coordList:
                validOptions.append(opt3)

        if opt4[0] != 0 and opt4[0] <= n and opt4[1] != 0 and opt4[1] <= n:
            if not opt4 in coordList:
                validOptions.append(opt4)

        # Break loop if there are no valid options
        if len(validOptions) == 0:
            gotStuck = gotStuck + 1
            break

        # Get random coordinate among current valid options
        newCoord = random.choice(validOptions)

        # Append new coordinate to the list of grid points visited (to be used
        # for checks)
        coordList.append(newCoord)

        # Break loop if lower right corner of the grid is reached
        if newCoord == [n, n]:
            foundRoute = foundRoute + 1
            break

        # If the loop is not broken, assign new coordinates
        i = newCoord[0]
        j = newCoord[1]
    k = k + 1

print 'Route found %i times' % foundRoute
print 'Route not found %i times' % gotStuck
4

1 に答える 1

3

あなたの問題はあなたがあなたの訪問した場所を決して片付けていないということです。while内側のループから抜け出すブロックを次のように変更します。

 if len(validOptions) == 0:
     gotStuck = gotStuck + 1
     coordList = [[1,1]]
     i,j = (1,1)
     break

また、成功したブロックを変更する必要があります。

if newCoord == [n, n]:
    foundRoute = foundRoute + 1
    coordList = [[1,1]]
    i,j = (1,1)
    break

whileまたは、このコードを内部ループの直前に配置することもできます。コードの先頭は次のようになります。

k = 0 # counter for number of simulations
n = 3 # Grid size  
foundRoute = 0 # counter for number of cases where the final point is reached
gotStuck = 0 # counter for number of cases where no valid options found
while k < 100:
    i,j = (1,1) 
    coordList = [[i,j]]
    while True:
        #Everything else
于 2013-01-18T19:29:37.940 に答える