Skip to content

Commit

Permalink
GoL_fast: Visualize the Game of Life cells using the PropertyLayer (#214
Browse files Browse the repository at this point in the history
)

Tested with latest Mesa 3.0.0b0
  • Loading branch information
EwoutH authored Oct 5, 2024
1 parent 84542d4 commit 1a5e104
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions examples/conways_game_of_life_fast/Readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Conway's Game of Life (Fast)
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.

![GoL_fast_screenshot.png](GoL_fast_screenshot.png)

### Overview
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.

Expand All @@ -18,8 +20,8 @@ The model is benchmarked in https://github.com/projectmesa/mesa/pull/1898#issuec

### Getting Started
#### Prerequisites
- Python 3.9 or higher
- Mesa 2.3 or higher
- Python 3.10 or higher
- Mesa 2.3 or higher (3.0.0b0 or higher for the visualisation)
- NumPy and SciPy

#### Running the Model
Expand Down
34 changes: 24 additions & 10 deletions examples/conways_game_of_life_fast/app.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
from mesa.visualization import SolaraViz, make_plot_measure
from mesa.visualization import SolaraViz, make_plot_measure, make_space_matplotlib
from model import GameOfLifeModel

propertylayer_portrayal = {
"cell_layer": {
"color": "Black",
"alpha": 1,
"colorbar": False,
},
}

model_params = {
"width": {
"type": "SliderInt",
"value": 10,
"value": 30,
"label": "Width",
"min": 5,
"max": 25,
"max": 60,
"step": 1,
},
"height": {
"type": "SliderInt",
"value": 10,
"value": 30,
"label": "Height",
"min": 5,
"max": 25,
"max": 60,
"step": 1,
},
"alive_fraction": {
"type": "SliderFloat",
"value": 0.2,
"label": "Cells alive",
"min": 0,
"max": 1,
"step": 0.01,
},
}

gol = GameOfLifeModel(10, 10)
gol = GameOfLifeModel()

layer_viz = make_space_matplotlib(propertylayer_portrayal=propertylayer_portrayal)
TotalAlivePlot = make_plot_measure("Cells alive")
FractionAlivePlot = make_plot_measure("Fraction alive")


page = SolaraViz(
gol,
components=[TotalAlivePlot, FractionAlivePlot],
components=[layer_viz, TotalAlivePlot],
model_params=model_params,
name="Game of Life Model",
)
page # noqa

0 comments on commit 1a5e104

Please sign in to comment.