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
// Reinforcement learning-based neural architecture search
//
// This module implements RL agents that learn to design optimal optimizer architectures
// through interaction with the optimization environment and reward signals.

#[allow(dead_code)]

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

use crate::error::{OptimError, Result};

/// Reinforcement learning configuration for NAS
#[derive(Debug, Clone)]
pub struct RLNASConfig<T: Float + Debug + Send + Sync + 'static> {
    /// Learning rate for policy updates
    pub learning_rate: T,
    
    /// Discount factor for future rewards
    pub discount_factor: T,
    
    /// Exploration rate (epsilon)
    pub exploration_rate: T,
    
    /// Exploration decay rate
    pub exploration_decay: T,
    
    /// Minimum exploration rate
    pub min_exploration_rate: T,
    
    /// Replay buffer size
    pub replay_buffer_size: usize,
    
    /// Batch size for training
    pub batch_size: usize,
    
    /// Number of training episodes
    pub num_episodes: usize,
    
    /// Maximum steps per episode
    pub max_steps_per_episode: usize,
    
    /// Target network update frequency
    pub target_update_frequency: usize,
    
    /// Architecture complexity penalty
    pub complexity_penalty: T,
}

/// Reinforcement learning agent for architecture search
#[derive(Debug)]
pub struct RLArchitectureAgent<T: Float + Debug + Send + Sync + 'static> {
    /// Agent configuration
    config: RLNASConfig<T>,
    
    /// Policy network for action selection
    policy_network: PolicyNetwork<T>,
    
    /// Value network for value estimation
    value_network: ValueNetwork<T>,
    
    /// Target networks for stable training
    target_policy_network: Option<PolicyNetwork<T>>,
    target_value_network: Option<ValueNetwork<T>>,
    
    /// Experience replay buffer
    replay_buffer: ReplayBuffer<T>,
    
    /// Current state representation
    current_state: Option<StateRepresentation<T>>,
    
    /// Training statistics
    training_stats: TrainingStatistics<T>,
    
    /// Episode history
    episode_history: Vec<Episode<T>>,
    
    /// Current episode
    current_episode: usize,
}

/// Policy network for action selection
#[derive(Debug, Clone)]
pub struct PolicyNetwork<T: Float + Debug + Send + Sync + 'static> {
    /// Network weights
    weights: Vec<Array2<T>>,
    
    /// Biases
    biases: Vec<Array1<T>>,
    
    /// Network architecture
    architecture: Vec<usize>,
    
    /// Output action space
    action_space: ActionSpace,
}

/// Value network for state value estimation
#[derive(Debug, Clone)]
pub struct ValueNetwork<T: Float + Debug + Send + Sync + 'static> {
    /// Network weights
    weights: Vec<Array2<T>>,
    
    /// Biases
    biases: Vec<Array1<T>>,
    
    /// Network architecture
    architecture: Vec<usize>,
}

/// Action space for architecture construction
#[derive(Debug, Clone)]
pub struct ActionSpace {
    /// Available layer types
    layer_types: Vec<LayerTypeAction>,
    
    /// Available connections
    connection_types: Vec<ConnectionAction>,
    
    /// Parameter ranges
    parameter_ranges: HashMap<String, (f64, f64)>,
    
    /// Maximum architecture depth
    max_depth: usize,
}

/// Layer type actions
#[derive(Debug, Clone, Copy)]
pub enum LayerTypeAction {
    AddDense { units: usize },
    AddConv1D { filters: usize, kernel_size: usize },
    AddLSTM { units: usize },
    AddAttention { heads: usize, dim: usize },
    AddBatchNorm,
    AddDropout { rate: f32 },
    AddActivation { activation_type: ActivationType },
    TerminateArchitecture,
}

/// Connection actions
#[derive(Debug, Clone, Copy)]
pub enum ConnectionAction {
    SequentialConnection,
    SkipConnection { from_layer: usize, to_layer: usize },
    DenseConnection,
    NoConnection,
}

