pub trait Environment {
type Action;
type State;
// Required methods
fn reset(&mut self);
fn get_observation(&self) -> Vec<f32>;
fn step(&mut self, action: Self::Action) -> StepResult;
fn observation_space(&self) -> SpaceInfo;
fn action_space(&self) -> SpaceInfo;
fn render(&self) -> Vec<u8> ⓘ;
fn close(&mut self);
fn clone_state(&self) -> Self::State;
fn restore_state(&mut self, state: &Self::State);
}Expand description
Core trait for RL environments
Required Associated Types§
Sourcetype Action
type Action
Action type accepted by Environment::step.
- Discrete envs set this to
i64. The index identifies which of theSpaceType::Discreteoptions was chosen. - Continuous envs set this to
Vec<f32>(orf32for a single-dim Box action). Each element is a real-valued coordinate of theSpaceType::Boxaction vector.
Existing call sites that bind actions as i64 should
constrain E: Environment<Action = i64> to keep the
type signature compatible.
Sourcetype State
type State
Snapshot type for this env. For fully deterministic envs without
internal RNG, this is the complete env state and restore_state
reproduces every subsequent step exactly. For envs that consume an
internal RNG (e.g. ball serve direction, food placement), the snapshot
captures the simulation step but not the RNG; see the env-level
documentation for which fields are preserved.
This associated type lets callers (MCTS-style search, replay tooling)
snapshot and later restore env state without re-running from
Environment::reset. The exact determinism guarantee is documented
per env.
Required Methods§
Sourcefn get_observation(&self) -> Vec<f32>
fn get_observation(&self) -> Vec<f32>
Get the current observation
Sourcefn step(&mut self, action: Self::Action) -> StepResult
fn step(&mut self, action: Self::Action) -> StepResult
Step the environment with an action
Sourcefn observation_space(&self) -> SpaceInfo
fn observation_space(&self) -> SpaceInfo
Get the observation space dimensions
Sourcefn action_space(&self) -> SpaceInfo
fn action_space(&self) -> SpaceInfo
Get the action space dimensions
Sourcefn clone_state(&self) -> Self::State
fn clone_state(&self) -> Self::State
Snapshot the current env state for later restoration.
The returned value can be passed back to
Environment::restore_state to rewind the env. For envs with
internal RNG this captures the simulation step only — the determinism
guarantee is documented per env.
Sourcefn restore_state(&mut self, state: &Self::State)
fn restore_state(&mut self, state: &Self::State)
Restore the env to a previously-snapshotted state.
After this call, the env’s observable state (the value returned by
Environment::get_observation) matches the state at the time of the
snapshot. For purely deterministic envs, the next call to
Environment::step with a given action produces the same
StepResult as it would have at the time the snapshot was taken.
For envs with internal RNG, see per-env docs for the exact
reproducibility contract.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".