Skip to content

Have the schedulers use the model's randomizer #366

@lmzintgraf

Description

@lmzintgraf

Regarding this TODO in the documentation:

TODO: Have the schedulers use the model's randomizer, to keep random number seeds consistent and allow for replication.

How about letting the model have a random state (using numpy) which the scheduler can then call? I.e.,

  • in the model init: self.random_state = numpy.random.RandomState(seed)
  • in the schedule step: self.model.random_state.shuffle(self.agents)
  • alternatively, in the schedule init: self.random_state = self.model.random_state (which I assume will make a copy; not important for reproducibility but it might be nicer to use the model's random state directly)

Some things I ran into while trying this:

  • It would now be required for new models to call the super().__init__() method (see Example models should call super in init #248 ) to make sure that models have a random state. Alternatively, we could put a check in the scheduler if the model has a random state, and if not, create a local one. The problem is that then the seed can't be specified, or, if it can be passed to the scheduler, it will only have effect if the model doesn't have a random state, which seems weird.
  • When testing this, test_time.py will crash:
    • First error is because super().__init__() is not called (see above), and the scheduler cannot call self.model.random_state
    • Another error occurs when mesa.time.random.shuffle is called in the test. It will either complain that the import isn't there (I removed it, import random in time.py is not necessary anymore), or the following happens:
    • test_random_activation_step_shuffles and test_shuffle_shuffles_agents crash. I guess it's because the way random calls are made changed (it's not mesa.time.random.shuffle, but via np.random). Not sure how to fix this.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions