2017-08-23 16 views
-4
import math, random, pylab 
#pseudo random => computer simulate the randomness 

class Location(object): 
    def __init__(self, x, y):#x-coordinate & y-coordinate 
     self.x = float(x) 
     self.y = float(y) 
    def move(self, xc, yc): 
     return Location(self.x+float(xc), self.y+float(yc))#increment x and y, perhaps increment by 0 
    def getCoords(self): 
     return self.x, self.y#return a tuple of two values. 
    def getDist(self, other): 
     ox, oy = other.getCoords() 
     xDist = self.x - ox 
     yDist = self.y - oy 
     return math.sqrt(xDist**2 + yDist**2)#pythagorean theorem to compute hypotenuse, how far a point was from the origin 

class CompassPt(object):#encapsulate four directions in one class in order to edit simply 
    possibles = ('N', 'S', 'E', 'W')#global variable 
    def __init__(self,pt): 
     if pt in self.possibles: self.pt = pt 
     else: raise ValueError('in CompassPt.__init__') 
    def move(self, dist): 
     if self.pt == 'N': return (0,dist) 
     elif self.pt == 'S': return (0, -dist) 
     elif self.pt == 'E': return (dist, 0) 
     elif self.pt == 'W': return (-dist, 0) 
     else: raise ValueError('in CompassPt.move') 

class Field(object): 
    def __init__(self, drunk, loc): 
     self.drunk = drunk 
     self.loc = loc#put the drunk in the field at that location 
    def move(self, cp, dist): 
     oldLoc = self.loc# the old location is the current location 
     xc, yc = cp.move(dist)#compass point move get the value, eg:return(0,1),return 0 => xc, return 1 => xy 
     self.loc = oldLoc.move(xc, yc)#get the move from class location 
    def getLoc(self): 
     return self.loc 
    def getDrunk(self): 
     return self.drunk 

class Drunk(object): 
    def __init__(self, name): 
     self.name = name 
    def move(self, field, cp, dist = 1):#if I dont't pass the time, use 1 
     if field.getDrunk().name != self.name:#Drunk doesn't in the field 
      raise ValueError('Drunk.move called with drunk not in field') 
     for i in range(dist): 
      field.move(cp, 1) 

class UsualDrunk(Drunk): 
    def move(self, field, dist = 1): 
     cp = random.choice(CompassPt.possibles)# which direction to move in the subclass 
     Drunk.move(self, field, CompassPt(cp), dist)#Note notation of call, which class to get move from 

class ColdDrunk(Drunk): 
    def move(self, field, dist = 1): 
     cp = random.choice(CompassPt.possibles) 
     if cp == 'S': 
      Drunk.move(self, field, CompassPt(cp), 2*dist) 
     else: 
      Drunk.move(self, field, CompassPt(cp), dist) 

class EWDrunk(Drunk): 
    def move(self, field, time = 1): 
     cp = random.choice(CompassPt.possibles) 
     while cp != 'E' and cp != 'W': 
      cp = random.choice(CompassPt.possibles) 
     Drunk.move(self, field, CompassPt(cp), time) 

def performTrial(time, f): 
    start = f.getLoc()#wherever the drunk happens to be at this point in time in the field 

    distances = [0.0] 
    locs = [start] 
    for t in range(1, time + 1): 
     f.getDrunk().move(f)#f dot get drunk returns an object of class drunk, select the move method to move the drunk 
     newLoc = f.getLoc() 
     distance = newLoc.getDist(start)#how far is the new location from wherever the starting location was 
     distances.append(distance)#collect the list of disctances 
     locs.append(newLoc) 
    return distances, locs 

def performSim(time, numTrials, drunkType):#get a distance list 
    distLists = [] 
    locLists = [] 
    for trial in range(numTrials): 
     d = drunkType('Drunk' + str(trial))# pass a name of class 
     f = Field(d, Location(0, 0)) 
     distances, locs = performTrial(time, f) 
     distLists.append(distances) 
     locLists.append(locs) 
    return distLists, locLists 