/// Activation types for layers
#[derive(Debug, Clone, Copy)]
pub enum ActivationType {
    ReLU,
    Tanh,
    Sigmoid,
    GELU,
    Swish,
}

/// State representation of current architecture being built
#[derive(Debug, Clone)]
pub struct StateRepresentation<T: Float + Debug + Send + Sync + 'static> {
    /// Current layers in architecture
    current_layers: Vec<LayerInfo>,
    
    /// Current connections
    current_connections: Vec<(usize, usize)>,
    
    /// Architecture complexity metrics
    complexity_metrics: ComplexityMetrics<T>,
    
    /// Performance history
    performance_history: Vec<T>,
    
    /// State embedding
    state_embedding: Array1<T>,
}

/// Layer information in state
#[derive(Debug, Clone)]
pub struct LayerInfo {
    /// Layer type
    layer_type: String,
    
    /// Input dimensions
    input_dims: Vec<usize>,
    
    /// Output dimensions
    output_dims: Vec<usize>,
    
    /// Parameters
    parameters: HashMap<String, f64>,
}

/// Architecture complexity metrics
#[derive(Debug, Clone)]
pub struct ComplexityMetrics<T: Float + Debug + Send + Sync + 'static> {
    /// Number of parameters
    parameter_count: usize,
    
    /// Estimated FLOPs
    flops: u64,
    
    /// Memory requirements
    memory_usage: usize,
    
    /// Architectural depth
    depth: usize,
    
    /// Connectivity complexity
    connectivity_complexity: T,
}

/// Experience replay buffer
#[derive(Debug)]
pub struct ReplayBuffer<T: Float + Debug + Send + Sync + 'static> {
    /// Buffer capacity
    capacity: usize,
    
    /// Stored experiences
    experiences: VecDeque<Experience<T>>,
    
    /// Current position in buffer
    position: usize,
}

/// Experience tuple for replay
#[derive(Debug, Clone)]
pub struct Experience<T: Float + Debug + Send + Sync + 'static> {
    /// State before action
    state: StateRepresentation<T>,
    
    /// Action taken
    action: Action,
    
    /// Reward received
    reward: T,
    
    /// Next state
    next_state: Option<StateRepresentation<T>>,
    
    /// Whether episode terminated
    done: bool,
}

/// Action taken by agent
#[derive(Debug, Clone)]
pub enum Action {
    /// Layer type action
    LayerAction(LayerTypeAction),
    
    /// Connection action
    ConnectionAction(ConnectionAction),
    
    /// Hyperparameter adjustment
    ParameterAction { parameter: String, value: f64 },
}

/// Training statistics for RL agent
#[derive(Debug, Clone)]
pub struct TrainingStatistics<T: Float + Debug + Send + Sync + 'static> {
    /// Episode rewards
    episode_rewards: Vec<T>,
    
    /// Average rewards over time
    average_rewards: Vec<T>,
    
    /// Policy losses
    policy_losses: Vec<T>,
    
    /// Value losses
    value_losses: Vec<T>,
    
    /// Exploration rates over time
    exploration_rates: Vec<T>,
    
    /// Architecture performance history
    architecture_performances: Vec<T>,
}

/// Episode information
#[derive(Debug, Clone)]
pub struct Episode<T: Float + Debug + Send + Sync + 'static> {
    /// Episode number
    episode_number: usize,
    
    /// Steps taken
    steps: usize,
    
    /// Total reward
    total_reward: T,
    
    /// Final architecture
    final_architecture: Option<StateRepresentation<T>>,
    
    /// Performance achieved
    performance: Option<T>,
}

