Python蒙特卡罗模拟循环
我正在编写一个简单的蒙特卡罗模拟脚本,稍后我会将其扩展到一个更大的项目中。该脚本是一个基本的爬虫程序,试图在网格中从点a到点B。点A的坐标是(1,1)(这是左上角),点B的坐标是(n,n)(这是右下角,n是网格的大小) 一旦爬虫开始移动,有四个选项,它可以向左、向右、向上或向下移动(不允许对角移动)。如果这四个选项中的任何一个满足以下条件:Python蒙特卡罗模拟循环,python,loops,random,simulation,montecarlo,Python,Loops,Random,Simulation,Montecarlo,我正在编写一个简单的蒙特卡罗模拟脚本,稍后我会将其扩展到一个更大的项目中。该脚本是一个基本的爬虫程序,试图在网格中从点a到点B。点A的坐标是(1,1)(这是左上角),点B的坐标是(n,n)(这是右下角,n是网格的大小) 一旦爬虫开始移动,有四个选项,它可以向左、向右、向上或向下移动(不允许对角移动)。如果这四个选项中的任何一个满足以下条件: 新点仍应在nxn栅格的边界内 以前不应访问新点 新的点将在剩余的有效选项中随机选择(据我所知,Python使用Mersenne Twister算法拾取随机数
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
随机导入
i=1#初始坐标左上角
j=1#初始坐标左上角
k=0#模拟次数计数器
n=3#网格大小
foundRoute=0#达到终点的情况数计数器
GOTSTACK=0#计数器,用于未找到有效选项的情况数
coordList=[[i,j]]
当k<100时:
尽管如此:
有效性=[]
opt1=[i-1,j]
opt2=[i,j+1]
opt3=[i+1,j]
opt4=[i,j-1]
#检查4个可能的选项是否超出边界和重新访问坐标
#丢弃:
如果opt1[0]!=0和opt1[0]您的问题是,您从未清理您访问的位置。将打破内部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
k=0#模拟次数计数器
n=3#网格大小
foundRoute=0#达到终点的情况数计数器
GOTSTACK=0#计数器,用于未找到有效选项的情况数
当k<100时:
i、 j=(1,1)
coordList=[[i,j]]
尽管如此:
#其他一切
ahh。。。当我放置外部而循环最新时,我把它搞砸了。看来我需要更多的咖啡。。。非常感谢你!