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

examples/gol: Add initial fraction alive, add sliders to viz #2489

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions mesa/examples/basic/conways_game_of_life/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,34 @@


model_params = {
"width": 50,
"height": 50,
"width": {
"type": "SliderInt",
"value": 50,
"label": "Width",
"min": 5,
"max": 60,
"step": 1,
},
"height": {
"type": "SliderInt",
"value": 50,
"label": "Height",
"min": 5,
"max": 60,
"step": 1,
},
"initial_fraction_alive": {
"type": "SliderFloat",
"value": 0.2,
"label": "Cells initially alive",
"min": 0,
"max": 1,
"step": 0.01,
},
}

# Create initial model instance
model1 = ConwaysGameOfLife(50, 50)
model1 = ConwaysGameOfLife()

Check warning on line 50 in mesa/examples/basic/conways_game_of_life/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/basic/conways_game_of_life/app.py#L50

Added line #L50 was not covered by tests

# Create visualization elements. The visualization elements are solara components
# that receive the model instance as a "prop" and display it in a certain way.
Expand Down
4 changes: 2 additions & 2 deletions mesa/examples/basic/conways_game_of_life/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class ConwaysGameOfLife(Model):
"""Represents the 2-dimensional array of cells in Conway's Game of Life."""

def __init__(self, width=50, height=50, seed=None):
def __init__(self, width=50, height=50, initial_fraction_alive=0.2, seed=None):
"""Create a new playing area of (width, height) cells."""
super().__init__(seed=seed)
# Use a simple grid, where edges wrap around.
Expand All @@ -16,7 +16,7 @@ def __init__(self, width=50, height=50, seed=None):
# ALIVE and some to DEAD.
for _contents, (x, y) in self.grid.coord_iter():
cell = Cell((x, y), self)
if self.random.random() < 0.1:
if self.random.random() < initial_fraction_alive:
cell.state = cell.ALIVE
self.grid.place_agent(cell, (x, y))

Expand Down
Loading