optirs-learned 0.3.1

OptiRS learned optimizers and meta-learning
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
use std::fmt::Debug;
// Reinforcement learning for neural architecture search
//
// This module implements RL-based NAS using controller networks,
// policy gradients, and other reinforcement learning methods.

use std::collections::{HashMap, VecDeque};
use scirs2_core::numeric::Float;
use scirs2_core::ndarray::{Array1, Array2};

use super::super::architecture::{ArchitectureSpec, ArchitectureCandidate, ActivationType};

/// Reinforcement learning search state
#[derive(Debug)]
pub struct RLSearchState<T: Float + Debug + Send + Sync + 'static> {
    /// Controller network
    pub controller: ControllerNetwork<T>,

    /// Action space
    pub action_space: ActionSpace,

    /// State representation
    pub state_representation: StateRepresentation<T>,

    /// Reward history
    pub reward_history: VecDeque<f64>,

    /// Policy parameters
    pub policy_parameters: PolicyParameters<T>,

    /// Experience replay buffer
    pub replay_buffer: ExperienceReplayBuffer<T>,

    /// Training configuration
    pub training_config: RLTrainingConfig,
}

/// Controller network for RL-based NAS
#[derive(Debug)]
pub struct ControllerNetwork<T: Float + Debug + Send + Sync + 'static> {
    /// Network weights
    pub weights: Vec<Array2<T>>,

    /// Network biases
    pub biases: Vec<Array1<T>>,

    /// Network architecture
    pub architecture: Vec<usize>,

    /// Activation functions
    pub activations: Vec<ActivationType>,

    /// Network type
    pub network_type: ControllerType,
}

/// Types of controller networks
#[derive(Debug, Clone, Copy)]
pub enum ControllerType {
    LSTM,
    Transformer,
    MLP,
    GRU,
}

/// Action space for architecture generation
#[derive(Debug, Clone)]
pub struct ActionSpace {
    /// Discrete actions
    pub discrete_actions: Vec<DiscreteAction>,

    /// Continuous actions
    pub continuous_actions: Vec<ContinuousAction>,

    /// Action constraints
    pub constraints: Vec<ActionConstraint>,

    /// Action embedding size
    pub embedding_size: usize,
}

/// Discrete action types
#[derive(Debug, Clone)]
pub struct DiscreteAction {
    /// Action name
    pub name: String,

    /// Possible values
    pub values: Vec<String>,

    /// Current probability distribution
    pub probabilities: Vec<f64>,
}

/// Continuous action types
#[derive(Debug, Clone)]
pub struct ContinuousAction {
    /// Action name
    pub name: String,

    /// Value range
    pub range: (f64, f64),

    /// Current mean and std
    pub mean: f64,
    pub std: f64,
}

/// Action constraints
#[derive(Debug, Clone)]
pub struct ActionConstraint {
    /// Constraint type
    pub constraint_type: ConstraintType,

    /// Constraint parameters
    pub parameters: HashMap<String, f64>,

    /// Constraint description
    pub description: String,
}

/// Types of action constraints
#[derive(Debug, Clone, Copy)]
pub enum ConstraintType {
    Dependency,    // Action depends on previous actions
    Exclusion,     // Action excludes other actions
    Range,         // Action value must be in range
    Conditional,   // Action only valid under conditions
}

/// State representation for the controller
#[derive(Debug)]
pub struct StateRepresentation<T: Float + Debug + Send + Sync + 'static> {
    /// Current architecture state
    pub architecture_state: Vec<T>,

    /// Performance history
    pub performance_history: Vec<T>,

    /// Resource usage state
    pub resource_state: Vec<T>,

    /// Search progress state
    pub progress_state: Vec<T>,

    /// State embedding
    pub embedding: Array1<T>,
}

/// Policy parameters
#[derive(Debug)]
pub struct PolicyParameters<T: Float + Debug + Send + Sync + 'static> {
    /// Policy type
    pub policy_type: PolicyType,

    /// Learning rate
    pub learning_rate: T,

    /// Discount factor
    pub gamma: T,

    /// Exploration parameters
    pub exploration: ExplorationParameters<T>,

    /// Entropy regularization weight
    pub entropy_weight: T,
}

/// Types of RL policies
#[derive(Debug, Clone, Copy)]
pub enum PolicyType {
    REINFORCE,
    ActorCritic,
    PPO,
    TRPO,
    SAC,
}

