-
Notifications
You must be signed in to change notification settings - Fork 8.6k
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
Make Tuple and Dicts be seedable with lists and dicts of seeds + make the seed in default initialization controllable #1774
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e003db1
Make the seed in default initialization controllable
RaghuSpaceRajan dba1186
Updated derived classes of Space to have their seeds controllable at …
RaghuSpaceRajan f8c415e
Merge branch 'master' into patch-1
RaghuSpaceRajan ca6165a
Allow Tuple's spaces to each have their own seed
RaghuSpaceRajan a083e53
Merge branch 'master' into patch-1
RaghuSpaceRajan 870090f
Added dict based seeding for Dict space; test cases for Tuple and Dic…
RaghuSpaceRajan 82ade50
Merge branch 'master' into patch-1
RaghuSpaceRajan c3c6891
Update discrete.py
RaghuSpaceRajan eea66e5
Update test_spaces.py
RaghuSpaceRajan 145160b
Merge branch 'openai:master' into patch-1
RaghuSpaceRajan 1d41682
Add seed to __init__()
RaghuSpaceRajan 7f1ced1
blacked
RaghuSpaceRajan 57f1242
Merge branch 'master' into patch-1
RaghuSpaceRajan 5944562
Fix black
RaghuSpaceRajan 6d7725d
Fix failing tests
RaghuSpaceRajan a6cd3fb
Merge branch 'master' into patch-1
RaghuSpaceRajan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,33 +10,44 @@ class Tuple(Space): | |
self.observation_space = spaces.Tuple((spaces.Discrete(2), spaces.Discrete(3))) | ||
""" | ||
|
||
def __init__(self, spaces): | ||
def __init__(self, spaces, seed=None): | ||
self.spaces = spaces | ||
for space in spaces: | ||
assert isinstance( | ||
space, Space | ||
), "Elements of the tuple must be instances of gym.Space" | ||
super(Tuple, self).__init__(None, None) | ||
super(Tuple, self).__init__(None, None, seed) | ||
|
||
def seed(self, seed=None): | ||
seed = super().seed(seed) | ||
try: | ||
subseeds = self.np_random.choice( | ||
np.iinfo(int).max, | ||
size=len(self.spaces), | ||
replace=False, # unique subseed for each subspace | ||
) | ||
except ValueError: | ||
subseeds = self.np_random.choice( | ||
np.iinfo(int).max, | ||
size=len(self.spaces), | ||
replace=True, # we get more than INT_MAX subspaces | ||
) | ||
seeds = [] | ||
|
||
if isinstance(seed, list): | ||
for i, space in enumerate(self.spaces): | ||
seeds += space.seed(seed[i]) | ||
elif isinstance(seed, int): | ||
seeds = super().seed(seed) | ||
try: | ||
subseeds = self.np_random.choice( | ||
np.iinfo(int).max, | ||
size=len(self.spaces), | ||
replace=False, # unique subseed for each subspace | ||
) | ||
except ValueError: | ||
subseeds = self.np_random.choice( | ||
np.iinfo(int).max, | ||
size=len(self.spaces), | ||
replace=True, # we get more than INT_MAX subspaces | ||
) | ||
|
||
for subspace, subseed in zip(self.spaces, subseeds): | ||
seed.append(subspace.seed(int(subseed))[0]) | ||
for subspace, subseed in zip(self.spaces, subseeds): | ||
seeds.append(subspace.seed(int(subseed))[0]) | ||
elif seed is None: | ||
for space in self.spaces: | ||
seeds += space.seed(seed) | ||
else: | ||
raise TypeError("Passed seed not of an expected type: list or int or None") | ||
|
||
return seed | ||
return seeds | ||
Comment on lines
21
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here for
|
||
|
||
def sample(self): | ||
return tuple([space.sample() for space in self.spaces]) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we call
super().seed(seed)
to seedself.np_random
as well whenseed
isdict
orNone
here?We could merge the last two cases (
seed
isint
orNone
) into one. After statementseeds = super().seed(seed)
,self.np_random
become seeded and variableseeds
is a list of integers.Should we only add the main seed of subspace (
subspace.seed()[0]
) to the return value rather than extend all items insubspace.seed()
(the opseeds +=
in the first and the last cases)?If the subpace of
Dict
here is another compound space (Tuple
orDict
), the length of the return list could be differentlen(d.seed(None)) != len(d.seed(0))
. (However, I think we probably only use the first item, and hardly ever use the rest in the list and the length of the list. This issue probably never affects normal users.)gym/gym/spaces/tests/test_spaces.py
Lines 270 to 277 in d35d211
We could add a new test
assert len(space.seed(None)) == len(space.seed(0))
here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_np_random
PRNG of the "base" class's object and make it consistent with other spaces in case allSpace
objects are expected to have an_np_random
that is notNone
, although I assume that only_np_random
s of the sub-classes are ever used.None
, I assume the user would want the seed of every sub-space to be as random as possible in the sense that they would want all of them to be seeded withNone
. But generating the seeds for sub-spaces using an_np_random
PRNG generated based onNone
(i.e., the suggested way) should also be fine.So, in general, I'm fine with any of the suggestions either being or not being implemented (also for the suggestions on
Tuple
below). Please let me know.