2013-03-06 228 views
-1

請你幫我弄清楚我的代碼有什麼問題。 我正在嘗試編寫一個程序,它可以生成二維隨機散步,並且當步數爲1000,最大步長爲0.9,以及兩個職位之間的間隔爲500步時,確定步行者的位置統計信息是0.001。Python中的隨機遊走代碼[2維]

import math 
import random 
import time 

print "RANDOM WALKS ANALYSIS IN ONE DIMENSION" 
NOFW_ = 1000 #The number of walks 
NOFS_= 500 #The number of steps in each walk 
MSS_ = 0.9 # The maximum step size[m] 
SOFP_ = 0.001 # The separation of positions considered equal[m] 

print "       Number of walks: %3g"% NOFW_ 
print "   Number of steps in each Walk: %3g"% NOFS_ 
print "      Maximum step size: %3g"% MSS_,"m" 
print "Separation of positions considered equal: %3g"% SOFP_,"m" 
print 
print "Please wait while random walks are generated and analyzed..." 
print "Date:" + time.ctime() 
print 

def initialPosition(): 
    return (0.0, 0.0) 

def distance(posA, posB): 
    """Calculates the distance between two positions posA and posB""" 
    distance = math.sqrt((posB[0] - posA[0])**2 + (posB[1] - posA[1])**2) 
    return distance 

def printstats(description, numbers): 
    minimum_value_ = min(numbers) 
    numbers.sort() 
    Tenth_percentile = abs(0.10*len(numbers) + 0.5) 
    Mean_value_ = (1./float(len(numbers))*sum(numbers)) 
    A = 0 
    for values in numbers: 
     B = distance(values, Mean_value_) 
     B = B**2 
     A = B + A 
    Standard_deviation = math.sqrt((1./(len(numbers)-1))*A) 
    Newposition_ = int(0.90*(len(numbers) + 0.5)) 
    Ninetieth_percentile =numbers[Newposition_] 
    maximum_value_ = max(numbers) 

    print "Analysis for"""+ description 
    print "Minimum value: %9.1f" % minimum_value_ 
    print "10th percentile: %7.1f" % Tenth_percentile 
    print "Mean value: %12.1f" % Mean_value_ 
    print "Standard deviation: %4.1f" % Standard_deviation 
    print "90th percentile: %7.1f" % Ninetieth_percentile 
    print "Maximum value: %9.1f" % maximum_value_ 


    list_1 = [minimum_value_, Tenth_percentile, Mean_value_, Standard_deviation, Ninetieth_percentile,maximum_value_] 
    return list_1 

def takeStep(prevPosition, maxStep): 
    x = random.random() 
    y = random.random() 
    minStep = -maxStep 
    Z = random.random()*2*math.pi 
    stepsize_ = random.random()*0.9 
    Stepx= stepsize_*math.cos(Z) 
    Stepy= stepsize_*math.sin(Z) 
    New_positionx = prevPosition[0] + Stepx 
    New_positiony = prevPosition[1] + Stepy 
    return (New_positionx, New_positiony) 

Step_100 = [] 
Step_500 = [] 
count_list = [] 
for walk in range(NOFW_): 
    Step1 = [] 
    Position = (0.0,0.0) 
    count = 0 
    for step in range(NOFS_): 
     Next_Step_ = takeStep(Position, MSS_) 
     for word in Step1: 
      if distance(Next_Step_, word) <= SOFP_: 
       count +=1 
     position = Next_Step_ 
     Step1.append(Next_Step_) 
    Step_100.append(Step1[-1]) 
    Step_500.append(Step1[-1]) 
    count_list.append(count) 

Step_100 = printstats("distance from start at step 100 [m]", Step_100) 
Step_500 = printstats("distance from start at step 500 [m]", Step_500) 
count_list = printstats("times position revisited", count_list) 
+0

該代碼不會給出所需的結果當我運行它 – 2013-03-06 03:39:47

回答

0

你的問題是在這裏

Mean_value_ = (1./float(len(numbers))*sum(numbers)) 

sum()應該得到一些數字,但您的變量numbers實際上是包含2個值

您可能需要定義自己的和函數的一些元組對於2個數的元組,或者將第一個值和第二個值分別求和

+0

謝謝,但請你能告訴我如何做到這一點 – 2013-03-06 04:01:03

+0

@AhmedAli說,它,取決於,你的平均值是距離的平均值相比,你的出發點? – sysko 2013-03-06 05:59:29