Skip to content

Commit 9ba1be2

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 9d0d6c0 commit 9ba1be2

File tree

26 files changed

+94
-202
lines changed

26 files changed

+94
-202
lines changed

examples/advanced/epstein_civil_violence/epstein_civil_violence/agent.py

+10-18
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@
55

66
class EpsteinAgent(mesa.experimental.cell_space.CellAgent):
77
def update_neighbors(self):
8-
"""
9-
Look around and see who my neighbors are
10-
"""
8+
"""Look around and see who my neighbors are"""
119
self.neighborhood = self.cell.get_neighborhood(radius=self.vision)
1210

1311
self.neighbors = self.neighborhood.agents
1412
self.empty_neighbors = [c for c in self.neighborhood if c.is_empty]
1513

1614

1715
class Citizen(EpsteinAgent):
18-
"""
19-
A member of the general population, may or may not be in active rebellion.
16+
"""A member of the general population, may or may not be in active rebellion.
2017
Summary of rule: If grievance - risk > threshold, rebel.
2118
2219
Attributes:
@@ -46,8 +43,8 @@ def __init__(
4643
threshold,
4744
vision,
4845
):
49-
"""
50-
Create a new Citizen.
46+
"""Create a new Citizen.
47+
5148
Args:
5249
model: the model to which the agent belongs
5350
hardship: Agent's 'perceived hardship (i.e., physical or economic
@@ -73,9 +70,7 @@ def __init__(
7370
self.arrest_probability = None
7471

7572
def step(self):
76-
"""
77-
Decide whether to activate, then move if applicable.
78-
"""
73+
"""Decide whether to activate, then move if applicable."""
7974
if self.jail_sentence:
8075
self.jail_sentence -= 1
8176
return # no other changes or movements if agent is in jail.
@@ -92,8 +87,7 @@ def step(self):
9287
self.move_to(new_cell)
9388

9489
def update_estimated_arrest_probability(self):
95-
"""
96-
Based on the ratio of cops to actives in my neighborhood, estimate the
90+
"""Based on the ratio of cops to actives in my neighborhood, estimate the
9791
p(Arrest | I go active).
9892
"""
9993
cops_in_vision = len([c for c in self.neighbors if isinstance(c, Cop)])
@@ -111,8 +105,7 @@ def update_estimated_arrest_probability(self):
111105

112106

113107
class Cop(EpsteinAgent):
114-
"""
115-
A cop for life. No defection.
108+
"""A cop for life. No defection.
116109
Summary of rule: Inspect local vision and arrest a random active agent.
117110
118111
Attributes:
@@ -123,8 +116,8 @@ class Cop(EpsteinAgent):
123116
"""
124117

125118
def __init__(self, model, vision):
126-
"""
127-
Create a new Cop.
119+
"""Create a new Cop.
120+
128121
Args:
129122
x, y: Grid coordinates
130123
vision: number of cells in each direction (N, S, E and W) that
@@ -135,8 +128,7 @@ def __init__(self, model, vision):
135128
self.vision = vision
136129

137130
def step(self):
138-
"""
139-
Inspect local vision and arrest a random active agent. Move if
131+
"""Inspect local vision and arrest a random active agent. Move if
140132
applicable.
141133
"""
142134
self.update_neighbors()

examples/advanced/epstein_civil_violence/epstein_civil_violence/model.py

+5-14
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55

66
class EpsteinCivilViolence(mesa.Model):
7-
"""
8-
Model 1 from "Modeling civil violence: An agent-based computational
7+
"""Model 1 from "Modeling civil violence: An agent-based computational
98
approach," by Joshua Epstein.
109
http://www.pnas.org/content/99/suppl_3/7243.full
1110
Attributes:
@@ -103,9 +102,7 @@ def __init__(
103102
self.datacollector.collect(self)
104103

105104
def step(self):
106-
"""
107-
Advance the model by one step and collect data.
108-
"""
105+
"""Advance the model by one step and collect data."""
109106
self.agents.shuffle_do("step")
110107
# collect data
111108
self.datacollector.collect(self)
@@ -115,9 +112,7 @@ def step(self):
115112

116113
@staticmethod
117114
def count_type_citizens(model, condition, exclude_jailed=True):
118-
"""
119-
Helper method to count agents by Quiescent/Active.
120-
"""
115+
"""Helper method to count agents by Quiescent/Active."""
121116
citizens = model.agents_by_type[Citizen]
122117

123118
if exclude_jailed:
@@ -133,14 +128,10 @@ def count_type_citizens(model, condition, exclude_jailed=True):
133128

134129
@staticmethod
135130
def count_jailed(model):
136-
"""
137-
Helper method to count jailed agents.
138-
"""
131+
"""Helper method to count jailed agents."""
139132
return len([a for a in model.agents_by_type[Citizen] if a.jail_sentence > 0])
140133

141134
@staticmethod
142135
def count_cops(model):
143-
"""
144-
Helper method to count jailed agents.
145-
"""
136+
"""Helper method to count jailed agents."""
146137
return len(model.agents_by_type[Cop])

examples/advanced/pd_grid/analysis.ipynb

+2-5
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@
5858
"\n",
5959
"\n",
6060
"def draw_grid(model, ax=None):\n",
61-
" \"\"\"\n",
62-
" Draw the current state of the grid, with Defecting agents in red\n",
61+
" \"\"\"Draw the current state of the grid, with Defecting agents in red\n",
6362
" and Cooperating agents in blue.\n",
6463
" \"\"\"\n",
6564
" if not ax:\n",
@@ -82,9 +81,7 @@
8281
"outputs": [],
8382
"source": [
8483
"def run_model(model):\n",
85-
" \"\"\"\n",
86-
" Run an experiment with a given model, and plot the results.\n",
87-
" \"\"\"\n",
84+
" \"\"\"Run an experiment with a given model, and plot the results.\"\"\"\n",
8885
" fig = plt.figure(figsize=(12, 8))\n",
8986
"\n",
9087
" ax1 = fig.add_subplot(231)\n",

examples/advanced/pd_grid/pd_grid/agent.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ class PDAgent(CellAgent):
55
"""Agent member of the iterated, spatial prisoner's dilemma model."""
66

77
def __init__(self, model, starting_move=None):
8-
"""
9-
Create a new Prisoner's Dilemma agent.
8+
"""Create a new Prisoner's Dilemma agent.
109
1110
Args:
1211
model: model instance
@@ -27,8 +26,8 @@ def is_cooroperating(self):
2726

2827
def step(self):
2928
"""Get the best neighbor's move, and change own move accordingly
30-
if better than own score."""
31-
29+
if better than own score.
30+
"""
3231
# neighbors = self.model.grid.get_neighbors(self.pos, True, include_center=True)
3332
neighbors = [*list(self.cell.neighborhood.agents), self]
3433
best_neighbor = max(neighbors, key=lambda a: a.score)

examples/advanced/pd_grid/pd_grid/model.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ class PdGrid(mesa.Model):
1717
def __init__(
1818
self, width=50, height=50, activation_order="Random", payoffs=None, seed=None
1919
):
20-
"""
21-
Create a new Spatial Prisoners' Dilemma Model.
20+
"""Create a new Spatial Prisoners' Dilemma Model.
2221
2322
Args:
2423
width, height: Grid size. There will be one agent per grid cell.

examples/advanced/pd_grid/pd_grid/portrayal.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
def portrayPDAgent(agent):
2-
"""
3-
This function is registered with the visualization server to be called
2+
"""This function is registered with the visualization server to be called
43
each tick to indicate how to draw the agent in its current state.
54
:param agent: the agent in the simulation
65
:return: the portrayal dictionary

examples/advanced/sugarscape_g1mt/app.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import numpy as np
22
import solara
33
from matplotlib.figure import Figure
4-
from mesa.visualization import SolaraViz, make_plot_measure
54
from sugarscape_g1mt.model import SugarscapeG1mt
65
from sugarscape_g1mt.trader_agents import Trader
76

7+
from mesa.visualization import SolaraViz, make_plot_measure
8+
89

910
def SpaceDrawer(model):
1011
def portray(g):

examples/advanced/sugarscape_g1mt/run.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import sys
22

33
import matplotlib.pyplot as plt
4-
import mesa
54
import networkx as nx
65
import pandas as pd
76
from sugarscape_g1mt.model import SugarscapeG1mt
87
from sugarscape_g1mt.server import server
98

9+
import mesa
10+
1011

1112
# Analysis
1213
def assess_results(results, single_agent):

examples/advanced/sugarscape_g1mt/sugarscape_g1mt/model.py

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from pathlib import Path
22

3-
import mesa
43
import numpy as np
4+
5+
import mesa
56
from mesa.experimental.cell_space import OrthogonalVonNeumannGrid
67

78
from .resource_agents import Resource
@@ -10,23 +11,19 @@
1011

1112
# Helper Functions
1213
def flatten(list_of_lists):
13-
"""
14-
helper function for model datacollector for trade price
14+
"""Helper function for model datacollector for trade price
1515
collapses agent price list into one list
1616
"""
1717
return [item for sublist in list_of_lists for item in sublist]
1818

1919

2020
def geometric_mean(list_of_prices):
21-
"""
22-
find the geometric mean of a list of prices
23-
"""
21+
"""Find the geometric mean of a list of prices"""
2422
return np.exp(np.log(list_of_prices).mean())
2523

2624

2725
def get_trade(agent):
28-
"""
29-
For agent reporters in data collector
26+
"""For agent reporters in data collector
3027
3128
return list of trade partners and None for other agents
3229
"""
@@ -37,9 +34,7 @@ def get_trade(agent):
3734

3835

3936
class SugarscapeG1mt(mesa.Model):
40-
"""
41-
Manager class to run Sugarscape with Traders
42-
"""
37+
"""Manager class to run Sugarscape with Traders"""
4338

4439
def __init__(
4540
self,
@@ -125,8 +120,7 @@ def __init__(
125120
)
126121

127122
def step(self):
128-
"""
129-
Unique step function that does staged activation of sugar and spice
123+
"""Unique step function that does staged activation of sugar and spice
130124
and then randomly activates traders
131125
"""
132126
# step Resource agents

examples/advanced/sugarscape_g1mt/sugarscape_g1mt/resource_agents.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33

44
class Resource(FixedAgent):
5-
"""
6-
Resource:
5+
"""Resource:
76
- contains an amount of sugar and spice
87
- grows 1 amount of sugar at each turn
98
- grows 1 amount of spice at each turn
@@ -18,8 +17,7 @@ def __init__(self, model, max_sugar, max_spice, cell):
1817
self.cell = cell
1918

2019
def step(self):
21-
"""
22-
Growth function, adds one unit of sugar and spice each step up to
20+
"""Growth function, adds one unit of sugar and spice each step up to
2321
max amount
2422
"""
2523
self.sugar_amount = min([self.max_sugar, self.sugar_amount + 1])

0 commit comments

Comments
 (0)