Skip to content

Commit c771e3b

Browse files
committed
Merge remote-tracking branch 'upstream/main'
2 parents 077aca5 + 3fce592 commit c771e3b

Some content is hidden

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

65 files changed

+1249
-1014
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""init file for BoltzmannWealth module."""

benchmarks/BoltzmannWealth/boltzmann_wealth.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1-
# https://github.com/projectmesa/mesa-examples/blob/main/examples/boltzmann_wealth_model_experimental/model.py
1+
"""boltmann wealth model for performance benchmarking.
2+
3+
https://github.com/projectmesa/mesa-examples/blob/main/examples/boltzmann_wealth_model_experimental/model.py
4+
"""
5+
26
import mesa
37

48

59
def compute_gini(model):
10+
"""Calculate gini for wealth in model.
11+
12+
Args:
13+
model: a Model instance
14+
15+
Returns:
16+
float: gini score
17+
18+
"""
619
agent_wealths = [agent.wealth for agent in model.agents]
720
x = sorted(agent_wealths)
821
n = model.num_agents
@@ -19,7 +32,15 @@ class BoltzmannWealth(mesa.Model):
1932
"""
2033

2134
def __init__(self, seed=None, n=100, width=10, height=10):
22-
super().__init__()
35+
"""Initializes the model.
36+
37+
Args:
38+
seed: the seed for random number generator
39+
n: the number of agents
40+
width: the width of the grid
41+
height: the height of the grid
42+
"""
43+
super().__init__(seed)
2344
self.num_agents = n
2445
self.grid = mesa.space.MultiGrid(width, height, True)
2546
self.schedule = mesa.time.RandomActivation(self)
@@ -38,11 +59,18 @@ def __init__(self, seed=None, n=100, width=10, height=10):
3859
self.datacollector.collect(self)
3960

4061
def step(self):
62+
"""Run the model for a single step."""
4163
self.agents.shuffle().do("step")
4264
# collect data
4365
self.datacollector.collect(self)
4466

4567
def run_model(self, n):
68+
"""Run the model for n steps.
69+
70+
Args:
71+
n: the number of steps for which to run the model
72+
73+
"""
4674
for _i in range(n):
4775
self.step()
4876

@@ -51,17 +79,24 @@ class MoneyAgent(mesa.Agent):
5179
"""An agent with fixed initial wealth."""
5280

5381
def __init__(self, model):
82+
"""Instantiate an agent.
83+
84+
Args:
85+
model: a Model instance
86+
"""
5487
super().__init__(model)
5588
self.wealth = 1
5689

5790
def move(self):
91+
"""Move the agent to a random neighboring cell."""
5892
possible_steps = self.model.grid.get_neighborhood(
5993
self.pos, moore=True, include_center=False
6094
)
6195
new_position = self.random.choice(possible_steps)
6296
self.model.grid.move_agent(self, new_position)
6397

