Python 蒙特卡洛模拟循环

标签 python loops random simulation montecarlo

我正在研究一个简单的蒙特卡洛模拟脚本,稍后我将扩展它以用于更大的项目。该脚本是一个基本的爬虫程序,试图从网格中的 A 点到达 B 点。 A点的坐标是(1,1)(这是左上角),B点的坐标是(n,n)(这是右下角,n是网格的大小)。

爬虫开始移动后,有四个选项,它可以向左、向右、向上或向下移动(不允许对角线移动)。如果这四个选项中的任何一个满足以下条件:

  1. 新点应该仍然在 n x n 网格的边界内
  2. 之前不应该访问新点

新点将在剩余的有效选项中随机选择(据我所知,Python 使用 Mersenne Twister 算法来选择随机数)。

我想运行模拟 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

最佳答案

您的问题是您永远不会清除您访问过的位置。将跳出内部 while 循环的 block 更改为如下所示:

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

您还需要在成功的地方更改您的区 block :

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

关于Python 蒙特卡洛模拟循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14405997/

相关文章:

Python IRC Bot 在某些服务器上不会加入 channel ,我不知道如何让它响应用户输入

python - 在纸牌游戏中拆分和附加列表

c - 在运行一些计算时写入 txt 文件

php - php中的foreach循环问题

r - 如何在 R 中编写分段函数进行一些模拟并将值存储在数据框中

tsql - 将 1 到 180 分钟随机添加到 SQL Server 中的现有日期时间值

python - Python 中的 SQL 查询,其中值可能为 None/Null

python - 匹配以逗号分隔的 2 位数字(不包括 float 字)

javascript - 如何在按下按键时暂停/播放 Javascript 循环?

linux - 作为非特权进程添加到 Linux 中的熵估计