2022-04-24 17:14:33 +01:00
|
|
|
import copy
|
|
|
|
|
2019-06-21 17:29:44 -04:00
|
|
|
import numpy as np
|
2022-03-31 12:50:38 -07:00
|
|
|
import pytest
|
2022-04-24 17:14:33 +01:00
|
|
|
from numpy.testing import assert_array_equal
|
2019-06-21 17:29:44 -04:00
|
|
|
|
2022-05-10 15:35:45 +01:00
|
|
|
from gym.spaces import Box, Dict, MultiDiscrete, Space, Tuple
|
2021-12-08 21:31:41 -05:00
|
|
|
from gym.vector.utils.spaces import batch_space, iterate
|
2022-04-24 17:14:33 +01:00
|
|
|
from tests.vector.utils import CustomSpace, assert_rng_equal, custom_spaces, spaces
|
2019-06-21 17:29:44 -04:00
|
|
|
|
|
|
|
expected_batch_spaces_4 = [
|
2021-07-29 02:26:34 +02:00
|
|
|
Box(low=-1.0, high=1.0, shape=(4,), dtype=np.float64),
|
2022-03-14 14:27:03 +00:00
|
|
|
Box(low=0.0, high=10.0, shape=(4, 1), dtype=np.float64),
|
2021-07-29 02:26:34 +02:00
|
|
|
Box(
|
2021-07-29 15:39:42 -04:00
|
|
|
low=np.array(
|
|
|
|
[[-1.0, 0.0, 0.0], [-1.0, 0.0, 0.0], [-1.0, 0.0, 0.0], [-1.0, 0.0, 0.0]]
|
|
|
|
),
|
|
|
|
high=np.array(
|
|
|
|
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]
|
|
|
|
),
|
2022-03-14 14:27:03 +00:00
|
|
|
dtype=np.float64,
|
2021-07-29 02:26:34 +02:00
|
|
|
),
|
|
|
|
Box(
|
|
|
|
low=np.array(
|
|
|
|
[
|
|
|
|
[[-1.0, 0.0], [0.0, -1.0]],
|
|
|
|
[[-1.0, 0.0], [0.0, -1.0]],
|
|
|
|
[[-1.0, 0.0], [0.0, -1]],
|
|
|
|
[[-1.0, 0.0], [0.0, -1.0]],
|
|
|
|
]
|
|
|
|
),
|
|
|
|
high=np.ones((4, 2, 2)),
|
2022-03-14 14:27:03 +00:00
|
|
|
dtype=np.float64,
|
2021-07-29 02:26:34 +02:00
|
|
|
),
|
2019-06-21 17:29:44 -04:00
|
|
|
Box(low=0, high=255, shape=(4,), dtype=np.uint8),
|
|
|
|
Box(low=0, high=255, shape=(4, 32, 32, 3), dtype=np.uint8),
|
|
|
|
MultiDiscrete([2, 2, 2, 2]),
|
2022-03-04 15:17:16 -05:00
|
|
|
Box(low=-2, high=2, shape=(4,), dtype=np.int64),
|
2019-06-21 17:29:44 -04:00
|
|
|
Tuple((MultiDiscrete([3, 3, 3, 3]), MultiDiscrete([5, 5, 5, 5]))),
|
2021-07-29 02:26:34 +02:00
|
|
|
Tuple(
|
|
|
|
(
|
|
|
|
MultiDiscrete([7, 7, 7, 7]),
|
|
|
|
Box(
|
|
|
|
low=np.array([[0.0, -1.0], [0.0, -1.0], [0.0, -1.0], [0.0, -1]]),
|
|
|
|
high=np.array([[1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0]]),
|
2022-03-14 14:27:03 +00:00
|
|
|
dtype=np.float64,
|
2021-07-29 02:26:34 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
Box(
|
|
|
|
low=np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]),
|
|
|
|
high=np.array([[10, 12, 16], [10, 12, 16], [10, 12, 16], [10, 12, 16]]),
|
|
|
|
dtype=np.int64,
|
|
|
|
),
|
2019-06-21 17:29:44 -04:00
|
|
|
Box(low=0, high=1, shape=(4, 19), dtype=np.int8),
|
2021-07-29 02:26:34 +02:00
|
|
|
Dict(
|
|
|
|
{
|
|
|
|
"position": MultiDiscrete([23, 23, 23, 23]),
|
2022-03-14 14:27:03 +00:00
|
|
|
"velocity": Box(low=0.0, high=1.0, shape=(4, 1), dtype=np.float64),
|
2021-07-29 02:26:34 +02:00
|
|
|
}
|
|
|
|
),
|
|
|
|
Dict(
|
|
|
|
{
|
|
|
|
"position": Dict(
|
|
|
|
{
|
|
|
|
"x": MultiDiscrete([29, 29, 29, 29]),
|
|
|
|
"y": MultiDiscrete([31, 31, 31, 31]),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
"velocity": Tuple(
|
|
|
|
(
|
|
|
|
MultiDiscrete([37, 37, 37, 37]),
|
|
|
|
Box(low=0, high=255, shape=(4,), dtype=np.uint8),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
}
|
|
|
|
),
|
2019-06-21 17:29:44 -04:00
|
|
|
]
|
|
|
|
|
2020-09-21 22:38:51 +02:00
|
|
|
expected_custom_batch_spaces_4 = [
|
|
|
|
Tuple((CustomSpace(), CustomSpace(), CustomSpace(), CustomSpace())),
|
2021-07-29 02:26:34 +02:00
|
|
|
Tuple(
|
|
|
|
(
|
|
|
|
Tuple((CustomSpace(), CustomSpace(), CustomSpace(), CustomSpace())),
|
|
|
|
Box(low=0, high=255, shape=(4,), dtype=np.uint8),
|
|
|
|
)
|
|
|
|
),
|
2020-09-21 22:38:51 +02:00
|
|
|
]
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"space,expected_batch_space_4",
|
|
|
|
list(zip(spaces, expected_batch_spaces_4)),
|
|
|
|
ids=[space.__class__.__name__ for space in spaces],
|
|
|
|
)
|
2019-06-21 17:29:44 -04:00
|
|
|
def test_batch_space(space, expected_batch_space_4):
|
|
|
|
batch_space_4 = batch_space(space, n=4)
|
|
|
|
assert batch_space_4 == expected_batch_space_4
|
2020-09-21 22:38:51 +02:00
|
|
|
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"space,expected_batch_space_4",
|
|
|
|
list(zip(custom_spaces, expected_custom_batch_spaces_4)),
|
|
|
|
ids=[space.__class__.__name__ for space in custom_spaces],
|
|
|
|
)
|
2020-09-21 22:38:51 +02:00
|
|
|
def test_batch_space_custom_space(space, expected_batch_space_4):
|
|
|
|
batch_space_4 = batch_space(space, n=4)
|
|
|
|
assert batch_space_4 == expected_batch_space_4
|
2021-12-08 21:31:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"space,batch_space",
|
|
|
|
list(zip(spaces, expected_batch_spaces_4)),
|
|
|
|
ids=[space.__class__.__name__ for space in spaces],
|
|
|
|
)
|
|
|
|
def test_iterate(space, batch_space):
|
|
|
|
items = batch_space.sample()
|
|
|
|
iterator = iterate(batch_space, items)
|
|
|
|
for i, item in enumerate(iterator):
|
|
|
|
assert item in space
|
|
|
|
assert i == 3
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"space,batch_space",
|
|
|
|
list(zip(custom_spaces, expected_custom_batch_spaces_4)),
|
|
|
|
ids=[space.__class__.__name__ for space in custom_spaces],
|
|
|
|
)
|
|
|
|
def test_iterate_custom_space(space, batch_space):
|
|
|
|
items = batch_space.sample()
|
|
|
|
iterator = iterate(batch_space, items)
|
|
|
|
for i, item in enumerate(iterator):
|
|
|
|
assert item in space
|
|
|
|
assert i == 3
|
2022-04-24 17:14:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"space", spaces, ids=[space.__class__.__name__ for space in spaces]
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize("n", [4, 5], ids=[f"n={n}" for n in [4, 5]])
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"base_seed", [123, 456], ids=[f"seed={base_seed}" for base_seed in [123, 456]]
|
|
|
|
)
|
|
|
|
def test_rng_different_at_each_index(space: Space, n: int, base_seed: int):
|
|
|
|
"""
|
|
|
|
Tests that the rng values produced at each index are different
|
|
|
|
to prevent if the rng is copied for each subspace
|
|
|
|
"""
|
|
|
|
space.seed(base_seed)
|
|
|
|
|
|
|
|
batched_space = batch_space(space, n)
|
|
|
|
assert space.np_random is not batched_space.np_random
|
|
|
|
assert_rng_equal(space.np_random, batched_space.np_random)
|
|
|
|
|
|
|
|
batched_sample = batched_space.sample()
|
|
|
|
sample = list(iterate(batched_space, batched_sample))
|
|
|
|
assert not all(np.all(element == sample[0]) for element in sample), sample
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"space", spaces, ids=[space.__class__.__name__ for space in spaces]
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize("n", [1, 2, 5], ids=[f"n={n}" for n in [1, 2, 5]])
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"base_seed", [123, 456], ids=[f"seed={base_seed}" for base_seed in [123, 456]]
|
|
|
|
)
|
|
|
|
def test_deterministic(space: Space, n: int, base_seed: int):
|
|
|
|
"""Tests the batched spaces are deterministic by using a copied version"""
|
|
|
|
# Copy the spaces and check that the np_random are not reference equal
|
|
|
|
space_a = space
|
|
|
|
space_a.seed(base_seed)
|
|
|
|
space_b = copy.deepcopy(space_a)
|
|
|
|
assert_rng_equal(space_a.np_random, space_b.np_random)
|
|
|
|
assert space_a.np_random is not space_b.np_random
|
|
|
|
|
|
|
|
# Batch the spaces and check that the np_random are not reference equal
|
|
|
|
space_a_batched = batch_space(space_a, n)
|
|
|
|
space_b_batched = batch_space(space_b, n)
|
|
|
|
assert_rng_equal(space_a_batched.np_random, space_b_batched.np_random)
|
|
|
|
assert space_a_batched.np_random is not space_b_batched.np_random
|
|
|
|
# Create that the batched space is not reference equal to the origin spaces
|
|
|
|
assert space_a.np_random is not space_a_batched.np_random
|
|
|
|
|
|
|
|
# Check that batched space a and b random number generator are not effected by the original space
|
|
|
|
space_a.sample()
|
|
|
|
space_a_batched_sample = space_a_batched.sample()
|
|
|
|
space_b_batched_sample = space_b_batched.sample()
|
|
|
|
for a_sample, b_sample in zip(
|
|
|
|
iterate(space_a_batched, space_a_batched_sample),
|
|
|
|
iterate(space_b_batched, space_b_batched_sample),
|
|
|
|
):
|
|
|
|
if isinstance(a_sample, tuple):
|
|
|
|
assert len(a_sample) == len(b_sample)
|
|
|
|
for a_subsample, b_subsample in zip(a_sample, b_sample):
|
|
|
|
assert_array_equal(a_subsample, b_subsample)
|
|
|
|
else:
|
|
|
|
assert_array_equal(a_sample, b_sample)
|