2012-02-23 277 views
6

我试图解决从书中探索python的二维随机游走问题。 但是,我无法弄清楚我该如何解决这个问题。我做了一些研究,但是这些太复杂了,无法理解它是什么。我是初学者。所以,我无法通过查看来理解代码。请详细解释我这个问题。另一个简单的随机游走模拟使用Python(二维)

无论如何,问题是:

上随机游走二维变化在网格的中间 开始,例如11 11阵列。在每一步醉酒都有四个选择:上,下,左或右。在本章前面我们描述了 如何创建一个二维数组数组。使用这个数据 类型,写一个二维随机游走的模拟。

好的,我所知道的; 我知道如何在Python创建二维数组:

times = [0] * 11 
for i in range(0,11): 
    times[i] = [0] * 11 

而且我得到了“randint”功能的想法:

而且也是我最近写了这个问题的一个尺寸变化。但它是一个意大利面代码,而且它非常脏,我也不确定它是否正确。

我的代码是在这里:

''' 
Created on Feb 11, 2012 

@author: msarialp 
''' 
from random import randint 

def drunken_man(): 
    steps = 0 
    times = [0] * 11 
    left_move = 0 
    right_move = 0 
    i = 0 
    while left_move < 5 or right_move < 5: 
     value = randint(0,1) 
     times[5] = 1 
     if value == 1: 
      steps += 1 
      print("He moved left") 
      left_move += 1 
      if right_move > 0: 
       right_move -= 1 
      if left_move == 1: 
       times[4] += 1 
      elif left_move == 2: 
       times[3] += 1 
      elif left_move == 3: 
       times[2] += 1 
      elif left_move == 4: 
       times[1] += 1 
      #elif left_move == 5: 
       #times[0] += 1 
     elif value == 0: 
      steps += 1 
      print("He moved right") 
      right_move += 1 
      if left_move > 0: 
       left_move -= 1 
      if right_move == 1: 
       times[6] += 1 
      elif right_move == 2: 
       times[7] += 1 
      elif right_move == 3: 
       times[8] += 1 
      elif right_move == 4: 
       times[9] += 1 
      #elif right_move == 5: 
       #times[10] += 1 
     times[i] += 1     
    for i in range(1,10): 
     print("He took {steps} steps until he reaches end of the sidewalk.".format(steps = steps), "He stood on {1} square at {0} times".format(times[i], i)) 

def main(): 
    drunken_man() 

    return 0 
if __name__ == '__main__': 
    main() 

编辑一个

从丹Gerhardsson采取了一些很好的建议后。 我决定编辑我的问题。 所以,我在这个问题上: 我明白我该如何遵循和检查我的醉酒男子在两个步骤的步骤。

使用元组来解决这个练习是非常容易理解和清楚的。

因此,毕竟我的代码段在这里,请检查并给我任何反馈。

def two_dimensional_random_walk(): 
    steps = 0 
    times = [0] * 11 
    for i in range(0,11): 
     times[i] = [0] * 11 
    x = 5 
    y = 5 
    moves = [(1,0), (0,1), (-1,0), (0,-1)] 
    while x<11 and x >= 0 or y < 11 and y >= 0: 
     dx, dy = moves[randint(0,3)] 
     x += dx 
     y += dy 
     if dx == 1 and dy == 0: 
      print("He moved right") 
     elif dx == 0 and dy == 1: 
      print("He moved up") 
     elif dx == -1 and dy == 0: 
      print("He moved left") 
     elif dx == 0 and dy == -1: 
      print("He moved down") 
     try: 
      times[x][y] += 1 
      steps += 1 
     except IndexError: 
      break 

我的打印功能:

for i in range(0,11): 
    for j in range(0,11): 
     print("He took {steps} steps until he reaches end of the sidewalk.".format(steps = steps), "He stood on {1}x{2} square at {0} times".format(times[i][j], i+1,j+1)) 

因此,所有的一切我想与帮助丹Gerhardsson,我解决了这个运动。

但是,为什么我不能用这些提示来改变我的一维解决方案。

def drunken_man(): 
steps = 0 
x = 6 
times = [0] * 11 
moves = [(1), (-1)] 

while x < 11 and x >= 0: 
    dx = moves[randint(0,1)] 
    print(dx, x) 
    x += dx 
    try: 
     times[x] += 1 
     steps += 1 
    except IndexError: 
     break   
for i in range(1,11): 
    print("He took {0} steps until he reaches end of the sidewalk.".format(steps), "He stood on {1} square at {0} times".format(times[i], i)) 

编辑两个(最后的润色)

我不知道是否有必要编辑自己的帖子由丹Gerhardsson应用提示。为了帮助那些错过了像我这样的观点的人,我决定把所有东西结合在一起。

因此,这里是我的功能与丹Gerhardsson提示合并:

def two_dimensional_random_walk(): 
steps = 0 # Steps counter for understand how many steps that our drunken man take 
grid_size = 11 # Grid size variable, 
# Creating Two dimensional array by using lists 
times = [0] * grid_size 
for i in range(0,grid_size): 
    times[i] = [0] * grid_size 
