Skip to content

Commit 41fa4ed

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

File tree

14 files changed

+44
-104
lines changed

14 files changed

+44
-104
lines changed

examples/color_patches/color_patches/model.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"""
2-
The model - a 2D lattice where agents live and have an opinion
3-
"""
1+
"""The model - a 2D lattice where agents live and have an opinion"""
42

53
from collections import Counter
64

@@ -14,16 +12,12 @@
1412

1513

1614
class ColorCell(CellAgent):
17-
"""
18-
Represents a cell's opinion (visualized by a color)
19-
"""
15+
"""Represents a cell's opinion (visualized by a color)"""
2016

2117
OPINIONS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
2218

2319
def __init__(self, model, initial_state):
24-
"""
25-
Create a cell, in the given state, at the given row, col position.
26-
"""
20+
"""Create a cell, in the given state, at the given row, col position."""
2721
super().__init__(model)
2822
self.state = initial_state
2923
self.next_state = None
@@ -37,8 +31,7 @@ def get_row(self):
3731
return self.cell.coordinate[1]
3832

3933
def determine_opinion(self):
40-
"""
41-
Determines the agent opinion for the next step by polling its neighbors
34+
"""Determines the agent opinion for the next step by polling its neighbors
4235
The opinion is determined by the majority of the 8 neighbors' opinion
4336
A choice is made at random in case of a tie
4437
The next state is stored until all cells have been polled
@@ -55,20 +48,15 @@ def determine_opinion(self):
5548
self.next_state = self.random.choice(tied_opinions)[0]
5649

5750
def assume_opinion(self):
58-
"""
59-
Set the state of the agent to the next state
60-
"""
51+
"""Set the state of the agent to the next state"""
6152
self.state = self.next_state
6253

6354

6455
class ColorPatches(mesa.Model):
65-
"""
66-
represents a 2D lattice where agents live
67-
"""
56+
"""represents a 2D lattice where agents live"""
6857

6958
def __init__(self, width=20, height=20):
70-
"""
71-
Create a 2D lattice with strict borders where agents live
59+
"""Create a 2D lattice with strict borders where agents live
7260
The agents next state is first determined before updating the grid
7361
"""
7462
super().__init__()
@@ -88,8 +76,7 @@ def __init__(self, width=20, height=20):
8876
self.running = True
8977

9078
def step(self):
91-
"""
92-
Perform the model step in two stages:
79+
"""Perform the model step in two stages:
9380
- First, all agents determine their next opinion based on their neighbors current opinions
9481
- Then, all agents update their opinion to the next opinion
9582
"""
@@ -98,8 +85,7 @@ def step(self):
9885

9986
@property
10087
def grid(self):
101-
"""
102-
/mesa/visualization/modules/CanvasGridVisualization.py
88+
"""/mesa/visualization/modules/CanvasGridVisualization.py
10389
is directly accessing Model.grid
10490
76 def render(self, model):
10591
77 grid_state = defaultdict(list)

examples/hex_ant/agent.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ class AntState(Enum):
99

1010

1111
class Ant(CellAgent):
12-
"""
13-
An Ant agent that navigates the HexGrid using pheromones.
14-
"""
12+
"""An Ant agent that navigates the HexGrid using pheromones."""
1513

1614
def __init__(self, model):
1715
super().__init__(model)
@@ -22,13 +20,11 @@ def __init__(self, model):
2220
self.wiggle_bias = self.random.uniform(-0.1, 0.1)
2321

2422
def step(self):
25-
"""
26-
Agent Life Cycle:
23+
"""Agent Life Cycle:
2724
1. SENSE: Check neighbors.
2825
2. DECIDE: Switch state if needed.
2926
3. ACT: Move and deposit pheromones.
3027
"""
31-
3228
# --- State Logic ---
3329
if self.state == AntState.FORAGING:
3430
self._step_foraging()
@@ -76,8 +72,7 @@ def _drop_food(self):
7672
self.state = AntState.FORAGING
7773

7874
def _move_towards_gradient(self, layer_name, randomness=0.1):
79-
"""
80-
Find the neighbor with the highest value in 'layer_name'.
75+
"""Find the neighbor with the highest value in 'layer_name'.
8176
With some probability, move randomly to explore.
8277
"""
8378
if self.random.random() < randomness:

examples/hex_ant/app.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111

1212
def agent_portrayal(agent):
13-
"""
14-
Portrayal for the Ant agents.
15-
"""
13+
"""Portrayal for the Ant agents."""
1614
if agent.state == AntState.FORAGING:
1715
return {"color": "tab:red", "size": 19}
1816
elif agent.state == AntState.RETURNING:
@@ -21,9 +19,7 @@ def agent_portrayal(agent):
2119

