CellularAutomata.py :  » Development » SimPy » SimPy-2.1.0beta » SimPyModels » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » SimPy 
SimPy » SimPy 2.1.0beta » SimPyModels » CellularAutomata.py
from SimPy.Simulation import *
"""CellularAutomata.py
Simulation of two-dimensional cellular automata. Plays game of Life.
"""

class Autom(Process):
    def __init__(self,coords):
        Process.__init__(self)
        self.x=coords[0]
        self.y=coords[1]
        self.state=False

    def nrActiveNeighbours(self,x,y):
        nr=0
        coords=[(xco+x,yco+y) for xco in (-1,0,1) for yco in (-1,0,1) if not (xco==0 and yco==0)]

        for a_coord in coords:
            try:
                if cells[a_coord].state: 
                    nr += 1
            except KeyError:
                ## wrap around
                nux=divmod(a_coord[0],size)[1]
                nuy=divmod(a_coord[1],size)[1]
                if cells[(nux,nuy)].state: nr += 1
        return nr

    def decide(self,nrActive):
        return  (self.state and (nrActive == 2 or nrActive == 3) or (nrActive==3))
                
    def celllife(self):
        while True:
            #calculate next state
            temp=self.decide(self.nrActiveNeighbours(self.x,self.y))
            yield hold,self,0.5
            #set next state
            self.state=temp
            yield hold,self,0.5

class Show(Process):
    def __init__(self):
        Process.__init__(self)

    def picture(self):
        while True:
            print "Generation %s" %now()
            for i in range(size):
                for j in range(size):
                    if cells[(i,j)].state:
                        print "*",
                    else:
                        print ".",
                print
            print
            yield hold,self,1

size=20
cells={}
initialize()
for i in range(size):
    for j in range(size):
        a=cells[(i,j)]=Autom((i,j))
        activate(a,a.celllife())

##R-pentomino
cells[(9,3)].state=True
cells[(10,3)].state=True
cells[(9,4)].state=True
cells[(8,4)].state=True
cells[(9,5)].state=True

cells[(5,5)].state=True
cells[(5,6)].state=True
cells[(4,5)].state=True
cells[(4,6)].state=True
cells[(4,7)].state=True
cells[(10,10)].state=True
cells[(10,11)].state=True
cells[(10,12)].state=True
cells[(10,13)].state=True
cells[(11,10)].state=True
cells[(11,11)].state=True
cells[(11,12)].state=True
cells[(11,13)].state=True

print 'CellularAutomata'
s=Show()
whenToStartShowing=10
activate(s,s.picture(),delay=whenToStartShowing)
nrGenerations=30
simulate(until=nrGenerations)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.