复合空间

class gymnasium.spaces.Dict(spaces: None | dict[str, Space] | Sequence[tuple[str, Space]] = None, seed: dict | int | np.random.Generator | None = None, **spaces_kwargs: Space)[source]

Space 实例的字典。

此空间的元素是来自组成空间的元素的(有序)字典。

示例

>>> from gymnasium.spaces import Dict, Box, Discrete
>>> observation_space = Dict({"position": Box(-1, 1, shape=(2,)), "color": Discrete(3)}, seed=42)
>>> observation_space.sample()
{'color': np.int64(0), 'position': array([-0.3991573 ,  0.21649833], dtype=float32)}

使用嵌套字典

>>> from gymnasium.spaces import Box, Dict, Discrete, MultiBinary, MultiDiscrete
>>> Dict(  
...     {
...         "ext_controller": MultiDiscrete([5, 2, 2]),
...         "inner_state": Dict(
...             {
...                 "charge": Discrete(100),
...                 "system_checks": MultiBinary(10),
...                 "job_status": Dict(
...                     {
...                         "task": Discrete(5),
...                         "progress": Box(low=0, high=100, shape=()),
...                     }
...                 ),
...             }
...         ),
...     }
... )

如果您想使复杂的观察或动作更易于人类理解,使用 Dict 空间可能很方便。通常,您无法直接在学习代码中使用此空间的元素。但是,您可以轻松地使用 gymnasium.wrappers.FlattenObservation 包装器将 Dict 观察结果转换为扁平数组。可以实现类似的包装器来处理 Dict 操作。

参数:
  • spaces – 空间的字典。这指定了 Dict 空间的结构

  • seed – 可选地,您可以使用此参数来为构成 Dict 空间的空间的 RNG 播种。

  • **spaces_kwargs – 如果 spacesNone,您需要将组成空间作为关键字参数传递,如上所述。

sample(mask: dict[str, Any] | None = None) dict[str, Any][source]

从此空间生成单个随机样本。

样本是从组成空间独立采样的有序字典。

参数:

mask – 每个子空间的可选掩码,期望与空间具有相同的键

返回值:

一个字典,具有与 :attr:`self.spaces` 中的相同键和采样值

seed(seed: int | dict[str, Any] | None = None) dict[str, int][source]

为该空间及其所有子空间的 PRNG 播种。

根据种子的类型,子空间将以不同的方式播种

  • None - 所有子空间都将使用随机初始种子

  • Int - 该整数用于为 Dict 空间播种,该空间用于为每个子空间生成种子值。警告,这不能保证所有子空间的种子都是唯一的,尽管这非常不可能。

  • Dict - 每个子空间的种子字典,要求每个子空间都有一个种子键。这支持对多个复合子空间的播种(Dict["space": Dict[...], ...] with {"space": {...}, ...})。

参数:

seed – 可选的整数或子空间键到整数的字典,用于为每个 PRNG 播种。有关更多详细信息,请参见上文。

返回值:

子空间种子值的字典

class gymnasium.spaces.Tuple(spaces: Iterable[Space[Any]], seed: int | Sequence[int] | np.random.Generator | None = None)[source]

Space 实例的元组(更准确地说是笛卡尔积)。

此空间的元素是组成空间元素的元组。

示例

>>> from gymnasium.spaces import Tuple, Box, Discrete
>>> observation_space = Tuple((Discrete(2), Box(-1, 1, shape=(2,))), seed=42)
>>> observation_space.sample()
(np.int64(0), array([-0.3991573 ,  0.21649833], dtype=float32))
参数:
  • spaces (Iterable[Space]) – 参与笛卡尔积的空间。

  • seed – 可选,可以使用此参数为 spaces 的 RNG 播种,以确保可重复采样。

sample(mask: tuple[Any | None, ...] | None = None) tuple[Any, ...][source]

在此空间内生成单个随机样本。

此方法从子空间中抽取独立样本。

参数:

mask – 每个子空间样本的可选元组,用于可选掩码,期望与空间相同的掩码数量

返回值:

子空间样本的元组

seed(seed: int | Sequence[int] | None = None) tuple[int, ...][source]

为该空间及其所有子空间的 PRNG 播种。

