border-core 0.0.8

Core functionality for Border
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! Generic implementation of replay buffers for reinforcement learning.
//!
//! This module provides a generic implementation of replay buffers that can store
//! and sample transitions of arbitrary observation and action types. It supports:
//! - Standard experience replay
//! - Prioritized experience replay (PER)
//! - Importance sampling weights for off-policy learning

mod iw_scheduler;
mod sum_tree;
use super::{config::PerConfig, BatchBase, GenericTransitionBatch, SimpleReplayBufferConfig};
use crate::{ExperienceBufferBase, ReplayBufferBase, TransitionBatch};
use anyhow::Result;
pub use iw_scheduler::IwScheduler;
use rand::{rngs::StdRng, RngCore, SeedableRng};
use sum_tree::SumTree;
pub use sum_tree::WeightNormalizer;

/// State management for Prioritized Experience Replay (PER).
///
/// This struct maintains the necessary state for PER, including:
/// - A sum tree for efficient priority sampling
/// - An importance weight scheduler for adjusting sample weights
struct PerState {
    /// A sum tree data structure for efficient priority sampling.
    sum_tree: SumTree,

    /// Scheduler for importance sampling weights.
    iw_scheduler: IwScheduler,
}

impl PerState {
    /// Creates a new PER state with the given configuration.
    ///
    /// # Arguments
    ///
    /// * `capacity` - Maximum number of transitions to store
    /// * `per_config` - Configuration for prioritized experience replay
    fn new(capacity: usize, per_config: &PerConfig) -> Self {
        Self {
            sum_tree: SumTree::new(capacity, per_config.alpha, per_config.normalize),
            iw_scheduler: IwScheduler::new(
                per_config.beta_0,
                per_config.beta_final,
                per_config.n_opts_final,
            ),
        }
    }
}

/// A generic implementation of a replay buffer for reinforcement learning.
///
/// This buffer can store transitions of arbitrary observation and action types,
/// making it suitable for a wide range of reinforcement learning tasks. It supports:
/// - Standard experience replay
/// - Prioritized experience replay (optional)
/// - Efficient sampling and storage
///
/// # Type Parameters
///
/// * `O` - The type of observations, must implement [`BatchBase`]
/// * `A` - The type of actions, must implement [`BatchBase`]
///
/// # Examples
///
/// ```ignore
/// let config = SimpleReplayBufferConfig {
///     capacity: 10000,
///     per_config: Some(PerConfig {
///         alpha: 0.6,
///         beta_0: 0.4,
///         beta_final: 1.0,
///         n_opts_final: 100000,
///         normalize: true,
///     }),
/// };
///
/// let mut buffer = SimpleReplayBuffer::<Tensor, Tensor>::build(&config);
///
/// // Add transitions
/// buffer.push(transition)?;
///
/// // Sample a batch
/// let batch = buffer.batch(32)?;
/// ```
pub struct SimpleReplayBuffer<O, A>
where
    O: BatchBase,
    A: BatchBase,
{
    /// Maximum number of transitions that can be stored.
    capacity: usize,

    /// Current insertion index.
    i: usize,

    /// Current number of stored transitions.
    size: usize,

    /// Storage for observations.
    obs: O,

    /// Storage for actions.
    act: A,

    /// Storage for next observations.
    next_obs: O,

    /// Storage for rewards.
    reward: Vec<f32>,

    /// Storage for termination flags.
    is_terminated: Vec<i8>,

    /// Storage for truncation flags.
    is_truncated: Vec<i8>,

    /// Random number generator for sampling.
    rng: StdRng,

    /// State for prioritized experience replay, if enabled.
    per_state: Option<PerState>,
}

