-
Notifications
You must be signed in to change notification settings - Fork 169
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
Closed
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 451cfcc
Add files via upload
jayash1973 e233178
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 318d2b6
Update test_epstein_civil_violence.py
jayash1973 032388f
Update test_wolf_sheep.py
jayash1973 78378c4
Update README.md
jayash1973 4879683
Merge remote-tracking branch 'upstream/main' into pr/152
EwoutH da085e8
generated epstein_civil_violence_experimental and wolf_sheep_experime…
jayash1973 84fc098
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] febb207
Update agent.py
jayash1973 e65406a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3c3e749
Update app.py
jayash1973 86c1a6a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 671d1dc
Update model.py
jayash1973 bc7ac1b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a849419
Update Readme.md
jayash1973 e19225c
Update test_epstein_civil_violence.py
jayash1973 3addf0b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8413dc3
Update agent.py
jayash1973 877df77
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 65f155a
Rename agent.py to agents.py
jayash1973 ceb2f66
Update app.py
jayash1973 f88c0fe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 825d8d3
Update model.py
jayash1973 0a027ab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 351fcfa
Create random_walk.py
jayash1973 08a945d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 407397b
Update Readme.md
jayash1973 a64fa2f
Create scheduler.py
jayash1973 67868fe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 816c146
Create test_random_walk.py
jayash1973 2016489
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8fffea1
Update test_wolf_sheep.py
jayash1973 bc0bae1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0704e60
Update wolf_sheep.py
jayash1973 db7a76b
Delete examples/epstein_civil_violence_experimental/run.py
jayash1973 a33250c
Delete examples/wolf_sheep_experimental/run.py
jayash1973 3a9218e
Update model.py
jayash1973 bb5b15e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b00184c
Update app.py
jayash1973 51c071c
Update agents.py
jayash1973 73c8210
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 14544fe
Update app.py
jayash1973 75b0cc9
Update model.py
jayash1973 0a5bfb6
Update model.py
jayash1973 1a02841
Delete examples/epstein_civil_violence/epstein_civil_violence.py
jayash1973 461f522
Delete examples/wolf_sheep/wolf_sheep/test_wolf_sheep.py
jayash1973 e99621d
Delete examples/wolf_sheep/wolf_sheep/wolf_sheep.py
jayash1973 768388b
Update agent.py
jayash1973 fda4f75
Update app.py
jayash1973 581b3e1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9533ff5
Delete examples/epstein_civil_violence_experimental/epstein_civil_vio…
jayash1973 3e2a2cb
Delete examples/epstein_civil_violence_experimental/test_epstein_civi…
jayash1973 c0fdb5c
Update Readme.md
jayash1973 609f468
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7563cf3
Update app.py
jayash1973 62d5a94
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 06ada49
Update Readme.md
jayash1973 8aa1276
Update agents.py
jayash1973 15d3165
Update app.py
jayash1973 00d56fa
Delete examples/wolf_sheep_experimental/random_walk.py
jayash1973 902ad5a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0e6c5ec
Delete examples/wolf_sheep_experimental/test_random_walk.py
jayash1973 6dc457f
Delete examples/wolf_sheep_experimental/test_wolf_sheep.py
jayash1973 a283918
Delete examples/wolf_sheep_experimental/wolf_sheep.py
jayash1973 c2a5fa8
Merge remote-tracking branch 'upstream/main' into pr/152
EwoutH 5219ff1
Update model.py
jayash1973 964ee75
Update agent.py
jayash1973 7d49abf
Update app.py
jayash1973 3759405
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3b0f83e
Delete examples/wolf_sheep_experimental/scheduler.py
jayash1973 04ad95d
Update model.py
jayash1973 6470e64
Update agents.py
jayash1973 7f9fd65
Update app.py
jayash1973 198cdb7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3f724d4
Update Readme.md
jayash1973 0eb1604
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 72d705f
Update Readme.md
jayash1973 05e7194
Update app.py
jayash1973 828091b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c949303
Update agent.py
jayash1973 bdc1250
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c6bf8be
Update model.py
jayash1973 a130421
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 67eab19
Update Readme.md
jayash1973 9042a9d
Update app.py
jayash1973 6b55589
Update app.py
jayash1973 77a000d
Update model.py
jayash1973 0465850
Update model.py
jayash1973 bb57253
Update Readme.md
jayash1973 96bce84
Update Readme.md
jayash1973 b48ec2b
Fix ruff formatting
EwoutH ae577e7
Update app.py
jayash1973 b375157
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ef58fb9
Update app.py
jayash1973 37ac37d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0d14832
Update app.py
jayash1973 0dad207
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] babbab3
Update app.py
jayash1973 9d8b6b7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 34299cb
Update agents.py
jayash1973 93f55dd
Update app.py
jayash1973 34386fa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2eeca91
Update model.py
jayash1973 6eaaed4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e8be376
Update agents.py
jayash1973 70ce34e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0b266cc
Create __init__.py
jayash1973 9700609
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d63ae24
Update app.py
jayash1973 97ad55e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a9cbd40
Update model.py
jayash1973 c03e0c9
Update Readme.md
jayash1973 0df095f
Update Readme.md
jayash1973 f6664b6
Update model.py
jayash1973 9637356
Update model.py
jayash1973 9ed4970
Add files via upload
jayash1973 68ee892
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 67116e3
Update model.py
jayash1973 3fe484e
Update app.py
jayash1973 d647b98
Create __init__.py
jayash1973 6ba9d73
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 78f97dc
Delete examples/bank_reserves/run.py
jayash1973 e974011
Delete examples/bank_reserves/batch_run.py
jayash1973 bb007e8
Delete examples/bank_reserves/bank_reserves/random_walk.py
jayash1973 3386786
Delete examples/bank_reserves/bank_reserves/server.py
jayash1973 69f061e
Create batch_run.py
jayash1973 fe89fab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 82296bb
Update Readme.md
jayash1973 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
273 changes: 273 additions & 0 deletions
273
examples/epstein_civil_violence/epstein_civil_violence.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
6 changes: 6 additions & 0 deletions
6
examples/epstein_civil_violence/test_epstein_civil_violence.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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