/// Exploration parameters
#[derive(Debug)]
pub struct ExplorationParameters<T: Float + Debug + Send + Sync + 'static> {
    /// Exploration strategy
    pub strategy: ExplorationStrategy,

    /// Initial exploration rate
    pub initial_rate: T,

    /// Final exploration rate
    pub final_rate: T,

    /// Decay schedule
    pub decay_schedule: DecaySchedule,

    /// Current exploration rate
    pub current_rate: T,
}

/// Exploration strategies
#[derive(Debug, Clone, Copy)]
pub enum ExplorationStrategy {
    EpsilonGreedy,
    BoltzmannExploration,
    UCB,
    ThompsonSampling,
    NoiseInjection,
}

/// Decay schedules for exploration
#[derive(Debug, Clone, Copy)]
pub enum DecaySchedule {
    Linear,
    Exponential,
    Polynomial,
    Cosine,
    Step,
}

/// Experience replay buffer
#[derive(Debug)]
pub struct ExperienceReplayBuffer<T: Float + Debug + Send + Sync + 'static> {
    /// Buffer capacity
    pub capacity: usize,

    /// Current size
    pub size: usize,

    /// Buffer index
    pub index: usize,

    /// States
    pub states: Vec<Vec<T>>,

    /// Actions
    pub actions: Vec<ActionSequence>,

    /// Rewards
    pub rewards: Vec<T>,

    /// Next states
    pub next_states: Vec<Vec<T>>,

    /// Done flags
    pub dones: Vec<bool>,
}

/// Action sequence for architecture generation
#[derive(Debug, Clone)]
pub struct ActionSequence {
    /// Discrete action indices
    pub discrete_actions: Vec<usize>,

    /// Continuous action values
    pub continuous_actions: Vec<f64>,

    /// Action probabilities
    pub probabilities: Vec<f64>,

    /// Action log probabilities
    pub log_probabilities: Vec<f64>,
}

/// RL training configuration
#[derive(Debug, Clone)]
pub struct RLTrainingConfig {
    /// Batch size for training
    pub batch_size: usize,

    /// Number of training epochs
    pub num_epochs: usize,

    /// Update frequency
    pub update_frequency: usize,

    /// Target network update frequency
    pub target_update_frequency: usize,

    /// Maximum episode length
    pub max_episode_length: usize,

    /// Reward normalization
    pub normalize_rewards: bool,

    /// Baseline subtraction
    pub use_baseline: bool,
}

impl Default for RLTrainingConfig {
    fn default() -> Self {
        Self {
            batch_size: 32,
            num_epochs: 10,
            update_frequency: 10,
            target_update_frequency: 100,
            max_episode_length: 50,
            normalize_rewards: true,
            use_baseline: true,
        }
    }
}

impl<T: Float + Debug + Default + std::fmt::Debug + Send + Sync> RLSearchState<T> {
    /// Create new RL search state
    pub fn new() -> Self {
        Self {
            controller: ControllerNetwork::new(ControllerType::LSTM),
            action_space: ActionSpace::default(),
            state_representation: StateRepresentation::new(),
            reward_history: VecDeque::with_capacity(1000),
            policy_parameters: PolicyParameters::default(),
            replay_buffer: ExperienceReplayBuffer::new(10000),
            training_config: RLTrainingConfig::default(),
        }
    }

    /// Generate architecture using the controller
    pub fn generate_architecture(
        &mut self,
        search_space: &super::super::space::ArchitectureSearchSpace,
    ) -> Result<ArchitectureCandidate, super::SearchError> {
        // Reset state
        self.state_representation.reset();

        // Generate action sequence
        let action_sequence = self.sample_action_sequence(search_space)?;

        // Convert action sequence to architecture
        let architecture = self.actions_to_architecture(&action_sequence, search_space)?;

        // Create candidate
        let candidate = ArchitectureCandidate::new(
            format!("rl_arch_{}", self.reward_history.len()),
            architecture,
        );

        Ok(candidate)
    }

    /// Sample action sequence from the controller
    fn sample_action_sequence(
        &mut self,
        _search_space: &super::super::space::ArchitectureSearchSpace,
    ) -> Result<ActionSequence, super::SearchError> {
        let mut discrete_actions = Vec::new();
        let mut continuous_actions = Vec::new();
        let mut probabilities = Vec::new();
        let mut log_probabilities = Vec::new();

        // Sample discrete actions
        for discrete_action in &self.action_space.discrete_actions {
            let action_idx = self.sample_discrete_action(discrete_action)?;
            discrete_actions.push(action_idx);
            
            if action_idx < discrete_action.probabilities.len() {
                let prob = discrete_action.probabilities[action_idx];
                probabilities.push(prob);
                log_probabilities.push(prob.ln());
            }
        }

        // Sample continuous actions
        for continuous_action in &self.action_space.continuous_actions {
            let action_value = self.sample_continuous_action(continuous_action)?;
            continuous_actions.push(action_value);
        }

        Ok(ActionSequence {
            discrete_actions,
            continuous_actions,
            probabilities,
            log_probabilities,
        })
    }

