numrs2 0.3.3

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//! Experience Replay Buffers for Deep RL
//!
//! This module provides experience replay buffers for deep reinforcement learning,
//! including standard replay and prioritized experience replay.

use crate::error::{NumRs2Error, Result};
use scirs2_core::ndarray::Array1;
use scirs2_core::random::{Distribution, Rng, Uniform};
use std::collections::VecDeque;

/// Experience tuple for replay buffer
#[derive(Debug, Clone)]
pub struct Experience {
    /// Current state
    pub state: Array1<f64>,
    /// Action taken
    pub action: usize,
    /// Reward received
    pub reward: f64,
    /// Next state
    pub next_state: Array1<f64>,
    /// Whether episode is done
    pub done: bool,
}

/// Standard experience replay buffer
///
/// Stores experiences in a circular buffer and samples uniformly at random.
/// Used in DQN and other off-policy algorithms.
///
/// # Mathematical Background
///
/// Experience replay breaks correlation between consecutive samples by storing
/// transitions (s, a, r, s', done) and sampling randomly from the buffer.
///
/// This provides several benefits:
/// - Breaks temporal correlation in data
/// - Improves data efficiency through reuse
/// - Smooths learning distribution
pub struct ExperienceReplay {
    buffer: VecDeque<Experience>,
    capacity: usize,
}

impl ExperienceReplay {
    /// Create new experience replay buffer
    ///
    /// # Arguments
    /// * `capacity` - Maximum number of experiences to store
    pub fn new(capacity: usize) -> Self {
        if capacity == 0 {
            panic!("Capacity must be positive");
        }

        Self {
            buffer: VecDeque::with_capacity(capacity),
            capacity,
        }
    }

    /// Add experience to buffer
    ///
    /// If buffer is full, oldest experience is removed.
    pub fn push(&mut self, experience: Experience) {
        if self.buffer.len() >= self.capacity {
            self.buffer.pop_front();
        }
        self.buffer.push_back(experience);
    }

    /// Sample batch of experiences uniformly at random
    ///
    /// # Arguments
    /// * `batch_size` - Number of experiences to sample
    /// * `rng` - Random number generator
    ///
    /// # Returns
    /// Vector of sampled experiences
    pub fn sample<R: Rng>(&self, batch_size: usize, rng: &mut R) -> Result<Vec<Experience>> {
        if batch_size > self.buffer.len() {
            return Err(NumRs2Error::ValueError(format!(
                "Requested batch size {} exceeds buffer size {}",
                batch_size,
                self.buffer.len()
            )));
        }

        let dist = Uniform::new(0, self.buffer.len())
            .map_err(|e| NumRs2Error::ValueError(format!("Uniform distribution error: {}", e)))?;

        let mut samples = Vec::with_capacity(batch_size);
        for _ in 0..batch_size {
            let idx = dist.sample(rng);
            samples.push(self.buffer[idx].clone());
        }

        Ok(samples)
    }

    /// Get current buffer size
    pub fn len(&self) -> usize {
        self.buffer.len()
    }

    /// Check if buffer is empty
    pub fn is_empty(&self) -> bool {
        self.buffer.is_empty()
    }

    /// Get buffer capacity
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Clear buffer
    pub fn clear(&mut self) {
        self.buffer.clear();
    }

    /// Check if buffer is full
    pub fn is_full(&self) -> bool {
        self.buffer.len() >= self.capacity
    }

    /// Get reference to all experiences
    pub fn experiences(&self) -> &VecDeque<Experience> {
        &self.buffer
    }
}

/// Prioritized experience replay buffer
///
/// Samples experiences based on their TD-error priority, allowing the agent
/// to learn more from surprising transitions.
///
/// # Mathematical Background
///
/// Experiences are sampled with probability:
///
/// ```text
/// P(i) = p_i^α / Σ_k p_k^α
/// ```
///
/// where p_i is the priority of experience i and α controls the amount of
/// prioritization (α=0 gives uniform sampling).
///
/// To correct for non-uniform sampling, importance-sampling weights are used:
///
/// ```text
/// w_i = (N * P(i))^(-β) / max_j w_j
/// ```
///
/// where β is annealed from β_start to 1 over training.
///
/// # References
///
/// Schaul et al. (2015). "Prioritized Experience Replay"
/// arXiv:1511.05952
pub struct PrioritizedExperienceReplay {
    buffer: VecDeque<Experience>,
    priorities: VecDeque<f64>,
    capacity: usize,
    alpha: f64,
    beta: f64,
    beta_increment: f64,
    epsilon: f64,
    max_priority: f64,
}

