Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup and restructure EpsteinCivilViolence and PdGrid examples #2408

Merged
merged 4 commits into from
Oct 24, 2024
Merged
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
4 changes: 4 additions & 0 deletions mesa/examples/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
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
Expand All @@ -10,4 +12,6 @@
"ConwaysGameOfLife",
"Schelling",
"VirusOnNetwork",
"EpsteinCivilViolence",
"PdGrid",
]
5 changes: 3 additions & 2 deletions mesa/examples/advanced/epstein_civil_violence/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 72 additions & 0 deletions mesa/examples/advanced/epstein_civil_violence/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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"
AGENT_REBEL_COLOR = "#FE6100"
JAIL_COLOR = "#808080"
JAIL_SHAPE = "rect"


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

portrayal = {
"size": 25,
"shape": "s", # square marker
}

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 == "s":
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

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import mesa

from .agent import Citizen, Cop
from mesa.examples.advanced.epstein_civil_violence.agents import Citizen, Cop


class EpsteinCivilViolence(mesa.Model):
Expand Down Expand Up @@ -43,8 +42,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
Expand All @@ -60,7 +60,7 @@ def __init__(
self.iteration = 0

self.grid = mesa.experimental.cell_space.OrthogonalMooreGrid(
(width, height), capacity=1, torus=True
(width, height), capacity=1, torus=True, random=self.random
)

model_reporters = {
Expand Down

This file was deleted.

3 changes: 0 additions & 3 deletions mesa/examples/advanced/epstein_civil_violence/run.py

This file was deleted.

51 changes: 51 additions & 0 deletions mesa/examples/advanced/pd_grid/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Solara-based visualization for the Spatial Prisoner's Dilemma Model.
"""

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):
"""
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import mesa
from mesa.examples.advanced.pd_grid.agents import PDAgent
from mesa.experimental.cell_space import OrthogonalMooreGrid

from .agent import PDAgent


class PdGrid(mesa.Model):
"""Model class for iterated, spatial prisoner's dilemma model."""
Expand Down
19 changes: 0 additions & 19 deletions mesa/examples/advanced/pd_grid/pd_grid/portrayal.py

This file was deleted.

21 changes: 0 additions & 21 deletions mesa/examples/advanced/pd_grid/pd_grid/server.py

This file was deleted.

7 changes: 4 additions & 3 deletions mesa/examples/advanced/pd_grid/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ 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

Launch the ``Demographic Prisoner's Dilemma Activation Schedule.ipynb`` notebook and run the code.

## 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
Expand Down
3 changes: 0 additions & 3 deletions mesa/examples/advanced/pd_grid/requirements.txt

This file was deleted.

3 changes: 0 additions & 3 deletions mesa/examples/advanced/pd_grid/run.py

This file was deleted.

Loading