Langton’s Ant, the rules


I°) Langton’s Ant, what is it ?!

Langton’s ant is a two-dimensional universal Turing machine with a very simple set of rules but complex emergent behavior. It was invented by Chris Langton in 1986 and runs on a square lattice of black and white cells.[1] The universality of Langton’s ant was proven in 2000.[2] The idea has been generalized in several different ways, such as turmites which add more colors and more states.

The ant moves on a two dimension grid, which is composed by two state. The start position of the ant can be random. The ant can only move at left, at right, at the bottom, and at the top. The rules are the following :

  • At a white square, turn 90° right, flip the color of the square, move forward one unit
  • At a black square, turn 90° left, flip the color of the square, move forward one unit
This image has an empty alt attribute; its file name is LangtonsAntAnimated.gif
Animation of first 200 steps of Langton’s Ant


II°) Behavior

Those rules extremely simple can lead to a complex global behavior. Three distinct modes of behavior are apparent, when starting on a completely white grid :

  • Simplicity. During the first few hundred moves it creates very simple patterns which are often symmetric.
  • Chaos. After a few hundred moves, a large, irregular pattern of black and white squares appears. The ant traces a pseudo-random path until around 10,000 steps.
  • Emergent order. Finally the ant starts building a recurrent “highway” pattern of 104 steps that repeats indefinitely.
This image has an empty alt attribute; its file name is langtons-ant-2-states-highway.png
Emergent order,ith the “highway” pattern

Finally, it is suggest, even though never demonstrated, that this “highway” pattern is an attractor of the Langton’s Ant. It is only known that the ant’s trajectory is always unbounded regardless of the initial configuration – this is known as the Cohen–Kung theorem.


III°) Code

// *****************************************************************************
//                                   PARAMETERS
// *****************************************************************************
// Grid parameters
int w, h;
int rows = 10; // rows size of one square from the grid
int cols = 10; // cols size of one square from the grid
int[][] grid;

int nbrCycle = 100;

// Ant motions parameters
int dir;
int x, y;
int ANT_UP = 0;
int ANT_RIGHT = 1;
int ANT_DOWN = 2;
int ANT_LEFT = 3;



// *****************************************************************************
//                                     SETUP
// *****************************************************************************
void setup() {
  size(960, 960);
  surface.setLocation(0,0);
  w = width/rows;
  h = height/cols;
  grid = new int[w][h];

  // Start position
  x = w/2;
  y = h/2;
  grid[x][y] = 1;
}



// *****************************************************************************
//                                     DRAW
// *****************************************************************************
void draw() {
  background(255);
  displayGridAndColor();

  // RULES
  for (int i = 0; i < nbrCycle; i++){
  int state = grid[x][y];
  if (state == 0) {
    turnRight();
    grid[x][y] = 1;
  } else {
    turnLeft();
    grid[x][y] = 0;
  }
  moveForward();}
}



// *****************************************************************************
//                                   METHODS
// *****************************************************************************

// ***********************
void displayGridAndColor() {
  stroke(0);
  strokeWeight(1);
  beginShape(QUAD);
  for (int i =0; i < w; i++) {
    for (int j =0; j < h; j++) {
      if (grid[i][j] == 1) {
        fill(255, 0, 0);
        vertex(i*rows, j*cols);
        vertex(i*rows, (j+1)*cols);
        vertex((i+1)*rows, (j+1)*cols);
        vertex((i+1)*rows, j*cols);
      }      
      if (grid[i][j] == 0) {
        fill(255);
        vertex(i*rows, j*cols);
        vertex(i*rows, (j+1)*cols);
        vertex((i+1)*rows, (j+1)*cols);
        vertex((i+1)*rows, j*cols);
      }
    }
  }
  endShape();
}


// ***********************
void moveForward() {
  if (dir == ANT_UP) {
    y--;
  }
  if (dir == ANT_RIGHT) {
    x++;
  }
  if (dir == ANT_DOWN) {
    y++;
  }
  if (dir == ANT_LEFT) {
    x--;
  }
  
  if (x > w-1) {x = 0;} else if (x < 0) {x = w-1;}
  if (y > h-1) {y = 0;} else if (y < 0) {y = h-1;}
}


// ***********************
void turnRight() {
  dir++;
  if (dir > ANT_LEFT) {
    dir = ANT_UP;
  }
}


// ***********************
void turnLeft() {
  dir--;
  if (dir < ANT_UP) {
    dir = ANT_LEFT;
  }
}


IV°) Results

Leave a comment

Design a site like this with WordPress.com
Get started
search previous next tag category expand menu location phone mail time cart zoom edit close