Gymnasium
lerax.compatibility.gym.GymToLeraxEnv
Bases: AbstractEnv[GymEnvState, Array, Array, None]
Wrap a Gymnasium environment for Lerax.
Note
io_callback makes reset and step slower than native JAX and prevents
vmapped rollout. Gymnasium info is discarded because its shape is unknown.
Call methods in order because state objects omit required internal state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
gym.Env
|
Gymnasium environment to wrap. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Name of the environment. |
action_space |
AbstractSpace
|
Action space of the environment. |
observation_space |
AbstractSpace
|
Observation space of the environment. |
env |
gym.Env
|
The original Gymnasium environment. |
action_space
instance-attribute
observation_space
instance-attribute
initial
Call the Gymnasium reset method.
Note
The key generates a reproducible seed unless one is provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Positional arguments to pass to |
()
|
key
|
Key[Array, '']
|
JAX PRNG key used to generate a seed when absent. |
required |
**kwargs
|
Any
|
Keyword arguments to pass to |
{}
|
Returns:
| Type | Description |
|---|---|
GymEnvState
|
The initial environment state. |
transition
Call the Gymnasium step method through io_callback.
The state is ignored, so call order matters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
GymEnvState
|
Current environment state. |
required |
action
|
Array
|
Action to take. |
required |
key
|
Key[Array, '']
|
Unused. |
required |
Returns:
| Type | Description |
|---|---|
GymEnvState
|
Next environment state. |
observation
Return the stored Gymnasium observation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
GymEnvState
|
Current environment state. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Stored observation. |
reward
reward(
state: GymEnvState,
action: Array,
next_state: GymEnvState,
*,
key: Key[Array, ""],
) -> Float[Array, ""]
Return the reward stored in the next state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
GymEnvState
|
Current environment state. |
required |
action
|
Array
|
Action taken. |
required |
next_state
|
GymEnvState
|
Next environment state. |
required |
Returns:
| Type | Description |
|---|---|
Float[Array, '']
|
Transition reward. |
terminal
Return the stored Gymnasium terminated flag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
GymEnvState
|
Current environment state. |
required |
Returns:
| Type | Description |
|---|---|
Bool[Array, '']
|
Whether the state is terminal. |
truncate
Return the stored Gymnasium truncated flag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
GymEnvState
|
Current environment state. |
required |
Returns:
| Type | Description |
|---|---|
Bool[Array, '']
|
Whether the state is truncated. |
state_info
Return empty info to keep JIT shapes stable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
GymEnvState
|
Current environment state. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Empty info. |
transition_info
Return empty info to keep JIT shapes stable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
GymEnvState
|
Current environment state. |
required |
action
|
Array
|
Action taken. |
required |
next_state
|
GymEnvState
|
Next environment state. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Empty info. |
default_renderer
render
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
|
StateType
|
A pytree of stacked environment states to render. |
required |
renderer
|
AbstractRenderer | Literal['auto']
|
The renderer to use for rendering. If "auto", uses the default renderer. |
'auto'
|
dt
|
float
|
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
|
Key[Array, '']
|
A JAX PRNG key for any stochasticity in the reset. |
required |
Returns:
| Type | Description |
|---|---|
tuple[StateType, ObsType, dict]
|
A tuple of the initial state, initial observation, and additional info. |
step
step(
state: StateType,
action: ActType,
*,
key: Key[Array, ""],
) -> 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
|
StateType
|
The current environment state. |
required |
action
|
ActType
|
The action to take. |
required |
key
|
Key[Array, '']
|
A JAX PRNG key for any stochasticity in the step. |
required |
Returns:
| Type | Description |
|---|---|
tuple[StateType, ObsType, Float[Array, ''], Bool[Array, ''], Bool[Array, ''], dict]
|
A tuple of the next state, observation, reward, terminal flag, truncate flag, and additional info. |
lerax.compatibility.gym.LeraxToGymEnv
Bases: gym.Env
Wrap a Lerax environment for Gymnasium.
Run Lerax in Python with internal environment state and a PRNG key.
Attributes:
| Name | Type | Description |
|---|---|---|
metadata |
dict
|
Metadata for the Gym environment. |
action_space |
gym.Space
|
Action space of the environment. |
observation_space |
gym.Space
|
Observation space of the environment. |
render_mode |
str | None
|
Render mode for the environment. |
env |
AbstractEnv[StateType, Array, Array, Any]
|
The Lerax environment to wrap. |
state |
StateType
|
Current state of the Lerax environment. |
key |
Key[Array, '']
|
PRNG key for the environment. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
AbstractEnv[StateType, Array, Array, Any]
|
Lerax environment to wrap. |
required |
render_mode
|
Literal['human'] | None
|
Render mode for the environment. |
None
|
lerax.compatibility.gym.gym_space_to_lerax_space
Convert a Gymnasium space to a Lerax space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
space
|
gymnasium.Space
|
Gymnasium space to convert. |
required |
Returns:
| Type | Description |
|---|---|
lerax.space.AbstractSpace
|
Corresponding Lerax space. |
lerax.compatibility.gym.lerax_to_gym_space
Convert a Lerax space to a Gymnasium space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
space
|
lerax.space.AbstractSpace
|
Lerax space to convert. |
required |
Returns:
| Type | Description |
|---|---|
gymnasium.Space
|
Corresponding Gymnasium space. |