From 8f552a091e84c2a9fabd66575e65ab0b0be82ab3 Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven Date: Wed, 23 Oct 2024 21:45:05 +0200 Subject: [PATCH 1/4] examples: Restructure EpsteinCivilViolence - Flatten structure - Update the viz and add new discrete grid portrayal - Remove unneeded viz - Add to init --- mesa/examples/__init__.py | 3 + .../advanced/epstein_civil_violence/Readme.md | 5 +- .../{epstein_civil_violence => }/__init__.py | 0 .../agent.py => agents.py} | 0 .../advanced/epstein_civil_violence/app.py | 71 ++++++++++++++++ .../epstein_civil_violence/portrayal.py | 33 -------- .../epstein_civil_violence/server.py | 81 ------------------- .../{epstein_civil_violence => }/model.py | 9 +-- .../epstein_civil_violence/requirements.txt | 3 - .../advanced/epstein_civil_violence/run.py | 3 - 10 files changed, 81 insertions(+), 127 deletions(-) rename mesa/examples/advanced/epstein_civil_violence/{epstein_civil_violence => }/__init__.py (100%) rename mesa/examples/advanced/epstein_civil_violence/{epstein_civil_violence/agent.py => agents.py} (100%) create mode 100644 mesa/examples/advanced/epstein_civil_violence/app.py delete mode 100644 mesa/examples/advanced/epstein_civil_violence/epstein_civil_violence/portrayal.py delete mode 100644 mesa/examples/advanced/epstein_civil_violence/epstein_civil_violence/server.py rename mesa/examples/advanced/epstein_civil_violence/{epstein_civil_violence => }/model.py (97%) delete mode 100644 mesa/examples/advanced/epstein_civil_violence/requirements.txt delete mode 100644 mesa/examples/advanced/epstein_civil_violence/run.py diff --git a/mesa/examples/__init__.py b/mesa/examples/__init__.py index b12b5261a4a..6d87b7ed771 100644 --- a/mesa/examples/__init__.py +++ b/mesa/examples/__init__.py @@ -4,10 +4,13 @@ from mesa.examples.basic.schelling.model import Schelling from mesa.examples.basic.virus_on_network.model import VirusOnNetwork +from mesa.examples.advanced.epstein_civil_violence.model import EpsteinCivilViolence + __all__ = [ "BoidFlockers", "BoltzmannWealthModel", "ConwaysGameOfLife", "Schelling", "VirusOnNetwork", + "EpsteinCivilViolence", ] diff --git a/mesa/examples/advanced/epstein_civil_violence/Readme.md b/mesa/examples/advanced/epstein_civil_violence/Readme.md index 2e715b33b99..84ac40ac6aa 100644 --- a/mesa/examples/advanced/epstein_civil_violence/Readme.md +++ b/mesa/examples/advanced/epstein_civil_violence/Readme.md @@ -18,8 +18,9 @@ Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and p ## Files -* ``EpsteinCivilViolence.py``: Core model and agent code. -* ``EpsteinCivilViolenceServer.py``: Sets up the interactive visualization. +* ``model.py``: Core model code. +* ``agent.py``: Agent classes. +* ``app.py``: Sets up the interactive visualization. * ``Epstein Civil Violence.ipynb``: Jupyter notebook conducting some preliminary analysis of the model. ## Further Reading diff --git a/mesa/examples/advanced/epstein_civil_violence/epstein_civil_violence/__init__.py b/mesa/examples/advanced/epstein_civil_violence/__init__.py similarity index 100% rename from mesa/examples/advanced/epstein_civil_violence/epstein_civil_violence/__init__.py rename to mesa/examples/advanced/epstein_civil_violence/__init__.py diff --git a/mesa/examples/advanced/epstein_civil_violence/epstein_civil_violence/agent.py b/mesa/examples/advanced/epstein_civil_violence/agents.py similarity index 100% rename from mesa/examples/advanced/epstein_civil_violence/epstein_civil_violence/agent.py rename to mesa/examples/advanced/epstein_civil_violence/agents.py diff --git a/mesa/examples/advanced/epstein_civil_violence/app.py b/mesa/examples/advanced/epstein_civil_violence/app.py new file mode 100644 index 00000000000..fe36ca78d20 --- /dev/null +++ b/mesa/examples/advanced/epstein_civil_violence/app.py @@ -0,0 +1,71 @@ +from mesa.visualization import SolaraViz, Slider, make_space_matplotlib, make_plot_measure + +from agents import Citizen, Cop +from model import EpsteinCivilViolence + +COP_COLOR = "#000000" +AGENT_QUIET_COLOR = "#648FFF" +AGENT_REBEL_COLOR = "#FE6100" +JAIL_COLOR = "#808080" +JAIL_SHAPE = "rect" + + +def citizen_cop_portrayal(agent): + if agent is None: + return + + portrayal = { + "size": 25, + # "shape": "circle", + "x": agent.cell.coordinate[0], + "y": agent.cell.coordinate[1], + # "filled": True, + } + + if isinstance(agent, Citizen): + color = AGENT_QUIET_COLOR if agent.condition == "Quiescent" else AGENT_REBEL_COLOR + color = JAIL_COLOR if agent.jail_sentence else color + shape = JAIL_SHAPE if agent.jail_sentence else "circle" + portrayal["color"] = color + portrayal["shape"] = shape + if shape == "rect": + portrayal["w"] = 0.9 + portrayal["h"] = 0.9 + else: + portrayal["r"] = 0.5 + portrayal["filled"] = False + portrayal["layer"] = 0 + + elif isinstance(agent, Cop): + portrayal["color"] = COP_COLOR + portrayal["r"] = 0.9 + portrayal["layer"] = 1 + + return portrayal + + +model_params = { + "height": 40, + "width": 40, + "citizen_density": Slider("Initial Agent Density", 0.7, 0.0, 0.9, 0.1), + "cop_density": Slider("Initial Cop Density", 0.04, 0.0, 0.1, 0.01), + "citizen_vision": Slider("Citizen Vision", 7, 1, 10, 1), + "cop_vision": Slider("Cop Vision", 7, 1, 10, 1), + "legitimacy": Slider("Government Legitimacy", 0.82, 0.0, 1, 0.01), + "max_jail_term": Slider("Max Jail Term", 30, 0, 50, 1), +} + +space_component = make_space_matplotlib(citizen_cop_portrayal) +chart_component = make_plot_measure(["Quiescent", "Active", "Jailed"]) + +epstein_model = EpsteinCivilViolence() + +page = SolaraViz( + epstein_model, + components=[ + space_component, + chart_component], + model_params=model_params, + name="Epstein Civil Violence", +) +page # noqa diff --git a/mesa/examples/advanced/epstein_civil_violence/epstein_civil_violence/portrayal.py b/mesa/examples/advanced/epstein_civil_violence/epstein_civil_violence/portrayal.py deleted file mode 100644 index 80134adcc79..00000000000 --- a/mesa/examples/advanced/epstein_civil_violence/epstein_civil_violence/portrayal.py +++ /dev/null @@ -1,33 +0,0 @@ -from .agent import Citizen, Cop - -COP_COLOR = "#000000" -AGENT_QUIET_COLOR = "#0066CC" -AGENT_REBEL_COLOR = "#CC0000" -JAIL_COLOR = "#757575" - - -def citizen_cop_portrayal(agent): - if agent is None: - return - - portrayal = { - "Shape": "circle", - "x": agent.pos[0], - "y": agent.pos[1], - "Filled": "true", - } - - if isinstance(agent, Citizen): - color = ( - AGENT_QUIET_COLOR if agent.condition == "Quiescent" else AGENT_REBEL_COLOR - ) - color = JAIL_COLOR if agent.jail_sentence else color - portrayal["Color"] = color - portrayal["r"] = 0.8 - portrayal["Layer"] = 0 - - elif isinstance(agent, Cop): - portrayal["Color"] = COP_COLOR - portrayal["r"] = 0.5 - portrayal["Layer"] = 1 - return portrayal diff --git a/mesa/examples/advanced/epstein_civil_violence/epstein_civil_violence/server.py b/mesa/examples/advanced/epstein_civil_violence/epstein_civil_violence/server.py deleted file mode 100644 index 560b94e5468..00000000000 --- a/mesa/examples/advanced/epstein_civil_violence/epstein_civil_violence/server.py +++ /dev/null @@ -1,81 +0,0 @@ -import mesa - -from .agent import Citizen, Cop -from .model import EpsteinCivilViolence - -COP_COLOR = "#000000" -AGENT_QUIET_COLOR = "#648FFF" -AGENT_REBEL_COLOR = "#FE6100" -JAIL_COLOR = "#808080" -JAIL_SHAPE = "rect" - - -def citizen_cop_portrayal(agent): - if agent is None: - return - - portrayal = { - "Shape": "circle", - "x": agent.pos[0], - "y": agent.pos[1], - "Filled": "true", - } - - if type(agent) is Citizen: - color = ( - AGENT_QUIET_COLOR if agent.condition == "Quiescent" else AGENT_REBEL_COLOR - ) - color = JAIL_COLOR if agent.jail_sentence else color - shape = JAIL_SHAPE if agent.jail_sentence else "circle" - portrayal["Color"] = color - portrayal["Shape"] = shape - if shape == "rect": - portrayal["w"] = 0.9 - portrayal["h"] = 0.9 - else: - portrayal["r"] = 0.5 - portrayal["Filled"] = "false" - portrayal["Layer"] = 0 - - elif type(agent) is Cop: - portrayal["Color"] = COP_COLOR - portrayal["r"] = 0.9 - portrayal["Layer"] = 1 - - return portrayal - - -model_params = { - "height": 40, - "width": 40, - "citizen_density": mesa.visualization.Slider( - "Initial Agent Density", 0.7, 0.0, 0.9, 0.1 - ), - "cop_density": mesa.visualization.Slider( - "Initial Cop Density", 0.04, 0.0, 0.1, 0.01 - ), - "citizen_vision": mesa.visualization.Slider("Citizen Vision", 7, 1, 10, 1), - "cop_vision": mesa.visualization.Slider("Cop Vision", 7, 1, 10, 1), - "legitimacy": mesa.visualization.Slider( - "Government Legitimacy", 0.82, 0.0, 1, 0.01 - ), - "max_jail_term": mesa.visualization.Slider("Max Jail Term", 30, 0, 50, 1), -} -canvas_element = mesa.visualization.CanvasGrid(citizen_cop_portrayal, 40, 40, 480, 480) -chart = mesa.visualization.ChartModule( - [ - {"Label": "Quiescent", "Color": "#648FFF"}, - {"Label": "Active", "Color": "#FE6100"}, - {"Label": "Jailed", "Color": "#808080"}, - ], - data_collector_name="datacollector", -) -server = mesa.visualization.ModularServer( - EpsteinCivilViolence, - [ - canvas_element, - chart, - ], - "Epstein Civil Violence", - model_params, -) diff --git a/mesa/examples/advanced/epstein_civil_violence/epstein_civil_violence/model.py b/mesa/examples/advanced/epstein_civil_violence/model.py similarity index 97% rename from mesa/examples/advanced/epstein_civil_violence/epstein_civil_violence/model.py rename to mesa/examples/advanced/epstein_civil_violence/model.py index 8bf06bf1540..b54441a9c48 100644 --- a/mesa/examples/advanced/epstein_civil_violence/epstein_civil_violence/model.py +++ b/mesa/examples/advanced/epstein_civil_violence/model.py @@ -1,6 +1,6 @@ import mesa -from .agent import Citizen, Cop +from agents import Citizen, Cop class EpsteinCivilViolence(mesa.Model): @@ -43,8 +43,9 @@ def __init__( arrest_prob_constant=2.3, movement=True, max_iters=1000, + seed=None ): - super().__init__() + super().__init__(seed=seed) self.width = width self.height = height self.citizen_density = citizen_density @@ -59,9 +60,7 @@ def __init__( self.max_iters = max_iters self.iteration = 0 - self.grid = mesa.experimental.cell_space.OrthogonalMooreGrid( - (width, height), capacity=1, torus=True - ) + self.grid = mesa.experimental.cell_space.OrthogonalMooreGrid((width, height), capacity=1, torus=True, random=self.random) model_reporters = { "Quiescent": lambda m: self.count_type_citizens(m, "Quiescent"), diff --git a/mesa/examples/advanced/epstein_civil_violence/requirements.txt b/mesa/examples/advanced/epstein_civil_violence/requirements.txt deleted file mode 100644 index da2b9972efd..00000000000 --- a/mesa/examples/advanced/epstein_civil_violence/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -jupyter -matplotlib -mesa~=2.0 diff --git a/mesa/examples/advanced/epstein_civil_violence/run.py b/mesa/examples/advanced/epstein_civil_violence/run.py deleted file mode 100644 index a4b62c855d8..00000000000 --- a/mesa/examples/advanced/epstein_civil_violence/run.py +++ /dev/null @@ -1,3 +0,0 @@ -from epstein_civil_violence.server import server - -server.launch(open_browser=True) From cf9c2330e35a1c9de977d24ee38796aadd0efae3 Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven Date: Wed, 23 Oct 2024 21:58:34 +0200 Subject: [PATCH 2/4] examples: Restructure PdGrid - Flatten structure - Update the viz and add new discrete grid portrayal - Remove unneeded viz - Add to init --- mesa/examples/__init__.py | 2 + .../pd_grid/{pd_grid => }/__init__.py | 0 .../pd_grid/{pd_grid/agent.py => agents.py} | 0 mesa/examples/advanced/pd_grid/app.py | 64 +++++++++++++++++++ .../advanced/pd_grid/{pd_grid => }/model.py | 2 +- .../advanced/pd_grid/pd_grid/portrayal.py | 19 ------ .../advanced/pd_grid/pd_grid/server.py | 21 ------ mesa/examples/advanced/pd_grid/readme.md | 7 +- .../advanced/pd_grid/requirements.txt | 3 - mesa/examples/advanced/pd_grid/run.py | 3 - 10 files changed, 71 insertions(+), 50 deletions(-) rename mesa/examples/advanced/pd_grid/{pd_grid => }/__init__.py (100%) rename mesa/examples/advanced/pd_grid/{pd_grid/agent.py => agents.py} (100%) create mode 100644 mesa/examples/advanced/pd_grid/app.py rename mesa/examples/advanced/pd_grid/{pd_grid => }/model.py (98%) delete mode 100644 mesa/examples/advanced/pd_grid/pd_grid/portrayal.py delete mode 100644 mesa/examples/advanced/pd_grid/pd_grid/server.py delete mode 100644 mesa/examples/advanced/pd_grid/requirements.txt delete mode 100644 mesa/examples/advanced/pd_grid/run.py diff --git a/mesa/examples/__init__.py b/mesa/examples/__init__.py index 6d87b7ed771..cfec8b4c9a1 100644 --- a/mesa/examples/__init__.py +++ b/mesa/examples/__init__.py @@ -5,6 +5,7 @@ from mesa.examples.basic.virus_on_network.model import VirusOnNetwork from mesa.examples.advanced.epstein_civil_violence.model import EpsteinCivilViolence +from mesa.examples.advanced.pd_grid.model import PdGrid __all__ = [ "BoidFlockers", @@ -13,4 +14,5 @@ "Schelling", "VirusOnNetwork", "EpsteinCivilViolence", + "PdGrid", ] diff --git a/mesa/examples/advanced/pd_grid/pd_grid/__init__.py b/mesa/examples/advanced/pd_grid/__init__.py similarity index 100% rename from mesa/examples/advanced/pd_grid/pd_grid/__init__.py rename to mesa/examples/advanced/pd_grid/__init__.py diff --git a/mesa/examples/advanced/pd_grid/pd_grid/agent.py b/mesa/examples/advanced/pd_grid/agents.py similarity index 100% rename from mesa/examples/advanced/pd_grid/pd_grid/agent.py rename to mesa/examples/advanced/pd_grid/agents.py diff --git a/mesa/examples/advanced/pd_grid/app.py b/mesa/examples/advanced/pd_grid/app.py new file mode 100644 index 00000000000..b0c2f428916 --- /dev/null +++ b/mesa/examples/advanced/pd_grid/app.py @@ -0,0 +1,64 @@ +""" +Solara-based visualization for the Spatial Prisoner's Dilemma Model. +""" + +import solara +from mesa.visualization import SolaraViz, make_space_matplotlib, make_plot_measure +from mesa.visualization.UserParam import Slider +from model import PdGrid + + +def pd_agent_portrayal(agent): + """ + Portrayal function for rendering PD agents in the visualization. + """ + return { + "color": "blue" if agent.move == "C" else "red", + "shape": "s", # square marker + "size": 25, + } + + +# Model parameters +model_params = { + "width": Slider( + "Grid Width", + value=50, + min=10, + max=100, + step=1 + ), + "height": Slider( + "Grid Height", + value=50, + min=10, + max=100, + step=1 + ), + "activation_order": { + "type": "Select", + "value": "Random", + "values": PdGrid.activation_regimes, + "label": "Activation Regime" + } +} + + +# Create grid visualization component using Altair +grid_viz = make_space_matplotlib(agent_portrayal=pd_agent_portrayal) + +# Create plot for tracking cooperating agents over time +plot_component = make_plot_measure("Cooperating_Agents") + +# Initialize model +initial_model = PdGrid() + +# Create visualization with all components +page = SolaraViz( + model=initial_model, + components=[grid_viz, plot_component], + model_params=model_params, + name="Spatial Prisoner's Dilemma", +) + +page diff --git a/mesa/examples/advanced/pd_grid/pd_grid/model.py b/mesa/examples/advanced/pd_grid/model.py similarity index 98% rename from mesa/examples/advanced/pd_grid/pd_grid/model.py rename to mesa/examples/advanced/pd_grid/model.py index 38ef5f5b945..bd57281a9bb 100644 --- a/mesa/examples/advanced/pd_grid/pd_grid/model.py +++ b/mesa/examples/advanced/pd_grid/model.py @@ -1,7 +1,7 @@ import mesa from mesa.experimental.cell_space import OrthogonalMooreGrid -from .agent import PDAgent +from agents import PDAgent class PdGrid(mesa.Model): diff --git a/mesa/examples/advanced/pd_grid/pd_grid/portrayal.py b/mesa/examples/advanced/pd_grid/pd_grid/portrayal.py deleted file mode 100644 index a7df44a439f..00000000000 --- a/mesa/examples/advanced/pd_grid/pd_grid/portrayal.py +++ /dev/null @@ -1,19 +0,0 @@ -def portrayPDAgent(agent): - """ - This function is registered with the visualization server to be called - each tick to indicate how to draw the agent in its current state. - :param agent: the agent in the simulation - :return: the portrayal dictionary - """ - if agent is None: - raise AssertionError - return { - "Shape": "rect", - "w": 1, - "h": 1, - "Filled": "true", - "Layer": 0, - "x": agent.pos[0], - "y": agent.pos[1], - "Color": "blue" if agent.isCooroperating else "red", - } diff --git a/mesa/examples/advanced/pd_grid/pd_grid/server.py b/mesa/examples/advanced/pd_grid/pd_grid/server.py deleted file mode 100644 index 57785acccac..00000000000 --- a/mesa/examples/advanced/pd_grid/pd_grid/server.py +++ /dev/null @@ -1,21 +0,0 @@ -import mesa - -from .model import PdGrid -from .portrayal import portrayPDAgent - -# Make a world that is 50x50, on a 500x500 display. -canvas_element = mesa.visualization.CanvasGrid(portrayPDAgent, 50, 50, 500, 500) - -model_params = { - "height": 50, - "width": 50, - "activation_order": mesa.visualization.Choice( - "Activation regime", - value="Random", - choices=PdGrid.activation_regimes, - ), -} - -server = mesa.visualization.ModularServer( - PdGrid, [canvas_element], "Prisoner's Dilemma", model_params -) diff --git a/mesa/examples/advanced/pd_grid/readme.md b/mesa/examples/advanced/pd_grid/readme.md index 51b91fd4287..e020b8c6ad3 100644 --- a/mesa/examples/advanced/pd_grid/readme.md +++ b/mesa/examples/advanced/pd_grid/readme.md @@ -19,7 +19,7 @@ The Demographic Prisoner's Dilemma demonstrates how simple rules can lead to the ##### Web based model simulation -To run the model interactively, run ``mesa runserver`` in this directory. +To run the model interactively, run ``solara run app.py`` in this directory. ##### Jupyter Notebook @@ -27,8 +27,9 @@ Launch the ``Demographic Prisoner's Dilemma Activation Schedule.ipynb`` notebook ## Files -* ``run.py`` is the entry point for the font-end simulations. -* ``pd_grid/``: contains the model and agent classes; the model takes a ``activation_order`` string as an argument, which determines in which order agents are activated: Sequential, Random or Simultaneous. +* ``agents.py``: contains the agent class. +* ``model.py``: contains the model class; the model takes a ``activation_order`` string as an argument, which determines in which order agents are activated: Sequential, Random or Simultaneous. +* ``app.py``: contains the interactive visualization server. * ``Demographic Prisoner's Dilemma Activation Schedule.ipynb``: Jupyter Notebook for running the scheduling experiment. This runs the model three times, one for each activation type, and demonstrates how the activation regime drives the model to different outcomes. ## Further Reading diff --git a/mesa/examples/advanced/pd_grid/requirements.txt b/mesa/examples/advanced/pd_grid/requirements.txt deleted file mode 100644 index da2b9972efd..00000000000 --- a/mesa/examples/advanced/pd_grid/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -jupyter -matplotlib -mesa~=2.0 diff --git a/mesa/examples/advanced/pd_grid/run.py b/mesa/examples/advanced/pd_grid/run.py deleted file mode 100644 index ae142aaaeea..00000000000 --- a/mesa/examples/advanced/pd_grid/run.py +++ /dev/null @@ -1,3 +0,0 @@ -from pd_grid.server import server - -server.launch(open_browser=True) From 3174cb067290d7ad698f433456284957d53fb594 Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven Date: Wed, 23 Oct 2024 22:05:10 +0200 Subject: [PATCH 3/4] advanced examples: Use absolute imports --- mesa/examples/advanced/epstein_civil_violence/app.py | 4 ++-- mesa/examples/advanced/epstein_civil_violence/model.py | 2 +- mesa/examples/advanced/pd_grid/app.py | 2 +- mesa/examples/advanced/pd_grid/model.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mesa/examples/advanced/epstein_civil_violence/app.py b/mesa/examples/advanced/epstein_civil_violence/app.py index fe36ca78d20..749bd611f33 100644 --- a/mesa/examples/advanced/epstein_civil_violence/app.py +++ b/mesa/examples/advanced/epstein_civil_violence/app.py @@ -1,7 +1,7 @@ from mesa.visualization import SolaraViz, Slider, make_space_matplotlib, make_plot_measure -from agents import Citizen, Cop -from model import EpsteinCivilViolence +from mesa.examples.advanced.epstein_civil_violence.agents import Citizen, Cop +from mesa.examples.advanced.epstein_civil_violence.model import EpsteinCivilViolence COP_COLOR = "#000000" AGENT_QUIET_COLOR = "#648FFF" diff --git a/mesa/examples/advanced/epstein_civil_violence/model.py b/mesa/examples/advanced/epstein_civil_violence/model.py index b54441a9c48..239681c5335 100644 --- a/mesa/examples/advanced/epstein_civil_violence/model.py +++ b/mesa/examples/advanced/epstein_civil_violence/model.py @@ -1,6 +1,6 @@ import mesa -from agents import Citizen, Cop +from mesa.examples.advanced.epstein_civil_violence.agents import Citizen, Cop class EpsteinCivilViolence(mesa.Model): diff --git a/mesa/examples/advanced/pd_grid/app.py b/mesa/examples/advanced/pd_grid/app.py index b0c2f428916..2364cee422d 100644 --- a/mesa/examples/advanced/pd_grid/app.py +++ b/mesa/examples/advanced/pd_grid/app.py @@ -5,7 +5,7 @@ import solara from mesa.visualization import SolaraViz, make_space_matplotlib, make_plot_measure from mesa.visualization.UserParam import Slider -from model import PdGrid +from mesa.examples.advanced.pd_grid.model import PdGrid def pd_agent_portrayal(agent): diff --git a/mesa/examples/advanced/pd_grid/model.py b/mesa/examples/advanced/pd_grid/model.py index bd57281a9bb..78de0b5244a 100644 --- a/mesa/examples/advanced/pd_grid/model.py +++ b/mesa/examples/advanced/pd_grid/model.py @@ -1,7 +1,7 @@ import mesa from mesa.experimental.cell_space import OrthogonalMooreGrid -from agents import PDAgent +from mesa.examples.advanced.pd_grid.agents import PDAgent class PdGrid(mesa.Model): From 495e4e248800c069478407084d2f07ff3b55dcbc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 23 Oct 2024 20:16:35 +0000 Subject: [PATCH 4/4] advanced examples: ruff format, pre-commit --- mesa/examples/__init__.py | 5 ++-- .../advanced/epstein_civil_violence/app.py | 23 +++++++++-------- .../advanced/epstein_civil_violence/model.py | 7 +++--- mesa/examples/advanced/pd_grid/app.py | 25 +++++-------------- mesa/examples/advanced/pd_grid/model.py | 3 +-- 5 files changed, 25 insertions(+), 38 deletions(-) diff --git a/mesa/examples/__init__.py b/mesa/examples/__init__.py index cfec8b4c9a1..0258243c9c3 100644 --- a/mesa/examples/__init__.py +++ b/mesa/examples/__init__.py @@ -1,12 +1,11 @@ +from mesa.examples.advanced.epstein_civil_violence.model import EpsteinCivilViolence +from mesa.examples.advanced.pd_grid.model import PdGrid from mesa.examples.basic.boid_flockers.model import BoidFlockers from mesa.examples.basic.boltzmann_wealth_model.model import BoltzmannWealthModel from mesa.examples.basic.conways_game_of_life.model import ConwaysGameOfLife from mesa.examples.basic.schelling.model import Schelling from mesa.examples.basic.virus_on_network.model import VirusOnNetwork -from mesa.examples.advanced.epstein_civil_violence.model import EpsteinCivilViolence -from mesa.examples.advanced.pd_grid.model import PdGrid - __all__ = [ "BoidFlockers", "BoltzmannWealthModel", diff --git a/mesa/examples/advanced/epstein_civil_violence/app.py b/mesa/examples/advanced/epstein_civil_violence/app.py index 749bd611f33..870e931c51d 100644 --- a/mesa/examples/advanced/epstein_civil_violence/app.py +++ b/mesa/examples/advanced/epstein_civil_violence/app.py @@ -1,7 +1,11 @@ -from mesa.visualization import SolaraViz, Slider, make_space_matplotlib, make_plot_measure - from mesa.examples.advanced.epstein_civil_violence.agents import Citizen, Cop from mesa.examples.advanced.epstein_civil_violence.model import EpsteinCivilViolence +from mesa.visualization import ( + Slider, + SolaraViz, + make_plot_measure, + make_space_matplotlib, +) COP_COLOR = "#000000" AGENT_QUIET_COLOR = "#648FFF" @@ -16,19 +20,18 @@ def citizen_cop_portrayal(agent): portrayal = { "size": 25, - # "shape": "circle", - "x": agent.cell.coordinate[0], - "y": agent.cell.coordinate[1], - # "filled": True, + "shape": "s", # square marker } if isinstance(agent, Citizen): - color = AGENT_QUIET_COLOR if agent.condition == "Quiescent" else AGENT_REBEL_COLOR + color = ( + AGENT_QUIET_COLOR if agent.condition == "Quiescent" else AGENT_REBEL_COLOR + ) color = JAIL_COLOR if agent.jail_sentence else color shape = JAIL_SHAPE if agent.jail_sentence else "circle" portrayal["color"] = color portrayal["shape"] = shape - if shape == "rect": + if shape == "s": portrayal["w"] = 0.9 portrayal["h"] = 0.9 else: @@ -62,9 +65,7 @@ def citizen_cop_portrayal(agent): page = SolaraViz( epstein_model, - components=[ - space_component, - chart_component], + components=[space_component, chart_component], model_params=model_params, name="Epstein Civil Violence", ) diff --git a/mesa/examples/advanced/epstein_civil_violence/model.py b/mesa/examples/advanced/epstein_civil_violence/model.py index 239681c5335..3d3708e1241 100644 --- a/mesa/examples/advanced/epstein_civil_violence/model.py +++ b/mesa/examples/advanced/epstein_civil_violence/model.py @@ -1,5 +1,4 @@ import mesa - from mesa.examples.advanced.epstein_civil_violence.agents import Citizen, Cop @@ -43,7 +42,7 @@ def __init__( arrest_prob_constant=2.3, movement=True, max_iters=1000, - seed=None + seed=None, ): super().__init__(seed=seed) self.width = width @@ -60,7 +59,9 @@ def __init__( self.max_iters = max_iters self.iteration = 0 - self.grid = mesa.experimental.cell_space.OrthogonalMooreGrid((width, height), capacity=1, torus=True, random=self.random) + self.grid = mesa.experimental.cell_space.OrthogonalMooreGrid( + (width, height), capacity=1, torus=True, random=self.random + ) model_reporters = { "Quiescent": lambda m: self.count_type_citizens(m, "Quiescent"), diff --git a/mesa/examples/advanced/pd_grid/app.py b/mesa/examples/advanced/pd_grid/app.py index 2364cee422d..4f97f9ff491 100644 --- a/mesa/examples/advanced/pd_grid/app.py +++ b/mesa/examples/advanced/pd_grid/app.py @@ -2,10 +2,9 @@ Solara-based visualization for the Spatial Prisoner's Dilemma Model. """ -import solara -from mesa.visualization import SolaraViz, make_space_matplotlib, make_plot_measure -from mesa.visualization.UserParam import Slider from mesa.examples.advanced.pd_grid.model import PdGrid +from mesa.visualization import SolaraViz, make_plot_measure, make_space_matplotlib +from mesa.visualization.UserParam import Slider def pd_agent_portrayal(agent): @@ -21,26 +20,14 @@ def pd_agent_portrayal(agent): # Model parameters model_params = { - "width": Slider( - "Grid Width", - value=50, - min=10, - max=100, - step=1 - ), - "height": Slider( - "Grid Height", - value=50, - min=10, - max=100, - step=1 - ), + "width": Slider("Grid Width", value=50, min=10, max=100, step=1), + "height": Slider("Grid Height", value=50, min=10, max=100, step=1), "activation_order": { "type": "Select", "value": "Random", "values": PdGrid.activation_regimes, - "label": "Activation Regime" - } + "label": "Activation Regime", + }, } diff --git a/mesa/examples/advanced/pd_grid/model.py b/mesa/examples/advanced/pd_grid/model.py index 78de0b5244a..35327a41ec5 100644 --- a/mesa/examples/advanced/pd_grid/model.py +++ b/mesa/examples/advanced/pd_grid/model.py @@ -1,7 +1,6 @@ import mesa -from mesa.experimental.cell_space import OrthogonalMooreGrid - from mesa.examples.advanced.pd_grid.agents import PDAgent +from mesa.experimental.cell_space import OrthogonalMooreGrid class PdGrid(mesa.Model):