Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fast PropertyLayer implementation of Game of Life #182

Merged
merged 8 commits into from
Aug 30, 2024

Conversation

EwoutH
Copy link
Member

@EwoutH EwoutH commented Aug 29, 2024

This pull request adds a fast implementation of Conway's Game of Life using the PropertyLayer. Unlike traditional approaches, this version doesn't rely on a grid or agents, making it simpler and more efficient (in this specific case).

Key features

  • The model uses a PropertyLayer to track cell states, removing the need for a grid or agents. In this layer, a cell value of 1 means being alive, and 0 means being dead.
  • Neighbor counts are calculated using 2D convolution, which speeds up the process of applying the Game of Life rules.
  • The model wraps the grid edges to create a continuous surface, so cells on the edges behave as if the grid is infinite.
  • The model tracks and records the number of living cells and the percentage of the grid that's alive over time.

Performance

As benchmarked in projectmesa/mesa#1898 (comment), this models is incredibly fast, about 100x over a traditional implementation.

runtime_comparison

Structure

This PR is structures in three commits:

  • 556afd8 adds the base model
  • bd854d8 adds a minimal Solara visualisation
  • d9ae175 adds metrics, a datacollector and visualizes these metrics in Solara
  • 5ab4720 adds a Readme
  • af7309c adds SciPy to the example test dependencies
  • 0a36938 allows a FutureWarning to stay a warning and not become an Error

Dependencies

This models needs SciPy for convolve2d, which is added to the test dependencies.

Future work

It would be nice if the PropertyLayer itself could also be visualized, so that you can see Game of Life patterns. That needs support in Mesa however (or a custom space_drawer implemented here).

Closes: #166

@EwoutH EwoutH force-pushed the GoL_propertylayer branch 4 times, most recently from 0e026ad to 0a36938 Compare August 29, 2024 14:18
@EwoutH EwoutH marked this pull request as ready for review August 29, 2024 14:27
@EwoutH
Copy link
Member Author

EwoutH commented Aug 29, 2024

@projectmesa/maintainers this new example model is ready for review! It's the first one that uses the PropertyLayer as added in projectmesa/mesa#1898.

If I can find the time I will also see if I can update another model - maybe the grass in WolfSheep.

@wang-boyu
Copy link
Member

Do you mind updating the top level README file to include this example? Perhaps with a new section for PropertyLayer examples?

@EwoutH
Copy link
Member Author

EwoutH commented Aug 29, 2024

Yes of course, done!

self.alive_count = 0
self.alive_fraction = 0
self.datacollector = DataCollector(
model_reporters={"Cells alive": "alive_count",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit, but this doesn't seem properly formatted.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already approved, so it is fine, not a big issue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find ruff fine for the Mesa code base, but for examples I find it quite limiting at times.

@EwoutH
Copy link
Member Author

EwoutH commented Aug 30, 2024

I can't figure out why I can't fast-forward merge, so rebase it is.

A special implementation of Conway's Game of Life, using only the PropertyLayer, without needing a Grid or even any Agents.
Add minimal Solara visualisation, to control model parameters.
- Add two metrics to the model
- Collect those with the datacollector
- Plot them as measures in SolaraViz
The fast GoL model needs it.
Otherwise we can't use this experimental feature.
`steps` is now increased automatically, it's not needed anymore!
@EwoutH EwoutH merged commit fc18ece into projectmesa:main Aug 30, 2024
1 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimized implementation of Game of Life based on PropertyLayer
3 participants