I have a question about seeding in open AI gym and utilizing it in custom environments. Let's take the lunar lander environment for example, the default seeding function is:
def seed(self, seed=None):
self.np_random, seed = seeding.np_random(seed)
return [seed]
And when generating they use:
height = self.np_random.uniform(0, H/2, size=(CHUNKS+1,) )
My question is, if I make a custom environment and use numpy or sci stats I would need to seed with np.random.seed()
to get an effect. How should I use self.np_random.
to seed in my custom environment? If I use np.random.uniform(0,0.02)
? Should I use self.np_random.uniform(0,0.02)
instead? What about sci-stats? How should I use it there if I use scipy.stats.truncnorm.rvs()
? Any consequence if I just set np.random.seed(seed)
?
I am using this workaround now: Can I create a local numpy random seed?
Is there a better solution?
As recommended by the official gymnasium doc,
https://gymnasium.farama.org/api/env/#gymnasium.Env.reset,
you should seed during the env reset when you call the gymanisum.Env.reset(), but only once at the beginning:
For Custom environments, the first line of reset() should be super().reset(seed=seed) which implements the seeding correctly. ... Therefore, reset() should (in the typical use case) be called with a seed right after initialization and then never again.
likewise, you give the user the option to choose a seed, as when he wants to make results reproducible. If the case, you need to seed Env.action_space.sample() as well, as follow:
Env.action_space.seed(SOME_SEED)
Since gym uses np.random.Generator, rather than seeding np.random, you may just use: self.np_random all along your custom environment.