Skip to content

Commit

Permalink
Pass through model.rgn in agent analogous to model.random (#2400)
Browse files Browse the repository at this point in the history
* pass through model.rgn in agent analogous to model.random

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add test docstring for ruff

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
quaquel and pre-commit-ci[bot] authored Oct 23, 2024
1 parent 3c0cd62 commit 44276fb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion mesa/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
# mypy
from typing import TYPE_CHECKING, Any, Literal, overload

import numpy as np

if TYPE_CHECKING:
# We ensure that these are not imported during runtime to prevent cyclic
# dependency.
Expand Down Expand Up @@ -85,9 +87,14 @@ def advance(self) -> None: # noqa: D102

@property
def random(self) -> Random:
"""Return a seeded rng."""
"""Return a seeded stdlib rng."""
return self.model.random

@property
def rng(self) -> np.random.Generator:
"""Return a seeded np.random rng."""
return self.model.rng


class AgentSet(MutableSet, Sequence):
"""A collection class that represents an ordered set of agents within an agent-based model (ABM).
Expand Down
8 changes: 8 additions & 0 deletions tests/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ def test_agent_membership():
assert AgentTest(model) not in agentset


def test_agent_rng():
"""Test whether agent.random and agent.rng are equal to model.random and model.rng."""
model = Model(seed=42)
agent = Agent(model)
assert agent.random is model.random
assert agent.rng is model.rng


def test_agent_add_remove_discard():
"""Test adding, removing and discarding agents from AgentSet."""
model = Model()
Expand Down

0 comments on commit 44276fb

Please sign in to comment.