Python 蒙特卡罗模拟循环

3
我正在编写一个简单的蒙特卡罗模拟脚本,稍后将对其进行扩展以用于一个更大的项目。该脚本是一个基本的爬虫程序,试图在一个网格中从点A到点B移动。点A的坐标为(1,1)(即左上角),点B的坐标为(n,n)(即右下角,其中n是网格的大小)。
一旦爬行者开始移动,就有四个选项,可以向左、向右、向上或向下移动(不允许对角线运动)。如果这四个选项中的任何一个满足以下条件:
  1. 新点仍应位于n x n网格的边界内
  2. 新点不应被先前访问过
则从其余有效选项中随机选择新点(据我所知,Python使用梅森旋转算法来选择随机数字)。
我想运行100万次模拟(下面的代码仅运行100次),每次迭代应终止:
  1. 爬行者被困住了(没有有效的移动选项)
  2. 爬行者到达网格的最终目的地(n,n)。
我认为我正确地实现了算法,但显然出了些问题。无论我运行多少次模拟(100次或100万次),我只获得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
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

啊...我把最外层的while循环放在了后面,搞砸了。看来我需要再喝杯咖啡......非常感谢! - marillion

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接