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>
impl<E> VecEnv<E>
Sourcepub fn new(env: E, num_envs: usize, seed: i64) -> Self
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.
Sourcepub fn with_params(
env: E,
params: E::Params<Native>,
num_envs: usize,
seed: i64,
) -> Self
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).
Sourcepub fn reset(&mut self, seed: Option<i64>)
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);Sourcepub fn obs_f32(&self) -> Vec<f32>
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.
Sourcepub fn obs_shape(&self) -> Option<Vec<usize>>
pub fn obs_shape(&self) -> Option<Vec<usize>>
The shape of one observation (that of the first environment), or
None if there are no environments.
Sourcepub fn obs_len(&self) -> usize
pub fn obs_len(&self) -> usize
The number of elements of one observation in the flat buffers of
VecEnv::step_into and VecEnv::obs_into.
Sourcepub fn obs_into(&self, out: &mut [f32])
pub fn obs_into(&self, out: &mut [f32])
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.
Sourcepub fn set_num_threads(&mut self, threads: usize)
pub fn set_num_threads(&mut self, threads: usize)
Sourcepub fn legal_action_masks_into(&self, out: &mut [bool]) -> bool
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.
Sourcepub fn num_threads(&self) -> usize
pub fn num_threads(&self) -> usize
The number of threads that step the environments.
Sourcepub fn step(&mut self, actions: &[i32]) -> &VecStep<E::Obs<Native>>
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.
Sourcepub fn step_actions(&mut self, actions: Actions<'_>) -> &VecStep<E::Obs<Native>>
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.
Sourcepub fn step_into(&mut self, actions: &[i32], out: StepBuffers<'_>)
pub fn step_into(&mut self, actions: &[i32], out: StepBuffers<'_>)
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());Sourcepub fn step_into_actions(&mut self, actions: Actions<'_>, out: StepBuffers<'_>)
pub fn step_into_actions(&mut self, actions: Actions<'_>, out: StepBuffers<'_>)
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.