根据种子的类型,子空间将以不同的方式播种

  • None - 所有子空间都将使用随机初始种子

  • Int - 整数用于为 Tuple 空间播种,该空间用于为每个子空间生成种子值。警告,这不能保证所有子空间的种子唯一。

  • List / Tuple - 用于为子空间播种的值。这允许播种多个复合子空间 [42, 54, ...]

参数:

seed – 可选的整数列表或整数,用于为(子)空间播种。

返回值:

所有子空间的种子值的元组

class gymnasium.spaces.Sequence(space: Space[Any], seed: int | np.random.Generator | None = None, stack: bool = False)[source]

此空间表示有限长度序列集。

此空间表示形式为 \((a_0, \dots, a_n)\) 的元组集,其中 \(a_i\) 属于在初始化期间指定的某个空间,而整数 \(n\) 不固定

示例

>>> from gymnasium.spaces import Sequence, Box
>>> observation_space = Sequence(Box(0, 1), seed=0)
>>> observation_space.sample()
(array([0.6822636], dtype=float32), array([0.18933342], dtype=float32), array([0.19049619], dtype=float32))
>>> observation_space.sample()
(array([0.83506], dtype=float32), array([0.9053838], dtype=float32), array([0.5836242], dtype=float32), array([0.63214064], dtype=float32))
使用堆叠观测的示例
>>> observation_space = Sequence(Box(0, 1), stack=True, seed=0)
>>> observation_space.sample()
array([[0.6822636 ],
       [0.18933342],
       [0.19049619]], dtype=float32)
参数:
  • space – 此空间表示的序列中的元素必须属于此空间。

  • seed – 可选,可以使用此参数为用于从空间中采样的 RNG 播种。

  • stack – 如果 True,则生成的样本将被堆叠。

sample(mask: None | tuple[None | np.integer | NDArray[np.integer], Any] = None) tuple[Any] | Any[source]

从此空间生成单个随机样本。

参数:

mask

序列长度(可选)和序列中的值(可选)的可选掩码。如果指定 mask,则它应该是一个形式为 (length_mask, sample_mask) 的元组,其中 length_mask

  • None 长度将从几何分布中随机抽取

  • np.ndarray 整数,在这种情况下,采样序列的长度将从该数组中随机抽取。

  • int 用于固定长度样本

掩码元组的第二个元素 sample 掩码指定在从基本空间采样元素时应用的掩码。对每个特征空间样本应用掩码。

返回值:

具有从 :attr:`feature_space` 中随机采样的元素的随机长度元组。

seed(seed: int | tuple[int, int] | None = None) tuple[int, int][source]

对 Sequence 空间和特征空间的 PRNG 播种。

根据种子的类型,子空间将以不同的方式播种

  • None - 所有子空间都将使用随机初始种子

  • Int - 整数用于为 Sequence 空间播种,该空间用于为特征空间生成种子值。

  • Tuple of ints - Sequence 和特征空间的元组。

参数:

seed – 可选的整数或整数元组,用于为 PRNG 播种。有关更多详细信息,请参见上文

返回值:

Sequence 和特征空间的播种值的元组

class gymnasium.spaces.Graph(node_space: Box | Discrete, edge_space: None | Box | Discrete, seed: int | np.random.Generator | None = None)[source]

表示图信息的空格,作为一系列 nodes,根据以一系列 edge_links 表示的邻接矩阵与 edges 连接。

示例

>>> from gymnasium.spaces import Graph, Box, Discrete
>>> observation_space = Graph(node_space=Box(low=-100, high=100, shape=(3,)), edge_space=Discrete(3), seed=123)
>>> observation_space.sample(num_nodes=4, num_edges=8)
GraphInstance(nodes=array([[ 36.47037 , -89.235794, -55.928024],
       [-63.125637, -64.81882 ,  62.4189  ],
       [ 84.669   , -44.68512 ,  63.950912],
       [ 77.97854 ,   2.594091, -51.00708 ]], dtype=float32), edges=array([2, 0, 2, 1, 2, 0, 2, 1]), edge_links=array([[3, 0],
       [0, 0],
       [0, 1],
       [0, 2],
       [1, 0],
       [1, 0],
       [0, 1],
       [0, 2]], dtype=int32))
