From 04714fa6f6a89322ccbf0418e260bd6a27fb4c4c Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 20 Sep 2024 15:00:17 +0200 Subject: [PATCH] fix: AgentSetDF.shuffle should use a seed generated from the object random generator --- mesa_frames/concrete/pandas/agentset.py | 5 ++++- mesa_frames/concrete/polars/agentset.py | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/mesa_frames/concrete/pandas/agentset.py b/mesa_frames/concrete/pandas/agentset.py index 66d12c0..07779b2 100644 --- a/mesa_frames/concrete/pandas/agentset.py +++ b/mesa_frames/concrete/pandas/agentset.py @@ -54,6 +54,7 @@ def step(self): from collections.abc import Callable, Collection, Iterable, Iterator, Sequence from typing import TYPE_CHECKING +import numpy as np import pandas as pd import polars as pl from typing_extensions import Any, Self, overload @@ -237,7 +238,9 @@ def select( # noqa : D102 def shuffle(self, inplace: bool = True) -> Self: # noqa : D102 obj = self._get_obj(inplace) - obj._agents = obj._agents.sample(frac=1) + obj._agents = obj._agents.sample( + frac=1, random_state=obj.random.integers(np.iinfo(np.int32).max) + ) return obj def sort( # noqa : D102 diff --git a/mesa_frames/concrete/polars/agentset.py b/mesa_frames/concrete/polars/agentset.py index 9b7030d..4bec1ea 100644 --- a/mesa_frames/concrete/polars/agentset.py +++ b/mesa_frames/concrete/polars/agentset.py @@ -73,6 +73,8 @@ def step(self): from mesa_frames.concrete.model import ModelDF from mesa_frames.concrete.pandas.agentset import AgentSetPandas +import numpy as np + @copydoc(AgentSetDF) class AgentSetPolars(AgentSetDF, PolarsMixin): @@ -255,7 +257,11 @@ def select( def shuffle(self, inplace: bool = True) -> Self: obj = self._get_obj(inplace) - obj._agents = obj._agents.sample(fraction=1, shuffle=True) + obj._agents = obj._agents.sample( + fraction=1, + shuffle=True, + seed=obj.random.integers(np.iinfo(np.int32).max), + ) return obj def sort(