get final location DEMO
来源:互联网 发布:灭绝师太扫矿软件下载 编辑:程序博客网 时间:2024/06/10 09:32
#quote from 'introduction to computation and programming #using Python, revised, MIT press'import randomimport pylabdef 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 getFinalLocs(numSteps, numTrials, dClass): locs = [] d = dClass() origin = Location(0,0) for t in range(numTrials): f = Field() f.addDrunk(d, origin) for s in range(numSteps): f.moveDrunk(d) locs.append(f.getLoc(d)) return locs def plotLocs(drunkKinds, numSteps, numTrials): styleChoice = styleIterator(('b+', 'r^', 'mo')) for dClass in drunkKinds: locs = getFinalLocs(numSteps, numTrials, dClass) xVals, yVals = [], [] for l in locs: xVals.append(l.getX()) yVals.append(l.getY()) meanX = sum(xVals)/float(len(xVals)) meanY = sum(yVals)/float(len(yVals)) curStyle = styleChoice.nextStyle() pylab.plot(xVals, yVals, curStyle, label = dClass.__name__ + ' Mean loc. = <' + str(meanX) + ', ' + str(meanY) + '>') pylab.title('Location at End of Walks (' + str(numSteps) + ' steps)') pylab.xlabel('Steps East/West of Origin') pylab.ylabel('Steps North/South of Origin') pylab.legend(loc = 'lower left', numpoints = 1) plotLocs((UsualDrunk, ColdDrunk, EWDrunk), 100, 200)pylab.show()
0 0
- get final location DEMO
- Get-Location 帮助信息
- get mouse location
- final in Java DEMO
- Http Post Get Demo
- Get请求Demo
- express get请求 demo
- How to get Location Using Location API JSR 179
- 代替window.location get方式post提交
- Java final以及static用法简单demo
- Description Resource Path Location Type Error generating final archive: Debug
- Description Resource Path Location Type Error generating final archive: Debug
- Description Resource Path Location Type Error generating final archive: Debug
- Using GPS to get current location in Android
- get sequence by its start and stop location
- R连接hbase错误(Can't get the location)
- java远程连接hbase can't get location
- Android中get请求无法获取Location信息的问题
- 有关安卓的设计(持续更新)
- 猫东formValidator对SELECT(单选)控件处理的bug
- pytho获取磁盘剩余空间
- 第一部分、十道海量数据处理面试题
- php多条件查询
- get final location DEMO
- 三星S6手机上找不到unity的main.so文件
- eclipse 手动安装svn
- C#测试连接access数据库步骤代码
- 关于串口写大量数据到WQ25中的一些注意点
- AngularJS学习笔记<1>环境配置
- ZOJ1524
- [LeetCode][Java] Restore IP Addresses
- Median of Two Sorted Arrays