impl<T: Float + Debug + Send + Sync + 'static + Default + Clone> RLArchitectureAgent<T> {
    /// Create new RL architecture agent
    pub fn new(config: RLNASConfig<T>) -> Result<Self> {
        let policy_network = PolicyNetwork::new(vec![128, 256, 256, 64])?;
        let value_network = ValueNetwork::new(vec![128, 256, 128, 1])?;
        let replay_buffer = ReplayBuffer::new(config.replay_buffer_size);
        let training_stats = TrainingStatistics::new();
        
        Ok(Self {
            config,
            policy_network,
            value_network,
            target_policy_network: None,
            target_value_network: None,
            replay_buffer,
            current_state: None,
            training_stats,
            episode_history: Vec::new(),
            current_episode: 0,
        })
    }

    /// Train the RL agent to learn architecture design
    pub fn train(&mut self, env: &mut dyn ArchitectureEnvironment<T>) -> Result<()> {
        // Initialize target networks
        self.target_policy_network = Some(self.policy_network.clone());
        self.target_value_network = Some(self.value_network.clone());
        
        for episode in 0..self.config.num_episodes {
            self.current_episode = episode;
            
            // Reset environment and get initial state
            let initial_state = env.reset()?;
            self.current_state = Some(initial_state);
            
            let mut episode_reward = T::zero();
            let mut steps = 0;
            
            // Run episode
            for step in 0..self.config.max_steps_per_episode {
                steps = step + 1;
                
                // Select action using epsilon-greedy policy
                let action = self.select_action(self.current_state.as_ref().expect("unwrap failed"))?;
                
                // Execute action in environment
                let (next_state, reward, done) = env.step(&action)?;
                episode_reward = episode_reward + reward;
                
                // Store experience in replay buffer
                let experience = Experience {
                    state: self.current_state.as_ref().expect("unwrap failed").clone(),
                    action,
                    reward,
                    next_state: if done { None } else { Some(next_state.clone()) },
                    done,
                };
                self.replay_buffer.add(experience);
                
                // Update current state
                self.current_state = if done { None } else { Some(next_state) };
                
                // Train networks if enough experiences collected
                if self.replay_buffer.size() >= self.config.batch_size {
                    self.train_networks()?;
                }
                
                if done {
                    break;
                }
            }
            
            // Record episode statistics
            self.training_stats.episode_rewards.push(episode_reward);
            self.update_training_statistics();
            
            // Update target networks periodically
            if episode % self.config.target_update_frequency == 0 {
                self.update_target_networks();
            }
            
            // Decay exploration rate
            self.decay_exploration_rate();
            
            // Log progress
            if episode % 100 == 0 {
                let avg_reward = self.training_stats.episode_rewards.iter()
                    .rev()
                    .take(100)
                    .cloned()
                    .fold(T::zero(), |acc, r| acc + r) / T::from(100.0.min(episode as f64 + 1.0)).expect("unwrap failed");
                println!("Episode {}: Average reward = {:.4}", episode, avg_reward.to_f64().unwrap_or(0.0));
            }
        }
        
        Ok(())
    }

    /// Select action using epsilon-greedy policy
    fn select_action(&self, state: &StateRepresentation<T>) -> Result<Action> {
        use scirs2_core::random::Rng;
        let mut rng = scirs2_core::random::thread_rng();
        
        // Epsilon-greedy exploration
        if rng.random::<f64>() < self.config.exploration_rate.to_f64().unwrap_or(0.1) {
            // Random action
            self.select_random_action(state)
        } else {
            // Policy-based action
            self.select_policy_action(state)
        }
    }

    /// Select random action for exploration
    fn select_random_action(&self, state: &StateRepresentation<T>) -> Result<Action> {
        use scirs2_core::random::Rng;
        let mut rng = scirs2_core::random::thread_rng();
        
        // Randomly choose action type
        let action_type = rng.gen_range(0..3);
        
        match action_type {
            0 => {
                // Layer action
                let layer_types = vec![
                    LayerTypeAction::AddDense { units: rng.gen_range(32..512) },
                    LayerTypeAction::AddLSTM { units: rng.gen_range(64..256) },
                    LayerTypeAction::AddAttention { heads: rng.gen_range(4..16)..dim: rng.gen_range(64..256) },
                    LayerTypeAction::AddBatchNorm,
                    LayerTypeAction::AddDropout { rate: rng.gen_range(0.1..0.5) },
                ];
                let layer_action = layer_types[rng.gen_range(0..layer_types.len())];
                Ok(Action::LayerAction(layer_action))
            }
            1 => {
                // Connection action
                let conn_action = if state.current_layers.len() > 1 {
                    ConnectionAction::SkipConnection { 
                        from_layer: rng.gen_range(0..state.current_layers.len()-1),
                        to_layer: rng.gen_range(1..state.current_layers.len())
                    }
                } else {
                    ConnectionAction::SequentialConnection
                };
                Ok(Action::ConnectionAction(conn_action))
            }
            _ => {
                // Parameter action
                Ok(Action::ParameterAction { 
                    parameter: "learning_rate".to_string(),
                    value: rng.gen_range(0.0001..0.01)
                })
            }
        }
    }

    /// Select action using policy network
    fn select_policy_action(&self, state: &StateRepresentation<T>) -> Result<Action> {
        // Forward pass through policy network
        let policy_output = self.policy_network.forward(&state.state_embedding)?;
        
        // Select action with highest probability
        let (max_idx, _max_val) = policy_output.iter()
            .enumerate()
            .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .unwrap_or((0, &T::zero()));
        
        // Map index to action (simplified mapping)
        match max_idx % 8 {
            0 => Ok(Action::LayerAction(LayerTypeAction::AddDense { units: 128 })),
            1 => Ok(Action::LayerAction(LayerTypeAction::AddLSTM { units: 128 })),
            2 => Ok(Action::LayerAction(LayerTypeAction::AddAttention { heads: 8, dim: 128 })),
            3 => Ok(Action::LayerAction(LayerTypeAction::AddBatchNorm)),
            4 => Ok(Action::LayerAction(LayerTypeAction::AddDropout { rate: 0.2 })),
            5 => Ok(Action::ConnectionAction(ConnectionAction::SequentialConnection)),
            6 => Ok(Action::ConnectionAction(ConnectionAction::DenseConnection)),
            _ => Ok(Action::LayerAction(LayerTypeAction::TerminateArchitecture)),
        }
    }

    /// Train policy and value networks
    fn train_networks(&mut self) -> Result<()> {
        // Sample batch from replay buffer
        let batch = self.replay_buffer.sample(self.config.batch_size)?;
        
        // Compute policy loss and update
        let policy_loss = self.compute_policy_loss(&batch)?;
        self.update_policy_network(policy_loss)?;
        
        // Compute value loss and update
        let value_loss = self.compute_value_loss(&batch)?;
        self.update_value_network(value_loss)?;
        
        // Record losses
        self.training_stats.policy_losses.push(policy_loss);
        self.training_stats.value_losses.push(value_loss);
        
        Ok(())
    }

    /// Compute policy loss (simplified)
    fn compute_policy_loss(&self, _batch: &[Experience<T>]) -> Result<T> {
        // Simplified policy loss computation
        // In practice, this would involve computing advantage estimates
        // and policy gradient loss
        Ok(scirs2_core::numeric::NumCast::from(0.1).unwrap_or_else(|| T::zero()))
    }

    /// Compute value loss (simplified)
    fn compute_value_loss(&self, _batch: &[Experience<T>]) -> Result<T> {
        // Simplified value loss computation
        // In practice, this would compute temporal difference error
        Ok(scirs2_core::numeric::NumCast::from(0.05).unwrap_or_else(|| T::zero()))
    }

    /// Update policy network (simplified)
    fn update_policy_network(&mut self, _loss: T) -> Result<()> {
        // Simplified network update
        // In practice, this would apply gradient descent
        Ok(())
    }

    /// Update value network (simplified)
    fn update_value_network(&mut self, _loss: T) -> Result<()> {
        // Simplified network update
        // In practice, this would apply gradient descent
        Ok(())
    }

    /// Update target networks
    fn update_target_networks(&mut self) {
        self.target_policy_network = Some(self.policy_network.clone());
        self.target_value_network = Some(self.value_network.clone());
    }

    /// Decay exploration rate
    fn decay_exploration_rate(&mut self) {
        let current_rate = self.config.exploration_rate;
        let decayed_rate = current_rate * self.config.exploration_decay;
        self.config.exploration_rate = decayed_rate.max(self.config.min_exploration_rate);
        self.training_stats.exploration_rates.push(self.config.exploration_rate);
    }

    /// Update training statistics
    fn update_training_statistics(&mut self) {
        if !self.training_stats.episode_rewards.is_empty() {
            let window_size = 100.min(self.training_stats.episode_rewards.len());
            let recent_rewards = &self.training_stats.episode_rewards[
                self.training_stats.episode_rewards.len().saturating_sub(window_size)..
            ];
            
            let average_reward = recent_rewards.iter().cloned().fold(T::zero(), |acc, r| acc + r) /
                T::from(recent_rewards.len() as f64).expect("unwrap failed");
            
            self.training_stats.average_rewards.push(average_reward);
        }
    }

    /// Get training statistics
    pub fn get_training_statistics(&self) -> &TrainingStatistics<T> {
        &self.training_stats
    }

    /// Get best architecture found during training
    pub fn get_best_architecture(&self) -> Option<StateRepresentation<T>> {
        self.episode_history.iter()
            .max_by(|a, b| a.performance.unwrap_or(T::zero()).partial_cmp(&b.performance.unwrap_or(T::zero())).unwrap_or(std::cmp::Ordering::Equal))
            .and_then(|episode| episode.final_architecture.clone())
    }
}

