From 44276fb6ac2ecfb589fe13123d3aad7eba1acaba Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Wed, 23 Oct 2024 10:31:48 +0200 Subject: [PATCH] Pass through model.rgn in agent analogous to model.random (#2400) * 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> --- mesa/agent.py | 9 ++++++++- tests/test_agent.py | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/mesa/agent.py b/mesa/agent.py index 868acf256c7..6b6c3d2e7a3 100644 --- a/mesa/agent.py +++ b/mesa/agent.py @@ -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. @@ -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). diff --git a/tests/test_agent.py b/tests/test_agent.py index f14c755ef2b..70183ca07bc 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -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()