Skip to content

Commit

Permalink
Update benchmarks for automatic unique_id
Browse files Browse the repository at this point in the history
If they stop throwing warnings, it goes probably faster.
  • Loading branch information
EwoutH committed Aug 29, 2024
1 parent d6ffcac commit 41cc79b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 21 deletions.
6 changes: 3 additions & 3 deletions benchmarks/BoltzmannWealth/boltzmann_wealth.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, seed=None, n=100, width=10, height=10):
)
# Create agents
for i in range(self.num_agents):
a = MoneyAgent(i, self)
a = MoneyAgent(self)
# Add the agent to a random grid cell
x = self.random.randrange(self.grid.width)
y = self.random.randrange(self.grid.height)
Expand All @@ -50,8 +50,8 @@ def run_model(self, n):
class MoneyAgent(mesa.Agent):
"""An agent with fixed initial wealth."""

def __init__(self, unique_id, model):
super().__init__(unique_id, model)
def __init__(self, model):
super().__init__(model)
self.wealth = 1

def move(self):
Expand Down
5 changes: 2 additions & 3 deletions benchmarks/Flocking/flocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class Boid(mesa.Agent):

def __init__(
self,
unique_id,
model,
speed,
direction,
Expand All @@ -42,6 +41,7 @@ def __init__(
Args:
unique_id: Unique agent identifier.
model: Model instance in which the agent is located.
speed: Distance to move per step.
direction: numpy vector for the Boid's direction of movement.
vision: Radius to look around for nearby Boids.
Expand All @@ -51,7 +51,7 @@ def __init__(
match: the relative importance of matching neighbors' directions
"""
super().__init__(unique_id, model)
super().__init__(model)
self.speed = speed
self.direction = direction
self.vision = vision
Expand Down Expand Up @@ -137,7 +137,6 @@ def __init__(
pos = np.array((x, y))
direction = np.random.random(2) * 2 - 1
boid = Boid(
unique_id=i,
model=self,
speed=speed,
direction=direction,
Expand Down
9 changes: 4 additions & 5 deletions benchmarks/Schelling/schelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ class SchellingAgent(CellAgent):
Schelling segregation agent
"""

def __init__(self, unique_id, model, agent_type, radius, homophily):
def __init__(self, model, agent_type, radius, homophily):
"""
Create a new Schelling agent.
Args:
unique_id: Unique identifier for the agent.
model: Model instance in which the agent is located.
x, y: Agent initial location.
agent_type: Indicator for the agent's type (minority=1, majority=0)
"""
super().__init__(unique_id, model)
super().__init__(model)
self.type = agent_type
self.radius = radius
self.homophily = homophily
Expand Down Expand Up @@ -81,9 +82,7 @@ def __init__(
for cell in self.grid:
if self.random.random() < density:
agent_type = 1 if self.random.random() < self.minority_pc else 0
agent = SchellingAgent(
self.next_id(), self, agent_type, radius, homophily
)
agent = SchellingAgent(self, agent_type, radius, homophily)
agent.move_to(cell)
self.schedule.add(agent)

Expand Down
15 changes: 5 additions & 10 deletions benchmarks/WolfSheep/wolf_sheep.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@


class Animal(CellAgent):
def __init__(self, unique_id, model, energy, p_reproduce, energy_from_food):
super().__init__(unique_id, model)
def __init__(self, model, energy, p_reproduce, energy_from_food):
super().__init__(model)
self.energy = energy
self.p_reproduce = p_reproduce
self.energy_from_food = energy_from_food
Expand All @@ -29,7 +29,6 @@ def random_move(self):
def spawn_offspring(self):
self.energy /= 2
offspring = self.__class__(
self.model.next_id(),
self.model,
self.energy,
self.p_reproduce,
Expand Down Expand Up @@ -107,7 +106,7 @@ def fully_grown(self, value: bool) -> None:
function_args=[self, "fully_grown", True],
)

def __init__(self, unique_id, model, fully_grown, countdown, grass_regrowth_time):
def __init__(self, model, fully_grown, countdown, grass_regrowth_time):
"""
TODO:: fully grown can just be an int --> so one less param (i.e. countdown)
Expand All @@ -119,7 +118,7 @@ def __init__(self, unique_id, model, fully_grown, countdown, grass_regrowth_time
grass_regrowth_time : time to fully regrow grass
countdown : Time for the patch of grass to be fully regrown if fully grown is False
"""
super().__init__(unique_id, model)
super().__init__(model)
self._fully_grown = fully_grown
self.grass_regrowth_time = grass_regrowth_time

Expand Down Expand Up @@ -189,7 +188,6 @@ def __init__(
)
energy = self.random.randrange(2 * sheep_gain_from_food)
sheep = Sheep(
self.next_id(),
self,
energy,
sheep_reproduce,
Expand All @@ -205,7 +203,6 @@ def __init__(
)
energy = self.random.randrange(2 * wolf_gain_from_food)
wolf = Wolf(
self.next_id(),
self,
energy,
wolf_reproduce,
Expand All @@ -221,9 +218,7 @@ def __init__(
countdown = grass_regrowth_time
else:
countdown = self.random.randrange(grass_regrowth_time)
patch = GrassPatch(
self.next_id(), self, fully_grown, countdown, grass_regrowth_time
)
patch = GrassPatch(self, fully_grown, countdown, grass_regrowth_time)
patch.move_to(cell)

def step(self):
Expand Down

0 comments on commit 41cc79b

Please sign in to comment.