Skip to content

Spaces

Spaces describe the actions, observations and states of an environment. They are returned by env.action_space(params), env.observation_space(params) and env.state_space(params).

Space

Abstract base class of all spaces.

sample

sample(key: Array) -> jax.Array

Sample a random element of the space.

Parameters:

  • key (Array) –

    PRNG key.

Returns:

  • Array –

    A random element of the space.

contains

contains(x: Array) -> Any

Check whether x is an element of the space.

Parameters:

  • x (Array) –

    Candidate element.

Returns:

  • Any –

    A boolean scalar array.

Discrete

Discrete(num_categories: int, *, dtype: Any = jnp.int32)

Bases: Space

Space of the integers 0, ..., num_categories - 1.

Parameters:

  • num_categories (int) –

    Number of integer values in the space.

  • dtype (Any, default: int32 ) –

    Integer dtype of sampled elements. int32 by default; int64 is available when JAX x64 mode is enabled.

Attributes:

  • n –

    Number of integer values in the space.

  • shape –

    Shape of an element, always ().

  • dtype –

    Integer dtype of sampled elements.

sample

sample(key: Array) -> jax.Array

Sample an integer uniformly from the space.

contains

contains(x: Array) -> jax.Array

Check whether x lies in [0, n) after casting it to the space dtype.

Box

Box(low: ndarray | float, high: ndarray | float, shape: Any, dtype: dtype = jnp.float32)

Bases: Space

Space of arrays with elementwise lower and upper bounds.

Parameters:

  • low (ndarray | float) –

    Lower bound, a scalar or an array broadcastable to shape.

  • high (ndarray | float) –

    Upper bound, a scalar or an array broadcastable to shape.

  • shape (Any) –

    Shape of an element.

  • dtype (dtype, default: float32 ) –

    Dtype of sampled elements.

sample

sample(key: Array) -> jax.Array

Sample an array uniformly between low and high.

contains

contains(x: Array) -> jax.Array

Check whether all entries of x lie within the bounds.

Dict

Dict(spaces: Any)

Bases: Space

Space of dictionaries whose values come from named subspaces.

Parameters:

  • spaces (Any) –

    Mapping from key to subspace.

sample

sample(key: Array) -> Any

Sample every subspace and return the samples as an ordered dictionary.

contains

contains(x: Array) -> bool

Check whether every entry of x lies in its subspace.

x may be a mapping or an object with one attribute per key, such as an environment state.

Tuple

Tuple(spaces: Sequence[Space])

Bases: Space

Space of tuples whose entries come from a sequence of subspaces.

Parameters:

  • spaces (Sequence[Space]) –

    Subspaces, one per tuple entry.

sample

sample(key: Array) -> Any

Sample every subspace and return the samples as a tuple.

contains

contains(x: Array) -> bool

Check whether every entry of x lies in its subspace.

Conversion

gymnax_space_to_gym_space

gymnax_space_to_gym_space(space: Space) -> gspc.Space

Convert a Gymnax space to the equivalent Gymnasium space.

Parameters:

  • space (Space) –

    Gymnax space; Dict and Tuple spaces are converted recursively.

Returns:

  • Space –

    The equivalent gymnasium.spaces space.

Raises:

  • NotImplementedError –

    If the space type has no Gymnasium equivalent.