Skip to content

Environment

Every environment derives from Environment. Users call reset and step; the remaining methods are hooks implemented by the individual environments. The environment pages list constructor arguments, parameters and state fields.

Environment

Bases: Generic[TEnvState, TEnvParams]

Abstract base class for environments.

An environment is a stateless collection of pure functions. Everything that changes during an episode lives in an explicit state, and everything that configures the dynamics lives in explicit parameters, so reset and step work with jax.jit, jax.vmap and jax.lax.scan.

Users call reset and step. Subclasses implement step_env, reset_env, observe, default_params and the space methods; see the custom environments guide for a complete example.

supports_transition_gradients property

supports_transition_gradients: bool

Whether this environment implements differentiable transitions.

transition_gradients_enabled property

transition_gradients_enabled: bool

Whether transition observations and states preserve gradients.

default_params property

default_params: EnvParams

Default parameters of the environment.

Use params.replace(...) to override individual fields before passing the parameters to reset and step.

name property

name: str

Name of the environment; for built-ins, the ID passed to gymnax.make.

num_actions property

num_actions: int

Number of discrete actions, or the action dimension if continuous.

with_transition_gradients

with_transition_gradients() -> TEnvironment

Return a copy of the environment that preserves transition gradients.

By default step stops gradients at the returned observation and state. The copy keeps them, so jax.grad can differentiate through transitions with respect to actions, states and parameters. The original environment is left unchanged.

Returns:

  • TEnvironment –

    A copy of the environment with transition gradients enabled.

Raises:

  • ValueError –

    If the environment does not support transition gradients.

step

step(key: Array, state: TEnvState, action: int | float | Array, params: TEnvParams | None = None) -> tuple[Any, TEnvState, jax.Array, jax.Array, jax.Array, dict[Any, Any]]

Advance the environment by one step and reset it if the episode ended.

When the episode terminates or is truncated, the returned observation and state already belong to a freshly reset episode. The last observation of the finished episode is available as info["final_observation"]; use it for bootstrapping.

Parameters:

  • key (Array) –

    PRNG key. It is split internally into one key for the transition and one for the potential reset.

  • state (TEnvState) –

    Current environment state.

  • action (int | float | Array) –

    Action to take.

  • params (TEnvParams | None, default: None ) –

    Environment parameters. Defaults to default_params.

Returns:

  • tuple[Any, TEnvState, Array, Array, Array, dict[Any, Any]] –

    A tuple (obs, state, reward, terminated, truncated, info). terminated marks a natural end of the episode and truncated marks the time limit params.max_steps_in_episode; both can be true at once. info holds the environment-specific entries plus terminated, truncated and final_observation, which equals obs unless the episode ended.

reset

reset(key: Array, params: TEnvParams | None = None) -> tuple[Any, TEnvState]

Start a new episode.

Parameters:

  • key (Array) –

    PRNG key.

  • params (TEnvParams | None, default: None ) –

    Environment parameters. Defaults to default_params.

Returns:

  • tuple[Any, TEnvState] –

    A tuple (obs, state) of the first observation and the initial state.

step_env

step_env(key: Array, state: TEnvState, action: int | float | Array, params: TEnvParams) -> tuple[Any, TEnvState, jax.Array, jax.Array, dict[Any, Any]]

Environment-specific transition, implemented by subclasses.

Unlike step, this neither resets the environment nor applies the time limit. Time limits must not be folded into terminated.

Parameters:

  • key (Array) –

    PRNG key.

  • state (TEnvState) –

    Current environment state.

  • action (int | float | Array) –

    Action to take.

  • params (TEnvParams) –

    Environment parameters.

Returns:

  • tuple[Any, TEnvState, Array, Array, dict[Any, Any]] –

    A tuple (obs, state, reward, terminated, info) describing the raw transition.

reset_env

reset_env(key: Array, params: TEnvParams) -> tuple[jax.Array, TEnvState]

Environment-specific reset, implemented by subclasses.

Parameters:

  • key (Array) –

    PRNG key.

  • params (TEnvParams) –

    Environment parameters.

Returns:

  • tuple[Array, TEnvState] –

    A tuple (obs, state) of the first observation and the initial state.

observe

observe(key: Array, state: TEnvState, action: int | float | Array | None, params: TEnvParams) -> Any

Return the observation for a state and the action that produced it.

Built-in environments that still implement get_obs are adapted here; new environments should implement this method directly.

Parameters:

  • key (Array) –

    PRNG key, for stochastic observations.

  • state (TEnvState) –

    Environment state to observe.

  • action (int | float | Array | None) –

    Action that produced state, or None after a reset.

  • params (TEnvParams) –

    Environment parameters.

Returns:

  • Any –

    The observation, an array or any PyTree of arrays.

get_obs

get_obs(state, params=None, key=None) -> Any

Legacy observation hook retained for existing environment classes.

is_truncated

is_truncated(state: TEnvState, params: TEnvParams) -> jax.Array

Check whether the episode reached params.max_steps_in_episode.

is_terminal

is_terminal(state: TEnvState, params: TEnvParams) -> jax.Array

Legacy alias of is_terminated; does not include the time limit.

is_terminated

is_terminated(state: TEnvState, params: TEnvParams) -> jax.Array

Check whether the state is a natural end of the episode.

Optional helper for subclasses; step uses the terminated flag returned by step_env.

discount

discount(state: TEnvState, params: TEnvParams) -> jax.Array

Legacy helper returning 0 for terminated states and 1 otherwise.

action_space

action_space(params: TEnvParams)

Space of valid actions for the given parameters.

observation_space

observation_space(params: TEnvParams)

Space of observations for the given parameters.

state_space

state_space(params: TEnvParams)

Space of environment states for the given parameters.

State and parameters

EnvState

Base state shared by all environments.

Environments subclass this dataclass and add their own fields. States are immutable PyTrees: step returns a new state and never modifies its input.

Attributes:

  • time (int) –

    Number of steps taken in the current episode.

EnvParams

Base parameters shared by all environments.

Parameters are passed explicitly to reset and step, so they can be changed, traced by jax.jit or batched with jax.vmap without rebuilding the environment. Use params.replace(...) to override individual fields.

Attributes:

  • max_steps_in_episode (int) –

    Number of steps after which the episode is truncated.