From 4ff712c415774986b9758eb159fba48c682c9c9b Mon Sep 17 00:00:00 2001 From: dlovell Date: Mon, 19 May 2025 14:56:46 -0400 Subject: [PATCH 1/4] fix(examples): update for mesa>=3 --- .../{color_patches/server.py => app.py} | 27 ++++++++++--------- examples/color_patches/color_patches/model.py | 10 +++++-- examples/color_patches/requirements.txt | 3 ++- examples/color_patches/run.py | 3 --- 4 files changed, 24 insertions(+), 19 deletions(-) rename examples/color_patches/{color_patches/server.py => app.py} (74%) delete mode 100644 examples/color_patches/run.py diff --git a/examples/color_patches/color_patches/server.py b/examples/color_patches/app.py similarity index 74% rename from examples/color_patches/color_patches/server.py rename to examples/color_patches/app.py index 34d0d744..d2d30cb6 100644 --- a/examples/color_patches/color_patches/server.py +++ b/examples/color_patches/app.py @@ -4,10 +4,11 @@ """ # import webbrowser - -import mesa - -from .model import ColorPatches +from color_patches.model import ColorPatches +from mesa.visualization import ( + SolaraViz, + make_space_component, +) _COLORS = [ "Aqua", @@ -54,15 +55,15 @@ def color_patch_draw(cell): return portrayal -canvas_element = mesa.visualization.CanvasGrid( - color_patch_draw, grid_rows, grid_cols, canvas_width, canvas_height +space_component = make_space_component( + color_patch_draw, + draw_grid=False, ) - -server = mesa.visualization.ModularServer( - ColorPatches, - [canvas_element], - "Color Patches", - {"width": grid_rows, "height": grid_cols}, +model = ColorPatches() +page = SolaraViz( + model, + components=[space_component], + model_params={"width": grid_rows, "height": grid_cols}, + name="Color Patches", ) - # webbrowser.open('http://127.0.0.1:8521') # TODO: make this configurable diff --git a/examples/color_patches/color_patches/model.py b/examples/color_patches/color_patches/model.py index f779c37a..45201950 100644 --- a/examples/color_patches/color_patches/model.py +++ b/examples/color_patches/color_patches/model.py @@ -5,9 +5,15 @@ from collections import Counter import mesa +from mesa.discrete_space.cell_agent import ( + CellAgent, +) +from mesa.discrete_space.grid import ( + OrthogonalMooreGrid, +) -class ColorCell(mesa.experimental.cell_space.CellAgent): +class ColorCell(CellAgent): """ Represents a cell's opinion (visualized by a color) """ @@ -66,7 +72,7 @@ def __init__(self, width=20, height=20): The agents next state is first determined before updating the grid """ super().__init__() - self._grid = mesa.experimental.cell_space.OrthogonalMooreGrid( + self._grid = OrthogonalMooreGrid( (width, height), torus=False, random=self.random ) diff --git a/examples/color_patches/requirements.txt b/examples/color_patches/requirements.txt index ecd07eaf..ee1a5c22 100644 --- a/examples/color_patches/requirements.txt +++ b/examples/color_patches/requirements.txt @@ -1 +1,2 @@ -mesa~=2.0 \ No newline at end of file +mesa[viz]>=3.0 +networkx diff --git a/examples/color_patches/run.py b/examples/color_patches/run.py deleted file mode 100644 index 53775321..00000000 --- a/examples/color_patches/run.py +++ /dev/null @@ -1,3 +0,0 @@ -from color_patches.server import server - -server.launch(open_browser=True) From ad7bec03a7b719c41c198db410ff99ac00ee774d Mon Sep 17 00:00:00 2001 From: dlovell Date: Mon, 19 May 2025 15:00:51 -0400 Subject: [PATCH 2/4] fix: use .state, not .get_state() --- examples/color_patches/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/color_patches/app.py b/examples/color_patches/app.py index d2d30cb6..f2d9e172 100644 --- a/examples/color_patches/app.py +++ b/examples/color_patches/app.py @@ -51,7 +51,7 @@ def color_patch_draw(cell): portrayal = {"Shape": "rect", "w": 1, "h": 1, "Filled": "true", "Layer": 0} portrayal["x"] = cell.get_row() portrayal["y"] = cell.get_col() - portrayal["Color"] = _COLORS[cell.get_state()] + portrayal["Color"] = _COLORS[cell.state] return portrayal From b3253b4b9a9b2af34d9281f81e069a6af0e8672f Mon Sep 17 00:00:00 2001 From: dlovell Date: Mon, 19 May 2025 15:20:37 -0400 Subject: [PATCH 3/4] fix(color_patches): use correct key for color in portrayal --- examples/color_patches/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/color_patches/app.py b/examples/color_patches/app.py index f2d9e172..d7c6741b 100644 --- a/examples/color_patches/app.py +++ b/examples/color_patches/app.py @@ -51,7 +51,7 @@ def color_patch_draw(cell): portrayal = {"Shape": "rect", "w": 1, "h": 1, "Filled": "true", "Layer": 0} portrayal["x"] = cell.get_row() portrayal["y"] = cell.get_col() - portrayal["Color"] = _COLORS[cell.state] + portrayal["color"] = _COLORS[cell.state] return portrayal From 0e3f2c233153d3b8a3507dccef9e193300aa88bd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 20 May 2025 09:39:36 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- examples/color_patches/color_patches/model.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/color_patches/color_patches/model.py b/examples/color_patches/color_patches/model.py index 85ed40f0..45201950 100644 --- a/examples/color_patches/color_patches/model.py +++ b/examples/color_patches/color_patches/model.py @@ -12,6 +12,7 @@ OrthogonalMooreGrid, ) + class ColorCell(CellAgent): """ Represents a cell's opinion (visualized by a color)