空间工具

gymnasium.spaces.utils.flatten_space(space: Space[Any]) Box | Dict | Sequence | Tuple | Graph[source]
gymnasium.spaces.utils.flatten_space(space: Box) Box
gymnasium.spaces.utils.flatten_space(space: Discrete | MultiBinary | MultiDiscrete) Box
gymnasium.spaces.utils.flatten_space(space: Discrete | MultiBinary | MultiDiscrete) Box
gymnasium.spaces.utils.flatten_space(space: Discrete | MultiBinary | MultiDiscrete) Box
gymnasium.spaces.utils.flatten_space(space: Tuple) Box | Tuple
gymnasium.spaces.utils.flatten_space(space: Dict) Box | Dict
gymnasium.spaces.utils.flatten_space(space: Graph) Graph
gymnasium.spaces.utils.flatten_space(space: Text) Box
gymnasium.spaces.utils.flatten_space(space: Sequence) Sequence
gymnasium.spaces.utils.flatten_space(space: OneOf) Box

将一个空间尽可能地扁平化。

此函数将尝试将 space 扁平化为一个单一的 gymnasium.spaces.Box 空间。然而,当 spacegymnasium.spaces.Graphgymnasium.spaces.Sequence 的实例,或包含 gymnasium.spaces.Graphgymnasium.spaces.Sequence 空间的复合空间时,这可能无法实现。这等同于 flatten(),但它作用于空间本身。对于非图空间,结果始终是具有扁平边界的 gymnasium.spaces.Box;而对于图空间,结果始终是 gymnasium.spaces.Graph,其 Graph.node_space 是具有扁平边界的 BoxGraph.edge_space 是具有扁平边界的 BoxNone。该 Box 具有精确的 flatdim() 维度。扁平化原始空间的一个样本与从扁平化空间中取样具有相同的效果。然而,从扁平化空间中取样不一定是可逆的。例如,从扁平化离散空间中取样与从 Box 中取样相同,结果可能不是整数或独热编码。这可能导致错误或非均匀取样。

参数:

space – 要扁平化的空间

返回:

一个扁平化的 Box

引发:

NotImplementedError – 如果空间未在 gymnasium.spaces 中定义。

示例 - 扁平化 spaces.Box
>>> from gymnasium.spaces import Box
>>> box = Box(0.0, 1.0, shape=(3, 4, 5))
>>> box
Box(0.0, 1.0, (3, 4, 5), float32)
>>> flatten_space(box)
Box(0.0, 1.0, (60,), float32)
>>> flatten(box, box.sample()) in flatten_space(box)
True
示例 - 扁平化 spaces.Discrete
>>> from gymnasium.spaces import Discrete
>>> discrete = Discrete(5)
>>> flatten_space(discrete)
Box(0, 1, (5,), int64)
>>> flatten(discrete, discrete.sample()) in flatten_space(discrete)
True
示例 - 扁平化 spaces.Dict
>>> from gymnasium.spaces import Dict, Discrete, Box
>>> space = Dict({"position": Discrete(2), "velocity": Box(0, 1, shape=(2, 2))})
>>> flatten_space(space)
Box(0.0, 1.0, (6,), float64)
>>> flatten(space, space.sample()) in flatten_space(space)
True
示例 - 扁平化 spaces.Graph
>>> from gymnasium.spaces import Graph, Discrete, Box
>>> space = Graph(node_space=Box(low=-100, high=100, shape=(3, 4)), edge_space=Discrete(5))
>>> flatten_space(space)
Graph(Box(-100.0, 100.0, (12,), float32), Box(0, 1, (5,), int64))
>>> flatten(space, space.sample()) in flatten_space(space)
True
gymnasium.spaces.utils.flatten(space: Space[T], x: T) ndarray[tuple[Any, ...], dtype[Any]] | dict[str, Any] | tuple[Any, ...] | GraphInstance[source]
gymnasium.spaces.utils.flatten(space: Box | MultiBinary, x: ndarray[tuple[Any, ...], dtype[Any]]) ndarray[tuple[Any, ...], dtype[Any]]
gymnasium.spaces.utils.flatten(space: Box | MultiBinary, x: ndarray[tuple[Any, ...], dtype[Any]]) ndarray[tuple[Any, ...], dtype[Any]]
gymnasium.spaces.utils.flatten(space: Discrete, x: int64) ndarray[tuple[Any, ...], dtype[int64]]
gymnasium.spaces.utils.flatten(space: MultiDiscrete, x: ndarray[tuple[Any, ...], dtype[int64]]) ndarray[tuple[Any, ...], dtype[int64]]
gymnasium.spaces.utils.flatten(space: Tuple, x: tuple[Any, ...]) tuple[Any, ...] | ndarray[tuple[Any, ...], dtype[Any]]
gymnasium.spaces.utils.flatten(space: Dict, x: dict[str, Any]) dict[str, Any] | ndarray[tuple[Any, ...], dtype[Any]]
gymnasium.spaces.utils.flatten(space: Graph, x: GraphInstance) GraphInstance
gymnasium.spaces.utils.flatten(space: Text, x: str) ndarray[tuple[Any, ...], dtype[int32]]
gymnasium.spaces.utils.flatten(space: Sequence, x: tuple[Any, ...] | Any) tuple[Any, ...] | Any
gymnasium.spaces.utils.flatten(space: OneOf, x: tuple[int, Any]) ndarray[tuple[Any, ...], dtype[Any]]

