Spaces Vector Utils#
- gymnasium.vector.utils.batch_space(space: Space, n: int = 1) Space [source]#
- gymnasium.vector.utils.batch_space(space: Box, n=1)
- gymnasium.vector.utils.batch_space(space: Discrete, n=1)
- gymnasium.vector.utils.batch_space(space: MultiDiscrete, n=1)
- gymnasium.vector.utils.batch_space(space: MultiBinary, n=1)
- gymnasium.vector.utils.batch_space(space: Tuple, n=1)
- gymnasium.vector.utils.batch_space(space: Dict, n=1)
- gymnasium.vector.utils.batch_space(space: Space, n=1)
Create a (batched) space, containing multiple copies of a single space.
- Parameters:
space – Space (e.g. the observation space) for a single environment in the vectorized environment.
n – Number of environments in the vectorized environment.
- Returns:
Space (e.g. the observation space)
- Raises:
ValueError – Cannot batch space that is not a valid
gym.Space
instance
Example
>>> from gymnasium.spaces import Box, Dict >>> import numpy as np >>> space = Dict({ ... 'position': Box(low=0, high=1, shape=(3,), dtype=np.float32), ... 'velocity': Box(low=0, high=1, shape=(2,), dtype=np.float32) ... }) >>> batch_space(space, n=5) Dict('position': Box(0.0, 1.0, (5, 3), float32), 'velocity': Box(0.0, 1.0, (5, 2), float32))
- gymnasium.vector.utils.concatenate(space: Space, items: Iterable, out: tuple | dict | ndarray) tuple | dict | ndarray [source]#
- gymnasium.vector.utils.concatenate(space: MultiBinary, items, out)
- gymnasium.vector.utils.concatenate(space: MultiDiscrete, items, out)
- gymnasium.vector.utils.concatenate(space: Discrete, items, out)
- gymnasium.vector.utils.concatenate(space: Box, items, out)
- gymnasium.vector.utils.concatenate(space: Tuple, items, out)
- gymnasium.vector.utils.concatenate(space: Dict, items, out)
- gymnasium.vector.utils.concatenate(space: Space, items, out)
Concatenate multiple samples from space into a single object.
- Parameters:
space – Observation space of a single environment in the vectorized environment.
items – Samples to be concatenated.
out – The output object. This object is a (possibly nested) numpy array.
- Returns:
The output object. This object is a (possibly nested)
- Raises:
ValueError – Space is not a valid
gym.Space
instance
Example
>>> from gymnasium.spaces import Box >>> import numpy as np >>> space = Box(low=0, high=1, shape=(3,), seed=42, dtype=np.float32) >>> out = np.zeros((2, 3), dtype=np.float32) >>> items = [space.sample() for _ in range(2)] >>> concatenate(space, items, out) array([[0.77395606, 0.43887845, 0.85859793], [0.697368 , 0.09417735, 0.97562236]], dtype=float32)
- gymnasium.vector.utils.iterate(space: Space, items) Iterator [source]#
- gymnasium.vector.utils.iterate(space: Discrete, items)
- gymnasium.vector.utils.iterate(space: MultiBinary, items)
- gymnasium.vector.utils.iterate(space: MultiDiscrete, items)
- gymnasium.vector.utils.iterate(space: Box, items)
- gymnasium.vector.utils.iterate(space: Tuple, items)
- gymnasium.vector.utils.iterate(space: Dict, items)
- gymnasium.vector.utils.iterate(space: Space, items)
Iterate over the elements of a (batched) space.
- Parameters:
space – Space to which items belong to.
items – Items to be iterated over.
- Returns:
Iterator over the elements in `items`.
- Raises:
ValueError – Space is not an instance of
gym.Space
Example
>>> from gymnasium.spaces import Box, Dict >>> import numpy as np >>> space = Dict({ ... 'position': Box(low=0, high=1, shape=(2, 3), seed=42, dtype=np.float32), ... 'velocity': Box(low=0, high=1, shape=(2, 2), seed=42, dtype=np.float32)}) >>> items = space.sample() >>> it = iterate(space, items) >>> next(it) OrderedDict([('position', array([0.77395606, 0.43887845, 0.85859793], dtype=float32)), ('velocity', array([0.77395606, 0.43887845], dtype=float32))]) >>> next(it) OrderedDict([('position', array([0.697368 , 0.09417735, 0.97562236], dtype=float32)), ('velocity', array([0.85859793, 0.697368 ], dtype=float32))]) >>> next(it) Traceback (most recent call last): ... StopIteration