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
原创粉丝点击