Skip to content

Commit

Permalink
Allow sequence to accept stacked np arrays if the feature space is Box (
Browse files Browse the repository at this point in the history
  • Loading branch information
jjshoots authored Jan 5, 2023
1 parent 82b839f commit c2a3877
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
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

0 comments on commit c2a3877

Please sign in to comment.