border_core/base/
replay_buffer.rs

1//! Replay buffer.
2use anyhow::Result;
3
4/// Interface of buffers of experiences from environments.
5///
6/// You can push items, which has an arbitrary type.
7/// This trait is usually required by processes sampling experiences.
8pub trait ExperienceBufferBase {
9    /// Items pushed into the buffer.
10    type Item;
11
12    /// Pushes a transition into the buffer.
13    fn push(&mut self, tr: Self::Item) -> Result<()>;
14
15    /// The number of samples in the buffer.
16    fn len(&self) -> usize;
17}
18
19/// Interface of replay buffers.
20///
21/// Ones implementing this trait generates a [`ReplayBufferBase::Batch`],
22/// which is used to train agents.
23pub trait ReplayBufferBase {
24    /// Configuration of the replay buffer.
25    type Config: Clone;
26
27    /// Batch generated from the buffer.
28    type Batch;
29
30    /// Build a replay buffer from [Self::Config].
31    fn build(config: &Self::Config) -> Self;
32
33    /// Constructs a batch.
34    ///
35    /// `beta` - The exponent for priority.
36    fn batch(&mut self, size: usize) -> Result<Self::Batch>;
37
38    /// Updates priority.
39    ///
40    /// Priority is commonly based on TD error.
41    ///
42    /// TODO: Consider to move this method to another trait.
43    /// There are cases where prioritization is not required.
44    fn update_priority(&mut self, ixs: &Option<Vec<usize>>, td_err: &Option<Vec<f32>>);
45}