random walk for different types of Drunks
2015-07-20 10:15
399 查看
#quote from 'introduction to computation and programming #using Python, revised, MIT press' import random import pylab def stdDev(X): mean = sum(X)/len(X) tot = 0.0 for x in X: tot += (x-mean)**2 return (tot/len(X))**0.5 def CV(X): mean = sum(X)/len(X) return stdDev(X)/mean class Location(object): def __init__(self, x, y): """x and y are floats""" self.x = x self.y = y def move(self, deltaX, deltaY): """deltaX and deltaY are floats""" return Location(self.x + deltaX, self.y + deltaY) def getX(self): return self.x def getY(self): return self.y def distFrom(self, other): ox = other.x oy = other.y xDist = self.x - ox yDist = self.y - oy return (xDist**2 + yDist**2)**0.5 def __str__(self): return '<' + str(self.x) + ', ' + str(self.y) + '>' class Field(object): def __init__(self): self.drunks = {} def addDrunk(self, drunk, loc): if drunk in self.drunks: raise ValueError('Duplicate drunk') else: self.drunks[drunk] = loc def moveDrunk(self, drunk): if drunk not in self.drunks: raise ValueError('Drunk not in field') xDist, yDist = drunk.takeStep() currentLocation = self.drunks[drunk] #use move method of Location to get new location self.drunks[drunk] = currentLocation.move(xDist, yDist) def getLoc(self, drunk): if drunk not in self.drunks: raise ValueError('Drunk not in field') return self.drunks[drunk] class Drunk(object): def __init__(self, name = None): """Assumes name is a str""" self.name = name def __str__(self): if self != None: return self.name return 'Anonymous' class UsualDrunk(Drunk): def takeStep(self): stepChoices = [(0.0, 1.0), (0.0, -1.0), (1.0, 0.0), (-1.0, 0.0)] return random.choice(stepChoices) class ColdDrunk(Drunk): def takeStep(self): stepChoices = [(0.0, 1.0), (0.0, -2.0), (1.0, 0.0), (-1.0, 0.0)] return random.choice(stepChoices) class EWDrunk(Drunk): def takeStep(self): stepChoices = [(1.0, 0.0), (-1.0, 0.0)] return random.choice(stepChoices) class styleIterator(object): def __init__(self, styles): self.index = 0 self.styles = styles def nextStyle(self): result = self.styles[self.index] if self.index == len(self.styles) - 1: self.index = 0 else: self.index += 1 return result def walk(f, d, numSteps): """Assumes: f a field, d a Drunk in f, and numSteps an int >= 0. Moves d numSteps times, and returns the difference between the final location and the location at the start of the walk.""" start = f.getLoc(d) for s in range(numSteps): f.moveDrunk(d) return start.distFrom(f.getLoc(d)) def simWalks(numSteps, numTrials, dClass): """Assumes numSteps an int >= 0, numTrials an int > 0, dClass a subclass of Drunk Simulates numTrials walks of numSteps steps each. Returns a list of the final distances for each trial""" Homer = dClass() origin = Location(0.0, 0.0) distances = [] for t in range(numTrials): f = Field() f.addDrunk(Homer, origin) distances.append(walk(f, Homer, numSteps)) return distances def simDrunk(numTrials, dClass, walkLengths): meanDistances = [] cvDistances = [] for numSteps in walkLengths: print 'Starting simulation of', numSteps, 'steps' trials = simWalks(numSteps, numTrials, dClass) mean = sum(trials)/float(len(trials)) meanDistances.append(mean) cvDistances.append(stdDev(trials)/mean) return (meanDistances, cvDistances) def simAll(drunkKinds, walkLengths, numTrials): styleChoice = styleIterator(('b-', 'r:', 'm-.')) for dClass in drunkKinds: curStyle = styleChoice.nextStyle() print 'Starting simulation of', dClass.__name__ means, cvs = simDrunk(numTrials, dClass, walkLengths) cvMean = sum(cvs)/float(len(cvs)) pylab.plot(walkLengths, means, curStyle, label = dClass.__name__ + '(CV = ' + str(round(cvMean, 4)) + ')') pylab.title('Mean Distances from origin (' + str(numTrials) + ' trials)') pylab.xlabel('Number of Steps') pylab.ylabel('Distances from Origin') pylab.legend(loc = 'best') pylab.semilogx() pylab.semilogy() simAll((UsualDrunk, ColdDrunk, EWDrunk), (10, 100, 1000, 10000), 100) pylab.show()
%run "C:\Users\Administrator\test.py"
Starting simulation of UsualDrunk
Starting simulation of 10 steps
Starting simulation of 100 steps
Starting simulation of 1000 steps
Starting simulation of 10000 steps
Starting simulation of 100000 steps
Starting simulation of ColdDrunk
Starting simulation of 10 steps
Starting simulation of 100 steps
Starting simulation of 1000 steps
Starting simulation of 10000 steps
Starting simulation of 100000 steps
Starting simulation of EWDrunk
Starting simulation of 10 steps
Starting simulation of 100 steps
Starting simulation of 1000 steps
Starting simulation of 10000 steps
Starting simulation of 100000 steps
%run "C:\Users\Administrator\test.py"
Starting simulation of UsualDrunk
Starting simulation of 10 steps
Starting simulation of 100 steps
Starting simulation of 1000 steps
Starting simulation of 10000 steps
Starting simulation of ColdDrunk
Starting simulation of 10 steps
Starting simulation of 100 steps
Starting simulation of 1000 steps
Starting simulation of 10000 steps
Starting simulation of EWDrunk
Starting simulation of 10 steps
Starting simulation of 100 steps
Starting simulation of 1000 steps
Starting simulation of 10000 steps
相关文章推荐
- 精通CSS+DIV简单小结(一)
- 你知道何时调用 const_reference front() const 吗?
- <a>标签的href和onclick属性
- 初识html5——试试博文编辑器
- html Javascript MD5
- js将控件隐藏及display属性
- [读书笔记]深入浅出NodeJS——Node中的异步I/O
- HTML5 LocalStorage 本地存储
- 【阅读】《head first html5》第一章——认识html5
- json字符串转为字典
- javascript单例模式的理解
- jquery学习记录一:jquery动态添加html元素后设置其绑定事件
- Jsp生成页面验证码的方法
- JavaScript权威指南_130_第15章_脚本化文档_15.6-创建、 插入、删除节点-删除和替换节点
- JS的基础类型与引用类型
- 邮箱屏蔽的邮件内容的样式<style></style>
- jsp 生成验证码代码
- jsp页面获取路径
- JS中常用函数(一)启动运行函数addLoadEvent
- 现在就开始使用AngularJS的三个重要原因