Skip to main content

VecEnv

Struct VecEnv 

Source
pub struct VecEnv<E: Environment> { /* private fields */ }
Expand description

A batch of native environments, stepped in parallel and reset automatically (Environment::step).

The environments are split into chunks run by a pool of worker threads (one per available core by default), which wait for the next step by spinning briefly, so that a step costs little more than its computation.

Each environment has its own key, split at every step as PureJaxRL does (key, step_key = split(key)).

§Examples

use ruxla_env::{CartPole, VecEnv};

let mut envs = VecEnv::new(CartPole, 8, 0);
let step = envs.step(&[1; 8]);
assert_eq!(step.reward, [1.0; 8]);
assert_eq!(envs.obs_f32().len(), 8 * 4);

Implementations§

Source§

impl<E> VecEnv<E>
where E: Environment, E::State<Native>: Send + Sync, E::Params<Native>: Sync, E::Obs<Native>: Send + Sync,

Source

pub fn new(env: E, num_envs: usize, seed: i64) -> Self

Creates num_envs environments with the default parameters and resets them; environment i starts from the key fold_in(key(seed), i), which it splits into its key and the key of the reset.

Source

pub fn with_params( env: E, params: E::Params<Native>, num_envs: usize, seed: i64, ) -> Self

Creates environments with the given parameters (see VecEnv::new).

Source

pub fn reset(&mut self, seed: Option<i64>)

Starts new episodes in every environment.

With a seed, the environments start over as in VecEnv::new with that seed. Without one, each environment splits its current key (key, reset_key = split(key)) and resets with the second half, so that the new episodes depend on the steps taken before.

§Examples
use ruxla_env::{CartPole, VecEnv};

let mut envs = VecEnv::new(CartPole, 4, 7);
let start = envs.obs_f32();
envs.step(&[0; 4]);
envs.reset(Some(7));
assert_eq!(envs.obs_f32(), start);
Source

pub fn num_envs(&self) -> usize

The number of environments.

Source

pub fn env(&self) -> &E

The environment.

Source

pub fn states(&self) -> &[E::State<Native>]

The current states.

Source

pub fn obs(&self) -> &[E::Obs<Native>]

The current observations.

Source

pub fn obs_f32(&self) -> Vec<f32>

The current observations as one contiguous buffer: the leaves of each observation converted to f32, environment after environment.

Source

pub fn obs_shape(&self) -> Option<Vec<usize>>
where E::Obs<Native>: FlatObs,

The shape of one observation (that of the first environment), or None if there are no environments.

Source

pub fn obs_len(&self) -> usize
where E::Obs<Native>: FlatObs,

The number of elements of one observation in the flat buffers of VecEnv::step_into and VecEnv::obs_into.

Source

pub fn obs_into(&self, out: &mut [f32])
where E::Obs<Native>: FlatObs,

Writes the current observations to a flat buffer, environment after environment.

§Panics

Panics if the buffer does not have VecEnv::obs_len elements per environment.

Source

pub fn set_num_threads(&mut self, threads: usize)

Uses threads threads (the caller included) for the steps.

§Panics

Panics if threads is zero.

Source

pub fn legal_action_masks_into(&self, out: &mut [bool]) -> bool

Writes the legal action mask of every environment (Environment::legal_action_mask, flattened, environment after environment) to out and returns true, or returns false if the environment has none.

§Panics

Panics if out has the wrong size.

Source

pub fn num_threads(&self) -> usize

The number of threads that step the environments.

Source

pub fn step(&mut self, actions: &[i32]) -> &VecStep<E::Obs<Native>>

Steps every environment with its action, in parallel, resetting the episodes that end, and returns the results (kept until the next step).

§Panics

Panics if the number of actions differs from the number of environments.

Source

pub fn step_actions(&mut self, actions: Actions<'_>) -> &VecStep<E::Obs<Native>>

VecEnv::step with actions of any kind (continuous actions as a flat buffer).

§Panics

Panics if the number or kind of actions is wrong.

Source

pub fn step_into(&mut self, actions: &[i32], out: StepBuffers<'_>)
where E::Obs<Native>: FlatObs,

Like VecEnv::step, and also writes the results to flat buffers from the threads that step the environments, so that the copy runs in parallel too.

§Panics

Panics if the number of actions differs from the number of environments, or a buffer has the wrong size.

§Examples
use ruxla_env::{CartPole, StepBuffers, VecEnv};

let mut envs = VecEnv::new(CartPole, 2, 0);
let (mut obs, mut final_obs) = ([0.0; 8], [0.0; 8]);
let (mut reward, mut terminated, mut truncated) = ([0.0; 2], [false; 2], [false; 2]);
envs.step_into(
    &[0, 1],
    StepBuffers {
        obs: &mut obs,
        reward: &mut reward,
        terminated: &mut terminated,
        truncated: &mut truncated,
        final_obs: &mut final_obs,
    },
);
assert_eq!(reward, [1.0; 2]);
assert_eq!(obs.to_vec(), envs.obs_f32());
Source

pub fn step_into_actions(&mut self, actions: Actions<'_>, out: StepBuffers<'_>)
where E::Obs<Native>: FlatObs,

VecEnv::step_into with actions of any kind.

§Panics

Panics if the number or kind of actions is wrong, or a buffer has the wrong size.

Trait Implementations§

Source§

impl<E: Debug + Environment> Debug for VecEnv<E>
where E::Params<Native>: Debug, E::State<Native>: Debug, E::Obs<Native>: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<E> !RefUnwindSafe for VecEnv<E>

§

impl<E> !UnwindSafe for VecEnv<E>

§

impl<E> Freeze for VecEnv<E>
where E: Freeze, <E as Environment>::Params<Native>: Freeze,

§

impl<E> Send for VecEnv<E>

§

impl<E> Sync for VecEnv<E>

§

impl<E> Unpin for VecEnv<E>

§

impl<E> UnsafeUnpin for VecEnv<E>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.