Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass through model.rgn in agent analogous to model.random #2400

Merged
merged 3 commits into from
Oct 23, 2024
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
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
Loading