2012-02-23 279 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()