复合空间

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, probability: dict[str, Any] | None = None) dict[str, Any][source]

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

样本是来自组成空间的独立样本的有序字典。

参数:
  • mask – 每个子空间的可选掩码,期望与空间相同的键

  • probability – 每个子空间的可选概率掩码,期望与空间相同的键

返回:

具有相同键和来自 :attr:`self.spaces` 的采样值的字典

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

为此空间和所有子空间的 PRNG 设定种子。

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

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

  • Int - 整数用于为 Dict 空间设定种子,该空间用于为每个子空间生成种子值。 警告,这不能保证所有子空间都具有唯一的种子,尽管这种情况不太可能发生。

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

参数:

seed – 可选的 int 或子空间键到 int 的字典,用于为每个 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, probability: tuple[Any | None, ...] | None = None) tuple[Any, ...][source]

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

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

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

  • probability – 每个子空间样本的可选概率掩码元组,期望的概率掩码数量与空间数量相同

返回:

子空间样本的元组

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 | int | NDArray[np.integer], Any] = None, probability: None | tuple[None | int | NDArray[np.integer], Any] = None) tuple[Any] | Any[source]

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

参数:
  • mask

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

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

    • int - 固定长度

    • np.ndarray 整数数组 - 采样序列的长度从此数组中随机抽取。

    元组的第二个元素 sample_mask 指定了特征空间的采样方式。 使用 mask 还是 probability 将影响使用的参数。

  • probability – 请参见上面的 mask 描述,唯一的区别在于特征空间的 sample_mask 是概率而不是 mask。

返回:

具有随机长度的元组,其中包含来自 :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 – 可选的 int 或 int 元组,用于为 PRNG 设定种子。 有关更多详细信息,请参见上文

返回:

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

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

一个空间,将图形信息表示为一系列通过 edges 连接的 nodes,根据邻接矩阵表示为一系列 edge_links

示例

>>> 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, probability: None | tuple[NDArray[Any] | tuple[Any, ...] | None, NDArray[Any] | tuple[Any, ...] | None] = None, num_nodes: int = 10, num_edges: int | None = None) GraphInstance[source]

生成一个单样本图,其 num_nodes 在 110 之间,从 Graph 中采样。

参数:
  • mask – 可选的节点和边掩码的可选元组,仅在 Discrete 空间中可能(Box 空间不支持样本掩码)。 如果未提供 num_edges,则 edge_mask 乘以边的数量

  • probability – 可选的节点和边概率掩码的可选元组,仅在 Discrete 空间中可能(Box 空间不支持样本概率掩码)。 如果未提供 num_edges,则 edge_mask 乘以边的数量

  • num_nodes – 将被采样的节点数,默认为 10 个节点

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

返回:

一个 :class:`GraphInstance`,包含属性 `.nodes`、`.edges` 和 `.edge_links`。

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 (Discrete in this case) and the second element is the sample from Discrete
(np.int64(0), np.int64(0))
>>> observation_space.sample()  # this time the Box space was sampled as index=1
(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, probability: tuple[Any | None, ...] | None = None) tuple[int, Any][source]

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

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

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

  • probability – 每个子空间样本的可选概率掩码元组,期望的概率掩码数量与空间数量相同

返回:

子空间样本的元组

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

为此空间和所有子空间的 PRNG 设定种子。

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

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

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

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

参数:

seed – 一个可选的整数或整数元组,用于为 OneOf 空间和子空间设置种子。详见上文。

返回:

一个用于为 OneOf 空间和子空间设置种子的整数元组