Langton's Ant is a simulation which has a simple set of rules, which can produce surprisingly complicated results. It is a great example of a Chaotic System, as is the case with most Cellular Automata.
This project includes:
- The core simulation engine
- A website which can run and render the simulation
- A specification, syntax and compiler for a simple language to express the rules of such a system
Clone the code and run:
npm i && npm start
To run the project locally, and check the Developer Guide to see how to work with the code.
Some interesting example simulations are below.
Langton's Ant is a trivial example of a Turmite. To allow different configurations to easily be conastructed and shared, I have defined a syntax for a 'program'. A program is simply the set of rules for the system.
The program syntax is designed to make it easy to express the rules of the system with plain text, in a readable format:
The simulation applies the rules of the matrix to a given state, producting a new state.
An interface is layered on top. It renders the state, runs the simulation and provides controls to configures parameters.
Before understanding how a program works, it is important to understand the transformation matrix.
The transformation matrix is the complete set of rules for a turmite or ant simulation. The universe looks like this:
An element of the transformation defines that when the ant is in a given ant state and on a tile with a given tile state, what ant direction change will be made, and what tile state change will occur on the tile the ant leaves. This is a three-tuple:
(Ant State Change, Ant Direction Change, Tile State Change)
For example:
(1, 90, 0): Ant State increases1, Ant turns90clockwise, Tile State increases0(0, -90, 1): Ant State increases0, Ant turns90counter-clockwise, Tile State increases1
The Transformation Matrix is the complete set of state transformations which are required to define a complete set of rules.
For example:
| | T: 0 | T: 1 |
|------|-----------|-----------|
| a: 0 | (1,-90,1) | (1,-90,0) |
| a: 1 | (0,90,1) | (0,0,1) |
In this matrix (which defines a Fibonacci Spiral Turmite) we see the transformations which are applied for every combination of ant state and tile state.
Directions can be specified in degrees (as above), or using L for left, R for right and U for U-turn (-90, 90 and 180 degrees respectively).
A program is just a representation of each element in the matrix. For example, the spiral matrix above can be written as:
(1,-90,1) (1,-90,0)
(0,90,1) (0,0,1)
One of the goals of this project is to facilitate the easy sharing of this matrix. Readability and compactness are important. The compiler which builds the matrix from the input follows the following rules:
- If the program only contains
LorRcharacters, it is expanded from shorthand, as described in the section on Shorthand Ant Transformation Matrices - Any semi-colon is converted into a newline (allowing a program to be written on a single line if needed
- All whitespace is eliminated, except the newline at the end of each row
- Commas are optional between tuples
- If the matrix is not rectangular, or there are an incorrect number of tuples, an error is thrown
The compiler itself can be used with the following code:
const { compiler } = require('langtonsant');
const input = `
(1, L, 1) (1, L, 1)
(1, R, 1) (0, 0, 0)
`;
const matrix = compiler(input);
console.log(matrix);The Langton's Ant transformation matrix is just a trivial form of a Turmite transformation matrix. An Ant is a Turmite which only has one state.
An ant program can be expressed using the full syntax above, or in a more compact form, composed just of Ls and Rs, e.g:
LLRL
Which is just shorthand for the following matrix:
| | T: 0 | T: 1 | T: 2 | T: 3 |
|------|-------------|-------------|-------------|-------------|
| a: 0 | (0, -90, 1) | (0, -90, 1) | (0, +90, 1) | (0, -90, 1) |
i.e. L L R L
Just clone the repo, then run:
npm install && npm start
To install dependencies and run the simulation in development mode.
The site is published to GitHub Pages at
dwmkerr.github.io/langtonsant. Every
push to master that passes lint and tests builds dist/ and deploys it; there
is nothing to run by hand. To check the production build locally:
make serve
The pipeline is a single GitHub Actions workflow,
main.yml:
- Every push and pull request lints and tests.
- A push to
masterthat passes deploys to GitHub Pages. - Pushing a semver tag (
v*) publishes to NPM (needs theNPM_TOKENsecret).
Bump the version with npm run release.
A set of parameters can be provided in the URL.
| Parameter | Usage |
|---|---|
p |
The program string, e.g. LLRL. |
Very useful information came from:









