Skip to content

A 1-D lightweight cellular automata framework built in C++ with python bindings.

Notifications You must be signed in to change notification settings

booherbg/PyAutomata

Repository files navigation

PyAutomata
(c) Blaine Booher 2011
https://github.com/booherbg/PyAutomata

PyAutomata is a lightweight 1D cellular automata framework. The original files are
written in C++ as entirely object oriented and exist in automata-1.0.0 as an entirely 
independent and pure C++ project.

The engine is based off of "Automata.cpp" (c) 2008 Alex Reynolds.  Unfortunately
I don't know the contact information for Alex Reynolds.  He released the engine
under version 2.0 of the Apache License  (http://www.apache.org/licenses/LICENSE-2.0.html).  

I've added quite a bit of functionality to the original engine, as well as a few of
my own routines that are specialized for my needs. The most important difference
is the ability to grab binary chunks of data out from the latest generation for 
the purpose of encoding some kind of phenotype behavior.

The CA is also set up to run an infinite number of generations. There is a buffer
that is implemented as a wrap-around history. If the buffer is, say, 100 generations
long, the 101st generation is stored in the 0th row, overwriting the first generation.
This clean solutions allows for arbitrary generations. You can, for example, run
10,000,000 generations without running out of memory. And it's fast, too.

This project is currently in "hacking" state. No official .egg files or clean
installer. The makefile isn't in the greatest shape.

November 20 2011
----------------
Added autodetect() set_python() and set_cpp() in __init__.py so that cpython
can be forced to use the pure python library. autodetect() will find out if
we are using pypy (checks for __pypy__) and sets python automatically


November 7 2011
---------------
Added a "python3" equivalent that isn't useful other than for the purpose of running
it through the berp haskell compiler (https://github.com/bjpop/berp). Currently
berp is not capable of using python's math libraries so this is a broken attempt.
either way, maybe the python3 version is useful to someone? Converted with
2to3 utility.

Also fixed a bug where an initialized value on the boundary of the CA would be pushed
to the left so that the entire value would be used, overwriting the value to the 
left of the boundary. Fixed so that the value is truncated correctly as intended.


August 30 2011
--------------
Added a pure python version that seems to be equivalent to the C++ build but has
not been tested thoroughly. See automata.py! And test_suite.py for compatibility
testing.

Here are benchmarks!

pure python with standard cpython 2.6.5
---------------------------------------
time python print_ca.py 30 10000 500 --python &>/dev/null
    user: 36.8sec
    
pure python with pypy (trunk [PyPy 1.6.0 with GCC 4.4.3])
---------------------------------------------------------
time pypy print_ca.py 30 10000 500 --python &>/dev/null
    user: 1.140sec (wow!)
    
c++ with cython bindings (equivalent behavior)
----------------------------------------------
time python print_ca.py 30 10000 500 &>/dev/null
    user: .290sec 
    
So the C++ is much much faster (~125x) than pure python. But only about 4x faster
than pypy. As it turns out, PyEvolve with pypy is at least that fast versus the
same algorithm with cpython, so we'll see which turns out to be overall faster!

To build
--------

The easiest thing to do is simply run "python setup.py build_ext". This will take
the .cpp file (that was automatically generated with cython) and build it into a
python extension that is wrapped around the automata C++ class. Requies the
package python-setuptools, python-dev, and cython to be installed

If you want to build by hand, just check out the makefile. There are two steps
needed to build the module.
1) run "cython --cplus pyAutomata.pyx". This will turn the .pyx file, which is
      the definitions for cython's interface, into a pyAutomata.cpp.
2) Run the necessary G++ commands to compile pyAutomata.cpp (along with the 
      files Automata.cpp and Automata.h which may be softlinked into automata-1.0.0/)
      into pyAutomata.so, a compiled python extension:

For me this looks like:
[blaine@macbook:~/Dropbox/src/PyAutomata Mon Aug 22]
7$ make
mkdir -p build/temp.linux-x86_64-2.6/
# Create some soft links just to make sure
ln -s automata-1.0.0/Automata.cpp Automata.cpp
ln -s automata-1.0.0/Automata.h Automata.h
# convert .pyx into .cpp file
cython --cplus pyAutomata.pyx
# The following was generated based on setup.py; python setup.py build_ext --inplace
gcc -O2 -pthread -fno-strict-aliasing -fwrapv -Wall -fPIC -I. -Iautomata-1.0.0/ -I/usr/include/python2.6 -c pyAutomata.cpp -o build/temp.linux-x86_64-2.6/pyAutomata.o
gcc -O2 -pthread -fno-strict-aliasing -fwrapv -Wall -fPIC -I. -Iautomata-1.0.0/ -I/usr/include/python2.6 -c automata-1.0.0//Automata.cpp -o build/temp.linux-x86_64-2.6//automata-1.0.0/Automata.o
g++ -lm -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions build/temp.linux-x86_64-2.6//pyAutomata.o build/temp.linux-x86_64-2.6//automata-1.0.0//Automata.o -lstdc++ -o pyAutomata.so

The cython step is not necessary unless you wish to modify pyAutomata.pyx


Previously here:
----------------

Some random thoughts and notes on cellular automata
00001000111000 is a pattern in rule 30 that causes finite state oscillatory pattern
    in other words, rule 30 is not random for all inputs, only for some obviously
    non-random inputs (like impulse 1).

About

A 1-D lightweight cellular automata framework built in C++ with python bindings.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published