/// Environment interface for RL training
pub trait ArchitectureEnvironment<T: Float + Debug + Send + Sync + 'static> {
    /// Reset environment and return initial state
    fn reset(&mut self) -> Result<StateRepresentation<T>>;
    
    /// Execute action and return (next_state, reward, done)
    fn step(&mut self, action: &Action) -> Result<(StateRepresentation<T>, T, bool)>;
    
    /// Get current state
    fn get_state(&self) -> Result<StateRepresentation<T>>;
    
    /// Evaluate current architecture
    fn evaluate_architecture(&self, state: &StateRepresentation<T>) -> Result<T>;
}

impl<T: Float + Debug + Send + Sync + 'static + Default + Clone> PolicyNetwork<T> {
    fn new(architecture: Vec<usize>) -> Result<Self> {
        let mut weights = Vec::new();
        let mut biases = Vec::new();
        
        for i in 0..architecture.len()-1 {
            let w = Array2::zeros((architecture[i], architecture[i+1]));
            let b = Array1::zeros(architecture[i+1]);
            weights.push(w);
            biases.push(b);
        }
        
        Ok(Self {
            weights,
            biases,
            architecture: architecture.clone(),
            action_space: ActionSpace::default(),
        })
    }
    
    fn forward(&self, input: &Array1<T>) -> Result<Array1<T>> {
        let mut activation = input.clone();
        
        for (w, b) in self.weights.iter().zip(self.biases.iter()) {
            let mut output = Array1::zeros(w.ncols());
            for i in 0..w.ncols() {
                for j in 0..w.nrows() {
                    output[i] = output[i] + activation[j] * w[[j, i]];
                }
                output[i] = output[i] + b[i];
            }
            
            // Apply activation function (ReLU)
            activation = output.mapv(|x| x.max(T::zero()));
        }
        
        Ok(activation)
    }
}

