Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion benchmarks/BoltzmannWealth/boltzmann_wealth.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def __init__(self, seed=None, n=100, width=10, height=10):
self.datacollector.collect(self)

def step(self):
self._advance_time()
self.agents.shuffle().do("step")
# collect data
self.datacollector.collect(self)
Expand Down
8 changes: 4 additions & 4 deletions mesa/batchrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ def _model_run_func(
"""
run_id, iteration, kwargs = run
model = model_cls(**kwargs)
while model.running and model._steps <= max_steps:
while model.running and model.steps <= max_steps:
model.step()

data = []

steps = list(range(0, model._steps, data_collection_period))
if not steps or steps[-1] != model._steps - 1:
steps.append(model._steps - 1)
steps = list(range(0, model.steps, data_collection_period))
if not steps or steps[-1] != model.steps - 1:
steps.append(model.steps - 1)

for step in steps:
model_data, all_agents_data = _collect_data(model, step)
Expand Down
4 changes: 2 additions & 2 deletions mesa/datacollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def _record_agents(self, model):
rep_funcs = self.agent_reporters.values()

def get_reports(agent):
_prefix = (agent.model._steps, agent.unique_id)
_prefix = (agent.model.steps, agent.unique_id)
reports = tuple(rep(agent) for rep in rep_funcs)
return _prefix + reports

Expand Down Expand Up @@ -216,7 +216,7 @@ def collect(self, model):

if self.agent_reporters:
agent_records = self._record_agents(model)
self._agent_records[model._steps] = list(agent_records)
self._agent_records[model.steps] = list(agent_records)

def add_table_row(self, table_name, row, ignore_missing=False):
"""Add a row dictionary to a specific table.
Expand Down
26 changes: 13 additions & 13 deletions mesa/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
from mesa.agent import Agent, AgentSet
from mesa.datacollection import DataCollector

TimeT = float | int


class Model:
"""Base class for models in the Mesa ABM library.
Expand All @@ -35,6 +33,8 @@ class Model:
Properties:
agents: An AgentSet containing all agents in the model
agent_types: A list of different agent types present in the model.
steps: An integer representing the number of steps the model has taken.
It increases automatically at the start of each step() call.

Methods:
get_agents_of_type: Returns an AgentSet of agents of the specified type.
Expand Down Expand Up @@ -62,10 +62,6 @@ def __new__(cls, *args: Any, **kwargs: Any) -> Any:
# advance.
obj._seed = random.random()
obj.random = random.Random(obj._seed)

# TODO: Remove these 2 lines just before Mesa 3.0
obj._steps = 0
obj._time = 0
return obj

def __init__(self, *args: Any, **kwargs: Any) -> None:
Expand All @@ -77,11 +73,20 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
self.running = True
self.schedule = None
self.current_id = 0
self.steps: int = 0

self._setup_agent_registration()

self._steps: int = 0
self._time: TimeT = 0 # the model's clock
# Wrap the user-defined step method
self._user_step = self.step
self.step = self._wrapped_step

def _wrapped_step(self, *args: Any, **kwargs: Any) -> None:
"""Automatically increments time and steps after calling the user's step method."""
# Automatically increment time and step counters
self.steps += 1
# Call the original user-defined step method
self._user_step(*args, **kwargs)

@property
def agents(self) -> AgentSet:
Expand Down Expand Up @@ -180,11 +185,6 @@ def run_model(self) -> None:
def step(self) -> None:
"""A single step. Fill in here."""

def _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
7 changes: 0 additions & 7 deletions mesa/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ def __init__(self, model: Model, agents: Iterable[Agent] | None = None) -> None:
self.model = model
self.steps = 0
self.time: TimeT = 0
self._original_step = self.step
self.step = self._wrapped_step

if agents is None:
agents = []
Expand Down Expand Up @@ -113,11 +111,6 @@ def step(self) -> None:
self.steps += 1
self.time += 1

def _wrapped_step(self):
"""Wrapper for the step method to include time and step updating."""
self._original_step()
self.model._advance_time()

def get_agent_count(self) -> int:
"""Returns the current number of agents in the queue."""
return len(self._agents)
Expand Down
2 changes: 1 addition & 1 deletion mesa/visualization/solara_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@
"""Advance the model by one step."""
model.step()
previous_step.value = current_step.value
current_step.value = model._steps
current_step.value = model.steps

Check warning on line 247 in mesa/visualization/solara_viz.py

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/solara_viz.py#L247

Added line #L247 was not covered by tests

def do_play():
"""Run the model continuously."""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_batch_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def get_local_model_param(self):
return 42

def step(self):
self.datacollector.collect(self)
self.schedule.step()
self.datacollector.collect(self)


def test_batch_run():
Expand Down
1 change: 1 addition & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ def test_examples(self):
model = model_class()
for _ in range(10):
model.step()
self.assertEqual(model.steps, 10)
4 changes: 3 additions & 1 deletion tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ def test_model_set_up():
assert model.current_id == 0
assert model.current_id + 1 == model.next_id()
assert model.current_id == 1
assert model.steps == 0
model.step()
assert model.steps == 1


def test_running():
Expand All @@ -18,12 +20,12 @@ class TestModel(Model):

def step(self):
"""Increase steps until 10."""
self.steps += 1
if self.steps == 10:
self.running = False

model = TestModel()
model.run_model()
assert model.steps == 10


def test_seed(seed=23):
Expand Down
Loading