def ansQuest(maxTime, numTrials, drunkType, title): 
    distLists, LocLists = performSim(maxTime, numTrials, drunkType) 
    means = [] 
    distLists = performSim(maxTime, numTrials, drunkType) 
    for t in range(maxTime + 1):# 1 step is necessary, 0 isn't move 
     tot = 0.0 
     for distL in distLists: 
      tot += distL[t]# Sum of values within an array of each element 
     means.append(tot/len(distLists))#divide by the number of lists 
    pylab.figure() 
    pylab.plot(means) 
    pylab.ylabel('distance') 
    pylab.xlabel('time') 
    pylab.title(title + 'Ave. Distance') 
    lastX = [] 
    lastY = [] 
    for locList in LocLists: 
     x, y = locList[-1].getCoords() 
     lastX.append(x) 
     lastY.append(y) 
    pylab.figure() 
    pylab.scatter(lastX, lastY) 
    pylab.xlabel('EW Distance') 
    pylab.ylabel('NW Distance') 
    pylab.title(title + 'Final locations') 
    pylab.figure() 
    pylab.hist(lastX) 
    pylab.xlabel('EW Value') 
    pylab.ylabel('Number of Trials') 
    pylab.title(title + 'Distribution of Trial EW Values') 

numSteps = 500 
numTrials = 300 
ansQuest(numSteps, numTrials, UsualDrunk, 'UsualDrunk' + str(numTrials) + 'Trials') 
ansQuest(numSteps, numTrials, ColdDrunk, 'ColdDrunk' + str(numTrials) + 'Trials') 
ansQuest(numSteps, numTrials, EWDrunk, 'EWDrunk' + str(numTrials) + 'Trials') 
pylab.show() 

このプログラムはランダムウォーキンググラフをプロットします。私は次のバグを受けました。TypeError:+ =: 'float'および 'list'のサポートされていないオペランドタイプ

File "C:\Users\oicq2\Google drive\Python\Program\Test_17.py", line 104, in ansQuest
tot += distL[t]# Sum of values within an array of each element
TypeError: unsupported operand type(s) for +=: 'float' and 'list'

私はすでに以下の行で引数をインスタンス化しています。

distLists, LocLists = performSim(maxTime, numTrials, drunkType)

私のプログラムには何が問題なのですか?どうすれば修正できますか?

+2

「tot」には明らかにfloatが含まれています。 'distLists'はリストのリストなので、' dist'はリストです。フロートとリストを追加することはできません。 – stark

答えて

0
distLists, LocLists = performSim(maxTime, numTrials, drunkType) 
    means = [] 
## distLists = performSim(maxTime, numTrials, drunkType) 
    for t in range(maxTime + 1):# 1 step is necessary, 0 isn't move 
     tot = 0.0 
     for distL in distLists: 
      tot += distL[t]# Sum of values within an array of each element 
     means.append(tot/len(distLists))#divide by the number of lists 
    pylab.figure() 
    pylab.plot(means) 
    pylab.ylabel('distance') 
    pylab.xlabel('time') 
    pylab.title(title + 'Ave. Distance') 
    lastX = [] 
    lastY = [] 
    for locList in LocLists: 
     x, y = locList[-1].getCoords() 
     lastX.append(x) 
     lastY.append(y) 
    pylab.figure() 
    pylab.scatter(lastX, lastY) 
    pylab.xlabel('EW Distance') 
    pylab.ylabel('NW Distance') 
    pylab.title(title + 'Final locations') 
    pylab.figure() 
    pylab.hist(lastX) 
    pylab.xlabel('EW Value') 
    pylab.ylabel('Number of Trials') 
    pylab.title(title + 'Distribution of Trial EW Values') 

リストを記述する行が2行あるため、distListsには問題があります。あなたは他の1つをコメントすることができるので、正しい答えを得ることができます。

distLists, LocLists = performSim(maxTime, numTrials, drunkType)

distLists = performSim(maxTime, numTrials, drunkType)

関連する問題