impl<T: Float + Debug + Send + Sync + 'static + Default + Clone> ValueNetwork<T> {
    fn new(architecture: Vec<usize>) -> Result<Self> {
        let mut weights = Vec::new();
        let mut biases = Vec::new();
        
        for i in 0..architecture.len()-1 {
            let w = Array2::zeros((architecture[i], architecture[i+1]));
            let b = Array1::zeros(architecture[i+1]);
            weights.push(w);
            biases.push(b);
        }
        
        Ok(Self {
            weights,
            biases,
            architecture,
        })
    }
}

impl<T: Float + Debug + Send + Sync + 'static + Default + Clone> ReplayBuffer<T> {
    fn new(capacity: usize) -> Self {
        Self {
            capacity,
            experiences: VecDeque::new(),
            position: 0,
        }
    }
    
    fn add(&mut self, experience: Experience<T>) {
        if self.experiences.len() < self.capacity {
            self.experiences.push_back(experience);
        } else {
            self.experiences[self.position] = experience;
            self.position = (self.position + 1) % self.capacity;
        }
    }
    
    fn sample(&self, batch_size: usize) -> Result<Vec<Experience<T>>> {
        use scirs2_core::random::Rng;
        if self.experiences.len() < batch_size {
            return Err(OptimError::InsufficientData(
                "Not enough experiences in replay buffer".to_string()
            ));
        }
        
        let mut rng = scirs2_core::random::thread_rng();
        let mut batch = Vec::with_capacity(batch_size);
        
        for _ in 0..batch_size {
            let idx = rng.gen_range(0..self.experiences.len());
            batch.push(self.experiences[idx].clone());
        }
        
        Ok(batch)
    }
    
    fn size(&self) -> usize {
        self.experiences.len()
    }
}

