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

Allow sequence to accept stacked np arrays if the feature space is Box #241

Merged
merged 7 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
10 changes: 7 additions & 3 deletions gymnasium/spaces/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,13 @@ def sample(

def contains(self, x: Any) -> bool:
"""Return boolean specifying if x is a valid member of this space."""
return isinstance(x, collections.abc.Sequence) and all(
self.feature_space.contains(item) for item in x
)
# by definition, any sequence is an iterable
if not isinstance(x, collections.abc.Iterable):
return False
if not all(self.feature_space.contains(item) for item in x):
return False

return True

def __repr__(self) -> str:
"""Gives a string representation of this space."""
Expand Down
9 changes: 9 additions & 0 deletions tests/spaces/test_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
import gymnasium as gym


def test_stacked_box():
"""Tests that sequence with a feature space of Box allows stacked np arrays."""
space = gym.spaces.Sequence(gym.spaces.Box(0, 1, shape=(3,)))
sample = np.float32(np.random.rand(5, 3))
assert space.contains(
sample
), "Something went wrong, should be able to accept stacked np arrays for Box feature space."


def test_sample():
"""Tests the sequence sampling works as expects and the errors are correctly raised."""
space = gym.spaces.Sequence(gym.spaces.Box(0, 1))
Expand Down