Skip to main content

Environment

Trait Environment 

Source
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§

Source

type Action

Action type accepted by Environment::step.

  • Discrete envs set this to i64. The index identifies which of the SpaceType::Discrete options was chosen.
  • Continuous envs set this to Vec<f32> (or f32 for a single-dim Box action). Each element is a real-valued coordinate of the SpaceType::Box action vector.

Existing call sites that bind actions as i64 should constrain E: Environment<Action = i64> to keep the type signature compatible.

Source

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§

Source

fn reset(&mut self)

Reset the environment and return initial observation

Source

fn get_observation(&self) -> Vec<f32>

Get the current observation

Source

fn step(&mut self, action: Self::Action) -> StepResult

Step the environment with an action

Source

fn observation_space(&self) -> SpaceInfo

Get the observation space dimensions

Source

fn action_space(&self) -> SpaceInfo

Get the action space dimensions

Source

fn render(&self) -> Vec<u8>

Render the current environment state

Source

fn close(&mut self)

Close the environment

Source

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.

Source

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".

Implementors§