mirror of
https://github.com/Farama-Foundation/Gymnasium.git
synced 2025-08-01 06:07:08 +00:00
* Initial version of vectorized environments * Raise an exception in the main process if child process raises an exception * Add list of exposed functions in vector module * Use deepcopy instead of np.copy * Add documentation for vector utils * Add tests for copy in AsyncVectorEnv * Add example in documentation for batch_space * Add cloudpickle dependency in setup.py * Fix __del__ in VectorEnv * Check if all observation spaces are equal in AsyncVectorEnv * Check if all observation spaces are equal in SyncVectorEnv * Fix spaces non equality in SyncVectorEnv for Python 2 * Handle None parameter in create_empty_array * Fix check_observation_space with spaces equality * Raise an exception when operations are out of order in AsyncVectorEnv * Add version requirement for cloudpickle * Use a state instead of binary flags in AsyncVectorEnv * Use numpy.zeros when initializing observations in vectorized environments * Remove poll from public API in AsyncVectorEnv * Remove close_extras from VectorEnv * Add test between AsyncVectorEnv and SyncVectorEnv * Remove close in check_observation_space * Add documentation for seed and close * Refactor exceptions for AsyncVectorEnv * Close pipes if the environment raises an error * Add tests for out of order operations * Change default argument in create_empty_array to np.zeros * Add get_attr and set_attr methods to VectorEnv * Improve consistency in SyncVectorEnv
142 lines
4.9 KiB
Python
142 lines
4.9 KiB
Python
import pytest
|
|
import numpy as np
|
|
|
|
from collections import OrderedDict
|
|
|
|
from gym.spaces import Tuple, Dict
|
|
from gym.vector.utils.spaces import _BaseGymSpaces
|
|
from gym.vector.tests.utils import spaces
|
|
|
|
from gym.vector.utils.numpy_utils import concatenate, create_empty_array
|
|
|
|
@pytest.mark.parametrize('space', spaces,
|
|
ids=[space.__class__.__name__ for space in spaces])
|
|
def test_concatenate(space):
|
|
def assert_type(lhs, rhs, n):
|
|
# Special case: if rhs is a list of scalars, lhs must be an np.ndarray
|
|
if np.isscalar(rhs[0]):
|
|
assert isinstance(lhs, np.ndarray)
|
|
assert all([np.isscalar(rhs[i]) for i in range(n)])
|
|
else:
|
|
assert all([isinstance(rhs[i], type(lhs)) for i in range(n)])
|
|
|
|
def assert_nested_equal(lhs, rhs, n):
|
|
assert isinstance(rhs, list)
|
|
assert (n > 0) and (len(rhs) == n)
|
|
assert_type(lhs, rhs, n)
|
|
if isinstance(lhs, np.ndarray):
|
|
assert lhs.shape[0] == n
|
|
for i in range(n):
|
|
assert np.all(lhs[i] == rhs[i])
|
|
|
|
elif isinstance(lhs, tuple):
|
|
for i in range(len(lhs)):
|
|
rhs_T_i = [rhs[j][i] for j in range(n)]
|
|
assert_nested_equal(lhs[i], rhs_T_i, n)
|
|
|
|
elif isinstance(lhs, OrderedDict):
|
|
for key in lhs.keys():
|
|
rhs_T_key = [rhs[j][key] for j in range(n)]
|
|
assert_nested_equal(lhs[key], rhs_T_key, n)
|
|
|
|
else:
|
|
raise TypeError('Got unknown type `{0}`.'.format(type(lhs)))
|
|
|
|
samples = [space.sample() for _ in range(8)]
|
|
array = create_empty_array(space, n=8)
|
|
concatenated = concatenate(samples, array, space)
|
|
|
|
assert np.all(concatenated == array)
|
|
assert_nested_equal(array, samples, n=8)
|
|
|
|
|
|
@pytest.mark.parametrize('n', [1, 8])
|
|
@pytest.mark.parametrize('space', spaces,
|
|
ids=[space.__class__.__name__ for space in spaces])
|
|
def test_create_empty_array(space, n):
|
|
|
|
def assert_nested_type(arr, space, n):
|
|
if isinstance(space, _BaseGymSpaces):
|
|
assert isinstance(arr, np.ndarray)
|
|
assert arr.dtype == space.dtype
|
|
assert arr.shape == (n,) + space.shape
|
|
|
|
elif isinstance(space, Tuple):
|
|
assert isinstance(arr, tuple)
|
|
assert len(arr) == len(space.spaces)
|
|
for i in range(len(arr)):
|
|
assert_nested_type(arr[i], space.spaces[i], n)
|
|
|
|
elif isinstance(space, Dict):
|
|
assert isinstance(arr, OrderedDict)
|
|
assert set(arr.keys()) ^ set(space.spaces.keys()) == set()
|
|
for key in arr.keys():
|
|
assert_nested_type(arr[key], space.spaces[key], n)
|
|
|
|
else:
|
|
raise TypeError('Got unknown type `{0}`.'.format(type(arr)))
|
|
|
|
array = create_empty_array(space, n=n, fn=np.empty)
|
|
assert_nested_type(array, space, n=n)
|
|
|
|
|
|
@pytest.mark.parametrize('n', [1, 8])
|
|
@pytest.mark.parametrize('space', spaces,
|
|
ids=[space.__class__.__name__ for space in spaces])
|
|
def test_create_empty_array_zeros(space, n):
|
|
|
|
def assert_nested_type(arr, space, n):
|
|
if isinstance(space, _BaseGymSpaces):
|
|
assert isinstance(arr, np.ndarray)
|
|
assert arr.dtype == space.dtype
|
|
assert arr.shape == (n,) + space.shape
|
|
assert np.all(arr == 0)
|
|
|
|
elif isinstance(space, Tuple):
|
|
assert isinstance(arr, tuple)
|
|
assert len(arr) == len(space.spaces)
|
|
for i in range(len(arr)):
|
|
assert_nested_type(arr[i], space.spaces[i], n)
|
|
|
|
elif isinstance(space, Dict):
|
|
assert isinstance(arr, OrderedDict)
|
|
assert set(arr.keys()) ^ set(space.spaces.keys()) == set()
|
|
for key in arr.keys():
|
|
assert_nested_type(arr[key], space.spaces[key], n)
|
|
|
|
else:
|
|
raise TypeError('Got unknown type `{0}`.'.format(type(arr)))
|
|
|
|
array = create_empty_array(space, n=n, fn=np.zeros)
|
|
assert_nested_type(array, space, n=n)
|
|
|
|
|
|
@pytest.mark.parametrize('space', spaces,
|
|
ids=[space.__class__.__name__ for space in spaces])
|
|
def test_create_empty_array_none_shape_ones(space):
|
|
|
|
def assert_nested_type(arr, space):
|
|
if isinstance(space, _BaseGymSpaces):
|
|
assert isinstance(arr, np.ndarray)
|
|
assert arr.dtype == space.dtype
|
|
assert arr.shape == space.shape
|
|
assert np.all(arr == 1)
|
|
|
|
elif isinstance(space, Tuple):
|
|
assert isinstance(arr, tuple)
|
|
assert len(arr) == len(space.spaces)
|
|
for i in range(len(arr)):
|
|
assert_nested_type(arr[i], space.spaces[i])
|
|
|
|
elif isinstance(space, Dict):
|
|
assert isinstance(arr, OrderedDict)
|
|
assert set(arr.keys()) ^ set(space.spaces.keys()) == set()
|
|
for key in arr.keys():
|
|
assert_nested_type(arr[key], space.spaces[key])
|
|
|
|
else:
|
|
raise TypeError('Got unknown type `{0}`.'.format(type(arr)))
|
|
|
|
array = create_empty_array(space, n=None, fn=np.ones)
|
|
assert_nested_type(array, space)
|