# Initial variables to start in the middle of grid 
x = 5 
y = 5 
# Tuples to get directions and decide where to go 
moves = [(1,0, "right"), (0,1, "up"), (-1,0, "left"), (0,-1, "down")] 
# My loop for evaluate the steps 
while True: 
    dx, dy, position = moves[randint(0,3)] # By using randint I could make decision randomly 
    x += dx 
    y += dy 
    print("He moved", position) 
    try: 
     times[x][y] += 1 # And here is, how many times have he stood on each square 
     steps += 1 
    except IndexError: # The exit of loop 
     break 
# My print function which answers these questions (How long will it be until he reaeches the end of the sidewalk, and how many times will he have stood on each square) 
for i in range(0,11): 
    for j in range(0,11): 
     print("He took {steps} steps until he reaches end of the sidewalk.".format(steps = steps), "He stood on {1}x{2} square at {0} times".format(times[i][j], i+1,j+1)) 

感谢您的帮助很大丹Gerhardsson。 我想我终于得到了解决方案。

+0

这功课吗? – amindfv 2012-02-23 21:59:40

+2

不,我只是在学习Python这本书,http://books.google.com.tr/books/about/Exploring_Python.html?id=VnAsHwAACAAJ&redir_esc=y – mustafaSarialp 2012-02-23 22:03:52

回答

8

我至少可以给你一些提示。所以你有四种可能的举措。

moves = [(0, 1), (1, 0), (0, -1), (-1, 0)] 

要设置在中心的开始位置:

grid_size = 11 
x = grid_size // 2 
y = grid_size // 2 

商店醉酒人的位置与各移动可以通过一个元组,其是在x和y方向上的位移被表示在模拟的每一步中更新它。事情是这样的:

# Displacement: 
dx, dy = random.choice(moves) 

# Update position: 
x += dx 
y += dy 

这可能不是新手级别的代码,但不是用if语句检查的界限,你可以尝试更新的次数,处理异常,这是如果位置升起在网格之外:

try: 
    # Update counter. 
    times[x][y] += 1 
except IndexError: 
    # Exit the simulation loop. 
    break 

希望这会有所帮助。

既然你想在每一步打印的方向,你可以添加到元组:

moves = [(0, 1, 'up'), (1, 0, 'right'), (0, -1, 'down'), (-1, 0, 'left')] 

然后,你可以,如果更换 -

编辑在第二版的评论 - 您打印方向的地方:

dx, dy, direction = random.choice(moves) 
print('He moved', direction) 

当您在当前解决方案中使用try-except时,不需要检查边界在声明中的白羊座。你可以这样做:

while True: 
    ... 

因为异常处理程序中的中断将退出循环。

我的最后一条建议是用变量替换一些数字文字。网格大小例如出现在多个地方。您应该创建一个变量,是指它在代码的其余部分:

grid_size = 11 
times = [0] * grid_size 
    for i in range(grid_size): 
     times[i] = [0] * grid_size 

使用变量而不是数量字面意思是,你只需要在一个地方做出改变,如果你想运行的代码不同的网格大小。

+0

很好的解释。 – amindfv 2012-02-23 22:22:59

+0

谢谢。但是,我错过了一些观点。其中之一是,根据醉酒男子应该从网格中间开始的问题。我的意思是5x5。我们如何评估这一点。而且我还需要一个打印功能来提示这些问题的答案(他会在人行道的尽头重复多少时间,并且他将在每个方块上站立多少次) 所以我写了一个打印功能就是这样? “他在{0}次站在{1} x {2}方块上”。格式(次数) [i] [j],i + 1,j + 1))' 我也有两个for循环。 – mustafaSarialp 2012-02-24 07:51:25

+0

我已经改变了x和y的起始值,所以我的问题就解决了(在网格中间) x = 5 y = 5 moves = [(1,0),(0,1), (-1,0),(0,-1)] 而x <= 10或y <= 10: ... 然后我还有一个问题要问,如何根据这些问题创建打印函数(直到他走到最后,他会在每个方格上站立多少次) – mustafaSarialp 2012-02-24 08:35:32

1

我做了一个类似的随机行走程序,允许醉酒男子在三维空间使用球坐标在任何方向行走。

import random 
import math 
def rw3(n,tries): 
    s = 0 
    for m in range(1,tries+1): 
     x = 0 
     y = 0 
     z = 0 
     pi = math.pi 
     for step in range(1,n+1): 
      t = random.uniform(0,2*pi) 
      f = random.uniform(0,2*pi) 
      p = 1 
      x += p*math.sin(f)*math.cos(t) 
      y += p*math.sin(f)*math.sin(t) 
      z += p*math.cos(f) 
     s += (x**2+y**2+z**2)**.5 
    return s/tries 
life = 42 
while life: 
    n = int(input("Please enter the number of steps: ")) 
    tries = int(input("How many times should I perform the experiment? ")) 
    print() 
    print(rw3(n,tries)) 
    print()