impl<O, A> SimpleReplayBuffer<O, A>
where
    O: BatchBase,
    A: BatchBase,
{
    /// Pushes rewards into the buffer at the specified index.
    ///
    /// # Arguments
    ///
    /// * `i` - Starting index for insertion
    /// * `b` - Vector of rewards to insert
    #[inline]
    fn push_reward(&mut self, i: usize, b: &Vec<f32>) {
        let mut j = i;
        for r in b.iter() {
            self.reward[j] = *r;
            j += 1;
            if j == self.capacity {
                j = 0;
            }
        }
    }

    /// Pushes termination flags into the buffer at the specified index.
    ///
    /// # Arguments
    ///
    /// * `i` - Starting index for insertion
    /// * `b` - Vector of termination flags to insert
    #[inline]
    fn push_is_terminated(&mut self, i: usize, b: &Vec<i8>) {
        let mut j = i;
        for d in b.iter() {
            self.is_terminated[j] = *d;
            j += 1;
            if j == self.capacity {
                j = 0;
            }
        }
    }

    /// Pushes truncation flags into the buffer at the specified index.
    ///
    /// # Arguments
    ///
    /// * `i` - Starting index for insertion
    /// * `b` - Vector of truncation flags to insert
    fn push_is_truncated(&mut self, i: usize, b: &Vec<i8>) {
        let mut j = i;
        for d in b.iter() {
            self.is_truncated[j] = *d;
            j += 1;
            if j == self.capacity {
                j = 0;
            }
        }
    }

    /// Samples rewards for the given indices.
    ///
    /// # Arguments
    ///
    /// * `ixs` - Indices to sample from
    ///
    /// # Returns
    ///
    /// Vector of sampled rewards
    fn sample_reward(&self, ixs: &Vec<usize>) -> Vec<f32> {
        ixs.iter().map(|ix| self.reward[*ix]).collect()
    }

    /// Samples termination flags for the given indices.
    ///
    /// # Arguments
    ///
    /// * `ixs` - Indices to sample from
    ///
    /// # Returns
    ///
    /// Vector of sampled termination flags
    fn sample_is_terminated(&self, ixs: &Vec<usize>) -> Vec<i8> {
        ixs.iter().map(|ix| self.is_terminated[*ix]).collect()
    }

    /// Samples truncation flags for the given indices.
    ///
    /// # Arguments
    ///
    /// * `ixs` - Indices to sample from
    ///
    /// # Returns
    ///
    /// Vector of sampled truncation flags
    fn sample_is_truncated(&self, ixs: &Vec<usize>) -> Vec<i8> {
        ixs.iter().map(|ix| self.is_truncated[*ix]).collect()
    }

    /// Sets priorities for newly added samples in prioritized experience replay.
    ///
    /// # Arguments
    ///
    /// * `batch_size` - Number of new samples to prioritize
    fn set_priority(&mut self, batch_size: usize) {
        let sum_tree = &mut self.per_state.as_mut().unwrap().sum_tree;
        let max_p = sum_tree.max();

        for j in 0..batch_size {
            let i = (self.i + j) % self.capacity;
            sum_tree.add(i, max_p);
        }
    }

    /// Returns a batch containing all actions in the buffer.
    ///
    /// # Warning
    ///
    /// This method should be used with caution on large replay buffers
    /// as it may consume significant memory.
    pub fn whole_actions(&self) -> A {
        let ixs = (0..self.size).collect::<Vec<_>>();
        self.act.sample(&ixs)
    }

    /// Returns the number of terminated episodes in the buffer.
    pub fn num_terminated_flags(&self) -> usize {
        self.is_terminated
            .iter()
            .map(|is_terminated| *is_terminated as usize)
            .sum()
    }

    /// Returns the number of truncated episodes in the buffer.
    pub fn num_truncated_flags(&self) -> usize {
        self.is_truncated
            .iter()
            .map(|is_truncated| *is_truncated as usize)
            .sum()
    }

    /// Returns the sum of all rewards in the buffer.
    pub fn sum_rewards(&self) -> f32 {
        self.reward.iter().sum()
    }
}