扁平化一个空间中的数据点。

当空间中的点必须传递给只理解扁平浮点数组的神经网络时,这非常有用。

参数:
  • space – 用于扁平化 x 的空间

  • x – 要扁平化的值

返回:

扁平化的数据点

引发:

NotImplementedError – 如果空间未在 gymnasium.spaces 中定义。

示例

>>> from gymnasium.spaces import Box, Discrete, Tuple
>>> space = Box(0, 1, shape=(3, 5))
>>> flatten(space, space.sample()).shape
(15,)
>>> space = Discrete(4)
>>> flatten(space, 2)
array([0, 0, 1, 0])
>>> space = Tuple((Box(0, 1, shape=(2,)), Box(0, 1, shape=(3,)), Discrete(3)))
>>> example = ((.5, .25), (1., 0., .2), 1)
>>> flatten(space, example)
array([0.5 , 0.25, 1.  , 0.  , 0.2 , 0.  , 1.  , 0.  ])
gymnasium.spaces.utils.flatdim(space: Space[Any]) int[source]
gymnasium.spaces.utils.flatdim(space: Box | MultiBinary) int
gymnasium.spaces.utils.flatdim(space: Box | MultiBinary) int
gymnasium.spaces.utils.flatdim(space: Discrete) int
gymnasium.spaces.utils.flatdim(space: MultiDiscrete) int
gymnasium.spaces.utils.flatdim(space: Tuple) int
gymnasium.spaces.utils.flatdim(space: Dict) int
gymnasium.spaces.utils.flatdim(space: Graph)
gymnasium.spaces.utils.flatdim(space: Text) int
gymnasium.spaces.utils.flatdim(space: OneOf) int

返回此空间的扁平化等效形式所具有的维度数。

参数:

space – 要返回扁平化空间维度的空间

返回:

扁平化空间的维度数

引发:
  • NotImplementedError – 如果空间未在 gym.spaces 中定义。

  • ValueError – 如果空间无法扁平化为 gymnasium.spaces.Box

示例

>>> from gymnasium.spaces import Dict, Discrete
>>> space = Dict({"position": Discrete(2), "velocity": Discrete(3)})
>>> flatdim(space)
5
gymnasium.spaces.utils.unflatten(space: Space[T], x: ndarray[tuple[Any, ...], dtype[Any]] | dict[str, Any] | tuple[Any, ...] | GraphInstance) T[source]
gymnasium.spaces.utils.unflatten(space: Box | MultiBinary, x: ndarray[tuple[Any, ...], dtype[Any]]) ndarray[tuple[Any, ...], dtype[Any]]
gymnasium.spaces.utils.unflatten(space: Box | MultiBinary, x: ndarray[tuple[Any, ...], dtype[Any]]) ndarray[tuple[Any, ...], dtype[Any]]
gymnasium.spaces.utils.unflatten(space: Discrete, x: ndarray[tuple[Any, ...], dtype[int64]]) int64
gymnasium.spaces.utils.unflatten(space: MultiDiscrete, x: ndarray[tuple[Any, ...], dtype[integer[Any]]]) ndarray[tuple[Any, ...], dtype[integer[Any]]]
gymnasium.spaces.utils.unflatten(space: Tuple, x: ndarray[tuple[Any, ...], dtype[Any]] | tuple[Any, ...]) tuple[Any, ...]
gymnasium.spaces.utils.unflatten(space: Dict, x: ndarray[tuple[Any, ...], dtype[Any]] | dict[str, Any]) dict[str, Any]
gymnasium.spaces.utils.unflatten(space: Graph, x: GraphInstance) GraphInstance
gymnasium.spaces.utils.unflatten(space: Text, x: ndarray[tuple[Any, ...], dtype[int32]]) str
gymnasium.spaces.utils.unflatten(space: Sequence, x: tuple[Any, ...]) tuple[Any, ...] | Any
gymnasium.spaces.utils.unflatten(space: OneOf, x: ndarray[tuple[Any, ...], dtype[Any]]) tuple[int, Any]

将扁平化的数据点恢复到空间结构。

这会反转由 flatten() 应用的变换。你必须确保 space 参数与 flatten() 调用时使用的相同。

参数:
  • space – 用于恢复 x 结构的原始空间

  • x – 要恢复结构的数组

返回:

一个结构与空间匹配的点。

引发:

NotImplementedError – 如果空间未在 gymnasium.spaces 中定义。