This example demonstrates a fast and efficient implementation of Conway's Game of Life using the PropertyLayer
from the Mesa framework.
Conway's Game of Life is a classic cellular automaton where each cell on a grid can either be alive or dead. The state of each cell changes over time based on a set of simple rules that depend on the number of alive neighbors.
- No grid or agents: This implementation uses the
PropertyLayer
to manage the state of cells, eliminating the need for traditional grids or agents. - Fast: By using 2D convolution to count neighbors, the model efficiently applies the rules of the Game of Life across the entire grid.
- Toroidal: The grid wraps around at the edges, creating a seamless, continuous surface.
The model is benchmarked in projectmesa/mesa#1898 (comment) to be about 100x faster over a traditional implementation.
- Benchmark code: benchmark_gol.zip
- Python 3.10 or higher
- Mesa 2.3 or higher (3.0.0b0 or higher for the visualisation)
- NumPy and SciPy
To run the model, open a new file or notebook and add:
from model import GameOfLifeModel
model = GameOfLifeModel(width=10, height=10, alive_fraction=0.2)
for i in range(10):
model.step()
Or to run visualized with Solara, run in your terminal:
solara run app.py
- Model initialization: The grid is represented by a
PropertyLayer
where each cell is randomly initialized as alive or dead based on a given probability. PropertyLayer
: In thecell_layer
(which is aPropertyLayer
), each cell has either a value of 1 (alive) or 0 (dead).- Step function: Each simulation step calculates the number of alive neighbors for each cell and applies the Game of Life rules.
- Data collection: The model tracks and reports the number of alive cells and the fraction of the grid that is alive.
You can easily modify the model parameters such as grid size and initial alive fraction to explore different scenarios. You can also add more metrics or visualisations.
This example provides a fast approach to modeling cellular automata using Mesa's PropertyLayer
.
Add visualisation of the PropertyLayer
in SolaraViz. See: