实用函数¶
向量化空间¶
- gymnasium.vector.utils.batch_space(space: Space[Any], n: int = 1) Space[Any] [源代码]¶
- gymnasium.vector.utils.batch_space(space: Box, n: int = 1)
- gymnasium.vector.utils.batch_space(space: Discrete, n: int = 1)
- gymnasium.vector.utils.batch_space(space: MultiDiscrete, n: int = 1)
- gymnasium.vector.utils.batch_space(space: MultiBinary, n: int = 1)
- gymnasium.vector.utils.batch_space(space: Tuple, n: int = 1)
- gymnasium.vector.utils.batch_space(space: Dict, n: int = 1)
- gymnasium.vector.utils.batch_space(space: Graph | Text | Sequence | OneOf, n: int = 1)
- gymnasium.vector.utils.batch_space(space: Graph | Text | Sequence | OneOf, n: int = 1)
- gymnasium.vector.utils.batch_space(space: Graph | Text | Sequence | OneOf, n: int = 1)
- gymnasium.vector.utils.batch_space(space: Graph | Text | Sequence | OneOf, n: int = 1)
- gymnasium.vector.utils.batch_space(space: Graph | Text | Sequence | OneOf, n: int = 1)
创建一个(批次的)空间,包含单个空间的多个副本。
- 参数:
space – 向量化环境中单个环境的 空间(例如,观察空间)。
n – 向量化环境中的环境数量。
- 返回:
空间 (例如,观测空间)
- 抛出:
ValueError – 无法批处理的空间没有注册的函数。
示例
>>> 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[Any, ...] | dict[str, Any] | ndarray) tuple[Any, ...] | dict[str, Any] | ndarray [源代码]¶
- gymnasium.vector.utils.concatenate(space: Box | Discrete | MultiDiscrete | MultiBinary, items: Iterable, out: ndarray) ndarray
- gymnasium.vector.utils.concatenate(space: Box | Discrete | MultiDiscrete | MultiBinary, items: Iterable, out: ndarray) ndarray
- gymnasium.vector.utils.concatenate(space: Box | Discrete | MultiDiscrete | MultiBinary, items: Iterable, out: ndarray) ndarray
- gymnasium.vector.utils.concatenate(space: Box | Discrete | MultiDiscrete | MultiBinary, items: Iterable, out: ndarray) ndarray
- gymnasium.vector.utils.concatenate(space: Tuple, items: Iterable, out: tuple[Any, ...]) tuple[Any, ...]
- gymnasium.vector.utils.concatenate(space: Dict, items: Iterable, out: dict[str, Any]) dict[str, Any]
- gymnasium.vector.utils.concatenate(space: Space, items: Iterable, out: None) tuple[Any, ...]
- gymnasium.vector.utils.concatenate(space: Space, items: Iterable, out: None) tuple[Any, ...]
- gymnasium.vector.utils.concatenate(space: Space, items: Iterable, out: None) tuple[Any, ...]
- gymnasium.vector.utils.concatenate(space: Space, items: Iterable, out: None) tuple[Any, ...]
- gymnasium.vector.utils.concatenate(space: Space, items: Iterable, out: None) tuple[Any, ...]
将来自空间的多个样本连接成一个单一对象。
- 参数:
space – 向量化环境中单个环境的观察空间。
items – 待连接的样本。
out – 输出对象。这个对象是一个(可能是嵌套的)numpy数组。
- 返回:
输出对象。这个对象是一个 (可能嵌套的)
- 抛出:
ValueError – 空间
示例
>>> 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[T_cov], items: Iterable[T_cov]) Iterator [源代码]¶
- gymnasium.vector.utils.iterate(space: Discrete, items: Iterable)
- gymnasium.vector.utils.iterate(space: Box | MultiDiscrete | MultiBinary, items: ndarray)
- gymnasium.vector.utils.iterate(space: Box | MultiDiscrete | MultiBinary, items: ndarray)
- gymnasium.vector.utils.iterate(space: Box | MultiDiscrete | MultiBinary, items: ndarray)
- gymnasium.vector.utils.iterate(space: Tuple, items: tuple[Any, ...])
- gymnasium.vector.utils.iterate(space: Dict, items: dict[str, Any])
遍历一个(批处理的)空间的元素。
- 参数:
space – 向量化环境中单个环境的观察空间。
items – 待连接的样本。
out – 输出对象。这个对象是一个(可能是嵌套的)numpy数组。
- 返回:
输出对象。这个对象是一个 (可能嵌套的)
- 抛出:
ValueError – Space 不是 :class:
gymnasium.Space
的一个实例
示例
>>> 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) {'position': array([0.77395606, 0.43887845, 0.85859793], dtype=float32), 'velocity': array([0.77395606, 0.43887845], dtype=float32)} >>> next(it) {'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
- gymnasium.vector.utils.create_empty_array(space: Space, n: int = 1, fn: callable = np.zeros) tuple[Any, ...] | dict[str, Any] | ndarray [源代码]¶
- gymnasium.vector.utils.create_empty_array(space: Box, n: int = 1, fn=np.zeros) ndarray
- gymnasium.vector.utils.create_empty_array(space: Box, n: int = 1, fn=np.zeros) ndarray
- gymnasium.vector.utils.create_empty_array(space: Box, n: int = 1, fn=np.zeros) ndarray
- gymnasium.vector.utils.create_empty_array(space: Box, n: int = 1, fn=np.zeros) ndarray
- gymnasium.vector.utils.create_empty_array(space: Tuple, n: int = 1, fn=np.zeros) tuple[Any, ...]
- gymnasium.vector.utils.create_empty_array(space: Dict, n: int = 1, fn=np.zeros) dict[str, Any]
- gymnasium.vector.utils.create_empty_array(space: Graph, n: int = 1, fn=np.zeros) tuple[GraphInstance, ...]
- gymnasium.vector.utils.create_empty_array(space: Text, n: int = 1, fn=np.zeros) tuple[str, ...]
- gymnasium.vector.utils.create_empty_array(space: Sequence, n: int = 1, fn=np.zeros) tuple[Any, ...]
- gymnasium.vector.utils.create_empty_array(space: OneOf, n: int = 1, fn=np.zeros)
- gymnasium.vector.utils.create_empty_array(space: ~gymnasium.spaces.space.Space, n=1, fn=<built-in function zeros>)
创建一个空的(可能是嵌套的)(通常基于numpy的)数组,与
concatenate(..., out=array)
一起使用。在大多数情况下,数组将包含在批处理空间内,但这并不能保证。
- 参数:
space – 向量化环境中单个环境的观察空间。
n – 向量化环境中的环境数量。如果为
None
,则从space
创建一个空样本。fn – 在创建空numpy数组时应用的函数。此类函数的示例包括
np.empty
或np.zeros
。
- 返回:
输出对象。这个对象是一个 (可能嵌套的)
- 抛出:
ValueError – Space 不是一个有效的 :class:
gymnasium.Space
实例
示例
>>> 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)}) >>> create_empty_array(space, n=2, fn=np.zeros) {'position': array([[0., 0., 0.], [0., 0., 0.]], dtype=float32), 'velocity': array([[0., 0.], [0., 0.]], dtype=float32)}