    /// Sample discrete action
    fn sample_discrete_action(&self, action: &DiscreteAction) -> Result<usize, super::SearchError> {
        let random_value: f64 = scirs2_core::random::random();
        let mut cumulative_prob = 0.0;

        for (idx, &prob) in action.probabilities.iter().enumerate() {
            cumulative_prob += prob;
            if random_value <= cumulative_prob {
                return Ok(idx);
            }
        }

        // Fallback to last action
        Ok(action.probabilities.len() - 1)
    }

    /// Sample continuous action
    fn sample_continuous_action(&self, action: &ContinuousAction) -> Result<f64, super::SearchError> {
        // Sample from normal distribution
        let normal_sample: f64 = scirs2_core::random::random::<f64>() * 2.0 - 1.0; // Simple approximation
        let value = action.mean + normal_sample * action.std;

        // Clamp to range
        Ok(value.max(action.range.0).min(action.range.1))
    }

    /// Convert action sequence to architecture
    fn actions_to_architecture(
        &self,
        actions: &ActionSequence,
        search_space: &super::super::space::ArchitectureSearchSpace,
    ) -> Result<ArchitectureSpec, super::SearchError> {
        use super::super::architecture::{LayerSpec, LayerDimensions, GlobalArchitectureConfig};

        // Simplified conversion
        let num_layers = if !actions.discrete_actions.is_empty() {
            (actions.discrete_actions[0] % 5) + 1  // 1-5 layers
        } else {
            3
        };

        let mut layers = Vec::new();
        for i in 0..num_layers {
            let layer_type_idx = if i < actions.discrete_actions.len() {
                actions.discrete_actions[i] % search_space.layer_types.len()
            } else {
                0
            };

            let layer_type = search_space.layer_types[layer_type_idx];

            let hidden_size_idx = if i < actions.continuous_actions.len() {
                (actions.continuous_actions[i] * search_space.hidden_sizes.len() as f64) as usize
            } else {
                0
            };
            let hidden_size = search_space.hidden_sizes[hidden_size_idx.min(search_space.hidden_sizes.len() - 1)];

            let activation_idx = if i + 1 < actions.discrete_actions.len() {
                actions.discrete_actions[i + 1] % search_space.activation_functions.len()
            } else {
                0
            };
            let activation = search_space.activation_functions[activation_idx];

            let dimensions = LayerDimensions {
                input_dim: if i == 0 { hidden_size } else { layers[i-1].dimensions.output_dim },
                output_dim: hidden_size,
                hidden_dims: vec![],
            };

            layers.push(LayerSpec::new(layer_type, dimensions, activation));
        }

        Ok(ArchitectureSpec::new(layers, GlobalArchitectureConfig::default()))
    }

    /// Update controller with reward
    pub fn update_with_reward(&mut self, reward: f64) {
        self.reward_history.push_back(reward);
        
        // Keep history bounded
        if self.reward_history.len() > 1000 {
            self.reward_history.pop_front();
        }

        // Update policy (simplified)
        if self.reward_history.len() % self.training_config.update_frequency == 0 {
            let _ = self.update_policy();
        }
    }

    /// Update policy using collected experiences
    fn update_policy(&mut self) -> Result<(), super::SearchError> {
        match self.policy_parameters.policy_type {
            PolicyType::REINFORCE => {
                self.update_reinforce()
            }
            PolicyType::ActorCritic => {
                self.update_actor_critic()
            }
            PolicyType::PPO => {
                self.update_ppo()
            }
            _ => {
                // Default to simple gradient ascent
                Ok(())
            }
        }
    }

    /// Update using REINFORCE
    fn update_reinforce(&mut self) -> Result<(), super::SearchError> {
        // Simplified REINFORCE update
        // In practice, would compute policy gradients and update controller weights
        Ok(())
    }

    /// Update using Actor-Critic
    fn update_actor_critic(&mut self) -> Result<(), super::SearchError> {
        // Simplified Actor-Critic update
        // Would update both actor and critic networks
        Ok(())
    }

