mirror of
https://github.com/Farama-Foundation/Gymnasium.git
synced 2025-08-31 18:12:53 +00:00
Allow sequence to accept stacked np arrays if the feature space is Box (#241)
This commit is contained in:
@@ -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
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@@ -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))
|
||||||
|
Reference in New Issue
Block a user