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

issue a user warning if random is None #2479

Merged
merged 12 commits into from
Nov 11, 2024
5 changes: 5 additions & 0 deletions mesa/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@
random (Random): the random number generator
"""
if random is None:
warnings.warn(

Check warning on line 123 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L123

Added line #L123 was not covered by tests
"Random number generator not specified, this can make models non-reproducible. Please pass a random number generator explicitly",
UserWarning,
stacklevel=2,
)
random = (
Random()
) # FIXME see issue 1981, how to get the central rng from model
Expand Down
2 changes: 1 addition & 1 deletion mesa/examples/advanced/pd_grid/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(
"""
super().__init__(seed=seed)
self.activation_order = activation_order
self.grid = OrthogonalMooreGrid((width, height), torus=True)
self.grid = OrthogonalMooreGrid((width, height), torus=True, random=self.random)

if payoffs is not None:
self.payoff = payoffs
Expand Down
4 changes: 3 additions & 1 deletion mesa/examples/advanced/sugarscape_g1mt/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def __init__(
self.running = True

# initiate mesa grid class
self.grid = OrthogonalVonNeumannGrid((self.width, self.height), torus=False)
self.grid = OrthogonalVonNeumannGrid(
(self.width, self.height), torus=False, random=self.random
)
# initiate datacollector
self.datacollector = mesa.DataCollector(
model_reporters={
Expand Down
10 changes: 8 additions & 2 deletions mesa/experimental/cell_space/cell_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import itertools
import warnings
from collections.abc import Callable, Iterable, Mapping
from functools import cached_property
from random import Random
Expand Down Expand Up @@ -45,7 +46,12 @@ def __init__(
self._capacity: int = next(iter(self._cells.keys())).capacity

if random is None:
random = Random() # FIXME
warnings.warn(
"Random number generator not specified, this can make models non-reproducible. Please pass a random number generator explicitly",
UserWarning,
stacklevel=2,
)
random = Random()
self.random = random

def __iter__(self): # noqa
Expand Down Expand Up @@ -115,4 +121,4 @@ def cell_generator(filter_func, at_most):
yield cell
count += 1

return CellCollection(cell_generator(filter_func, at_most))
return CellCollection(cell_generator(filter_func, at_most), random=self.random)
12 changes: 10 additions & 2 deletions mesa/experimental/cell_space/discrete_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import warnings
from collections.abc import Callable
from functools import cached_property
from random import Random
Expand Down Expand Up @@ -44,7 +45,12 @@ def __init__(
self.capacity = capacity
self._cells: dict[tuple[int, ...], T] = {}
if random is None:
random = Random() # FIXME should default to default rng from model
warnings.warn(
"Random number generator not specified, this can make models non-reproducible. Please pass a random number generator explicitly",
UserWarning,
stacklevel=2,
)
random = Random()
self.random = random
self.cell_klass = cell_klass

Expand All @@ -67,7 +73,9 @@ def _connect_single_cell(self, cell: T): ...
@cached_property
def all_cells(self):
"""Return all cells in space."""
return CellCollection({cell: cell.agents for cell in self._cells.values()})
return CellCollection(
{cell: cell.agents for cell in self._cells.values()}, random=self.random
)

def __iter__(self): # noqa
return iter(self._cells.values())
Expand Down
Loading
Loading