Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Contents
Introduction.............................................................................................................................3 Requirements.........................................................................................................................3 Description.............................................................................................................................4 What is a cellular automaton?...........................................................................................4 Use cases..........................................................................................................................4 C++ approach........................................................................................................................5 Python approach....................................................................................................................6 What do i need?.................................................................................................................6 Naive version:................................................................................................................6 Advanced version:.........................................................................................................6 Control flow........................................................................................................................7 Data types..........................................................................................................................8
1 Introduction
While being caught by the flue I decided to play a bit with cellular automata. My first approach is to use C++ with Qt for the user interface. The second approach is using python with wxPython for the user interface. The python approach comes in two versions: a naive slow first approach in just python and a much faster solution using the numpy software. As python is a very pleasant programming language to work with, it seemed an interesting goal to see whether python would be fast enough for the task. Note that this is not a particular fancy program.
2 Requirements
The software used for the C++ appoach is: - kubuntu 10.4 - kdevelop - Qt The software for the python approach is: - kubuntu 10.4 - python 2.6 - wxpython 2.8 - numpy 1:1.3.0
3 Description
3.1 What is a cellular automaton?
From wikipedia:
A cellular automaton (pl. cellular automata, abbrev. CA) is a discrete model studied in computability theory, mathematics, physics, complexity science, theoretical biology and microstructure modeling. It consists of a regular grid of cells, each in one of a finite number of states, such as "On" and "Off" (in contrast to a coupled map lattice).
4 C++ approach
TBD
5 Python approach
5.1 Naive version 5.1.1 What do i need?
TBD
The context consists of a wx.PySimpleApp and wx.Frame that will be subclassed. The subclassed Frame, where the cellular automaton will be shown, will have a 256 by 256 dimension. The overall Frame area is 520 by 600.
We also define two constant cells representing a 'dead' and a 'life' cell:
# # Currently just work with 2 color values. # One for dead cells and one for life cells deadCellAR = makeByteArray((1,1)) DCR = deadCellR = deadCellAR['r'][0,0] DCG = deadCellG = deadCellAR['g'][0,0] DCB = deadCellB = deadCellAR['b'][0,0] DCA = deadCellA = deadCellAR['a'][0,0] deadCell = deadCellAR[0][0] lifeCellAR = makeByteArray((1,1)) LCR = lifeCellR = lifeCellAR['r'][0,0] LCG = lifeCellG = lifeCellAR['g'][0,0] LCB = lifeCellB = lifeCellAR['b'][0,0] LCA = lifeCellA = lifeCellAR['a'][0,0] lifeCell = lifeCellAR[0][0] = = = = 200 0 0 255
= = = =
0 200 0 255
# class the implements the automaton rules: class caRules: def __init__(self): def parity(self): def life(self): def lwd(self): def lwd2(self): def som23_4(self): def initRules(self): def getAllRulesAsStrings(self): def getRule(self, ruleAsString): def setBuffer(self, buf): def getBuffer(self): def dumpInfo(self, buf, ker, res, label):
The blue part shows the controls for selecting the pattern rule and the number of generations. Additionally the transparency (the A part of the RGBA) can be set. The buttons for executing the pattern rule, resetting the playarea, loading or saving the playarea are available. The red part shows the play area. Initially of software generated pattern is visible.
5.5.1 Ininialization
The instance of caDialog is created, as we have seen, with:
caDialog(frame, (256, 256))
I will describe the blue colored functions. The caRules() creates an instance of the caRules class. This will be described in detail further on. The self.buildInterface() function builds the user interface as shown on the previous page.
10
The makeBitmapBuffer((self.xdim, self.ydim), DCR, DCG, DCB, DCA) calls the function:
def makeBitmapBuffer(size, red, green, blue, alpha): # make the array arr = makeBitmapArray(size, red, green, blue, alpha) bwa = makeBWArray(size) d = {} d['i'] = None d['b'] = arr d['s'] = bwa return d
The makeByteArray and makeBWArray functions have been described in the 'Data types' paragraph earlier. The array is used to create the actual bitmap. The bitmap is initialized with a pattern (see user interface earlier):
def initBitmap(img, dim): # This is for generating an image to start from: w = dim[0]; h = dim[1] for y in xrange(h): for x in xrange(w): if ((x > (w/2+0.0*w) and x < (w/2+0.1*w)) and (y > (h/2+0.0*h) and y < (h/2+0.1*h))) or ((x > (w/2+0.2*w) and x < (w/2+0.4*w)) and (y > (h/2+0.2*h) and y < (h/2+0.4*h))) or ((x > (w/2-0.4*w) and x < (w/2+0.0*w)) and (y > (h/2-0.4*h) and y < (h/2+0.0*h))): img['b'][x,y] = lifeCell img['s'][x,y] = 1
\ \ \ \ \
This will create the three green rectangular areas. The Refresh and Update functions of wx.Panel will then be called to show the dialog.
11
In the constructor of caDialog, an instance of caRules has been created. The constructor of caRules looks as follows:
class caRules: def __init__(self): self.initRules() def initRules(self): self.patternlistT = [ "parity", "life", "lwd", "lwd2", "som23_4" ] self.patternlistF = [ self.parity, self.life, self.lwd, self.lwd2,
self.som23_4 ]
In the constructor of caDialog the setBuffer() function of caRules has been used:
def setBuffer(self, buf): self.buf = buf[0] self.bufbw = buf[1] self.DA = buf[2] self.LA = buf[3] size = self.bufbw.shape xdim = range(1,size[0]-1) ydim = range(1,size[1]-1) self.xydims = [(x,y) for x in xdim for y in ydim]
12
The constants DOAPPLYMAP and DOUSENUMPY are used to experiment with different solutions. We assume here that DOAPPLYMAP is false and DOUSENUMPY is true. The argument 'rule' of applyRule is called. The actual value of rule is one of the functions in the caRules class:
[ self.parity, self.life, self.lwd, self.lwd2, self.som23_4 ]
13