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

This commit is contained in:
Jet
2023-01-05 20:46:37 +00:00
committed by GitHub
parent 82b839fcd1
commit c2a387702c
2 changed files with 11 additions and 1 deletions

View File

@@ -119,7 +119,8 @@ class Sequence(Space[typing.Tuple[Any, ...]]):
def contains(self, x: Any) -> bool: def contains(self, x: Any) -> bool:
"""Return boolean specifying if x is a valid member of this space.""" """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 self.feature_space.contains(item) for item in x
) )

View File

@@ -6,6 +6,15 @@ import pytest
import gymnasium as gym 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(): def test_sample():
"""Tests the sequence sampling works as expects and the errors are correctly raised.""" """Tests the sequence sampling works as expects and the errors are correctly raised."""
space = gym.spaces.Sequence(gym.spaces.Box(0, 1)) space = gym.spaces.Sequence(gym.spaces.Box(0, 1))