From da1987f1bc193e1129711885e6f77a9ed1ca92a3 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Wed, 23 Oct 2024 08:38:17 +0200 Subject: [PATCH 1/3] pass through model.rgn in agent analogous to model.random --- mesa/agent.py | 8 +++++++- tests/test_agent.py | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/mesa/agent.py b/mesa/agent.py index 868acf256c7..63c3894af1b 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,13 @@ 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..5a892f4eabd 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -161,6 +161,12 @@ def test_agent_membership(): assert agents[0] in agentset assert AgentTest(model) not in agentset +def test_agent_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.""" From 2f1dd4a045bdfa2f7341932784b6bda798496ccc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 23 Oct 2024 06:42:08 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa/agent.py | 1 + tests/test_agent.py | 1 + 2 files changed, 2 insertions(+) diff --git a/mesa/agent.py b/mesa/agent.py index 63c3894af1b..6b6c3d2e7a3 100644 --- a/mesa/agent.py +++ b/mesa/agent.py @@ -95,6 +95,7 @@ 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 5a892f4eabd..f194c966039 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -161,6 +161,7 @@ def test_agent_membership(): assert agents[0] in agentset assert AgentTest(model) not in agentset + def test_agent_rng(): model = Model(seed=42) agent = Agent(model) From 452b559901786f2853e858f809686de14fd6c32c Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Wed, 23 Oct 2024 08:47:07 +0200 Subject: [PATCH 3/3] add test docstring for ruff --- tests/test_agent.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_agent.py b/tests/test_agent.py index f194c966039..70183ca07bc 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -163,6 +163,7 @@ def test_agent_membership(): 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