impl PrioritizedExperienceReplay {
    /// Create new prioritized experience replay buffer
    ///
    /// # Arguments
    /// * `capacity` - Maximum number of experiences to store
    /// * `alpha` - Priority exponent (α). Higher values prioritize more. Typical: 0.6
    /// * `beta` - Initial importance-sampling exponent (β). Typical: 0.4
    /// * `beta_increment` - Amount to increment β per sample. Typical: 1e-6
    /// * `epsilon` - Small constant to ensure non-zero priorities. Typical: 1e-6
    pub fn new(
        capacity: usize,
        alpha: f64,
        beta: f64,
        beta_increment: f64,
        epsilon: f64,
    ) -> Result<Self> {
        if capacity == 0 {
            return Err(NumRs2Error::ValueError(
                "Capacity must be positive".to_string(),
            ));
        }
        if alpha < 0.0 {
            return Err(NumRs2Error::ValueError(
                "alpha must be non-negative".to_string(),
            ));
        }
        if !(0.0..=1.0).contains(&beta) {
            return Err(NumRs2Error::ValueError(
                "beta must be in [0, 1]".to_string(),
            ));
        }
        if epsilon <= 0.0 {
            return Err(NumRs2Error::ValueError(
                "epsilon must be positive".to_string(),
            ));
        }

        Ok(Self {
            buffer: VecDeque::with_capacity(capacity),
            priorities: VecDeque::with_capacity(capacity),
            capacity,
            alpha,
            beta,
            beta_increment,
            epsilon,
            max_priority: 1.0,
        })
    }

    /// Add experience to buffer with initial max priority
    pub fn push(&mut self, experience: Experience) {
        if self.buffer.len() >= self.capacity {
            self.buffer.pop_front();
            self.priorities.pop_front();
        }

        self.buffer.push_back(experience);
        self.priorities.push_back(self.max_priority);
    }

    /// Sample batch of experiences based on priorities
    ///
    /// # Returns
    /// Tuple of (experiences, importance_sampling_weights, indices)
    pub fn sample<R: Rng>(
        &mut self,
        batch_size: usize,
        rng: &mut R,
    ) -> Result<(Vec<Experience>, Vec<f64>, Vec<usize>)> {
        if batch_size > self.buffer.len() {
            return Err(NumRs2Error::ValueError(format!(
                "Requested batch size {} exceeds buffer size {}",
                batch_size,
                self.buffer.len()
            )));
        }

        // Compute sampling probabilities
        let priorities_alpha: Vec<f64> = self
            .priorities
            .iter()
            .map(|&p| (p + self.epsilon).powf(self.alpha))
            .collect();

        let total_priority: f64 = priorities_alpha.iter().sum();
        let probabilities: Vec<f64> = priorities_alpha
            .iter()
            .map(|&p| p / total_priority)
            .collect();

        // Sample indices based on priorities using inverse transform sampling
        let mut indices = Vec::with_capacity(batch_size);
        let mut experiences = Vec::with_capacity(batch_size);
        let mut weights = Vec::with_capacity(batch_size);

        let uniform_dist = Uniform::new(0.0, 1.0)
            .map_err(|e| NumRs2Error::ValueError(format!("Uniform distribution error: {}", e)))?;

        for _ in 0..batch_size {
            // Inverse transform sampling
            let u = uniform_dist.sample(rng);
            let mut cumsum = 0.0;
            let mut idx = 0;

            for (i, &prob) in probabilities.iter().enumerate() {
                cumsum += prob;
                if u <= cumsum {
                    idx = i;
                    break;
                }
            }

            indices.push(idx);
            experiences.push(self.buffer[idx].clone());

            // Compute importance-sampling weight
            let prob = probabilities[idx];
            let weight = (self.buffer.len() as f64 * prob).powf(-self.beta);
            weights.push(weight);
        }

        // Normalize weights by max weight
        let max_weight = weights.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
        let normalized_weights: Vec<f64> = weights.iter().map(|&w| w / max_weight).collect();

        // Increment beta
        self.beta = (self.beta + self.beta_increment).min(1.0);

        Ok((experiences, normalized_weights, indices))
    }

