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

#2191 issue moved https://github.com/projectmesa/mesa/tree/main/mesa/experimental/devs/examples to mesa-examples reository #152

Closed
wants to merge 129 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
129 commits
Select commit Hold shift + click to select a range
4078c1b
Add files via upload
jayash1973 Aug 10, 2024
451cfcc
Add files via upload
jayash1973 Aug 10, 2024
e233178
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 10, 2024
318d2b6
Update test_epstein_civil_violence.py
jayash1973 Aug 10, 2024
032388f
Update test_wolf_sheep.py
jayash1973 Aug 10, 2024
78378c4
Update README.md
jayash1973 Aug 11, 2024
4879683
Merge remote-tracking branch 'upstream/main' into pr/152
EwoutH Aug 11, 2024
da085e8
generated epstein_civil_violence_experimental and wolf_sheep_experime…
jayash1973 Aug 11, 2024
84fc098
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2024
febb207
Update agent.py
jayash1973 Aug 11, 2024
e65406a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2024
3c3e749
Update app.py
jayash1973 Aug 11, 2024
86c1a6a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2024
671d1dc
Update model.py
jayash1973 Aug 11, 2024
bc7ac1b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2024
a849419
Update Readme.md
jayash1973 Aug 11, 2024
e19225c
Update test_epstein_civil_violence.py
jayash1973 Aug 11, 2024
3addf0b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2024
8413dc3
Update agent.py
jayash1973 Aug 11, 2024
877df77
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2024
65f155a
Rename agent.py to agents.py
jayash1973 Aug 11, 2024
ceb2f66
Update app.py
jayash1973 Aug 11, 2024
f88c0fe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2024
825d8d3
Update model.py
jayash1973 Aug 11, 2024
0a027ab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2024
351fcfa
Create random_walk.py
jayash1973 Aug 11, 2024
08a945d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2024
407397b
Update Readme.md
jayash1973 Aug 11, 2024
a64fa2f
Create scheduler.py
jayash1973 Aug 11, 2024
67868fe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2024
816c146
Create test_random_walk.py
jayash1973 Aug 11, 2024
2016489
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2024
8fffea1
Update test_wolf_sheep.py
jayash1973 Aug 11, 2024
bc0bae1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2024
0704e60
Update wolf_sheep.py
jayash1973 Aug 11, 2024
db7a76b
Delete examples/epstein_civil_violence_experimental/run.py
jayash1973 Aug 11, 2024
a33250c
Delete examples/wolf_sheep_experimental/run.py
jayash1973 Aug 11, 2024
3a9218e
Update model.py
jayash1973 Aug 11, 2024
bb5b15e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2024
b00184c
Update app.py
jayash1973 Aug 11, 2024
51c071c
Update agents.py
jayash1973 Aug 11, 2024
73c8210
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2024
14544fe
Update app.py
jayash1973 Aug 11, 2024
75b0cc9
Update model.py
jayash1973 Aug 11, 2024
0a5bfb6
Update model.py
jayash1973 Aug 11, 2024
1a02841
Delete examples/epstein_civil_violence/epstein_civil_violence.py
jayash1973 Aug 11, 2024
461f522
Delete examples/wolf_sheep/wolf_sheep/test_wolf_sheep.py
jayash1973 Aug 11, 2024
e99621d
Delete examples/wolf_sheep/wolf_sheep/wolf_sheep.py
jayash1973 Aug 11, 2024
768388b
Update agent.py
jayash1973 Aug 12, 2024
fda4f75
Update app.py
jayash1973 Aug 12, 2024
581b3e1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2024
9533ff5
Delete examples/epstein_civil_violence_experimental/epstein_civil_vio…
jayash1973 Aug 12, 2024
3e2a2cb
Delete examples/epstein_civil_violence_experimental/test_epstein_civi…
jayash1973 Aug 12, 2024
c0fdb5c
Update Readme.md
jayash1973 Aug 12, 2024
609f468
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2024
7563cf3
Update app.py
jayash1973 Aug 12, 2024
62d5a94
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2024
06ada49
Update Readme.md
jayash1973 Aug 12, 2024
8aa1276
Update agents.py
jayash1973 Aug 12, 2024
15d3165
Update app.py
jayash1973 Aug 12, 2024
00d56fa
Delete examples/wolf_sheep_experimental/random_walk.py
jayash1973 Aug 12, 2024
902ad5a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2024
0e6c5ec
Delete examples/wolf_sheep_experimental/test_random_walk.py
jayash1973 Aug 12, 2024
6dc457f
Delete examples/wolf_sheep_experimental/test_wolf_sheep.py
jayash1973 Aug 12, 2024
a283918
Delete examples/wolf_sheep_experimental/wolf_sheep.py
jayash1973 Aug 12, 2024
c2a5fa8
Merge remote-tracking branch 'upstream/main' into pr/152
EwoutH Aug 12, 2024
5219ff1
Update model.py
jayash1973 Aug 12, 2024
964ee75
Update agent.py
jayash1973 Aug 12, 2024
7d49abf
Update app.py
jayash1973 Aug 12, 2024
3759405
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2024
3b0f83e
Delete examples/wolf_sheep_experimental/scheduler.py
jayash1973 Aug 12, 2024
04ad95d
Update model.py
jayash1973 Aug 12, 2024
6470e64
Update agents.py
jayash1973 Aug 12, 2024
7f9fd65
Update app.py
jayash1973 Aug 12, 2024
198cdb7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2024
3f724d4
Update Readme.md
jayash1973 Aug 12, 2024
0eb1604
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2024
72d705f
Update Readme.md
jayash1973 Aug 12, 2024
05e7194
Update app.py
jayash1973 Aug 12, 2024
828091b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2024
c949303
Update agent.py
jayash1973 Aug 12, 2024
bdc1250
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2024
c6bf8be
Update model.py
jayash1973 Aug 12, 2024
a130421
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2024
67eab19
Update Readme.md
jayash1973 Aug 12, 2024
9042a9d
Update app.py
jayash1973 Aug 12, 2024
6b55589
Update app.py
jayash1973 Aug 12, 2024
77a000d
Update model.py
jayash1973 Aug 12, 2024
0465850
Update model.py
jayash1973 Aug 12, 2024
bb57253
Update Readme.md
jayash1973 Aug 12, 2024
96bce84
Update Readme.md
jayash1973 Aug 12, 2024
b48ec2b
Fix ruff formatting
EwoutH Aug 12, 2024
ae577e7
Update app.py
jayash1973 Aug 12, 2024
b375157
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2024
ef58fb9
Update app.py
jayash1973 Aug 12, 2024
37ac37d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2024
0d14832
Update app.py
jayash1973 Aug 12, 2024
0dad207
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2024
babbab3
Update app.py
jayash1973 Aug 12, 2024
9d8b6b7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2024
34299cb
Update agents.py
jayash1973 Aug 12, 2024
93f55dd
Update app.py
jayash1973 Aug 13, 2024
34386fa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 13, 2024
2eeca91
Update model.py
jayash1973 Aug 13, 2024
6eaaed4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 13, 2024
e8be376
Update agents.py
jayash1973 Aug 13, 2024
70ce34e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 13, 2024
0b266cc
Create __init__.py
jayash1973 Aug 13, 2024
9700609
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 13, 2024
d63ae24
Update app.py
jayash1973 Aug 13, 2024
97ad55e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 13, 2024
a9cbd40
Update model.py
jayash1973 Aug 13, 2024
c03e0c9
Update Readme.md
jayash1973 Aug 13, 2024
0df095f
Update Readme.md
jayash1973 Aug 13, 2024
f6664b6
Update model.py
jayash1973 Aug 13, 2024
9637356
Update model.py
jayash1973 Aug 13, 2024
9ed4970
Add files via upload
jayash1973 Aug 14, 2024
68ee892
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 14, 2024
67116e3
Update model.py
jayash1973 Aug 14, 2024
3fe484e
Update app.py
jayash1973 Aug 14, 2024
d647b98
Create __init__.py
jayash1973 Aug 14, 2024
6ba9d73
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 14, 2024
78f97dc
Delete examples/bank_reserves/run.py
jayash1973 Aug 14, 2024
e974011
Delete examples/bank_reserves/batch_run.py
jayash1973 Aug 14, 2024
bb007e8
Delete examples/bank_reserves/bank_reserves/random_walk.py
jayash1973 Aug 14, 2024
3386786
Delete examples/bank_reserves/bank_reserves/server.py
jayash1973 Aug 14, 2024
69f061e
Create batch_run.py
jayash1973 Aug 14, 2024
fe89fab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 14, 2024
82296bb
Update Readme.md
jayash1973 Aug 14, 2024
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
273 changes: 273 additions & 0 deletions examples/epstein_civil_violence/epstein_civil_violence.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this model already exist? I don't think this files needs to be added

Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
import enum
import math