2220

2321
def property_portrayal(layer):
24-
"""
25-
Portrayal for the Environment (PropertyLayers).
26-
"""
22+
"""Portrayal for the Environment (PropertyLayers)."""
2723
if layer.name == "food":
2824
# Dense Food clusters
2925
return PropertyLayerStyle(

examples/hex_ant/model.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66

77
class AntForaging(mesa.Model):
8-
"""
9-
Au ant foraging model on a Hexagonal Grid.
8+
"""Au ant foraging model on a Hexagonal Grid.
109
1110
This demonstrates the power of PropertyLayers for efficient environmental
1211
simulation (pheromones) combined with complex agent movement.
@@ -98,9 +97,7 @@ def step(self):
9897
self.agents.shuffle_do("step")
9998

10099
def _update_pheromone_layer(self, layer_name):
101-
"""
102-
Apply evaporation to a pheromone layer.
103-
"""
100+
"""Apply evaporation to a pheromone layer."""
104101
np_layer = self.grid.property_layers[layer_name]
105102
np_layer *= 1.0 - self.evaporation_rate
106103

examples/humanitarian_aid_distribution/app.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66

77
def agent_portrayal(agent):
8-
"""
9-
Defines how agents look in the browser visualization.
8+
"""Defines how agents look in the browser visualization.
109
1110
Color Coding for Beneficiaries (Needs-Based status):
1211
- RED -> Desperate

examples/humanitarian_aid_distribution/humanitarian_aid_distribution/agents.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33

44
class Beneficiary(CellAgent):
5-
"""
6-
Simulates a person with dynamic needs (water and food).
5+
"""Simulates a person with dynamic needs (water and food).
76
87
The agent follows a Needs-Based Architecture:
98
1. Biological drives (hunger/thirst) increase over time.
@@ -21,8 +20,7 @@ class Beneficiary(CellAgent):
2120
def __init__(
2221
self, model, cell, water_decay=2, food_decay=1, critical_days_threshold=5
2322
):
24-
"""
25-
Create a new Beneficiary agent.
23+
"""Create a new Beneficiary agent.
2624
2725
Args:
2826
model: The Mesa model instance.
@@ -49,9 +47,7 @@ def __init__(
4947
CRITICAL = 90 # "Emergency"
5048

5149
def move_towards(self, target_pos):
52-
"""
53-
Moves the agent one step closer to the target position.
54-
"""
50+
"""Moves the agent one step closer to the target position."""
5551
current_x, current_y = self.cell.coordinate
5652
target_x, target_y = target_pos
5753

@@ -72,8 +68,7 @@ def move_towards(self, target_pos):
7268
self.cell = self.model.grid[(next_x, next_y)]
7369

7470
def step(self):
75-
"""
76-
Advance the agent by one step.
71+
"""Advance the agent by one step.
7772
7873
Lifecycle:
7974
1. Biological Decay: Needs increase naturally.
@@ -151,9 +146,9 @@ def wander(self):
151146
self.cell = self.cell.neighborhood.select_random_cell()
152147

153148
def find_nearest_truck(self, radius=None):
154-
"""
155-
Scans the neighborhood for a Truck agent.
149+
"""Scans the neighborhood for a Truck agent.
156150
Returns the nearest Truck agent or None.
151+
157152
Args:
158153
radius (int, optional): limit search to this distance. None = global.
159154
"""
@@ -184,8 +179,7 @@ def get_dist(t):
184179

185180

186181
class Truck(CellAgent):
187-
"""
188-
A delivery agent that distributes supplies to Beneficiaries.
182+
"""A delivery agent that distributes supplies to Beneficiaries.
189183
190184
Behavior:
191185
- Scans for beneficiaries with high needs.
@@ -206,7 +200,6 @@ def __init__(self, model, cell):
206200

207201
def distribute_aid(self, beneficiary, amount=10):
208202
"""Split aid proportionally to need intensity"""
209-
210203
total_need = beneficiary.water_urgency + beneficiary.food_urgency
211204

212205
if total_need == 0:
@@ -231,9 +224,7 @@ def distribute_aid(self, beneficiary, amount=10):
231224
return water_satisfied + food_satisfied
232225

233226
def move_towards(self, target_pos):
234-
"""
235-
Moves the agent one step closer to the target position.
236-
"""
227+
"""Moves the agent one step closer to the target position."""
237228
current_x, current_y = self.cell.coordinate
238229
target_x, target_y = target_pos
239230

@@ -255,8 +246,7 @@ def get_distance(self, pos):
255246
return abs(x1 - x2) + abs(y1 - y2)
256247

257248
def step(self):
258-
"""
259-
Advance the truck by one step.
249+
"""Advance the truck by one step.
260250
261251
Lifecycle:
262252
1. Logistics: Refill if empty.

examples/humanitarian_aid_distribution/humanitarian_aid_distribution/model.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66

77
class HumanitarianModel(mesa.Model):
8-
"""
9-
The central environment for the Needs-Based Architecture simulation.
8+
"""The central environment for the Needs-Based Architecture simulation.
109
1110
This model manages:
1211
1. A grid space where agents interact.
@@ -28,8 +27,7 @@ def __init__(
2827
rng=None,
2928
critical_days_threshold=5,
3029
):
31-
"""
32-
Create a new Humanitarian model with the given parameters.
30+
"""Create a new Humanitarian model with the given parameters.
3331
3432
Args:
3533
num_beneficiaries (int): Number of beneficiaries to create.
@@ -69,9 +67,7 @@ def __init__(
6967
self.running = True
7068

7169
def create_agents(self):
72-
"""
73-
Create and place agents using high-level grid methods.
74-
"""
70+
"""Create and place agents using high-level grid methods."""
7571
random_cells = self.random.choices(
7672
list(self.grid.all_cells), k=self.num_beneficiaries
7773
)
@@ -92,8 +88,7 @@ def create_agents(self):
9288
)
9389

9490
def step(self):
95-
"""
96-
Advance the model by one step.
91+
"""Advance the model by one step.
9792
9893
Lifecycle:
9994
1. Collect Data: Record current model stats (e.g. Average Urgency).
@@ -104,8 +99,7 @@ def step(self):
10499

105100
@staticmethod
106101
def get_average_urgency(model):
107-
"""
108-
Helper for data collection: Calculates the average urgency of all beneficiaries.
102+
"""Helper for data collection: Calculates the average urgency of all beneficiaries.
109103
110104
Args:
111105
model (HumanitarianModel): The model instance.

examples/rumor_mill/app.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
"""
2-
Visualization interface for the Rumor Mill model using Mesa's SolaraViz.
3-
"""
1+
"""Visualization interface for the Rumor Mill model using Mesa's SolaraViz."""
42

53
from mesa.visualization import SolaraViz, SpaceRenderer, make_plot_component
64
from mesa.visualization.components import AgentPortrayalStyle
75
from rumor_mill.model import RumorMillModel
86

97

108
def agent_portrayal(agent):
11-
"""
12-
Define how agents are displayed in the grid visualization.
9+
"""Define how agents are displayed in the grid visualization.
1310
Red = knows rumor, Blue = doesn't know rumor
1411
"""
1512
return AgentPortrayalStyle(color="red" if agent.knows_rumor else "blue", size=50)

examples/rumor_mill/rumor_mill/agent.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33

44
class Person(CellAgent):
5-
"""
6-
A person agent that can know and spread a rumor.
7-
"""
5+
"""A person agent that can know and spread a rumor."""
86

97
def __init__(self, model, cell, rumor_spread_chance=0.5, color=None):
10-
"""
11-
Initialize a Person agent.
8+
"""Initialize a Person agent.
129
1310
Args:
1411
model: The model instance
@@ -26,9 +23,7 @@ def __init__(self, model, cell, rumor_spread_chance=0.5, color=None):
2623
self.color = color if color is not None else self.random.choice(["red", "blue"])
2724

2825
def step(self):
29-
"""
30-
Agent behavior each step: if knows rumor, tell a random neighbor.
31-
"""
26+
"""Agent behavior each step: if knows rumor, tell a random neighbor."""
3227
if self.knows_rumor:
3328
# Get all neighbors in the cell's neighborhood (excluding self)
3429
neighbors = [

examples/rumor_mill/rumor_mill/model.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66

77

88
class RumorMillModel(Model):
9-
"""
10-
Model simulating rumor spread through a population on a grid.
11-
"""
9+
"""Model simulating rumor spread through a population on a grid."""
1210

1311
def __init__(
1412
self,
@@ -19,8 +17,7 @@ def __init__(
1917
eight_neightborhood=False,
2018
rng=None,
2119
):
22-
"""
23-
Initialize the Rumor Mill model.
20+
"""Initialize the Rumor Mill model.
2421
2522
Args:
2623
width: Grid width

0 commit comments

Comments
 (0)