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 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
3 changes: 2 additions & 1 deletion gymnasium/spaces/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ 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(
# by definition, any sequence is an iterable
return isinstance(x, collections.abc.Iterable) and all(
self.feature_space.contains(item) for item in x
)

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