from mesa import Agent, Model
from mesa.experimental.devs.simulator import ABMSimulator
from mesa.space import SingleGrid


class EpsteinAgent(Agent):
def __init__(self, unique_id, model, vision, movement):
super().__init__(unique_id, model)
self.vision = vision
self.movement = movement


class AgentState(enum.IntEnum):
QUIESCENT = enum.auto()
ARRESTED = enum.auto()
ACTIVE = enum.auto()


class Citizen(EpsteinAgent):
"""
A member of the general population, may or may not be in active rebellion.
Summary of rule: If grievance - risk > threshold, rebel.

Attributes:
unique_id: unique int
model :
hardship: Agent's 'perceived hardship (i.e., physical or economic
privation).' Exogenous, drawn from U(0,1).
regime_legitimacy: Agent's perception of regime legitimacy, equal
across agents. Exogenous.
risk_aversion: Exogenous, drawn from U(0,1).
threshold: if (grievance - (risk_aversion * arrest_probability)) >
threshold, go/remain Active
vision: number of cells in each direction (N, S, E and W) that agent
can inspect
condition: Can be "Quiescent" or "Active;" deterministic function of
greivance, perceived risk, and
grievance: deterministic function of hardship and regime_legitimacy;
how aggrieved is agent at the regime?
arrest_probability: agent's assessment of arrest probability, given
rebellion
"""

