pub struct ReservoirBuffer<T: Clone> { /* private fields */ }Expand description
Reservoir-sampled buffer backing the NFSP average-policy supervised
dataset. Implements Vitter (1985) Algorithm R: every distinct item
streamed through push is retained with probability
capacity / stream_index (1-indexed across the full lifetime of
the buffer). Under capacity, all pushes are kept; at capacity, each
push at stream index i ≥ capacity + 1 is inserted with probability
capacity / i replacing a uniformly-random existing slot.
§Correctness
The key invariant Vitter’s algorithm guarantees is that at any point in time, the held items are a uniform sample of the entire stream history. This is the property NFSP needs from §3.2 — see the module doc for why FIFO would be wrong.
§Determinism
The internal RNG is seedable via ReservoirBuffer::with_seed so
the trainer can produce repeatable buffer trajectories under a
fixed NfspConfig::seed.
Implementations§
Source§impl<T: Clone> ReservoirBuffer<T>
impl<T: Clone> ReservoirBuffer<T>
Sourcepub fn with_seed(capacity: usize, seed: u64) -> Self
pub fn with_seed(capacity: usize, seed: u64) -> Self
Construct an empty reservoir with the given capacity, seeded from a fixed value for reproducible tests.
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Construct an empty reservoir using a fresh entropy-seeded RNG.
Sourcepub fn stream_length(&self) -> usize
pub fn stream_length(&self) -> usize
Total stream length (number of push calls observed,
regardless of retention). Vitter’s i for the next incoming
item is stream_length() + 1.
Sourcepub fn push(&mut self, item: T) -> bool
pub fn push(&mut self, item: T) -> bool
Push one item through the reservoir.
Under capacity: appended unconditionally. At capacity: kept
with probability capacity / i (where i is the new
1-indexed stream position), replacing a uniformly-random
existing slot.
Returns true if the item was retained (either appended or
replaced an existing slot), false if it was discarded.
Sourcepub fn sample_with_replacement(&mut self, n: usize) -> Vec<T>
pub fn sample_with_replacement(&mut self, n: usize) -> Vec<T>
Sample n items uniformly at random with replacement from
the currently-held reservoir. Returns an empty vec if the
reservoir is empty.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for ReservoirBuffer<T>
impl<T> RefUnwindSafe for ReservoirBuffer<T>where
T: RefUnwindSafe,
impl<T> Send for ReservoirBuffer<T>where
T: Send,
impl<T> Sync for ReservoirBuffer<T>where
T: Sync,
impl<T> Unpin for ReservoirBuffer<T>where
T: Unpin,
impl<T> UnsafeUnpin for ReservoirBuffer<T>
impl<T> UnwindSafe for ReservoirBuffer<T>where
T: UnwindSafe,
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