    /// Update priorities for sampled experiences based on TD-errors
    ///
    /// # Arguments
    /// * `indices` - Indices of experiences to update
    /// * `td_errors` - TD-errors for each experience (unsigned)
    pub fn update_priorities(&mut self, indices: &[usize], td_errors: &[f64]) -> Result<()> {
        if indices.len() != td_errors.len() {
            return Err(NumRs2Error::ValueError(
                "indices and td_errors must have same length".to_string(),
            ));
        }

        for (&idx, &td_error) in indices.iter().zip(td_errors.iter()) {
            if idx >= self.priorities.len() {
                return Err(NumRs2Error::ValueError(format!(
                    "Index {} out of bounds for buffer of size {}",
                    idx,
                    self.priorities.len()
                )));
            }

            let priority = td_error.abs() + self.epsilon;
            self.priorities[idx] = priority;
            self.max_priority = self.max_priority.max(priority);
        }

        Ok(())
    }

    /// Get current buffer size
    pub fn len(&self) -> usize {
        self.buffer.len()
    }

    /// Check if buffer is empty
    pub fn is_empty(&self) -> bool {
        self.buffer.is_empty()
    }

    /// Get buffer capacity
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Clear buffer
    pub fn clear(&mut self) {
        self.buffer.clear();
        self.priorities.clear();
        self.max_priority = 1.0;
    }

    /// Get current beta value
    pub fn beta(&self) -> f64 {
        self.beta
    }

