|
| 1 | +## Conway's Game of Life (Fast) |
| 2 | +This example demonstrates a fast and efficient implementation of Conway's Game of Life using the [`PropertyLayer`](https://github.com/projectmesa/mesa/pull/1898) from the Mesa framework. |
| 3 | + |
| 4 | +### Overview |
| 5 | +Conway's [Game of Life](https://en.wikipedia.org/wiki/Conway%27s_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. |
| 6 | + |
| 7 | +#### Key features: |
| 8 | +- **No grid or agents:** This implementation uses the `PropertyLayer` to manage the state of cells, eliminating the need for traditional grids or agents. |
| 9 | +- **Fast:** By using 2D convolution to count neighbors, the model efficiently applies the rules of the Game of Life across the entire grid. |
| 10 | +- **Toroidal:** The grid wraps around at the edges, creating a seamless, continuous surface. |
| 11 | + |
| 12 | +#### Performance |
| 13 | +The model is benchmarked in https://github.com/projectmesa/mesa/pull/1898#issuecomment-1849000346 to be about 100x faster over a traditional implementation. |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +- Benchmark code: [benchmark_gol.zip](https://github.com/projectmesa/mesa/files/13628343/benchmark_gol.zip) |
| 18 | + |
| 19 | +### Getting Started |
| 20 | +#### Prerequisites |
| 21 | +- Python 3.9 or higher |
| 22 | +- Mesa 2.3 or higher |
| 23 | +- NumPy and SciPy |
| 24 | + |
| 25 | +#### Running the Model |
| 26 | +To run the model, open a new file or notebook and add: |
| 27 | + |
| 28 | +```Python |
| 29 | +from model import GameOfLifeModel |
| 30 | +model = GameOfLifeModel(width=10, height=10, alive_fraction=0.2) |
| 31 | +for i in range(10): |
| 32 | + model.step() |
| 33 | +``` |
| 34 | +Or to run visualized with Solara, run in your terminal: |
| 35 | + |
| 36 | +```bash |
| 37 | +solara run app.py |
| 38 | +``` |
| 39 | + |
| 40 | +### Understanding the Code |
| 41 | +- **Model initialization:** The grid is represented by a `PropertyLayer` where each cell is randomly initialized as alive or dead based on a given probability. |
| 42 | +- **`PropertyLayer`:** In the `cell_layer` (which is a `PropertyLayer`), each cell has either a value of 1 (alive) or 0 (dead). |
| 43 | +- **Step function:** Each simulation step calculates the number of alive neighbors for each cell and applies the Game of Life rules. |
| 44 | +- **Data collection:** The model tracks and reports the number of alive cells and the fraction of the grid that is alive. |
| 45 | + |
| 46 | +### Customization |
| 47 | +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. |
| 48 | + |
| 49 | +### Summary |
| 50 | +This example provides a fast approach to modeling cellular automata using Mesa's `PropertyLayer`. |
| 51 | + |
| 52 | +### Future work |
| 53 | +Add visualisation of the `PropertyLayer` in SolaraViz. See: |
| 54 | +- https://github.com/projectmesa/mesa/issues/2138 |
0 commit comments