Spaces¶
Utilities to operate on Gymnasium spaces.
Overview¶
The utilities described in this section supports the following Gymnasium spaces:
Type |
Supported spaces |
|---|---|
Fundamental |
|
Composite |
The following table provides a snapshot of the space sample conversion functions:
Input |
Function |
Output |
|---|---|---|
Space (NumPy / int) |
Space (PyTorch / JAX) |
|
Space (PyTorch / JAX) |
Space (NumPy / int) |
|
Space (PyTorch / JAX) |
PyTorch tensor / JAX array |
|
PyTorch tensor / JAX array |
Space (PyTorch / JAX) |
API¶
PyTorch¶
Get the low and high limits of a space. |
|
Get the size (number of elements) of a space. |
|
Converts a gym space to a gymnasium space. |
|
Flatten a tensorized space. |
|
Generates a random sample from the specified space. |
|
Convert the sample/value items of a given gymnasium space to PyTorch tensors. |
|
Unflatten a tensor to create a tensorized space. |
|
Convert a tensorized space to a gymnasium space with expected sample/value item types. |
- skrl.utils.spaces.torch.compute_space_limits(space: spaces.Space | None, *, occupied_size: bool = False, device: str | torch.device | None = None, none_if_unbounded: Literal['both', 'below', 'above', 'any'] | None = None) tuple[torch.Tensor | None, torch.Tensor | None][source]¶
Get the low and high limits of a space.
Note
Only the
Boxspace has low and high limits. Other spaces are not bounded (low is-infand high isinf).- Parameters:
space – Gymnasium space.
occupied_size – Whether the limits are returned for the number of elements occupied by the space. It only affects
Discrete(occupied space is 1), andMultiDiscrete(occupied space is the number of discrete spaces).device – Device on which a tensor/array is or will be allocated.
none_if_unbounded – Whether to return
Noneif the space is unbounded. If"both", low and high limits will beNoneif the space is unbounded in both directions. If"below", low limit will beNoneif the space is unbounded below. If"above", high limit will beNoneif the space is unbounded above. If"any", low or high limit will beNoneif the space is unbounded below or above, respectively. If not specified, low and high limits will be defined using-infandinf, respectively, when the space is unbounded.
- Returns:
Low and high limits of the space, or
Noneif the given space isNoneor unbounded (andnone_if_unboundedis specified).
- skrl.utils.spaces.torch.compute_space_size(space: spaces.Space | list[int] | int | None, *, occupied_size: bool = False) int[source]¶
Get the size (number of elements) of a space.
- Parameters:
space – Gymnasium space.
occupied_size – Whether the number of elements occupied by the space is returned. It only affects
Discrete(occupied space is 1), andMultiDiscrete(occupied space is the number of discrete spaces).
- Returns:
Size of the space (number of elements), or
0if the given space isNone.
- skrl.utils.spaces.torch.convert_gym_space(space: 'gym.Space' | None, *, squeeze_batch_dimension: bool = False) gymnasium.Space | None[source]¶
Converts a gym space to a gymnasium space.
- Parameters:
space – Gym space to convert to.
squeeze_batch_dimension – Whether to remove fundamental spaces’ first dimension. It currently affects
Boxspace only.
- Returns:
Converted space, or
Noneif the given space isNone.- Raises:
ValueError – The given space is not supported.
- skrl.utils.spaces.torch.flatten_tensorized_space(x: Any) torch.Tensor | None[source]¶
Flatten a tensorized space.
- Parameters:
x – Tensorized space sample/value.
- Returns:
A tensor. The returned tensor will have shape (batch, space size), or
Noneif the given tensorized sample/value isNone.- Raises:
ValueError – The given sample/value type is not supported.
- skrl.utils.spaces.torch.sample_space(space: spaces.Space | None, *, batch_size: int = 1, backend: Literal['numpy', 'native'] = 'numpy', device: str | torch.device | None = None) Any[source]¶
Generates a random sample from the specified space.
- Parameters:
space – Gymnasium space.
batch_size – Size of the sampled batch.
backend – Whether backend will be used to construct the fundamental spaces.
device – Device on which a tensor/array is or will be allocated. This parameter is used when the backend is
"native"(PyTorch).
- Returns:
Sample of the space, or
Noneif the given space isNone.- Raises:
ValueError – The given space or backend is not supported.
- skrl.utils.spaces.torch.tensorize_space(space: spaces.Space | None, x: Any, *, device: str | torch.device | None = None) Any[source]¶
Convert the sample/value items of a given gymnasium space to PyTorch tensors.
- Parameters:
space – Gymnasium space.
x – Sample/value of the given space to tensorize to.
device – Device on which a tensor/array is or will be allocated. This parameter is used when the space value is not a PyTorch tensor (e.g.: NumPy array, number).
- Returns:
Sample/value space with items converted to tensors, or
Noneif the given space or the sample/value isNone.- Raises:
ValueError – The given space or the sample/value type is not supported.
- skrl.utils.spaces.torch.unflatten_tensorized_space(space: spaces.Space | None, x: torch.Tensor | None) Any[source]¶
Unflatten a tensor to create a tensorized space.
- Parameters:
space – Gymnasium space.
x – A tensor with shape (batch, space size).
- Returns:
Tensorized space value, or
Noneif the given space or the tensor isNone.- Raises:
ValueError – The given space is not supported.
- skrl.utils.spaces.torch.untensorize_space(space: spaces.Space | None, x: Any, *, squeeze_batch_dimension: bool = True) Any[source]¶
Convert a tensorized space to a gymnasium space with expected sample/value item types.
- Parameters:
space – Gymnasium space.
x – Tensorized space (sample/value space where items are tensors).
squeeze_batch_dimension – Whether to remove the batch dimension. If True, only the sample/value with a batch dimension of size 1 will be affected.
- Returns:
Sample/value space with expected item types, or
Noneif the given space or the tensorized sample/value isNone.- Raises:
ValueError – The given space or the sample/value type is not supported.
JAX¶
Get the low and high limits of a space. |
|
Get the size (number of elements) of a space. |
|
Converts a gym space to a gymnasium space. |
|
Flatten a tensorized space. |
|
Generates a random sample from the specified space. |
|
Convert the sample/value items of a given gymnasium space to JAX or NumPy array. |
|
Unflatten a tensor to create a tensorized space. |
|
Convert a tensorized space to a gymnasium space with expected sample/value item types. |
- skrl.utils.spaces.jax.compute_space_limits(space: spaces.Space | None, *, occupied_size: bool = False, device: str | jax.Device | None = None, none_if_unbounded: Literal['both', 'below', 'above', 'any'] | None = None) tuple[jax.Array | None, jax.Array | None][source]¶
Get the low and high limits of a space.
Note
Only the
Boxspace has low and high limits. Other spaces are not bounded (low is-infand high isinf).- Parameters:
space – Gymnasium space.
occupied_size – Whether the limits are returned for the number of elements occupied by the space. It only affects
Discrete(occupied space is 1), andMultiDiscrete(occupied space is the number of discrete spaces).device – Device on which a tensor/array is or will be allocated.
none_if_unbounded – Whether to return
Noneif the space is unbounded. If"both", low and high limits will beNoneif the space is unbounded in both directions. If"below", low limit will beNoneif the space is unbounded below. If"above", high limit will beNoneif the space is unbounded above. If"any", low or high limit will beNoneif the space is unbounded below or above, respectively. If not specified, low and high limits will be defined using-infandinf, respectively, when the space is unbounded.
- Returns:
Low and high limits of the space, or
Noneif the given space isNoneor unbounded (andnone_if_unboundedis specified).
- skrl.utils.spaces.jax.compute_space_size(space: spaces.Space | list[int] | int | None, *, occupied_size: bool = False) int[source]¶
Get the size (number of elements) of a space.
- Parameters:
space – Gymnasium space.
occupied_size – Whether the number of elements occupied by the space is returned. It only affects
Discrete(occupied space is 1), andMultiDiscrete(occupied space is the number of discrete spaces).
- Returns:
Size of the space (number of elements), or
0if the given space isNone.
- skrl.utils.spaces.jax.convert_gym_space(space: 'gym.Space' | None, *, squeeze_batch_dimension: bool = False) gymnasium.Space | None[source]¶
Converts a gym space to a gymnasium space.
- Parameters:
space – Gym space to convert to.
squeeze_batch_dimension – Whether to remove fundamental spaces’ first dimension. It currently affects
Boxspace only.
- Returns:
Converted space, or
Noneif the given space isNone.- Raises:
ValueError – The given space is not supported.
- skrl.utils.spaces.jax.flatten_tensorized_space(x: Any) jax.Array | np.ndarray | None[source]¶
Flatten a tensorized space.
- Parameters:
x – Tensorized space sample/value.
- Returns:
A tensor. The returned tensor will have shape (batch, space size), or
Noneif the given tensorized sample/value isNone.- Raises:
ValueError – The given sample/value type is not supported.
- skrl.utils.spaces.jax.sample_space(space: spaces.Space | None, *, batch_size: int = 1, backend: Literal['numpy', 'native'] = 'numpy', device: str | jax.Device | None = None) Any[source]¶
Generates a random sample from the specified space.
- Parameters:
space – Gymnasium space.
batch_size – Size of the sampled batch.
backend – Whether backend will be used to construct the fundamental spaces.
device – Device on which a tensor/array is or will be allocated. This parameter is used when the backend is
"native"(JAX).
- Returns:
Sample of the space, or
Noneif the given space isNone.- Raises:
ValueError – The given space or backend is not supported.
- skrl.utils.spaces.jax.tensorize_space(space: spaces.Space | None, x: Any, *, device: str | jax.Device | None = None) Any[source]¶
Convert the sample/value items of a given gymnasium space to JAX or NumPy array.
- Parameters:
space – Gymnasium space.
x – Sample/value of the given space to tensorize to.
device – Device on which a tensor/array is or will be allocated. This parameter is used when the space value is not a JAX array (e.g.: NumPy array, number).
- Returns:
Sample/value space with items converted to tensors, or
Noneif the given space or the sample/value isNone.- Raises:
ValueError – The given space or the sample/value type is not supported.
- skrl.utils.spaces.jax.unflatten_tensorized_space(space: spaces.Space | None, x: jax.Array | np.ndarray | None) Any[source]¶
Unflatten a tensor to create a tensorized space.
- Parameters:
space – Gymnasium space.
x – A tensor with shape (batch, space size).
- Returns:
Tensorized space value, or
Noneif the given space or the tensor isNone.- Raises:
ValueError – The given space is not supported.
- skrl.utils.spaces.jax.untensorize_space(space: spaces.Space | None, x: Any, *, squeeze_batch_dimension: bool = True) Any[source]¶
Convert a tensorized space to a gymnasium space with expected sample/value item types.
- Parameters:
space – Gymnasium space.
x – Tensorized space (sample/value space where items are tensors).
squeeze_batch_dimension – Whether to remove the batch dimension. If True, only the sample/value with a batch dimension of size 1 will be affected.
- Returns:
Sample/value space with expected item types, or
Noneif the given space or the tensorized sample/value isNone.- Raises:
ValueError – The given space or the sample/value type is not supported.
Warp¶
Get the low and high limits of a space. |
|
Get the size (number of elements) of a space. |
|
Converts a gym space to a gymnasium space. |
|
Flatten a tensorized space. |
|
Generates a random sample from the specified space. |
|
Convert the sample/value items of a given gymnasium space to Warp arrays. |
|
Unflatten a tensor to create a tensorized space. |
|
Convert a tensorized space to a gymnasium space with expected sample/value item types. |
- skrl.utils.spaces.warp.compute_space_limits(space: spaces.Space | None, *, occupied_size: bool = False, device: str | wp.Device | None = None, none_if_unbounded: Literal['both', 'below', 'above', 'any'] | None = None) tuple[wp.array | None, wp.array | None][source]¶
Get the low and high limits of a space.
Note
Only the
Boxspace has low and high limits. Other spaces are not bounded (low is-infand high isinf).- Parameters:
space – Gymnasium space.
occupied_size – Whether the limits are returned for the number of elements occupied by the space. It only affects
Discrete(occupied space is 1), andMultiDiscrete(occupied space is the number of discrete spaces).device – Device on which a tensor/array is or will be allocated.
none_if_unbounded – Whether to return
Noneif the space is unbounded. If"both", low and high limits will beNoneif the space is unbounded in both directions. If"below", low limit will beNoneif the space is unbounded below. If"above", high limit will beNoneif the space is unbounded above. If"any", low or high limit will beNoneif the space is unbounded below or above, respectively. If not specified, low and high limits will be defined using-infandinf, respectively, when the space is unbounded.
- Returns:
Low and high limits of the space, or
Noneif the given space isNoneor unbounded (andnone_if_unboundedis specified).
- skrl.utils.spaces.warp.compute_space_size(space: spaces.Space | list[int] | int | None, *, occupied_size: bool = False) int[source]¶
Get the size (number of elements) of a space.
- Parameters:
space – Gymnasium space.
occupied_size – Whether the number of elements occupied by the space is returned. It only affects
Discrete(occupied space is 1), andMultiDiscrete(occupied space is the number of discrete spaces).
- Returns:
Size of the space (number of elements), or
0if the given space isNone.
- skrl.utils.spaces.warp.convert_gym_space(space: 'gym.Space' | None, *, squeeze_batch_dimension: bool = False) gymnasium.Space | None[source]¶
Converts a gym space to a gymnasium space.
- Parameters:
space – Gym space to convert to.
squeeze_batch_dimension – Whether to remove fundamental spaces’ first dimension. It currently affects
Boxspace only.
- Returns:
Converted space, or
Noneif the given space isNone.- Raises:
ValueError – The given space is not supported.
- skrl.utils.spaces.warp.flatten_tensorized_space(x: Any) wp.array | None[source]¶
Flatten a tensorized space.
- Parameters:
x – Tensorized space sample/value.
- Returns:
A tensor. The returned tensor will have shape (batch, space size), or
Noneif the given tensorized sample/value isNone.- Raises:
ValueError – The given sample/value type is not supported.
- skrl.utils.spaces.warp.sample_space(space: spaces.Space | None, *, batch_size: int = 1, backend: Literal['numpy', 'native'] = 'numpy', device: str | wp.Device | None = None) Any[source]¶
Generates a random sample from the specified space.
- Parameters:
space – Gymnasium space.
batch_size – Size of the sampled batch.
backend – Whether backend will be used to construct the fundamental spaces.
device – Device on which a tensor/array is or will be allocated. This parameter is used when the backend is
"native"(Warp).
- Returns:
Sample of the space, or
Noneif the given space isNone.- Raises:
ValueError – The given space or backend is not supported.
- skrl.utils.spaces.warp.tensorize_space(space: spaces.Space | None, x: Any, *, device: str | wp.Device | None = None) Any[source]¶
Convert the sample/value items of a given gymnasium space to Warp arrays.
- Parameters:
space – Gymnasium space.
x – Sample/value of the given space to tensorize to.
device – Device on which a tensor/array is or will be allocated. This parameter is used when the space value is not a Warp array (e.g.: NumPy array, number).
- Returns:
Sample/value space with items converted to tensors, or
Noneif the given space or the sample/value isNone.- Raises:
ValueError – The given space or the sample/value type is not supported.
- skrl.utils.spaces.warp.unflatten_tensorized_space(space: spaces.Space | None, x: wp.array | None) Any[source]¶
Unflatten a tensor to create a tensorized space.
- Parameters:
space – Gymnasium space.
x – A tensor with shape (batch, space size).
- Returns:
Tensorized space value, or
Noneif the given space or the tensor isNone.- Raises:
ValueError – The given space is not supported.
- skrl.utils.spaces.warp.untensorize_space(space: spaces.Space | None, x: Any, *, squeeze_batch_dimension: bool = True) Any[source]¶
Convert a tensorized space to a gymnasium space with expected sample/value item types.
- Parameters:
space – Gymnasium space.
x – Tensorized space (sample/value space where items are tensors).
squeeze_batch_dimension – Whether to remove the batch dimension. If True, only the sample/value with a batch dimension of size 1 will be affected.
- Returns:
Sample/value space with expected item types, or
Noneif the given space or the tensorized sample/value isNone.- Raises:
ValueError – The given space or the sample/value type is not supported.