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

model: Move random seed and random to __init__ #1940

Merged
merged 13 commits into from
Sep 15, 2024
21 changes: 8 additions & 13 deletions mesa/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,24 @@ class Model:

"""

def __new__(cls, *args: Any, **kwargs: Any) -> Any:
"""Create a new model object and instantiate its RNG automatically."""
obj = object.__new__(cls)
obj._seed = kwargs.get("seed")
if obj._seed is None:
# We explicitly specify the seed here so that we know its value in
# advance.
obj._seed = random.random()
obj.random = random.Random(obj._seed)
return obj

def __init__(self, *args: Any, **kwargs: Any) -> None:
def __init__(self, *args: Any, seed: float | None = None, **kwargs: Any) -> None:
"""Create a new model. Overload this method with the actual code to
start the model. Always start with super().__init__() to initialize the
model object properly.
"""

self.running = True
self.schedule = None
self.steps: int = 0

self._setup_agent_registration()

self._seed = seed
if self._seed is None:
# We explicitly specify the seed here so that we know its value in
# advance.
self._seed = random.random()
self.random = random.Random(self._seed)

# Wrap the user-defined step method
self._user_step = self.step
self.step = self._wrapped_step
Expand Down
6 changes: 2 additions & 4 deletions mesa/visualization/solara_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,8 @@
set_model_parameters({**model_parameters, name: value})

def create_model():
model.value = model.value.__class__.__new__(
model.value.__class__, **model_parameters, seed=reactive_seed.value
)
model.value.__init__(**model_parameters)
model.value = model.value.__class__(**model_parameters)
model.value._seed = reactive_seed.value

Check warning on line 264 in mesa/visualization/solara_viz.py

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/solara_viz.py#L263-L264

Added lines #L263 - L264 were not covered by tests

solara.use_effect(create_model, [model_parameters, reactive_seed.value])

Expand Down
1 change: 0 additions & 1 deletion tests/test_solara_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ def test_call_space_drawer(mocker):
)

model = mesa.Model()
mocker.patch.object(mesa.Model, "__new__", return_value=model)
mocker.patch.object(mesa.Model, "__init__", return_value=None)

agent_portrayal = {
Expand Down
4 changes: 3 additions & 1 deletion tests/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import unittest
from unittest import TestCase, mock

from mesa import Agent, Model
from mesa.agent import Agent
from mesa.model import Model
from mesa.time import (
BaseScheduler,
RandomActivation,
Expand Down Expand Up @@ -111,6 +112,7 @@ def test_no_shuffle(self):
"""
Testing the staged activation without shuffling.
"""

model = MockModel(shuffle=False)
model.step()
model.step()
Expand Down
Loading