fork download
  1. import math, random, pylab
  2.  
  3. class Location(object):
  4. def __init__(self, x, y):
  5. self.x = float(x)
  6. self.y = float(y)
  7.  
  8. def move(self, xc, yc):
  9. return Location(self.x+float(xc), self.y+float(yc))
  10.  
  11. def getCoords(self):
  12. return self.x, self.y
  13.  
  14. def getDist(self, other):
  15. ox, oy = other.getCoords()
  16. xDist = self.x - ox
  17. yDist = self.y - oy
  18. return math.sqrt(xDist**2 + yDist**2)
  19.  
  20.  
  21. class CompassPt(object):
  22. possibles = ('N', 'S', 'E', 'W')
  23.  
  24. def __init__(self, pt):
  25. if pt in self.possibles: self.pt = pt
  26. else: raise ValueError('in CompassPt.__init__')
  27.  
  28. def move(self, dist):
  29. if self.pt == 'N': return (0, dist)
  30. elif self.pt == 'S': return (0, -dist)
  31. elif self.pt == 'E': return (dist, 0)
  32. elif self.pt == 'W': return (-dist, 0)
  33. else: raise ValueError('in CompassPt.move')
  34.  
  35.  
  36. class Field(object):
  37.  
  38. def __init__(self, drunk, loc):
  39. self.drunk = drunk
  40. self.loc = loc
  41.  
  42. def move(self, cp, dist):
  43. oldLoc = self.loc
  44. xc, yc = cp.move(dist)
  45. self.loc = oldLoc.move(xc, yc)
  46.  
  47. def getLoc(self):
  48. return self.loc
  49.  
  50. def getDrunk(self):
  51. return self.drunk
  52.  
  53. class Drunk(object):
  54. def __init__(self, name):
  55. self.name = name
  56.  
  57. def move(self, field, time = 1):
  58. if field.getDrunk() != self:
  59. raise ValueError('Drunk.move called with drunk not in field')
  60. for i in range(time):
  61. pt = CompassPt(random.choice(CompassPt.possibles))
  62. field.move(pt, 1)
  63.  
  64. def performTrial(time, f):
  65. start = f.getLoc()
  66. distances = [0.0]
  67. for t in range(1, time + 1):
  68. f.getDrunk().move(f)
  69. newLoc = f.getLoc()
  70. distance = newLoc.getDist(start)
  71. distances.append(distance)
  72. return distances
  73.  
  74. drunk = Drunk('Homer Simpson')
  75.  
  76. for i in range(3):
  77. f = Field(drunk, Location(0, 0))
  78. distances = performTrial(500, f)
  79. pylab.plot(distances)
  80.  
  81. pylab.title('Homer\'s Random Walk')
  82. pylab.xlabel('Time')
  83. pylab.ylabel('Distance from Origin')
  84.  
  85. def performSim(time, numTrials):
  86. distLists = []
  87. for trial in range(numTrials):
  88. d = Drunk('Drunk' + str(trial))
  89. f = Field(d, Location(0, 0))
  90. distances = performTrial(time, f)
  91. distLists.append(distances)
  92. return distLists
  93.  
  94. def ansQuest(maxTime, numTrials):
  95. means = []
  96. distLists = performSim(maxTime, numTrials)
  97. for t in range(maxTime + 1):
  98. tot = 0.0
  99. for distL in distLists:
  100. tot += distL[t]
  101. means.append(tot/len(distLists))
  102. pylab.figure()
  103. pylab.plot(means)
  104. pylab.ylabel('distance')
  105. pylab.xlabel('time')
  106. pylab.title('Average Distance vs. Time (' + str(len(distLists)) + ' trials)')
  107.  
  108. ansQuest(500, 300)
  109. pylab.show()
  110.  
Runtime error #stdin #stdout #stderr 0.09s 11040KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 1, in <module>
    import math, random, pylab
ImportError: No module named pylab