Skip to content

Commit

Permalink
model: Implement internal clock
Browse files Browse the repository at this point in the history
  • Loading branch information
rht committed Jan 7, 2024
1 parent 3da91da commit 3fec08c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 1 deletion.
12 changes: 11 additions & 1 deletion mesa/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
from collections import defaultdict

# mypy
from typing import Any
from typing import Any, Union

from mesa.agent import Agent, AgentSet
from mesa.datacollection import DataCollector

TimeT = Union[float, int]


class Model:
"""Base class for models in the Mesa ABM library.
Expand Down Expand Up @@ -67,6 +69,9 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
self.current_id = 0
self._agents: defaultdict[type, dict] = defaultdict(dict)

self.steps = 0
self.time: TimeT = 0 # the model's clock

# Warning flags for current experimental features. These make sure a warning is only printed once per model.
self.agentset_experimental_warning_given = False

Expand Down Expand Up @@ -97,6 +102,11 @@ def run_model(self) -> None:
def step(self) -> None:
"""A single step. Fill in here."""

def step_and_advance_time(self, deltat: TimeT = 1):
"""Increment the model's steps counter and clock."""
self.steps += 1
self.time += deltat

def next_id(self) -> int:
"""Return the next unique ID for agents, increment current_id"""
self.current_id += 1
Expand Down
1 change: 1 addition & 0 deletions tests/test_batch_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def get_local_model_param(self):
def step(self):
self.datacollector.collect(self)
self.schedule.step()
self.step_and_advance_time()


def test_batch_run():
Expand Down
1 change: 1 addition & 0 deletions tests/test_datacollector.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def test_model_calc_comp(self, input1, input2):
def step(self):
self.schedule.step()
self.datacollector.collect(self)
self.step_and_advance_time()


class TestDataCollector(unittest.TestCase):
Expand Down
1 change: 1 addition & 0 deletions tests/test_lifespan.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def step(self):
self.schedule.add(
FiniteLifeAgent(self.next_id(), self.agent_lifetime, self)
)
self.step_and_advance_time()

def run_model(self, step_count=100):
for _ in range(step_count):
Expand Down
1 change: 1 addition & 0 deletions tests/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def __init__(self, shuffle=False, activation=STAGED, enable_kill_other_agent=Fal

def step(self):
self.schedule.step()
self.step_and_advance_time()

def model_stage(self):
self.log.append("model_stage")
Expand Down
1 change: 1 addition & 0 deletions tests/test_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(self, width, height, key1=103, key2=104):

def step(self):
self.schedule.step()
self.step_and_advance_time()


class TestModularServer(TestCase):
Expand Down

0 comments on commit 3fec08c

Please sign in to comment.