impl<T: Float + Debug + Send + Sync + 'static + Default + Clone> TrainingStatistics<T> {
    fn new() -> Self {
        Self {
            episode_rewards: Vec::new(),
            average_rewards: Vec::new(),
            policy_losses: Vec::new(),
            value_losses: Vec::new(),
            exploration_rates: Vec::new(),
            architecture_performances: Vec::new(),
        }
    }
}

impl Default for ActionSpace {
    fn default() -> Self {
        let mut parameter_ranges = HashMap::new();
        parameter_ranges.insert("units".to_string(), (32.0, 512.0));
        parameter_ranges.insert("filters".to_string(), (16.0, 256.0));
        parameter_ranges.insert("kernel_size".to_string(), (1.0, 7.0));
        parameter_ranges.insert("dropout_rate".to_string(), (0.1, 0.5));
        
        Self {
            layer_types: vec![
                LayerTypeAction::AddDense { units: 128 },
                LayerTypeAction::AddLSTM { units: 128 },
                LayerTypeAction::AddAttention { heads: 8, dim: 128 },
            ],
            connection_types: vec![
                ConnectionAction::SequentialConnection,
                ConnectionAction::DenseConnection,
            ],
            parameter_ranges,
            max_depth: 20,
        }
    }
}

impl<T: Float + Debug + Send + Sync + 'static + Default + Clone> Default for RLNASConfig<T> {
    fn default() -> Self {
        Self {
            learning_rate: scirs2_core::numeric::NumCast::from(0.001).unwrap_or_else(|| T::zero()),
            discount_factor: scirs2_core::numeric::NumCast::from(0.99).unwrap_or_else(|| T::zero()),
            exploration_rate: scirs2_core::numeric::NumCast::from(1.0).unwrap_or_else(|| T::zero()),
            exploration_decay: scirs2_core::numeric::NumCast::from(0.995).unwrap_or_else(|| T::zero()),
            min_exploration_rate: scirs2_core::numeric::NumCast::from(0.01).unwrap_or_else(|| T::zero()),
            replay_buffer_size: 10000,
            batch_size: 32,
            num_episodes: 1000,
            max_steps_per_episode: 50,
            target_update_frequency: 100,
            complexity_penalty: scirs2_core::numeric::NumCast::from(0.01).unwrap_or_else(|| T::zero()),
        }
    }
}