    /// Update using PPO
    fn update_ppo(&mut self) -> Result<(), super::SearchError> {
        // Simplified PPO update
        // Would implement clipped surrogate objective
        Ok(())
    }

    /// Check if training should stop
    pub fn should_stop_training(&self) -> bool {
        // Stop if converged (no improvement in recent rewards)
        if self.reward_history.len() < 100 {
            return false;
        }

        let recent_mean = self.reward_history
            .iter()
            .rev()
            .take(50)
            .sum::<f64>() / 50.0;

        let older_mean = self.reward_history
            .iter()
            .rev()
            .skip(50)
            .take(50)
            .sum::<f64>() / 50.0;

        (recent_mean - older_mean).abs() < 0.001
    }
}

impl<T: Float + Debug + Default + Send + Sync> ControllerNetwork<T> {
    /// Create new controller network
    pub fn new(network_type: ControllerType) -> Self {
        let architecture = match network_type {
            ControllerType::LSTM => vec![128, 64, 32],
            ControllerType::Transformer => vec![256, 128, 64],
            ControllerType::MLP => vec![128, 64, 32, 16],
            ControllerType::GRU => vec![128, 64, 32],
        };

        let weights = Vec::new(); // Would initialize with random weights
        let biases = Vec::new();  // Would initialize with zeros
        let activations = vec![ActivationType::ReLU; architecture.len()];

        Self {
            weights,
            biases,
            architecture,
            activations,
            network_type,
        }
    }

    /// Forward pass through the network
    pub fn forward(&self, input: &[T]) -> Result<Vec<T>, super::SearchError> {
        // Simplified forward pass
        // In practice would implement actual network computation
        Ok(input.to_vec())
    }
}

impl Default for ActionSpace {
    fn default() -> Self {
        let discrete_actions = vec![
            DiscreteAction {
                name: "layer_type".to_string(),
                values: vec!["linear".to_string(), "lstm".to_string(), "attention".to_string()],
                probabilities: vec![0.33, 0.33, 0.34],
            },
            DiscreteAction {
                name: "activation".to_string(),
                values: vec!["relu".to_string(), "gelu".to_string(), "tanh".to_string()],
                probabilities: vec![0.33, 0.33, 0.34],
            },
        ];

        let continuous_actions = vec![
            ContinuousAction {
                name: "hidden_size".to_string(),
                range: (32.0, 512.0),
                mean: 128.0,
                std: 64.0,
            },
            ContinuousAction {
                name: "dropout_rate".to_string(),
                range: (0.0, 0.5),
                mean: 0.1,
                std: 0.05,
            },
        ];

        Self {
            discrete_actions,
            continuous_actions,
            constraints: Vec::new(),
            embedding_size: 64,
        }
    }
}

impl<T: Float + Debug + Default + Send + Sync> StateRepresentation<T> {
    /// Create new state representation
    pub fn new() -> Self {
        Self {
            architecture_state: Vec::new(),
            performance_history: Vec::new(),
            resource_state: Vec::new(),
            progress_state: Vec::new(),
            embedding: Array1::zeros(64),
        }
    }

    /// Reset state representation
    pub fn reset(&mut self) {
        self.architecture_state.clear();
        self.performance_history.clear();
        self.resource_state.clear();
        self.progress_state.clear();
        self.embedding.fill(T::zero());
    }

    /// Update state with new information
    pub fn update(&mut self, architecture: &ArchitectureSpec, performance: f64) {
        // Update architecture state
        self.architecture_state = vec![
            T::from(architecture.layers.len()).expect("unwrap failed"),
            T::from(architecture.parameter_count()).expect("unwrap failed"),
        ];

        // Update performance history
        self.performance_history.push(scirs2_core::numeric::NumCast::from(performance).unwrap_or_else(|| T::zero()));
        if self.performance_history.len() > 10 {
            self.performance_history.remove(0);
        }

        // Update embedding (simplified)
        for (i, value) in self.architecture_state.iter().enumerate() {
            if i < self.embedding.len() {
                self.embedding[i] = *value;
            }
        }
    }
}

impl<T: Float + Debug + Default + Send + Sync> PolicyParameters<T> {
    /// Create default policy parameters
    pub fn default() -> Self {
        Self {
            policy_type: PolicyType::REINFORCE,
            learning_rate: scirs2_core::numeric::NumCast::from(0.001).unwrap_or_else(|| T::zero()),
            gamma: scirs2_core::numeric::NumCast::from(0.99).unwrap_or_else(|| T::zero()),
            exploration: ExplorationParameters::default(),
            entropy_weight: scirs2_core::numeric::NumCast::from(0.01).unwrap_or_else(|| T::zero()),
        }
    }
}