6498
def give_money(self):
99+
"""Give money to a random cell mate."""
65100
cellmates = self.model.grid.get_cell_list_contents([self.pos])
66101
cellmates.pop(
67102
cellmates.index(self)
@@ -72,6 +107,7 @@ def give_money(self):
72107
self.wealth -= 1
73108

74109
def step(self):
110+
"""Run the agent for 1 step."""
75111
self.move()
76112
if self.wealth > 0:
77113
self.give_money()

benchmarks/Flocking/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""initi for flocking benchmark model."""

benchmarks/Flocking/flocking.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
"""
2-
Flockers
3-
=============================================================
4-
A Mesa implementation of Craig Reynolds's Boids flocker model.
1+
"""A Mesa implementation of Craig Reynolds's Boids flocker model.
2+
53
Uses numpy arrays to represent vectors.
64
"""
75

@@ -11,8 +9,7 @@
119

1210

1311
class Boid(mesa.Agent):
14-
"""
15-
A Boid-style flocker agent.
12+
"""A Boid-style flocker agent.
1613
1714
The agent follows three behaviors to flock:
1815
- Cohesion: steering towards neighboring agents.
@@ -36,10 +33,10 @@ def __init__(
3633
separate=0.015,
3734
match=0.05,
3835
):
39-
"""
40-
Create a new Boid flocker agent.
36+
"""Create a new Boid flocker agent.
4137
4238
Args:
39+
model: a Model instance
4340
speed: Distance to move per step.
4441
direction: numpy vector for the Boid's direction of movement.
4542
vision: Radius to look around for nearby Boids.
@@ -59,10 +56,7 @@ def __init__(
5956
self.match_factor = match
6057

6158
def step(self):
62-
"""
63-
Get the Boid's neighbors, compute the new vector, and move accordingly.
64-
"""
65-
59+
"""Get the Boid's neighbors, compute the new vector, and move accordingly."""
6660
neighbors = self.model.space.get_neighbors(self.pos, self.vision, False)
6761
n = 0
6862
match_vector, separation_vector, cohere = np.zeros((3, 2))
@@ -84,9 +78,7 @@ def step(self):
8478

8579

8680
class BoidFlockers(mesa.Model):
87-
"""
88-
Flocker model class. Handles agent creation, placement and scheduling.
89-
"""
81+
"""Flocker model class. Handles agent creation, placement and scheduling."""
9082

9183
def __init__(
9284
self,
@@ -102,18 +94,21 @@ def __init__(
10294
match=0.05,
10395
simulator=None,
10496
):
105-
"""
106-
Create a new Flockers model.
97+
"""Create a new Flockers model.
10798
10899
Args:
100+
seed: seed for random number generator
109101
population: Number of Boids
110-
width, height: Size of the space.
102+
width: the width of the space
103+
height: the height of the space
111104
speed: How fast should the Boids move.
112105
vision: How far around should each Boid look for its neighbors
113-
separation: What's the minimum distance each Boid will attempt to
114-
keep from any other
115-
cohere, separate, match: factors for the relative importance of
106+
separation: What's the minimum distance each Boid will attempt to keep from any other
107+
cohere: the relative importance of matching neighbors' positions'
108+
separate: the relative importance of avoiding close neighbors
109+
match: factors for the relative importance of
116110
the three drives.
111+
simulator: a Simulator Instance
117112
"""
118113
super().__init__(seed=seed)
119114
self.population = population
@@ -146,6 +141,7 @@ def __init__(
146141
self.schedule.add(boid)
147142

148143
def step(self):
144+
"""Run the model for one step."""
149145
self.schedule.step()
150146

151147

benchmarks/Schelling/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Schelling separation for performance benchmarking."""

benchmarks/Schelling/schelling.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1+
"""Schelling separation for performance benchmarking."""
2+
13
from mesa import Model
24
from mesa.experimental.cell_space import CellAgent, OrthogonalMooreGrid
35
from mesa.time import RandomActivation
46

57

68
class SchellingAgent(CellAgent):
7-
"""
8-
Schelling segregation agent
9-
"""
9+
"""Schelling segregation agent."""
1010

1111
def __init__(self, model, agent_type, radius, homophily):
12-
"""
13-
Create a new Schelling agent.
12+
"""Create a new Schelling agent.
13+
1414
Args:
15-
x, y: Agent initial location.
16-
agent_type: Indicator for the agent's type (minority=1, majority=0)
15+
model: model instance
16+
agent_type: type of agent (minority=1, majority=0)
17+
radius: size of neighborhood of agent
18+
homophily: fraction of neighbors of the same type that triggers movement
1719
"""
1820
super().__init__(model)
1921
self.type = agent_type
2022
self.radius = radius
2123
self.homophily = homophily
2224

2325
def step(self):
26+
"""Run one step of the agent."""
2427
similar = 0
2528
neighborhood = self.cell.neighborhood(radius=self.radius)
2629
for neighbor in neighborhood.agents:
@@ -35,9 +38,7 @@ def step(self):
3538

3639

3740
class Schelling(Model):
38-
"""
39-
Model class for the Schelling segregation model.
40-
"""
41+
"""Model class for the Schelling segregation model."""
4142

4243
def __init__(
4344
self,
@@ -50,16 +51,17 @@ def __init__(
5051
seed=None,
5152
simulator=None,
5253
):
53-
"""
54-
Create a new Schelling model.
54+
"""Create a new Schelling model.
5555
5656
Args:
57-
height, width: Size of the space.
58-
density: Initial Chance for a cell to populated
59-
minority_pc: Chances for an agent to be in minority class
57+
height: height of the grid
58+
width: width of the grid
6059
homophily: Minimum number of agents of same class needed to be happy
6160
radius: Search radius for checking similarity
62-
seed: Seed for Reproducibility
61+
density: Initial Chance for a cell to populated
62+
minority_pc: Chances for an agent to be in minority class
63+
seed: the seed for the random number generator
64+
simulator: a simulator instance
6365
"""
6466
super().__init__(seed=seed)
6567
self.minority_pc = minority_pc
@@ -85,9 +87,7 @@ def __init__(
8587
self.schedule.add(agent)
8688

8789
def step(self):
88-
"""
89-
Run one step of the model.
90-
"""
90+
"""Run one step of the model."""
9191
self.happy = 0 # Reset counter of happy agents
9292
self.schedule.step()
9393

benchmarks/WolfSheep/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Wolf-Sheep Predation Model for performance benchmarking."""

0 commit comments

Comments
 (0)