Code / Graphics

Langton’s Ant in Python

I started a new repo on github that computes the Langton’s Ant cellular automata in python. For more information on Langton’s Ant, see its wikipedia page.

Using classes in OOP is relatively unfamiliar to me, since most of the programing I’ve done in the last several years has been analysis scripts to extract trends and statistics out of DFT simulations. I’m slowly unlearning being a physicist-programmer and trying to learn programming ‘the right way.’ That being said, I’m still using plain ol’ trusty Matplotlib to render the ants and worlds in this code.

The Langton’s ant program can use any arbitrary rule set in the form of a string like ‘rllr’ which determines the ant’s turning directive as a function of the present board state. In addition to performing the rules on a flat 2D grid with edges where the CA will stop if the ant falls of an edge, the program can also use a torus or Klein bottle topology. Making gifs is also supported, but rendering them takes a very long time. Below are some ouput .png files. The first is 11000 steps of the ‘rl’ directive:


Here’s 60000 steps of the ‘rllr’ directive, where this figure helps to demonstrate that a cyclic list of doubled directive characters will produce a high degree of symmetry:


The ant on a Klein bottle is an interesting case. A Klein bottle has a torus boundary condition joining two opposing edges and Möbius boundary conditions joining the remaining two, in which there is a ‘twist’ in the boundary that maps the top of the world to the bottom. I’d love to investigate the effect of topological closure on ant patterns further; it seems that that area is not well-studied. The ant’s behavior at the boundary in the ‘rl’ rule populates the entire Klein bottle boundary region when it is discovered, like in this gif:


I used this program for my Recurse center interview; the pair programming exercise consisted of adding animation to the program from a less-finished state. I was accepted, and that’s a topic for another post 😉

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s