Skip to content

Commit 57f0880

Browse files
dlovelltpike3pre-commit-ci[bot]
authored
fix(examples): color patches (#269)
* fix(examples): update for mesa>=3 * fix: use .state, not .get_state() * fix(color_patches): use correct key for color in portrayal * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Tom Pike <tpike3@gmu.edu> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent b23f89c commit 57f0880

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

examples/color_patches/color_patches/server.py renamed to examples/color_patches/app.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
"""
55

66
# import webbrowser
7-
8-
import mesa
9-
10-
from .model import ColorPatches
7+
from color_patches.model import ColorPatches
8+
from mesa.visualization import (
9+
SolaraViz,
10+
make_space_component,
11+
)
1112

1213
_COLORS = [
1314
"Aqua",
@@ -50,19 +51,19 @@ def color_patch_draw(cell):
5051
portrayal = {"Shape": "rect", "w": 1, "h": 1, "Filled": "true", "Layer": 0}
5152
portrayal["x"] = cell.get_row()
5253
portrayal["y"] = cell.get_col()
53-
portrayal["Color"] = _COLORS[cell.get_state()]
54+
portrayal["color"] = _COLORS[cell.state]
5455
return portrayal
5556

5657

57-
canvas_element = mesa.visualization.CanvasGrid(
58-
color_patch_draw, grid_rows, grid_cols, canvas_width, canvas_height
58+
space_component = make_space_component(
59+
color_patch_draw,
60+
draw_grid=False,
5961
)
60-
61-
server = mesa.visualization.ModularServer(
62-
ColorPatches,
63-
[canvas_element],
64-
"Color Patches",
65-
{"width": grid_rows, "height": grid_cols},
62+
model = ColorPatches()
63+
page = SolaraViz(
64+
model,
65+
components=[space_component],
66+
model_params={"width": grid_rows, "height": grid_cols},
67+
name="Color Patches",
6668
)
67-
6869
# webbrowser.open('http://127.0.0.1:8521') # TODO: make this configurable

examples/color_patches/color_patches/model.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55
from collections import Counter
66

77
import mesa
8+
from mesa.discrete_space.cell_agent import (
9+
CellAgent,
10+
)
11+
from mesa.discrete_space.grid import (
12+
OrthogonalMooreGrid,
13+
)
814

915

10-
class ColorCell(mesa.discrete_space.cell_agent.CellAgent):
16+
class ColorCell(CellAgent):
1117
"""
1218
Represents a cell's opinion (visualized by a color)
1319
"""
@@ -66,7 +72,7 @@ def __init__(self, width=20, height=20):
6672
The agents next state is first determined before updating the grid
6773
"""
6874
super().__init__()
69-
self._grid = mesa.experimental.cell_space.OrthogonalMooreGrid(
75+
self._grid = OrthogonalMooreGrid(
7076
(width, height), torus=False, random=self.random
7177
)
7278

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
mesa~=2.0
1+
mesa[viz]>=3.0
2+
networkx

examples/color_patches/run.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)