pub struct PrioritizedReplayBuffer { /* private fields */ }Expand description
Fixed-capacity prioritized replay buffer.
Stores (obs, action, reward, next_obs, done) transitions in a ring
buffer (same FIFO eviction as super::ReplayBuffer), plus a
per-transition priority in a SumTree for O(log N) proportional
sampling.
New transitions are pushed with the maximum existing priority
(or 1.0 for an empty buffer), guaranteeing each fresh transition is
sampled at least once before its priority can be updated by a TD
error.
Implementations§
Source§impl PrioritizedReplayBuffer
impl PrioritizedReplayBuffer
Sourcepub fn new(capacity: usize, obs_dim: usize, alpha: f32, epsilon: f32) -> Self
pub fn new(capacity: usize, obs_dim: usize, alpha: f32, epsilon: f32) -> Self
Create a new prioritized replay buffer.
§Arguments
capacity- Maximum number of transitions stored. Must be > 0.obs_dim- Length of one observation vector. Must be > 0.alpha- Priority exponentα ∈ [0, 1].0recovers uniform sampling;1is fully proportional. Typical value0.6.epsilon- Tiny constant added to|TD error|before raising toα, so transitions with zero TD error still have a tiny chance of being resampled. Typical value1e-6.
§Panics
Panics if capacity == 0, obs_dim == 0, alpha is outside
[0, 1], or epsilon < 0.
Sourcepub fn push(
&mut self,
obs: &[f32],
action: i64,
reward: f32,
next_obs: &[f32],
done: bool,
)
pub fn push( &mut self, obs: &[f32], action: i64, reward: f32, next_obs: &[f32], done: bool, )
Push a single transition into the buffer with the current
maximum priority (or 1.0 if the buffer is empty).
When the buffer is at capacity, this overwrites the oldest transition (FIFO eviction); the corresponding leaf in the sum-tree is overwritten with the new priority.
Sourcepub fn max_priority(&self) -> f32
pub fn max_priority(&self) -> f32
Maximum leaf priority currently in the tree.
Useful for diagnostics; the buffer also tracks this internally
so push can hand fresh transitions a max-priority slot.
Sourcepub fn is_ready(&self, min_size: usize) -> bool
pub fn is_ready(&self, min_size: usize) -> bool
true if the buffer holds at least min_size transitions.
Sourcepub fn sample<R: Rng>(
&self,
batch_size: usize,
beta: f32,
rng: &mut R,
) -> PrioritizedBatch
pub fn sample<R: Rng>( &self, batch_size: usize, beta: f32, rng: &mut R, ) -> PrioritizedBatch
Sample a minibatch using stratified proportional sampling.
Divides [0, total_priority] into batch_size equal segments
and draws one uniform p from each, then walks the sum-tree to
find the leaf whose cumulative-priority interval contains p.
Stratification reduces variance vs. drawing batch_size
independent uniforms from [0, total] (Schaul §3.3).
Importance-sampling weights are computed as
wᵢ = (1 / (N · P(i)))^β and then normalized by max(wⱼ) so the
largest weight is exactly 1.0.
§Arguments
batch_size- Number of transitions to draw. Must be > 0.beta- IS-weight exponent. Caller anneals from a small value (e.g.0.4) to1.0over training.rng- source of randomness.
§Panics
Panics if self.is_empty(), batch_size == 0, or the tree’s
total priority is zero (all leaves were explicitly set to zero —
shouldn’t happen in normal use because push always uses
max_priority ≥ ε^α > 0).
Sourcepub fn update_priorities(&mut self, indices: &[usize], td_errors: &[f32])
pub fn update_priorities(&mut self, indices: &[usize], td_errors: &[f32])
Update the priorities of a set of leaf indices with new TD errors.
For each (idx, td_err) pair, the stored priority becomes
(|td_err| + ε)^α. The max_priority tracker is bumped so
future Self::push calls inherit at least this value.
§Panics
Panics if indices and td_errors have different lengths or if
any index is out of range.
Trait Implementations§
Source§impl Clone for PrioritizedReplayBuffer
impl Clone for PrioritizedReplayBuffer
Source§fn clone(&self) -> PrioritizedReplayBuffer
fn clone(&self) -> PrioritizedReplayBuffer
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for PrioritizedReplayBuffer
impl RefUnwindSafe for PrioritizedReplayBuffer
impl Send for PrioritizedReplayBuffer
impl Sync for PrioritizedReplayBuffer
impl Unpin for PrioritizedReplayBuffer
impl UnsafeUnpin for PrioritizedReplayBuffer
impl UnwindSafe for PrioritizedReplayBuffer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more