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

Lazily seed np rng #1913

Merged
merged 2 commits into from
May 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions gym/spaces/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@ def __init__(self, shape=None, dtype=None):
import numpy as np # takes about 300-400ms to import, so we load lazily
self.shape = None if shape is None else tuple(shape)
self.dtype = None if dtype is None else np.dtype(dtype)
self.np_random = None
self.seed()
self._np_random = None

@property
def np_random(self):
"""Lazily seed the rng since this is expensive and only needed if
sampling from this space.
"""
if self._np_random is None:
self.seed()

return self._np_random

def sample(self):
"""Randomly sample an element of this space. Can be
Expand All @@ -20,7 +29,7 @@ def sample(self):

def seed(self, seed=None):
"""Seed the PRNG of this space. """
self.np_random, seed = seeding.np_random(seed)
self._np_random, seed = seeding.np_random(seed)
return [seed]

def contains(self, x):
Expand Down