2012-02-08 45 views
1

我一直在通過觀看麻省理工學院的開放課程來學習python,並且在關於隨機遊走模擬模型的講座17之後,我寫了一段簡單的代碼,但它沒有起作用,並顯示如下錯誤:「TypeError:'醉'對象不可調用」。希望有人能幫助我找出錯誤的地方。簡單的隨機遊走模型中的類型錯誤 - Python

from math import* 
import random,pylab 

    class location(object): 

    def __init__(self,x,y): 
     self.x=x 
     self.y=y 
    def move(self,xc,yc): 
     return location(self.x+float(xc),self.y+float(yc)) 
    def getCoordinates(self): 
     return self.x,self.y 
    def getDistance(self,other): 
     ox,oy=other.getCoordinates() 
     x=fabs(other.x-self.x) 
     y=fabs(other.y-self.y) 
     return sqrt(x**2+y**2) 


class compasspt(object): 

    possibles=('n','s','e','w') 
    def __init__(self,pt): 
     if pt in self.possibles: 
      self.pt=pt 
     else: 
      raise ValueError 
    def move(self,dist): 
     if self.pt=='n':return (0,dist) 
     elif self.pt=='s':return (0,-dist) 
     elif self.pt=='w':return (dist,0) 
     elif self.pt=='e':return (-dist,0) 
     else:raise ValueError 


class field(object): 

    def __init__(self,drunk,location): 
     self.drunk=drunk 
     self.location=location 
    def move(self,cp,dist): 
     oldLocation=self.location 
     xc,yc=cp.move(dist) # shadowing 
     self.location=oldLocation.move(xc,yc) 
    def getLocation(self): 
     return self.location 
    def getDrunk(self): 
     return self.drunk 


class drunk(object): 
    def __init__(self,name): 
     self.name=name 
    def move(self,field,time=1): 
     if field.getDrunk()!=self: 
      raise ValueError('the drunk is not in the field.') 
     for i in range(time): 
      pt=compasspt(random.choice(compasspt.possibles)) # random walk 
      field.move(pt,1) # shadowing 


def performTrial(time,f): 
    start=f.getLocation() 
    distances=[0.0] 
    for t in range(1,time+1): 
     f.getDrunk().move(f) 
     newLocation=f.getLocation() 
     distance=newLocation.getDistance(start) 
     distances.append(distance) 
    return distances 


drunk=drunk('Alexander') 
for i in range(3): 
    f=field(drunk,location(0,0)) 
    distances=performTrial(1000,f) 
    pylab.plot(distances) 
pylab.title('Alexander\'s random walk') 
pylab.xlabel('Time') 
pylab.ylabel('Distance from Origin') 


def performSimulation(time, numTrials): 
    distLists=[] 
    for trial in range(numTrials): 
     d = drunk('Drunk'+str(trial)) 
     f=field(d,location(0,0)) 
     distances=performTrial(time,f) 
     distLists.append(distances) 
    return distLists 


def ansQuest(maxTime,numTrials): 
    means=[] 
    distLists=performSimulation(maxTime,numTrials) 
    for t in range(maxTime+1): 
     tot=0.0 
     for distL in distLists: 
      tot+=distL[t] 
     means.append(tot/len(distL)) 
    pylab.figure() 
    pylab.plot(means) 
    pylab.ylabel('distance') 
    pylab.xlabel('time') 
    pylab.title('Average Distance vs. Time('+str(len(distLists))+'trials)') 

ansQuest(1000,300) 
pylab.show() 

回答

1

我覺得你的問題來自使用醉類名和實例名都......試着改變你的類醉,然後

drunk=Drunk('Alexander') 

和...

d = Drunk('Drunk'+str(trial)) 

我沒有安裝pylab,所以我刪除了所有這些行,它運行乾淨(儘管沒有輸出)

我正在與o'Reilly一起學習Python,並且實際上是試圖回答作爲任務一部分的問題,所以我希望這對你有所幫助。

+0

它的工作原理!非常感謝! – Sol 2012-02-08 08:02:09

+0

嘿,不客氣。如果我向我的教練展示這個,你介意嗎?另外,你如何找到麻省理工學院的課程?它是複雜還是相當簡單? – 2012-02-08 08:08:04

+0

沒關係。這些課程內容廣泛,但並不難學。對於初學者我覺得很好。 – Sol 2012-02-08 08:14:53