pub trait Environment:
Send
+ Sync
+ 'static {
type State<B: Backend>: Tree<B, On<Native> = Self::State<Native>, On<Trace> = Self::State<Trace>>;
type Params<B: Backend>: Tree<B, On<Native> = Self::Params<Native>, On<Trace> = Self::Params<Trace>>;
type Obs<B: Backend>: Tree<B, On<Native> = Self::Obs<Native>, On<Trace> = Self::Obs<Trace>>;
type Action<B: Backend>: Tree<B, On<Native> = Self::Action<Native>, On<Trace> = Self::Action<Trace>> + ActionValue<B> + Clone;
// Required methods
fn name(&self) -> &str;
fn action_space(&self) -> ActionSpace;
fn default_params<B: Backend>(&self) -> Self::Params<B>;
fn reset<B: Backend>(
&self,
key: Key<B>,
params: &Self::Params<B>,
) -> (Self::Obs<B>, Self::State<B>);
fn step_env<B: Backend>(
&self,
key: Key<B>,
state: &Self::State<B>,
action: Self::Action<B>,
params: &Self::Params<B>,
) -> Transition<B, Self::State<B>, Self::Obs<B>>;
fn is_truncated<B: Backend>(
&self,
state: &Self::State<B>,
params: &Self::Params<B>,
) -> Bool<B>;
// Provided methods
fn num_actions(&self) -> usize { ... }
fn step_env_uses_key(&self) -> bool { ... }
fn legal_action_mask<B: Backend>(
&self,
state: &Self::State<B>,
) -> Option<Array<B, bool>> { ... }
fn step<B: Backend>(
&self,
key: Key<B>,
state: &Self::State<B>,
action: Self::Action<B>,
params: &Self::Params<B>,
) -> Step<B, Self::State<B>, Self::Obs<B>> { ... }
}Expand description
A reinforcement learning environment with gymnax’s API.
The methods are generic over the backend and must be pure functions of
their arguments. Actions are discrete (I32) or continuous
(Array<B, f32>), as Environment::action_space says.
The bounds on the associated types say that a state on one backend
becomes the same structure on the other (State<Native> and
State<Trace>), which #[derive(Tree)] gives.
Required Associated Types§
Sourcetype State<B: Backend>: Tree<B, On<Native> = Self::State<Native>, On<Trace> = Self::State<Trace>>
type State<B: Backend>: Tree<B, On<Native> = Self::State<Native>, On<Trace> = Self::State<Trace>>
The state of an episode.
Sourcetype Params<B: Backend>: Tree<B, On<Native> = Self::Params<Native>, On<Trace> = Self::Params<Trace>>
type Params<B: Backend>: Tree<B, On<Native> = Self::Params<Native>, On<Trace> = Self::Params<Trace>>
The parameters of the environment.
Required Methods§
Sourcefn action_space(&self) -> ActionSpace
fn action_space(&self) -> ActionSpace
The actions (gymnax’s action_space).
Sourcefn default_params<B: Backend>(&self) -> Self::Params<B>
fn default_params<B: Backend>(&self) -> Self::Params<B>
The default parameters.
Sourcefn reset<B: Backend>(
&self,
key: Key<B>,
params: &Self::Params<B>,
) -> (Self::Obs<B>, Self::State<B>)
fn reset<B: Backend>( &self, key: Key<B>, params: &Self::Params<B>, ) -> (Self::Obs<B>, Self::State<B>)
Starts an episode (gymnax’s reset_env).
Provided Methods§
Sourcefn num_actions(&self) -> usize
fn num_actions(&self) -> usize
The number of discrete actions (0 for continuous actions).
Sourcefn step_env_uses_key(&self) -> bool
fn step_env_uses_key(&self) -> bool
Whether step_env uses its key (the default). An environment whose
steps are deterministic returns false, so that step natively
splits the key only when the episode ends and the reset needs it;
the results are the same (testing::check_backends compares the
native results with the traced ones, which always split).
Sourcefn legal_action_mask<B: Backend>(
&self,
state: &Self::State<B>,
) -> Option<Array<B, bool>>
fn legal_action_mask<B: Backend>( &self, state: &Self::State<B>, ) -> Option<Array<B, bool>>
The legal actions in state, for environments that have them (board
games, Jumanji): a mask over the joint actions (shape [n], or
nvec for multi-discrete actions); None (the default) if every
action is legal. Tests and benchmarks take legal actions with
choose_legal. Environments with more joint actions than
MAX_MASK_ACTIONS have no mask (ActionSpace::mask_len is 0).
Sourcefn step<B: Backend>(
&self,
key: Key<B>,
state: &Self::State<B>,
action: Self::Action<B>,
params: &Self::Params<B>,
) -> Step<B, Self::State<B>, Self::Obs<B>>
fn step<B: Backend>( &self, key: Key<B>, state: &Self::State<B>, action: Self::Action<B>, params: &Self::Params<B>, ) -> Step<B, Self::State<B>, Self::Obs<B>>
Advances an episode by one step and starts a new one if it ended, as
gymnax’s Environment.step does: the key is split in two, both
step_env and reset are computed, and the reset state and
observation are selected where the episode ended. Natively, the reset
is computed only when the episode ends (lax::select_with), with
the same results.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".