impl<O, A> ExperienceBufferBase for SimpleReplayBuffer<O, A>
where
    O: BatchBase,
    A: BatchBase,
{
    type Item = GenericTransitionBatch<O, A>;

    /// Returns the current number of transitions in the buffer.
    fn len(&self) -> usize {
        self.size
    }

    /// Adds a new transition to the buffer.
    ///
    /// # Arguments
    ///
    /// * `tr` - The transition to add
    ///
    /// # Returns
    ///
    /// `Ok(())` if the transition was added successfully
    ///
    /// # Errors
    ///
    /// Returns an error if the buffer is full and cannot accept more transitions
    fn push(&mut self, tr: Self::Item) -> Result<()> {
        let len = tr.len(); // batch size
        let (obs, act, next_obs, reward, is_terminated, is_truncated, _, _) = tr.unpack();
        self.obs.push(self.i, obs);
        self.act.push(self.i, act);
        self.next_obs.push(self.i, next_obs);
        self.push_reward(self.i, &reward);
        self.push_is_terminated(self.i, &is_terminated);
        self.push_is_truncated(self.i, &is_truncated);

        if self.per_state.is_some() {
            self.set_priority(len)
        };

        self.i = (self.i + len) % self.capacity;
        self.size += len;
        if self.size >= self.capacity {
            self.size = self.capacity;
        }

        Ok(())
    }
}

impl<O, A> ReplayBufferBase for SimpleReplayBuffer<O, A>
where
    O: BatchBase,
    A: BatchBase,
{
    type Config = SimpleReplayBufferConfig;
    type Batch = GenericTransitionBatch<O, A>;

    /// Creates a new replay buffer with the given configuration.
    ///
    /// # Arguments
    ///
    /// * `config` - Configuration for the replay buffer
    ///
    /// # Returns
    ///
    /// A new instance of the replay buffer
    fn build(config: &Self::Config) -> Self {
        let capacity = config.capacity;
        let per_state = match &config.per_config {
            Some(per_config) => Some(PerState::new(capacity, per_config)),
            None => None,
        };

        Self {
            capacity,
            i: 0,
            size: 0,
            obs: O::new(capacity),
            act: A::new(capacity),
            next_obs: O::new(capacity),
            reward: vec![0.; capacity],
            is_terminated: vec![0; capacity],
            is_truncated: vec![0; capacity],
            rng: StdRng::seed_from_u64(config.seed as _),
            per_state,
        }
    }

    /// Samples a batch of transitions from the buffer.
    ///
    /// If prioritized experience replay is enabled, samples are selected
    /// according to their priorities. Otherwise, uniform random sampling is used.
    ///
    /// # Arguments
    ///
    /// * `size` - Number of transitions to sample
    ///
    /// # Returns
    ///
    /// A batch of sampled transitions
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The buffer is empty
    /// - The requested batch size is larger than the buffer size
    fn batch(&mut self, size: usize) -> Result<Self::Batch> {
        let (ixs, weight) = if let Some(per_state) = &self.per_state {
            let sum_tree = &per_state.sum_tree;
            let beta = per_state.iw_scheduler.beta();
            let (ixs, weight) = sum_tree.sample(size, beta);
            let ixs = ixs.iter().map(|&ix| ix as usize).collect();
            (ixs, Some(weight))
        } else {
            let ixs = (0..size)
                // .map(|_| self.rng.usize(..self.size))
                .map(|_| (self.rng.next_u32() as usize) % self.size)
                .collect::<Vec<_>>();
            let weight = None;
            (ixs, weight)
        };

        Ok(Self::Batch {
            obs: self.obs.sample(&ixs),
            act: self.act.sample(&ixs),
            next_obs: self.next_obs.sample(&ixs),
            reward: self.sample_reward(&ixs),
            is_terminated: self.sample_is_terminated(&ixs),
            is_truncated: self.sample_is_truncated(&ixs),
            ix_sample: Some(ixs),
            weight,
        })
    }

    /// Updates the priorities of transitions in the buffer.
    ///
    /// This method is used in prioritized experience replay to adjust
    /// the sampling probabilities based on TD errors.
    ///
    /// # Arguments
    ///
    /// * `ixs` - Optional indices of transitions to update
    /// * `td_errs` - Optional TD errors for the transitions
    fn update_priority(&mut self, ixs: &Option<Vec<usize>>, td_errs: &Option<Vec<f32>>) {
        if let Some(per_state) = &mut self.per_state {
            let ixs = ixs
                .as_ref()
                .expect("ixs should be Some(_) in update_priority().");
            let td_errs = td_errs
                .as_ref()
                .expect("td_errs should be Some(_) in update_priority().");
            for (&ix, &td_err) in ixs.iter().zip(td_errs.iter()) {
                per_state.sum_tree.update(ix, td_err);
            }
            per_state.iw_scheduler.add_n_opts();
        }
    }
}