def __init__(
self,
unique_id,
model,
vision,
movement,
hardship,
regime_legitimacy,
risk_aversion,
threshold,
arrest_prob_constant,
):
"""
Create a new Citizen.
Args:
unique_id: unique int
model : model instance
hardship: Agent's 'perceived hardship (i.e., physical or economic
privation).' Exogenous, drawn from U(0,1).
regime_legitimacy: Agent's perception of regime legitimacy, equal
across agents. Exogenous.
risk_aversion: Exogenous, drawn from U(0,1).
threshold: if (grievance - (risk_aversion * arrest_probability)) >
threshold, go/remain Active
vision: number of cells in each direction (N, S, E and W) that
agent can inspect. Exogenous.
"""
super().__init__(unique_id, model, vision, movement)
self.hardship = hardship
self.regime_legitimacy = regime_legitimacy
self.risk_aversion = risk_aversion
self.threshold = threshold
self.condition = AgentState.QUIESCENT
self.grievance = self.hardship * (1 - self.regime_legitimacy)
self.arrest_probability = None
self.arrest_prob_constant = arrest_prob_constant

def step(self):
"""
Decide whether to activate, then move if applicable.
"""
self.update_neighbors()
self.update_estimated_arrest_probability()
net_risk = self.risk_aversion * self.arrest_probability
if self.grievance - net_risk > self.threshold:
self.condition = AgentState.ACTIVE
else:
self.condition = AgentState.QUIESCENT
if self.movement and self.empty_neighbors:
new_pos = self.random.choice(self.empty_neighbors)
self.model.grid.move_agent(self, new_pos)

def update_neighbors(self):
"""
Look around and see who my neighbors are
"""
self.neighborhood = self.model.grid.get_neighborhood(
self.pos, moore=True, radius=self.vision
)
self.neighbors = self.model.grid.get_cell_list_contents(self.neighborhood)
self.empty_neighbors = [
c for c in self.neighborhood if self.model.grid.is_cell_empty(c)
]

def update_estimated_arrest_probability(self):
"""
Based on the ratio of cops to actives in my neighborhood, estimate the
p(Arrest | I go active).
"""
cops_in_vision = len([c for c in self.neighbors if isinstance(c, Cop)])
actives_in_vision = 1.0 # citizen counts herself
for c in self.neighbors:
if isinstance(c, Citizen) and c.condition == AgentState.ACTIVE:
actives_in_vision += 1
self.arrest_probability = 1 - math.exp(
-1 * self.arrest_prob_constant * (cops_in_vision / actives_in_vision)
)

def sent_to_jail(self, value):
self.model.active_agents.remove(self)
self.condition = AgentState.ARRESTED
self.model.simulator.schedule_event_relative(self.release_from_jail, value)

