Simple usage of zasim

First, we import the cagen package, so that we have a few pre-defined simulators ready to use. Then, we import the console module from the display package, which offers ascii-art based display of cellular automaton configurations.


In [1]:
from zasim import cagen
from zasim.display import console

Now we can create a simulator, that calculates the elementary cellular automaton number 153 (as described by Stephen Wolfram)


In [2]:
simulator_object = cagen.ElementarySimulator(size=[50], rule=122)

In order to see something, a display needs to be created. For the ipython notebook, the most suitable display object is a OneDimConsolePainter.

Normally, the "lines" parameter would let us specify, how many lines of history to keep, but since the Painter prints the configuration after every step anyway, we set the number of lines to 1.


In [3]:
display = console.OneDimConsolePainter(simulator_object, lines=1)

Now, connecting the display and stepping the simulator object will cause a line of ascii art characters to be printed to the screen. Let's do a few steps:


In [4]:
for i in xrange(20):
    simulator_object.step()

Inspecting the Simulator

The simulator object offers a member called t, which stores all data, that is relevant for one specific execution. This usually includes the configuration, but also data like the statistics gathered by activity or histogram calculations, that can optionally be added to the object.

Let's create a simulator like the one above, but add a few more interesting bits of data to it:


In [5]:
sim = cagen.ElementarySimulator(size=[50], histogram=True, activity=True, rule=122)
dsp = console.OneDimConsolePainter(sim, 1)
for i in range(10): sim.step()

In [6]:
print "the attrs are:", sim.t.attrs
print "the rule number is", sim.t.rule
print "the current configuration is", sim.t.cconf
print "the activity in the last step was", sim.t.activity
print "the distribution of zeros and ones was:", sim.t.histogram

Post-processing each step

The simulator object offers signals, that can be used for data analysis. The most interesting one for us right now is "updated". We can use it like this:


In [7]:
activity_history = []
def analyse_step():
    activity_history.append(tuple(sim.t.activity))

sim.updated.connect(analyse_step)

for i in range(5):
    sim.step()

print "the activities over the last 5 steps were"
print "\n".join(["%d / %d active" % (act, inact + act) for inact, act in activity_history])

Watch out, though: When you re-run the cell, you will end up with multiple instances of the analyse_step function connected to the updated signal, so that your list will contain more than jus the five entries you're expecting. In that case, just re-run the cell, that created the sim and display objects.