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
¶
Whether this environment implements differentiable transitions.
transition_gradients_enabled
property
¶
Whether transition observations and states preserve gradients.
default_params
property
¶
Default parameters of the environment.
Use params.replace(...) to override individual fields before passing the
parameters to reset and step.
num_actions
property
¶
Number of discrete actions, or the action dimension if continuous.
with_transition_gradients ¶
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).terminatedmarks a natural end of the episode andtruncatedmarks the time limitparams.max_steps_in_episode; both can be true at once.infoholds the environment-specific entries plusterminated,truncatedandfinal_observation, which equalsobsunless the episode ended.
reset ¶
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 ¶
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, orNoneafter a reset. -
params(TEnvParams) –Environment parameters.
Returns:
-
Any–The observation, an array or any PyTree of arrays.
get_obs ¶
Legacy observation hook retained for existing environment classes.
is_truncated ¶
Check whether the episode reached params.max_steps_in_episode.
is_terminal ¶
Legacy alias of is_terminated; does not include the time limit.
is_terminated ¶
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 ¶
Legacy helper returning 0 for terminated states and 1 otherwise.
observation_space ¶
Space of observations 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.