diff --git a/examples/conways_game_of_life_fast/app.py b/examples/conways_game_of_life_fast/app.py index 9a1281ac..4a077097 100644 --- a/examples/conways_game_of_life_fast/app.py +++ b/examples/conways_game_of_life_fast/app.py @@ -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, }, } @@ -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( diff --git a/examples/conways_game_of_life_fast/model.py b/examples/conways_game_of_life_fast/model.py index 40499b64..ff8b0efb 100644 --- a/examples/conways_game_of_life_fast/model.py +++ b/examples/conways_game_of_life_fast/model.py @@ -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 @@ -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 diff --git a/gis/urban_growth/app.py b/gis/urban_growth/app.py index 68a4bc97..337e15ef 100644 --- a/gis/urban_growth/app.py +++ b/gis/urban_growth/app.py @@ -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, ],