Gymnasium
lerax.compatibility.gym.GymToLeraxEnv
Bases:
Wrapper of a Gymnasium environment to make it compatible with Lerax.
Note
Uses jax's io_callback to wrap the env's reset and step functions.
In general, this will be slower than a native JAX environment and prevents
vmapped rollout. Also removes the info dict returned by Gymnasium envs since
the shape cannot be known ahead of time. Even more so than normal it is
important to only call methods in order since the state objects do not
contain all necessary information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
|
Gymnasium environment to wrap. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
|
|
Name of the environment. |
|
|
Action space of the environment. |
|
|
Observation space of the environment. |
|
|
The original Gymnasium environment. |
action_space
instance-attribute
observation_space
instance-attribute
initial
Forwards to the Gymnasium reset.
Note
A seed is generated if none is provided to increase reproducibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Positional arguments to pass to |
()
|
|
key
|
|
JAX PRNG key, used to generate a seed if none is provided. |
required |
**kwargs
|
Keyword arguments to pass to |
{}
|
Returns:
| Type | Description |
|---|---|
|
The initial environment state. |
transition
Forwards to the Gymnasium step.
In practice, this just calls the env's step function via io_callback. This means that the state is ignored and order of operations is important.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
|
Current environment state. |
required |
action
|
|
Action to take. |
required |
key
|
|
Unused. |
required |
Returns:
| Type | Description |
|---|---|
|
Next environment state. |
observation
Forwards to the Gymnasium observation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
|
Current environment state. |
required |
Returns:
| Type | Description |
|---|---|
|
Observation corresponding to the environment state. |
reward
reward(
state: GymEnvState,
action: Array,
next_state: GymEnvState,
*,
key: Key,
) -> Float[Array, ""]
Forwards to the Gymnasium reward.
In practice, this just reads the reward from the next_state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
|
Current environment state. |
required |
action
|
|
Action taken. |
required |
next_state
|
|
Next environment state. |
required |
Returns:
| Type | Description |
|---|---|
|
Reward obtained from the transition. |
terminal
Forwards to the Gymnasium terminated flag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
|
Current environment state. |
required |
Returns:
| Type | Description |
|---|---|
|
Boolean indicating whether the state is terminal. |
truncate
Forwards to the Gymnasium truncated flag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
|
Current environment state. |
required |
Returns:
| Type | Description |
|---|---|
|
Boolean indicating whether the state is truncated. |
state_info
Empty info dict to ensure stable shapes for JIT compilation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
|
Current environment state. |
required |
Returns:
| Type | Description |
|---|---|
|
Empty info dict. |
transition_info
Empty info dict to ensure stable shapes for JIT compilation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
|
Current environment state. |
required |
action
|
|
Action taken. |
required |
next_state
|
|
Next environment state. |
required |
Returns:
| Type | Description |
|---|---|
|
Empty info dict. |
default_renderer
Not supported for Gymnasium environments.
Raises:
| Type | Description |
|---|---|
|
Always. |
render
Not supported for Gymnasium environments.
Raises:
| Type | Description |
|---|---|
|
Always. |
render_stacked
render_stacked(
states: StateType,
renderer: AbstractRenderer | Literal["auto"] = "auto",
dt: float = 0.0,
)
Render multiple frames from stacked states.
Stacked states are typically batched states stored in a pytree structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states
|
|
A pytree of stacked environment states to render. |
required |
renderer
|
|
The renderer to use for rendering. If "auto", uses the default renderer. |
'auto'
|
dt
|
|
The time delay between rendering each frame, in seconds. |
0.0
|
reset
Wrap the functional logic into a Gym API reset method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
|
A JAX PRNG key for any stochasticity in the reset. |
required |
Returns:
| Type | Description |
|---|---|
|
A tuple of the initial state, initial observation, and additional info. |
step
step(
state: StateType, action: ActType, *, key: Key
) -> tuple[
StateType,
ObsType,
Float[Array, ""],
Bool[Array, ""],
Bool[Array, ""],
dict,
]
Wrap the functional logic into a Gym API step method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
|
The current environment state. |
required |
action
|
|
The action to take. |
required |
key
|
|
A JAX PRNG key for any stochasticity in the step. |
required |
Returns:
| Type | Description |
|---|---|
|
A tuple of the next state, observation, reward, terminal flag, truncate flag, and additional info. |
lerax.compatibility.gym.LeraxToGymEnv
Bases:
Wrapper of an Lerax environment to make it compatible with Gymnasium.
Executes the Lerax env directly (Python side). Keeps an internal eqx state and PRNG.
Attributes:
| Name | Type | Description |
|---|---|---|
|
|
Metadata for the Gym environment. |
|
|
Action space of the environment. |
|
|
Observation space of the environment. |
|
|
Render mode for the environment. |
|
|
The Lerax environment to wrap. |
|
|
Current state of the Lerax environment. |
|
|
PRNG key for the environment. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
|
Lerax environment to wrap. |
required |
render_mode
|
|
Render mode for the environment. |
None
|
lerax.compatibility.gym.gym_space_to_lerax_space
Returns a Lerax space corresponding to the given Gymnasium space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
space
|
|
Gymnasium space to convert. |
required |
Returns:
| Type | Description |
|---|---|
|
The corresponding Lerax space. |
lerax.compatibility.gym.lerax_to_gym_space
Returns a Gymnasium space corresponding to the given Lerax space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
space
|
|
Lerax space to convert. |
required |
Returns:
| Type | Description |
|---|---|
|
The corresponding Gymnasium space. |