轉 python trace walk DEMO

來源:互聯網
上載者:User

標籤:and   sum   sel   for   tps   log   ace   AMM   add   

46965125

 

#quote from ‘introduction to computation and programming #using Python, revised, MIT press‘import randomimport 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 traceWalk(drunkKinds, numSteps):    styleChoices = styleIterator((‘b+‘, ‘r^‘, ‘mo‘))    f = Field()    for dClass in drunkKinds:        d = dClass()        f.addDrunk(d, Location(0, 0))        locs = []        for s in range(numSteps):            f.moveDrunk(d)            locs.append(f.getLoc(d))        xVals = []        yVals = []        for l in locs:            xVals.append(l.getX())            yVals.append(l.getY())        curStyle = styleChoices.nextStyle()        pylab.plot(xVals, yVals, curStyle,                   label = dClass.__name__)    pylab.title(‘Spots Visited on Walk (‘                 + str(numSteps) + ‘ steps)‘)    pylab.xlabel(‘Steps East/West of Origin‘)    pylab.ylabel(‘Steps North/South of Origin‘)    pylab.legend(loc = ‘best‘) traceWalk((UsualDrunk, ColdDrunk, EWDrunk), 200)pylab.show()

  

轉 python trace walk DEMO

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.