Skip to content

Commit 82d3c19

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

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed

examples/continuous_predator_prey/prey_predator/agents.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import math
2+
23
from mesa.experimental.continuous_space import ContinuousSpaceAgent
34

45

56
class Prey(ContinuousSpaceAgent):
6-
#a prey agent which has random motion in continuous space
7+
# a prey agent which has random motion in continuous space
78

89
def __init__(self, space, model, pos, speed=1.0, max_age=40):
910
# unique_id is handled automatically by super()
1011
super().__init__(space, model)
1112
self.position = pos
1213
self.speed = speed
13-
14+
1415
# we need give them random starting age so they don't all die on the exact same step!
1516
self.age = self.random.randint(0, max_age)
1617
self.max_age = max_age # it's maximum lifespan of prey agent
@@ -37,7 +38,7 @@ def random_move(self):
3738

3839
def step(self):
3940
# it calls the random move function to move the agent in each step of the simulation
40-
41+
4142
# 1. DYING OLD: increase age, and die if too old
4243
self.age += 1 # age increasing each step
4344
if self.age > self.max_age:
@@ -47,27 +48,34 @@ def step(self):
4748
self.random_move()
4849

4950
# check surroundings for mating and overcrowding
50-
neighbors, _ = self.get_neighbors_in_radius(radius=2.5) # it get nearby agents within radius
51-
prey_neighbors = [n for n in neighbors if isinstance(n, Prey)] # it filter to find only prey
51+
neighbors, _ = self.get_neighbors_in_radius(
52+
radius=2.5
53+
) # it get nearby agents within radius
54+
prey_neighbors = [
55+
n for n in neighbors if isinstance(n, Prey)
56+
] # it filter to find only prey
5257

5358
# 2. MATING & CROWDING: must have at least 1 mate nearby (>0),
5459
# but won't reproduce if it's too overcrowded (<6)
5560
# it check not too lonely and not too crowded
56-
if 0 < len(prey_neighbors) < 6 and self.random.random() < self.model.prey_reproduce:
61+
if (
62+
0 < len(prey_neighbors) < 6
63+
and self.random.random() < self.model.prey_reproduce
64+
):
5765
# create new prey at the exact same position (asexual reproduction)
5866
Prey(self.model.space, self.model, self.position, self.speed, self.max_age)
5967

6068

6169
class Predator(ContinuousSpaceAgent):
62-
#a predator agent which moves randomly but also hunts nearby prey
70+
# a predator agent which moves randomly but also hunts nearby prey
6371

6472
def __init__(self, space, model, pos, speed=1.5, energy=0, max_age=60):
6573
# unique_id is handled automatically by super()
6674
super().__init__(space, model)
6775
self.position = pos
6876
self.speed = speed
6977
self.energy = energy # energy level of the predator; it need for survival and reproduction
70-
78+
7179
# dying old - we need give them random starting age
7280
self.age = self.random.randint(0, max_age)
7381
self.max_age = max_age # it's maximum lifespan of predator agent
@@ -95,13 +103,17 @@ def step(self):
95103
self.energy -= 1 # predator lose energy each step; it's cost of living
96104

97105
# get nearby agents within hunting radius (increased from 2.0 so they can find food easier)
98-
neighbors, _ = self.get_neighbors_in_radius(radius=4.0)
99-
prey_neighbors = [n for n in neighbors if isinstance(n, Prey)] # it filter the nearby agents to find the prey
106+
neighbors, _ = self.get_neighbors_in_radius(radius=4.0)
107+
prey_neighbors = [
108+
n for n in neighbors if isinstance(n, Prey)
109+
] # it filter the nearby agents to find the prey
100110

101111
if prey_neighbors:
102-
prey_to_eat = self.random.choice(prey_neighbors) # choose random prey to eat
112+
prey_to_eat = self.random.choice(
113+
prey_neighbors
114+
) # choose random prey to eat
103115
self.energy += self.model.predator_gain_from_food # gain energy from eating
104-
116+
105117
# Modern Mesa 4.0 removal (replaces all the contextlib fallbacks)
106118
prey_to_eat.remove() # prey agent removed from simulation
107119

@@ -120,5 +132,5 @@ def step(self):
120132
self.position,
121133
self.speed,
122134
int(self.energy),
123-
self.max_age
135+
self.max_age,
124136
) # new predator starts with half parent's energy

examples/continuous_predator_prey/prey_predator/app.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
from matplotlib.markers import MarkerStyle
2-
1+
from agents import Predator, Prey
32
from mesa.visualization import Slider, SolaraViz, SpaceRenderer, make_plot_component
43
from mesa.visualization.components import AgentPortrayalStyle
5-
64
from model import PredatorPreyModel
7-
from agents import Prey, Predator
85

96

107
def agent_draw(agent):
@@ -13,13 +10,13 @@ def agent_draw(agent):
1310
return AgentPortrayalStyle(
1411
color="blue",
1512
size=15,
16-
marker="o" # Circle for prey
13+
marker="o", # Circle for prey
1714
)
1815
elif isinstance(agent, Predator):
1916
return AgentPortrayalStyle(
2017
color="red",
2118
size=30,
22-
marker="^" # Triangle for predator
19+
marker="^", # Triangle for predator
2320
)
2421

2522

@@ -74,9 +71,7 @@ def agent_draw(agent):
7471

7572

7673
# Set up the Population Line Chart
77-
population_chart = make_plot_component(
78-
{"Prey": "blue", "Predators": "red"}
79-
)
74+
population_chart = make_plot_component({"Prey": "blue", "Predators": "red"})
8075

8176

8277
# Create the Solara webpage

examples/continuous_predator_prey/prey_predator/model.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import mesa
2-
from mesa.experimental.continuous_space import ContinuousSpace
32
from agents import Predator, Prey
3+
from mesa.experimental.continuous_space import ContinuousSpace
44

55

66
class PredatorPreyModel(mesa.Model):
@@ -62,13 +62,21 @@ def __init__(
6262
# it keeps track of no. of prey and predators in each step of the simulation for analysis and visualization purposes
6363
self.datacollector = mesa.DataCollector(
6464
model_reporters={
65-
"Prey": lambda m: sum(1 for a in m.agents if isinstance(a, Prey)), # it's count the number of prey by checking instances of prey class in the agents
66-
"Predators": lambda m: sum(1 for a in m.agents if isinstance(a, Predator)), # it's count the number of predators by checking instances of predator class in the agents
65+
"Prey": lambda m: sum(
66+
1 for a in m.agents if isinstance(a, Prey)
67+
), # it's count the number of prey by checking instances of prey class in the agents
68+
"Predators": lambda m: sum(
69+
1 for a in m.agents if isinstance(a, Predator)
70+
), # it's count the number of predators by checking instances of predator class in the agents
6771
}
6872
)
6973

7074
def step(self):
7175
# advancing to next step, model need to collect data before the agents take their actions
72-
self.datacollector.collect(self) # collect the data for the current step of the simulation
76+
self.datacollector.collect(
77+
self
78+
) # collect the data for the current step of the simulation
7379
# Mesa 4.0 native agent activation (replaces the old scheduler)
74-
self.agents.shuffle_do("step") # it step all agents during the scheduling process
80+
self.agents.shuffle_do(
81+
"step"
82+
) # it step all agents during the scheduling process

0 commit comments

Comments
 (0)