Skip to content

Commit c70a401

Browse files
authored
Sync pyproject with mesa (#268)
* Sync the pre-commit config from the Mesa repository * Add the codespellignore config from the Mesa repository * refactor(pyproject.toml): update the file to match the mesa repo a bit closer * refactor: various ruff fixes after syncing the rules with the mesa repo * refactor: more ruff fixes * chore: updated ruff fixes after merging with main * chore: remove the hard-codes python version for ruff
1 parent 7512f6b commit c70a401

File tree

46 files changed

+214
-315
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+214
-315
lines changed

examples/aco_tsp/aco_tsp/model.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,10 @@ def from_tsp_file(cls, file_path: str) -> "TSPGraph":
7979

8080

8181
class AntTSP(CellAgent):
82-
"""
83-
An agent
84-
"""
82+
"""An agent"""
8583

8684
def __init__(self, model, alpha: float = 1.0, beta: float = 5.0):
87-
"""
88-
Customize the agent
89-
"""
85+
"""Customize the agent"""
9086
super().__init__(model)
9187
self.alpha = alpha
9288
self.beta = beta
@@ -141,11 +137,9 @@ def decide_next_city(self):
141137
return new_city
142138

143139
def step(self):
144-
"""
145-
Modify this method to change what an individual agent will do during each step.
140+
"""Modify this method to change what an individual agent will do during each step.
146141
Can include logic based on neighbors states.
147142
"""
148-
149143
for _ in range(self.model.num_cities - 1):
150144
# Pick a random city that isn't in the list of cities visited
151145
new_city = self.decide_next_city()
@@ -158,8 +152,7 @@ def step(self):
158152

159153

160154
class AcoTspModel(mesa.Model):
161-
"""
162-
The model class holds the model-level attributes, manages the agents, and generally handles
155+
"""The model class holds the model-level attributes, manages the agents, and generally handles
163156
the global level of our model.
164157
165158
There is only one model-level parameter: how many agents the model contains. When a new model
@@ -168,12 +161,16 @@ class AcoTspModel(mesa.Model):
168161

169162
def __init__(
170163
self,
164+
tsp_graph: TSPGraph | None,
171165
num_agents: int = 20,
172-
tsp_graph: TSPGraph = TSPGraph.from_random(20),
173166
max_steps: int = int(1e6),
174167
ant_alpha: float = 1.0,
175168
ant_beta: float = 5.0,
176169
):
170+
# Don't create the TSPGraph.from_random as argument default: https://docs.astral.sh/ruff/rules/function-call-in-default-argument/
171+
if tsp_graph is None:
172+
tsp_graph = TSPGraph.from_random(20)
173+
177174
super().__init__()
178175
self.num_agents = num_agents
179176
self.tsp_graph = tsp_graph
@@ -224,15 +221,13 @@ def update_pheromone(self, q: float = 100, ro: float = 0.5):
224221
# Evaporate
225222
tau_ij = (1 - ro) * self.grid.G[i][j]["pheromone"]
226223
# Add ant's contribution
227-
for k, delta_tau_ij_k in delta_tau_ij.items():
224+
for _, delta_tau_ij_k in delta_tau_ij.items():
228225
tau_ij += delta_tau_ij_k.get((i, j), 0.0)
229226

230227
self.grid.G[i][j]["pheromone"] = tau_ij
231228

232229
def step(self):
233-
"""
234-
A model step. Used for activating the agents and collecting data.
235-
"""
230+
"""A model step. Used for activating the agents and collecting data."""
236231
self.agents.shuffle_do("step")
237232
self.update_pheromone()
238233

examples/aco_tsp/app.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"""
2-
Configure visualization elements and instantiate a server
3-
"""
1+
"""Configure visualization elements and instantiate a server"""
42

53
import networkx as nx
64
import solara

examples/bank_reserves/bank_reserves/agents.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""
2-
The following code was adapted from the Bank Reserves model included in Netlogo
1+
"""The following code was adapted from the Bank Reserves model included in Netlogo
32
Model information can be found at:
43
http://ccl.northwestern.edu/netlogo/models/BankReserves
54
Accessed on: November 2, 2017
@@ -18,7 +17,8 @@ class Bank:
1817
class. This is because there is only one bank in this model, and it does not
1918
use any Mesa-specific features like the scheduler or the grid, and doesn't
2019
have a step method. It is just used to keep track of the bank's reserves and
21-
the amount it can loan out, for Person agents to interact with."""
20+
the amount it can loan out, for Person agents to interact with.
21+
"""
2222

2323
def __init__(self, model, reserve_percent=50):
2424
self.model = model
@@ -63,8 +63,9 @@ def __init__(self, model, moore, bank, rich_threshold):
6363
self.bank = bank
6464

6565
def do_business(self):
66-
"""check if person has any savings, any money in wallet, or if the
67-
bank can loan them any money"""
66+
"""Check if person has any savings, any money in wallet, or if the
67+
bank can loan them any money
68+
"""
6869
if (self.savings > 0 or self.wallet > 0 or self.bank.bank_to_loan > 0) and len(
6970
self.cell.agents
7071
) > 1:
@@ -164,8 +165,9 @@ def repay_a_loan(self, amount):
164165

165166
# part of balance_books()
166167
def take_out_loan(self, amount):
167-
"""borrow from the bank to put money in my wallet, and increase my
168-
outstanding loans"""
168+
"""Borrow from the bank to put money in my wallet, and increase my
169+
outstanding loans
170+
"""
169171
self.loans += amount
170172
self.wallet += amount
171173
# decresae the amount the bank can loan right now

examples/bank_reserves/bank_reserves/model.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""
2-
The following code was adapted from the Bank Reserves model included in Netlogo
1+
"""The following code was adapted from the Bank Reserves model included in Netlogo
32
Model information can be found at:
43
http://ccl.northwestern.edu/netlogo/models/BankReserves
54
Accessed on: November 2, 2017
@@ -25,39 +24,34 @@
2524

2625

2726
def get_num_rich_agents(model):
28-
"""return number of rich agents"""
29-
27+
"""Return number of rich agents"""
3028
rich_agents = [a for a in model.agents if a.savings > model.rich_threshold]
3129
return len(rich_agents)
3230

3331

3432
def get_num_poor_agents(model):
35-
"""return number of poor agents"""
36-
33+
"""Return number of poor agents"""
3734
poor_agents = [a for a in model.agents if a.loans > 10]
3835
return len(poor_agents)
3936

4037

4138
def get_num_mid_agents(model):
42-
"""return number of middle class agents"""
43-
39+
"""Return number of middle class agents"""
4440
mid_agents = [
4541
a for a in model.agents if a.loans < 10 and a.savings < model.rich_threshold
4642
]
4743
return len(mid_agents)
4844

4945

5046
def get_total_savings(model):
51-
"""sum of all agents' savings"""
52-
47+
"""Sum of all agents' savings"""
5348
agent_savings = [a.savings for a in model.agents]
5449
# return the sum of agents' savings
5550
return np.sum(agent_savings)
5651

5752

5853
def get_total_wallets(model):
59-
"""sum of amounts of all agents' wallets"""
60-
54+
"""Sum of amounts of all agents' wallets"""
6155
agent_wallets = [a.wallet for a in model.agents]
6256
# return the sum of all agents' wallets
6357
return np.sum(agent_wallets)
@@ -80,8 +74,7 @@ def get_total_loans(model):
8074

8175

8276
class BankReservesModel(mesa.Model):
83-
"""
84-
This model is a Mesa implementation of the Bank Reserves model from NetLogo.
77+
"""This model is a Mesa implementation of the Bank Reserves model from NetLogo.
8578
It is a highly abstracted, simplified model of an economy, with only one
8679
type of agent and a single bank representing all banks in an economy. People
8780
(represented by circles) move randomly within the grid. If two or more people
@@ -160,5 +153,5 @@ def step(self):
160153
self.datacollector.collect(self)
161154

162155
def run_model(self):
163-
for i in range(self.run_time):
156+
for _ in range(self.run_time):
164157
self.step()

examples/bank_reserves/batch_run.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""
2-
The following code was adapted from the Bank Reserves model included in Netlogo
1+
"""The following code was adapted from the Bank Reserves model included in Netlogo
32
Model information can be found at:
43
http://ccl.northwestern.edu/netlogo/models/BankReserves
54
Accessed on: November 2, 2017

examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ def step(self):
4040
def compute_gini(self):
4141
agent_wealths = [agent.wealth for agent in self.agents]
4242
x = sorted(agent_wealths)
43-
N = self.num_agents
44-
B = sum(xi * (N - i) for i, xi in enumerate(x)) / (N * sum(x))
45-
return 1 + (1 / N) - 2 * B
43+
num_agents = self.num_agents
44+
B = sum(xi * (num_agents - i) for i, xi in enumerate(x)) / (num_agents * sum(x)) # noqa: N806
45+
return 1 + (1 / num_agents) - 2 * B

examples/caching_and_replay/cacheablemodel.py

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

44

55
class CacheableSchelling(CacheableModel):
6-
"""
7-
A wrapper around the original Schelling model to make the simulation cacheable
6+
"""A wrapper around the original Schelling model to make the simulation cacheable
87
and replay-able. Uses CacheableModel from the Mesa-Replay library,
98
which is a wrapper that can be put around any regular mesa model to make it
109
"cacheable".

examples/caching_and_replay/model.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55

66

77
class SchellingAgent(CellAgent):
8-
"""
9-
Schelling segregation agent
10-
"""
8+
"""Schelling segregation agent"""
119

1210
def __init__(self, model, agent_type):
13-
"""
14-
Create a new Schelling agent.
11+
"""Create a new Schelling agent.
1512
1613
Args:
1714
x, y: Agent initial location.
@@ -34,9 +31,7 @@ def step(self):
3431

3532

3633
class Schelling(mesa.Model):
37-
"""
38-
Model class for the Schelling segregation model.
39-
"""
34+
"""Model class for the Schelling segregation model."""
4035

4136
def __init__(
4237
self,
@@ -48,8 +43,7 @@ def __init__(
4843
minority_pc=0.3,
4944
seed=None,
5045
):
51-
"""
52-
Create a new Schelling model.
46+
"""Create a new Schelling model.
5347
5448
Args:
5549
width, height: Size of the space.
@@ -59,7 +53,6 @@ def __init__(
5953
radius: Search radius for checking similarity
6054
seed: Seed for Reproducibility
6155
"""
62-
6356
super().__init__(seed=seed)
6457
self.height = height
6558
self.width = width
@@ -88,9 +81,7 @@ def __init__(
8881
self.datacollector.collect(self)
8982

9083
def step(self):
91-
"""
92-
Run one step of the model.
93-
"""
84+
"""Run one step of the model."""
9485
self.happy = 0 # Reset counter of happy agents
9586
self.agents.shuffle_do("step")
9687

examples/caching_and_replay/run.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111

1212
def get_cache_file_status(_):
13-
"""
14-
Display an informational text about caching and the status of the cache file (existing versus not existing)
15-
"""
13+
"""Display an informational text about caching and the status of the cache file (existing versus not existing)"""
1614
cache_file = Path(model_params["cache_file_path"])
1715
return (
1816
f"Only activate the 'Replay cached run?' switch when a cache file already exists, otherwise it will fail. "

examples/caching_and_replay/server.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@
55

66

77
def get_happy_agents(model):
8-
"""
9-
Display a text count of how many happy agents there are.
10-
"""
8+
"""Display a text count of how many happy agents there are."""
119
return f"Happy agents: {model.happy}"
1210

1311

1412
def schelling_draw(agent):
15-
"""
16-
Portrayal Method for canvas
17-
"""
13+
"""Portrayal Method for canvas"""
1814
if agent is None:
1915
return
2016
portrayal = {"Shape": "circle", "r": 0.5, "Filled": "true", "Layer": 0}

0 commit comments

Comments
 (0)