    /// Get alpha value
    pub fn alpha(&self) -> f64 {
        self.alpha
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::random::thread_rng;

    fn create_dummy_experience(value: f64) -> Experience {
        Experience {
            state: Array1::from_vec(vec![value]),
            action: 0,
            reward: value,
            next_state: Array1::from_vec(vec![value + 1.0]),
            done: false,
        }
    }

    #[test]
    fn test_experience_replay_creation() {
        let replay = ExperienceReplay::new(100);
        assert_eq!(replay.capacity(), 100);
        assert_eq!(replay.len(), 0);
        assert!(replay.is_empty());
    }

    #[test]
    #[should_panic(expected = "Capacity must be positive")]
    fn test_experience_replay_zero_capacity() {
        let _replay = ExperienceReplay::new(0);
    }

    #[test]
    fn test_experience_replay_push() {
        let mut replay = ExperienceReplay::new(3);
        replay.push(create_dummy_experience(1.0));
        assert_eq!(replay.len(), 1);

        replay.push(create_dummy_experience(2.0));
        replay.push(create_dummy_experience(3.0));
        assert_eq!(replay.len(), 3);
        assert!(replay.is_full());
    }

    #[test]
    fn test_experience_replay_overflow() {
        let mut replay = ExperienceReplay::new(2);
        replay.push(create_dummy_experience(1.0));
        replay.push(create_dummy_experience(2.0));
        replay.push(create_dummy_experience(3.0));

        assert_eq!(replay.len(), 2);
        // Should have removed first experience (value=1.0)
        let experiences = replay.experiences();
        assert_eq!(experiences[0].reward, 2.0);
        assert_eq!(experiences[1].reward, 3.0);
    }

    #[test]
    fn test_experience_replay_sample() -> Result<()> {
        let mut replay = ExperienceReplay::new(10);
        for i in 0..5 {
            replay.push(create_dummy_experience(i as f64));
        }

        let mut rng = thread_rng();
        let samples = replay.sample(3, &mut rng)?;
        assert_eq!(samples.len(), 3);
        Ok(())
    }

    #[test]
    fn test_experience_replay_sample_too_large() -> Result<()> {
        let mut replay = ExperienceReplay::new(10);
        replay.push(create_dummy_experience(1.0));

        let mut rng = thread_rng();
        let result = replay.sample(5, &mut rng);
        assert!(result.is_err());
        Ok(())
    }

    #[test]
    fn test_experience_replay_clear() {
        let mut replay = ExperienceReplay::new(10);
        replay.push(create_dummy_experience(1.0));
        replay.push(create_dummy_experience(2.0));
        replay.clear();

        assert_eq!(replay.len(), 0);
        assert!(replay.is_empty());
    }

    #[test]
    fn test_prioritized_replay_creation() -> Result<()> {
        let replay = PrioritizedExperienceReplay::new(100, 0.6, 0.4, 1e-6, 1e-6)?;
        assert_eq!(replay.capacity(), 100);
        assert_eq!(replay.len(), 0);
        assert_eq!(replay.alpha(), 0.6);
        assert_eq!(replay.beta(), 0.4);
        Ok(())
    }

    #[test]
    fn test_prioritized_replay_invalid_params() {
        assert!(PrioritizedExperienceReplay::new(0, 0.6, 0.4, 1e-6, 1e-6).is_err());
        assert!(PrioritizedExperienceReplay::new(100, -0.1, 0.4, 1e-6, 1e-6).is_err());
        assert!(PrioritizedExperienceReplay::new(100, 0.6, -0.1, 1e-6, 1e-6).is_err());
        assert!(PrioritizedExperienceReplay::new(100, 0.6, 1.5, 1e-6, 1e-6).is_err());
        assert!(PrioritizedExperienceReplay::new(100, 0.6, 0.4, 1e-6, 0.0).is_err());
    }

    #[test]
    fn test_prioritized_replay_push() -> Result<()> {
        let mut replay = PrioritizedExperienceReplay::new(3, 0.6, 0.4, 1e-6, 1e-6)?;
        replay.push(create_dummy_experience(1.0));
        assert_eq!(replay.len(), 1);

        replay.push(create_dummy_experience(2.0));
        replay.push(create_dummy_experience(3.0));
        assert_eq!(replay.len(), 3);
        Ok(())
    }

    #[test]
    fn test_prioritized_replay_overflow() -> Result<()> {
        let mut replay = PrioritizedExperienceReplay::new(2, 0.6, 0.4, 1e-6, 1e-6)?;
        replay.push(create_dummy_experience(1.0));
        replay.push(create_dummy_experience(2.0));
        replay.push(create_dummy_experience(3.0));

        assert_eq!(replay.len(), 2);
        Ok(())
    }

    #[test]
    fn test_prioritized_replay_sample() -> Result<()> {
        let mut replay = PrioritizedExperienceReplay::new(10, 0.6, 0.4, 1e-6, 1e-6)?;
        for i in 0..5 {
            replay.push(create_dummy_experience(i as f64));
        }

        let mut rng = thread_rng();
        let (experiences, weights, indices) = replay.sample(3, &mut rng)?;

        assert_eq!(experiences.len(), 3);
        assert_eq!(weights.len(), 3);
        assert_eq!(indices.len(), 3);

        // Check that weights are normalized (max should be 1.0)
        let max_weight = weights.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
        assert!((max_weight - 1.0).abs() < 1e-6);

        Ok(())
    }

    #[test]
    fn test_prioritized_replay_update_priorities() -> Result<()> {
        let mut replay = PrioritizedExperienceReplay::new(10, 0.6, 0.4, 1e-6, 1e-6)?;
        for i in 0..5 {
            replay.push(create_dummy_experience(i as f64));
        }

        let mut rng = thread_rng();
        let (_, _, indices) = replay.sample(3, &mut rng)?;

        let td_errors = vec![1.0, 2.0, 0.5];
        replay.update_priorities(&indices, &td_errors)?;

        Ok(())
    }

    #[test]
    fn test_prioritized_replay_beta_increment() -> Result<()> {
        let mut replay = PrioritizedExperienceReplay::new(10, 0.6, 0.4, 0.1, 1e-6)?;
        for i in 0..5 {
            replay.push(create_dummy_experience(i as f64));
        }

        let initial_beta = replay.beta();
        let mut rng = thread_rng();

        for _ in 0..5 {
            let _ = replay.sample(2, &mut rng)?;
        }

        assert!(replay.beta() > initial_beta);
        assert!(replay.beta() <= 1.0);
        Ok(())
    }

    #[test]
    fn test_prioritized_replay_invalid_update() -> Result<()> {
        let mut replay = PrioritizedExperienceReplay::new(10, 0.6, 0.4, 1e-6, 1e-6)?;
        for i in 0..5 {
            replay.push(create_dummy_experience(i as f64));
        }

        // Mismatched lengths
        let result = replay.update_priorities(&[0, 1], &[1.0]);
        assert!(result.is_err());

        // Out of bounds index
        let result = replay.update_priorities(&[100], &[1.0]);
        assert!(result.is_err());

        Ok(())
    }

    #[test]
    fn test_prioritized_replay_clear() -> Result<()> {
        let mut replay = PrioritizedExperienceReplay::new(10, 0.6, 0.4, 1e-6, 1e-6)?;
        replay.push(create_dummy_experience(1.0));
        replay.push(create_dummy_experience(2.0));
        replay.clear();

        assert_eq!(replay.len(), 0);
        assert!(replay.is_empty());
        Ok(())
    }

    #[test]
    fn test_experience_clone() {
        let exp = create_dummy_experience(1.0);
        let cloned = exp.clone();

        assert_eq!(exp.state[0], cloned.state[0]);
        assert_eq!(exp.action, cloned.action);
        assert_eq!(exp.reward, cloned.reward);
        assert_eq!(exp.next_state[0], cloned.next_state[0]);
        assert_eq!(exp.done, cloned.done);
    }
}