参数:
  • node_space (Union[Box, Discrete]) – 节点特征的空间。

  • edge_space (Union[None, Box, Discrete]) – 边缘特征的空间。

  • seed – 可选,可以使用此参数为用于从空间中采样的 RNG 播种。

sample(mask: None | tuple[NDArray[Any] | tuple[Any, ...] | None, NDArray[Any] | tuple[Any, ...] | None] = None, num_nodes: int = 10, num_edges: int | None = None) GraphInstance[source]

从 Graph 中生成一个包含 num_nodes(1 到 10 之间)个节点的单个样本图。

参数:
  • mask – 可选的节点和边缘掩码元组,仅在使用离散空间时有效(Box 空间不支持样本掩码)。如果没有提供 `num_edges`,则 `edge_mask` 将乘以边缘数量。

  • num_nodes – 要采样的节点数量,默认为 10 个节点。

  • num_edges – 可选的边缘数量,否则为 0 到 \(num_nodes^2\) 之间的随机数。

返回值:

具有属性 `.nodes`、`.edges` 和 `.edge_links` 的 :class:`GraphInstance`。

seed(seed: int | tuple[int, int] | tuple[int, int, int] | None = None) tuple[int, int] | tuple[int, int, int][source]

为该空间以及节点/边缘子空间的 PRNG 设置种子。

根据种子的类型,子空间将以不同的方式播种

  • None - 随机初始化根、节点和边缘子空间的 PRNG。

  • Int - 整数用于为 Graph 空间设置种子,该空间用于为节点和边缘子空间生成种子值。

  • Tuple[int, int] - 使用特定值对 Graph 和节点子空间设置种子。仅当未指定边缘子空间时。

  • Tuple[int, int, int] - 使用特定值对 Graph、节点和边缘子空间设置种子。

参数:

seed – 该空间以及节点/边缘子空间的可选整数或整数元组。有关更多详细信息,请参见上文。

返回值:

一个包含两个或三个整数的元组,具体取决于是否指定了边缘子空间。

class gymnasium.spaces.OneOf(spaces: Iterable[Space[Any]], seed: int | Sequence[int] | np.random.Generator | None = None)[source]

一组互斥的元组(更准确地说:直接和)Space 实例。

此空间的元素是其中一个组成空间的元素。

示例

>>> from gymnasium.spaces import OneOf, Box, Discrete
>>> observation_space = OneOf((Discrete(2), Box(-1, 1, shape=(2,))), seed=123)
>>> observation_space.sample()  # the first element is the space index (Box in this case) and the second element is the sample from Box
(np.int64(0), np.int64(0))
>>> observation_space.sample()  # this time the Discrete space was sampled as index=0
(np.int64(1), array([-0.00711833, -0.7257502 ], dtype=float32))
>>> observation_space[0]
Discrete(2)
>>> observation_space[1]
Box(-1.0, 1.0, (2,), float32)
>>> len(observation_space)
2
参数:
  • spaces (Iterable[Space]) – 参与笛卡尔积的空间。

  • seed – 可选,可以使用此参数为 spaces 的 RNG 播种,以确保可重复采样。

sample(mask: tuple[Any | None, ...] | None = None) tuple[int, Any][source]

在此空间内生成单个随机样本。

此方法从子空间中抽取独立样本。

参数:

mask – 每个子空间样本的可选元组,用于可选掩码,期望与空间相同的掩码数量

返回值:

子空间样本的元组

seed(seed: int | tuple[int, ...] | None = None) tuple[int, ...][source]

为该空间及其所有子空间的 PRNG 播种。

根据种子的类型,子空间将以不同的方式播种

  • None - 所有子空间都将使用随机初始种子

  • Int - 整数用于为 Tuple 空间播种,该空间用于为每个子空间生成种子值。警告,这不能保证所有子空间的种子唯一。

  • Tuple[int, ...] - 用于对子空间设置种子的值,第一个值对 OneOf 设置种子,后续值对子空间设置种子。这允许对多个复合子空间进行播种 [42, 54, ...]

参数:

seed – 可选的整数或整数元组,用于对 OneOf 空间和子空间设置种子。有关更多详细信息,请参见上文。

返回值:

用于对 OneOf 空间和子空间设置种子的整数元组。