def release_from_jail(self):
self.model.active_agents.add(self)
self.condition = AgentState.QUIESCENT


class Cop(EpsteinAgent):
"""
A cop for life. No defection.
Summary of rule: Inspect local vision and arrest a random active agent.

Attributes:
unique_id: unique int
x, y: Grid coordinates
vision: number of cells in each direction (N, S, E and W) that cop is
able to inspect
"""

def __init__(self, unique_id, model, vision, movement, max_jail_term):
super().__init__(unique_id, model, vision, movement)
self.max_jail_term = max_jail_term

def step(self):
"""
Inspect local vision and arrest a random active agent. Move if
applicable.
"""
self.update_neighbors()
active_neighbors = []
for agent in self.neighbors:
if isinstance(agent, Citizen) and agent.condition == "Active":
active_neighbors.append(agent)
if active_neighbors:
arrestee = self.random.choice(active_neighbors)
arrestee.sent_to_jail(self.random.randint(0, self.max_jail_term))
if self.movement and self.empty_neighbors:
new_pos = self.random.choice(self.empty_neighbors)
self.model.grid.move_agent(self, new_pos)

def update_neighbors(self):
"""
Look around and see who my neighbors are.
"""
self.neighborhood = self.model.grid.get_neighborhood(
self.pos, moore=True, radius=self.vision
)
self.neighbors = self.model.grid.get_cell_list_contents(self.neighborhood)
self.empty_neighbors = [
c for c in self.neighborhood if self.model.grid.is_cell_empty(c)
]


class EpsteinCivilViolence(Model):
"""
Model 1 from "Modeling civil violence: An agent-based computational
approach," by Joshua Epstein.
http://www.pnas.org/content/99/suppl_3/7243.full
Attributes:
height: grid height
width: grid width
citizen_density: approximate % of cells occupied by citizens.
cop_density: approximate % of cells occupied by cops.
citizen_vision: number of cells in each direction (N, S, E and W) that
citizen can inspect
cop_vision: number of cells in each direction (N, S, E and W) that cop
can inspect
legitimacy: (L) citizens' perception of regime legitimacy, equal
across all citizens
max_jail_term: (J_max)
active_threshold: if (grievance - (risk_aversion * arrest_probability))
> threshold, citizen rebels
arrest_prob_constant: set to ensure agents make plausible arrest
probability estimates
movement: binary, whether agents try to move at step end
max_iters: model may not have a natural stopping point, so we set a
max.
"""

def __init__(
self,
width=40,
height=40,
citizen_density=0.7,
cop_density=0.074,
citizen_vision=7,
cop_vision=7,
legitimacy=0.8,
max_jail_term=1000,
active_threshold=0.1,
arrest_prob_constant=2.3,
movement=True,
max_iters=1000,
seed=None,
):
super().__init__(seed)
if cop_density + citizen_density > 1:
raise ValueError("Cop density + citizen density must be less than 1")

self.width = width
self.height = height
self.citizen_density = citizen_density
self.cop_density = cop_density

self.max_iters = max_iters

self.grid = SingleGrid(self.width, self.height, torus=True)

for _, pos in self.grid.coord_iter():
if self.random.random() < self.cop_density:
agent = Cop(
self.next_id(),
self,
cop_vision,
movement,
max_jail_term,
)
elif self.random.random() < (self.cop_density + self.citizen_density):
agent = Citizen(
self.next_id(),
self,
citizen_vision,
movement,
hardship=self.random.random(),
regime_legitimacy=legitimacy,
risk_aversion=self.random.random(),
threshold=active_threshold,
arrest_prob_constant=arrest_prob_constant,
)
else:
continue
self.grid.place_agent(agent, pos)

self.active_agents = self.agents

def step(self):
self.active_agents.shuffle(inplace=True).do("step")


if __name__ == "__main__":
model = EpsteinCivilViolence(seed=15)
simulator = ABMSimulator()

simulator.setup(model)

simulator.run(time_delta=100)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from epstein_civil_violence.epstein_civil_violence import EpsteinCivilViolence


def test_epstein_civil_violence():
model = EpsteinCivilViolence(seed=15)
assert model is not None
6 changes: 6 additions & 0 deletions examples/wolf_sheep/wolf_sheep/test_wolf_sheep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from wolf_sheep.wolf_sheep import WolfSheep


def test_wolf_sheep():
model = WolfSheep(seed=15)
assert model is not None
Loading