Skip to content

Commit

Permalink
tests: Replace scheduler in lifespan tests
Browse files Browse the repository at this point in the history
Also adhere to the new structure of stepping first and then collecting
  • Loading branch information
EwoutH committed Sep 26, 2024
1 parent 4292d38 commit b37fc45
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions tests/test_lifespan.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from mesa import Agent, Model
from mesa.datacollection import DataCollector
from mesa.time import RandomActivation


class LifeTimeModel(Model):
Expand All @@ -28,19 +27,20 @@ def __init__(self, agent_lifetime=1, n_agents=10, seed=None): # noqa: D107
)

self.current_ID = 0
self.schedule = RandomActivation(self)

for _ in range(n_agents):
self.schedule.add(FiniteLifeAgent(self.agent_lifetime, self))
FiniteLifeAgent(self.agent_lifetime, self)

self.datacollector.collect(self)

def step(self):
"""Add agents back to n_agents in each step."""
self.agents.shuffle_do("step")
self.datacollector.collect(self)
self.schedule.step()

if len(self.schedule.agents) < self.n_agents:
for _ in range(self.n_agents - len(self.schedule.agents)):
self.schedule.add(FiniteLifeAgent(self.agent_lifetime, self))
if len(self.agents) < self.n_agents:
for _ in range(self.n_agents - len(self.agents)):
FiniteLifeAgent(self.agent_lifetime, self)

def run_model(self, step_count=100): # noqa: D102
for _ in range(step_count):
Expand All @@ -64,12 +64,12 @@ def step(self): # noqa: D102
if not inactivated:
self.steps += 1 # keep track of how many ticks are seen
if np.random.binomial(1, 0.1) != 0: # 10% chance of dying
self.model.schedule.remove(self)
self.remove()

def inactivate(self): # noqa: D102
self.remaining_life -= 1
if self.remaining_life < 0:
self.model.schedule.remove(self)
self.remove()
return True
return False

Expand Down

0 comments on commit b37fc45

Please sign in to comment.