Skip to content

Commit

Permalink
examples/gol: Add initial fraction alive, sldiers (#2489)
Browse files Browse the repository at this point in the history
- Add a initial_fraction_alive keyword argument for the how many cells are alive at the start
- Add sliders for width, height and the initial fraction alive.
  • Loading branch information
EwoutH committed Nov 11, 2024
1 parent fed87c6 commit 0d10efb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
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 @@ def post_process(ax):


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()

# 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

0 comments on commit 0d10efb

Please sign in to comment.