Spaces Utils#
- gymnasium.spaces.utils.flatten_space(space: Space[Any]) Box | Dict | Sequence | Tuple | Graph #
- 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
Flatten a space into a space that is as flat as possible.
This function will attempt to flatten
space
into a singlegymnasium.spaces.Box
space. However, this might not be possible whenspace
is an instance ofgymnasium.spaces.Graph
,gymnasium.spaces.Sequence
or a compound space that contains agymnasium.spaces.Graph
orgymnasium.spaces.Sequence
space. This is equivalent toflatten()
, but operates on the space itself. The result for non-graph spaces is always agymnasium.spaces.Box
with flat boundaries. While the result for graph spaces is always agymnasium.spaces.Graph
withGraph.node_space
being aBox
with flat boundaries andGraph.edge_space
being aBox
with flat boundaries orNone
. The box has exactlyflatdim()
dimensions. Flattening a sample of the original space has the same effect as taking a sample of the flattened space. However, sampling from the flattened space is not necessarily reversible. For example, sampling from a flattened Discrete space is the same as sampling from a Box, and the results may not be integers or one-hot encodings. This may result in errors or non-uniform sampling.- Example::
>>> from gymnasium.spaces import Box >>> box = Box(0.0, 1.0, shape=(3, 4, 5)) >>> box Box(3, 4, 5) >>> flatten_space(box) Box(60,) >>> flatten(box, box.sample()) in flatten_space(box) True
- Example that flattens a discrete space::
>>> from gymnasium.spaces import Discrete >>> discrete = Discrete(5) >>> flatten_space(discrete) Box(5,) >>> flatten(box, box.sample()) in flatten_space(box) True
- Example that recursively flattens a dict::
>>> from gymnasium.spaces import Dict, Discrete, Box >>> space = Dict({"position": Discrete(2), "velocity": Box(0, 1, shape=(2, 2))}) >>> flatten_space(space) Box(6,) >>> flatten(space, space.sample()) in flatten_space(space) True
Example that flattens a graph:
>>> 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
- Parameters:
space – The space to flatten
- Returns:
A flattened Box
- Raises:
NotImplementedError – if the space is not defined in
gymnasium.spaces
.
- gymnasium.spaces.utils.flatten(space: Space[T], x: T) Union[ndarray[Any, dtype[Any]], Dict[str, Any], Tuple[Any, ...], GraphInstance] #
- gymnasium.spaces.utils.flatten(space: Box | MultiBinary, x: NDArray[Any]) NDArray[Any]
- gymnasium.spaces.utils.flatten(space: Box | MultiBinary, x: NDArray[Any]) NDArray[Any]
- gymnasium.spaces.utils.flatten(space: Discrete, x: int64) ndarray[Any, dtype[int64]]
- gymnasium.spaces.utils.flatten(space: MultiDiscrete, x: ndarray[Any, dtype[int64]]) ndarray[Any, dtype[int64]]
- gymnasium.spaces.utils.flatten(space: Tuple, x: tuple[Any, ...]) tuple[Any, ...] | NDArray[Any]
- gymnasium.spaces.utils.flatten(space: Dict, x: dict[str, Any]) dict[str, Any] | NDArray[Any]
- gymnasium.spaces.utils.flatten(space: Graph, x: GraphInstance) GraphInstance
- gymnasium.spaces.utils.flatten(space: Text, x: str) ndarray[Any, dtype[int32]]
- gymnasium.spaces.utils.flatten(space: Sequence, x: tuple[Any, ...]) tuple[Any, ...]
Flatten a data point from a space.
This is useful when e.g. points from spaces must be passed to a neural network, which only understands flat arrays of floats.
- Example usage::
>>> 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. ])
- Parameters:
space – The space that
x
is flattened byx – The value to flatten
- Returns:
The flattened datapoint –
For
gymnasium.spaces.Box
andgymnasium.spaces.MultiBinary
, this is a flattened arrayFor
gymnasium.spaces.Discrete
andgymnasium.spaces.MultiDiscrete
, this is a flattened one-hot array of the sampleFor
gymnasium.spaces.Tuple
andgymnasium.spaces.Dict
, this is a concatenated array the subspaces (does not support graph subspaces)- For graph spaces, returns
GraphInstance
where: GraphInstance.nodes
are n x k arraysGraphInstance.edges
are either:m x k arrays
None
GraphInstance.edge_links
are either:m x 2 arrays
None
- For graph spaces, returns
- Raises:
NotImplementedError – If the space is not defined in
gymnasium.spaces
.
- gymnasium.spaces.utils.flatdim(space: Space[Any]) int #
- 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
Return the number of dimensions a flattened equivalent of this space would have.
Example usage:
>>> from gymnasium.spaces import Discrete, Dict >>> space = Dict({"position": Discrete(2), "velocity": Discrete(3)}) >>> flatdim(space) 5
- Parameters:
space – The space to return the number of dimensions of the flattened spaces
- Returns:
The number of dimensions for the flattened spaces
- Raises:
NotImplementedError – if the space is not defined in
gym.spaces
.ValueError – if the space cannot be flattened into a
gymnasium.spaces.Box
- gymnasium.spaces.utils.unflatten(space: Space[T], x: Union[ndarray[Any, dtype[Any]], Dict[str, Any], Tuple[Any, ...], GraphInstance]) T #
- gymnasium.spaces.utils.unflatten(space: Box | MultiBinary, x: NDArray[Any]) NDArray[Any]
- gymnasium.spaces.utils.unflatten(space: Box | MultiBinary, x: NDArray[Any]) NDArray[Any]
- gymnasium.spaces.utils.unflatten(space: Discrete, x: ndarray[Any, dtype[int64]]) int64
- gymnasium.spaces.utils.unflatten(space: MultiDiscrete, x: ndarray[Any, dtype[integer[Any]]]) ndarray[Any, dtype[integer[Any]]]
- gymnasium.spaces.utils.unflatten(space: Tuple, x: NDArray[Any] | tuple[Any, ...]) tuple[Any, ...]
- gymnasium.spaces.utils.unflatten(space: Dict, x: NDArray[Any] | dict[str, Any]) dict[str, Any]
- gymnasium.spaces.utils.unflatten(space: Graph, x: GraphInstance) GraphInstance
- gymnasium.spaces.utils.unflatten(space: Text, x: ndarray[Any, dtype[int32]]) str
- gymnasium.spaces.utils.unflatten(space: Sequence, x: tuple[Any, ...]) tuple[Any, ...]
Unflatten a data point from a space.
This reverses the transformation applied by
flatten()
. You must ensure that thespace
argument is the same as for theflatten()
call.- Parameters:
space – The space used to unflatten
x
x – The array to unflatten
- Returns:
A point with a structure that matches the space.
- Raises:
NotImplementedError – if the space is not defined in
gymnasium.spaces
.