impl<T: Float + Debug + Default + Send + Sync> ExplorationParameters<T> {
    /// Create default exploration parameters
    pub fn default() -> Self {
        Self {
            strategy: ExplorationStrategy::EpsilonGreedy,
            initial_rate: scirs2_core::numeric::NumCast::from(1.0).unwrap_or_else(|| T::zero()),
            final_rate: scirs2_core::numeric::NumCast::from(0.01).unwrap_or_else(|| T::zero()),
            decay_schedule: DecaySchedule::Exponential,
            current_rate: scirs2_core::numeric::NumCast::from(1.0).unwrap_or_else(|| T::zero()),
        }
    }

    /// Update exploration rate
    pub fn update_rate(&mut self, step: usize, total_steps: usize) {
        let progress = scirs2_core::numeric::NumCast::from(step as f64 / total_steps as f64).unwrap_or_else(|| T::zero());
        
        self.current_rate = match self.decay_schedule {
            DecaySchedule::Linear => {
                self.initial_rate - (self.initial_rate - self.final_rate) * progress
            }
            DecaySchedule::Exponential => {
                self.initial_rate * (self.final_rate / self.initial_rate).powf(progress)
            }
            _ => self.initial_rate,
        };
    }
}

impl<T: Float + Debug + Default + Send + Sync> ExperienceReplayBuffer<T> {
    /// Create new experience replay buffer
    pub fn new(capacity: usize) -> Self {
        Self {
            capacity,
            size: 0,
            index: 0,
            states: Vec::with_capacity(capacity),
            actions: Vec::with_capacity(capacity),
            rewards: Vec::with_capacity(capacity),
            next_states: Vec::with_capacity(capacity),
            dones: Vec::with_capacity(capacity),
        }
    }

    /// Add experience to buffer
    pub fn add_experience(
        &mut self,
        state: Vec<T>,
        action: ActionSequence,
        reward: T,
        next_state: Vec<T>,
        done: bool,
    ) {
        if self.size < self.capacity {
            self.states.push(state);
            self.actions.push(action);
            self.rewards.push(reward);
            self.next_states.push(next_state);
            self.dones.push(done);
            self.size += 1;
        } else {
            self.states[self.index] = state;
            self.actions[self.index] = action;
            self.rewards[self.index] = reward;
            self.next_states[self.index] = next_state;
            self.dones[self.index] = done;
        }

        self.index = (self.index + 1) % self.capacity;
    }

    /// Sample batch from buffer
    pub fn sample_batch(&self, batch_size: usize) -> Vec<usize> {
        let mut indices = Vec::new();
        
        for _ in 0..batch_size {
            let idx = scirs2_core::random::random::<usize>() % self.size;
            indices.push(idx);
        }
        
        indices
    }
}

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

    #[test]
    fn test_rl_state_creation() {
        let state: RLSearchState<f64> = RLSearchState::new();
        assert_eq!(state.reward_history.len(), 0);
        assert!(matches!(state.controller.network_type, ControllerType::LSTM));
    }

    #[test]
    fn test_controller_network() {
        let controller: ControllerNetwork<f64> = ControllerNetwork::new(ControllerType::MLP);
        assert!(matches!(controller.network_type, ControllerType::MLP));
        assert!(!controller.architecture.is_empty());
    }

    #[test]
    fn test_action_space_default() {
        let action_space = ActionSpace::default();
        assert!(!action_space.discrete_actions.is_empty());
        assert!(!action_space.continuous_actions.is_empty());
    }

    #[test]
    fn test_experience_replay_buffer() {
        let mut buffer: ExperienceReplayBuffer<f64> = ExperienceReplayBuffer::new(100);
        
        buffer.add_experience(
            vec![1.0, 2.0],
            ActionSequence {
                discrete_actions: vec![0, 1],
                continuous_actions: vec![0.5],
                probabilities: vec![0.5, 0.3],
                log_probabilities: vec![-0.693, -1.204],
            },
            1.0,
            vec![2.0, 3.0],
            false,
        );
        
        assert_eq!(buffer.size, 1);
    }

    #[test]
    fn test_exploration_parameters() {
        let mut exploration: ExplorationParameters<f64> = ExplorationParameters::default();
        exploration.update_rate(50, 100);
        assert!(exploration.current_rate < exploration.initial_rate);
    }
}