📚 The CoCalc Library - books, templates and other resources
License: OTHER
Game of Life
Code examples from Think Complexity, 2nd edition.
Copyright 2016 Allen Downey, MIT License
Game of Life entities
The following function creates a Life
object and sets the initial condition using strings of 0
and 1
characters.
A beehive is a stable entity, also called a "still life"
Here's what it looks like after one step:
A toad is an oscillator with period 2. Here's are its two configurations:
Here's what the toad looks like as an animation.
A glider is a spaceship that translates one unit down and to the right with period 4.
Here's an animation showing glider movement.
Exercise: If you start GoL from a random configuration, it usually runs chaotically for a while and then settles into stable patterns that include blinkers, blocks, and beehives, ships, boats, and loaves.
For a list of common "natually" occurring patterns, see Achim Flammenkamp, "Most seen natural occurring ash objects in Game of Life",
Start GoL in a random state and run it until it stabilizes (try 1000 steps). What stable patterns can you identify?
Hint: use np.random.randint
.
Methuselas
Most initial conditions run for a short time and reach a steady state. But some initial conditional run for a surprisingly long time; they are called Methuselahs.
The r-pentomino starts with only five live cells, but it runs for 1103 steps before stabilizing.
Here are the start and finish configurations.
And here's the animation that shows the steps.
Conway's conjecture
Most initial conditions run for a short time and reach a steady state. Some, like the r-pentomino, run for a long time before they reach steady state. Another example is rabbits, which starts with only nine cells and runs 17331 steps before reaching steady state.
To run my implementation of rabbits, open a terminal in ThinkComplexity2/code
and run
Patterns that take a long time to reach steady state are called Methuselahs
Patterns like these prompted Conway's conjecture, which asks whether there are any initial conditions where the number of live cells is unbounded.
Gosper's glider gun was the first entity to be discovered that produces an unbounded number of live cells, which refutes Conway's conjecture.
Here's the initial configuration:
And here's what it looks like running:
Puffer train
Another way to "refute" Conway's conjecture is a puffer train.
To see a puffer train run, open a terminal and run
Implementing Game of Life
As an example, I'll start with an array of random cells:
The following is a straightforward translation of the GoL rules using for
loops and array slicing.
Here's a smaller, faster version using cross correlation.
Using a kernel that gives a weight of 10 to the center cell, we can simplify the logic a little.
More importantly, the second version of the kernel makes it possible to use a look up table to get the next state, which is faster and even more concise.
Exercise: Many Game of Life patterns are available in portable file formats. For one source, see http://www.conwaylife.com/wiki/Main_Page.
Write a function to parse one of these formats and initialize the array.
Highlife
One variation of GoL, called "Highlife", has the same rules as GoL, plus one additional rule: a dead cell with 6 neighbors comes to life.
You can try out different rules by inheriting from Life
and changing the lookup table.
Exercise: Modify the table below to add the new rule.
One of the more interesting patterns in Highlife is the replicator, which has the following initial configuration.
Make a MyLife
object with n=100
and use add_cells
to put a replicator near the middle.
Make an animation with about 200 frames and see how it behaves.
Exercise:
If you generalize the Turing machine to two dimensions, or add a read-write head to a 2-D CA, the result is a cellular automaton called a Turmite. It is named after a termite because of the way the read-write head moves, but spelled wrong as an homage to Alan Turing.
The most famous Turmite is Langton's Ant, discovered by Chris Langton in 1986. See http://en.wikipedia.org/wiki/Langton_ant.
The ant is a read-write head with four states, which you can think of as facing north, south, east or west. The cells have two states, black and white.
The rules are simple. During each time step, the ant checks the color of the cell it is on. If black, the ant turns to the right, changes the cell to white, and moves forward one space. If the cell is white, the ant turns left, changes the cell to black, and moves forward.
Given a simple world, a simple set of rules, and only one moving part, you might expect to see simple behavior---but you should know better by now. Starting with all white cells, Langton's ant moves in a seemingly random pattern for more than 10 000 steps before it enters a cycle with a period of 104 steps. After each cycle, the ant is translated diagonally, so it leaves a trail called the "highway".
Write an implementation of Langton's Ant.