Skip to content
Open
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
23 changes: 20 additions & 3 deletions examples/conways_game_of_life_fast/app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
from mesa.visualization import SolaraViz, make_plot_component, make_space_component
from model import GameOfLifeModel


def cell_portrayal(agent):
if agent is None:
return

#
x, y = agent.pos
is_alive = agent.model.cell_layer.data[x, y]

return {
# If alive Black, If dead White
"color": "black" if is_alive else "white",
"size": 50, # 50 fills the cell completely
"marker": "s", # square shape
}


propertylayer_portrayal = {
"cell_layer": {
"color": "Black",
"alpha": 1,
"color": "Cyan",
"alpha": "cell_layer",
"colorbar": False,
},
}
Expand Down Expand Up @@ -37,8 +54,8 @@
}

gol = GameOfLifeModel()
layer_viz = make_space_component(agent_portrayal=cell_portrayal, draw_grid=False)

layer_viz = make_space_component(propertylayer_portrayal=propertylayer_portrayal)
TotalAlivePlot = make_plot_component("Cells alive")

page = SolaraViz(
Expand Down
21 changes: 16 additions & 5 deletions examples/conways_game_of_life_fast/model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
from mesa import Model
from mesa import Agent, Model
from mesa.datacollection import DataCollector
from mesa.space import PropertyLayer
from mesa.space import PropertyLayer, SingleGrid
from scipy.signal import convolve2d


Expand All @@ -10,9 +10,20 @@ class GameOfLifeModel(Model):
def __init__(self, width=10, height=10, alive_fraction=0.2):
super().__init__()
# Initialize the property layer for cell states
self.cell_layer = PropertyLayer("cells", width, height, False, dtype=bool)
# Randomly set cells to alive
self.cell_layer.data = np.random.choice([True, False], size=(width, height), p=[alive_fraction, 1 - alive_fraction])

self.grid = SingleGrid(width, height, torus=True)


self.cell_layer = PropertyLayer("cell_layer", width, height, False, dtype=bool)
self.cell_layer.data = np.random.choice(
[True, False],
size=(width, height),
p=[alive_fraction, 1 - alive_fraction]
)

for _, (x, y) in self.grid.coord_iter():
dummy = Agent(self)
self.grid.place_agent(dummy, (x, y))

# Metrics and datacollector
self.cells = width * height
Expand Down
7 changes: 5 additions & 2 deletions gis/urban_growth/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ def make_plot_urbanized(model):
}

model = UrbanGrowth()
map_component = make_geospace_component(cell_portrayal, zoom=12.1)

# explicitly pass components=
page = SolaraViz(
model,
[
make_geospace_component(cell_portrayal, zoom=12.1),
components=[
map_component,
make_plot_component(["Percentage Urbanized"]),
make_plot_urbanized,
],
Expand Down