optirs-nas 0.3.1

OptiRS Neural Architecture Search and hyperparameter optimization
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
// Architecture controllers for neural architecture search
//
// Implements various controllers for generating and managing optimizer architectures
// during the search process.

#[allow(dead_code)]

use scirs2_core::ndarray::{s, Array1, Array2};
use scirs2_core::numeric::Float;
use scirs2_core::random::Rng;
use scirs2_core::RngExt;
use scirs2_core::random::Rng as SCRRng;
use std::collections::{HashMap, VecDeque};
use std::fmt::Debug;

use super::{
    architecture_space::{ComponentType, OptimizerComponent},
    OptimizerArchitecture, SearchResult, SearchSpaceConfig,
};
use crate::error::{OptimError, Result};

/// Base trait for architecture controllers
pub trait ArchitectureController<T: Float + Debug + Send + Sync + 'static>: Send + Sync {
    /// Initialize the controller
    fn initialize(&mut self, searchspace: &SearchSpaceConfig) -> Result<()>;

    /// Generate new architecture
    fn generate_architecture(
        &mut self,
        context: &SearchContext<T>,
    ) -> Result<OptimizerArchitecture<T>>;

    /// Update controller with search results
    fn update_with_results(&mut self, results: &[SearchResult<T>]) -> Result<()>;

    /// Get controller statistics
    fn get_statistics(&self) -> ControllerStatistics<T>;

    /// Get controller name
    fn name(&self) -> &str;

    /// Reset controller state
    fn reset(&mut self) -> Result<()>;
}

/// Search context for architecture generation
#[derive(Debug, Clone)]
pub struct SearchContext<T: Float + Debug + Send + Sync + 'static> {
    /// Current generation
    pub generation: usize,

    /// Search history
    pub history: Vec<SearchResult<T>>,

    /// Current best performance
    pub best_performance: T,

    /// Current average performance
    pub average_performance: T,

    /// Resource constraints
    pub resource_budget: ResourceBudget<T>,

    /// Search preferences
    pub preferences: SearchPreferences,
}

/// Resource budget for search
#[derive(Debug, Clone)]
pub struct ResourceBudget<T: Float + Debug + Send + Sync + 'static> {
    /// Remaining memory budget (GB)
    pub memory_gb: T,

    /// Remaining computation budget (FLOPS)
    pub computation_flops: T,

    /// Remaining time budget (seconds)
    pub time_seconds: T,

    /// Remaining energy budget (kWh)
    pub energy_kwh: T,
}

/// Search preferences
#[derive(Debug, Clone)]
pub struct SearchPreferences {
    /// Exploration vs exploitation balance
    pub exploration_factor: f64,

    /// Preferred architecture complexity
    pub complexity_preference: ComplexityPreference,

    /// Domain-specific preferences
    pub domain_preferences: Vec<DomainPreference>,
}

/// Architecture complexity preferences
#[derive(Debug, Clone, Copy)]
pub enum ComplexityPreference {
    Simple,
    Moderate,
    Complex,
    Adaptive,
}

/// Domain-specific preferences
#[derive(Debug, Clone)]
pub struct DomainPreference {
    pub domain: String,
    pub weight: f64,
    pub constraints: Vec<String>,
}

/// Controller statistics
#[derive(Debug, Clone)]
pub struct ControllerStatistics<T: Float + Debug + Send + Sync + 'static> {
    /// Total architectures generated
    pub total_generated: usize,

    /// Success rate (valid architectures)
    pub success_rate: f64,

    /// Average generation time
    pub avg_generation_time_ms: f64,

    /// Diversity measure
    pub diversity_score: T,

    /// Controller-specific metrics
    pub controller_metrics: HashMap<String, T>,
}

/// RNN-based architecture controller
pub struct RNNController<
    T: Float
        + Default
        + Clone
        + Send
        + Sync
        + 'static
        + std::iter::Sum
        + for<'a> std::iter::Sum<&'a T>
        + scirs2_core::ndarray::ScalarOperand,
> {
    /// Controller configuration
    config: RNNControllerConfig,

    /// RNN layers
    rnn_layers: Vec<RNNLayer<T>>,

    /// Output projection layer
    output_layer: OutputLayer<T>,

    /// Hidden states
    hidden_states: Vec<Array1<T>>,

    /// Cell states (for LSTM)
    cell_states: Vec<Array1<T>>,

    /// Embedding layer for component types
    embedding_layer: EmbeddingLayer<T>,

    /// Search space information
    search_space: Option<SearchSpaceConfig>,

    /// Controller statistics
    statistics: ControllerStatistics<T>,

    /// Training history
    training_history: VecDeque<TrainingBatch<T>>,

    /// Action space mapping
    action_space: ActionSpace,

    /// Generation count
    generation_count: usize,
}

/// RNN controller configuration
#[derive(Debug, Clone)]
pub struct RNNControllerConfig {
    /// Hidden size
    pub hiddensize: usize,

    /// Number of layers
    pub numlayers: usize,

    /// RNN type
    pub rnn_type: RNNType,

    /// Dropout rate
    pub dropout_rate: f64,

    /// Learning rate
    pub learning_rate: f64,

    /// Temperature for sampling
    pub temperature: f64,

    /// Use attention mechanism
    pub use_attention: bool,

    /// Sequence length limit
    pub max_sequence_length: usize,
}

/// RNN types
#[derive(Debug, Clone, Copy)]
pub enum RNNType {
    LSTM,
    GRU,
    RNN,
}

/// RNN layer
#[derive(Debug, Clone)]
pub struct RNNLayer<T: Float + Debug + Send + Sync + 'static> {
    /// Layer type
    layer_type: RNNType,

    /// Input to hidden weights
    weight_ih: Array2<T>,

    /// Hidden to hidden weights
    weight_hh: Array2<T>,

    /// Input to hidden bias
    bias_ih: Array1<T>,

    /// Hidden to hidden bias
    bias_hh: Array1<T>,

    /// Layer normalization (optional)
    layer_norm: Option<LayerNorm<T>>,

    /// Dropout mask
    dropout_mask: Option<Array1<bool>>,
}

/// Layer normalization
#[derive(Debug, Clone)]
pub struct LayerNorm<T: Float + Debug + Send + Sync + 'static> {
    /// Scale parameters
    scale: Array1<T>,

    /// Shift parameters
    shift: Array1<T>,

    /// Epsilon for numerical stability
    eps: T,
}

/// Output projection layer
#[derive(Debug, Clone)]
pub struct OutputLayer<T: Float + Debug + Send + Sync + 'static> {
    /// Weight matrix
    weight: Array2<T>,

    /// Bias vector
    bias: Array1<T>,

    /// Output activation
    activation: ActivationType,
}

/// Activation types
#[derive(Debug, Clone, Copy)]
pub enum ActivationType {
    Linear,
    Softmax,
    Sigmoid,
    Tanh,
    ReLU,
}

/// Embedding layer for discrete inputs
#[derive(Debug, Clone)]
pub struct EmbeddingLayer<T: Float + Debug + Send + Sync + 'static> {
    /// Embedding matrix
    embeddings: Array2<T>,

    /// Vocabulary size
    _vocab_size: usize,

    /// Embedding dimension
    embeddingdim: usize,
}

/// Training batch for controller
#[derive(Debug, Clone)]
pub struct TrainingBatch<T: Float + Debug + Send + Sync + 'static> {
    /// Input sequences
    inputs: Vec<Array1<usize>>,

    /// Target sequences
    targets: Vec<Array1<usize>>,

    /// Rewards
    rewards: Vec<T>,

    /// Sequence lengths
    lengths: Vec<usize>,
}

/// Action space for architecture generation
#[derive(Debug, Clone)]
pub struct ActionSpace {
    /// Available component types
    component_types: Vec<ComponentType>,

    /// Available connections
    connection_types: Vec<String>,

    /// Hyperparameter ranges
    hyperparameter_ranges: HashMap<ComponentType, Vec<(String, f64, f64)>>,

    /// Maximum architecture size
    max_components: usize,
}

/// Transformer-based architecture controller
pub struct TransformerController<T: Float + Debug + Send + Sync + 'static> {
    /// Model configuration
    config: TransformerConfig,

    /// Transformer layers
    layers: Vec<TransformerLayer<T>>,

    /// Positional encoding
    positional_encoding: PositionalEncoding<T>,

    /// Input embedding
    input_embedding: EmbeddingLayer<T>,

    /// Output projection
    output_projection: OutputLayer<T>,

    /// Layer normalization
    layer_norm: LayerNorm<T>,

    /// Search space information
    search_space: Option<SearchSpaceConfig>,

    /// Controller statistics
    statistics: ControllerStatistics<T>,

    /// Generation count
    generation_count: usize,
}

/// Transformer configuration
#[derive(Debug, Clone)]
pub struct TransformerConfig {
    /// Model dimension
    pub _model_dim: usize,

    /// Number of attention heads
    pub numheads: usize,

    /// Number of layers
    pub numlayers: usize,

    /// Feed-forward dimension
    pub ffdim: usize,

    /// Dropout rate
    pub dropout_rate: f64,

    /// Learning rate
    pub learning_rate: f64,

    /// Temperature for sampling
    pub temperature: f64,

    /// Maximum sequence length
    pub max_sequence_length: usize,
}

/// Transformer layer
#[derive(Debug, Clone)]
pub struct TransformerLayer<T: Float + Debug + Send + Sync + 'static> {
    /// Multi-head attention
    attention: MultiHeadAttention<T>,

    /// Feed-forward network
    feed_forward: FeedForward<T>,

    /// Layer normalization for attention
    norm1: LayerNorm<T>,

    /// Layer normalization for feed-forward
    norm2: LayerNorm<T>,

    /// Dropout
    dropout: f64,
}

/// Multi-head attention
#[derive(Debug, Clone)]
pub struct MultiHeadAttention<T: Float + Debug + Send + Sync + 'static> {
    /// Number of heads
    numheads: usize,

    /// Head dimension
    head_dim: usize,

    /// Query projection
    query_proj: Array2<T>,

    /// Key projection
    key_proj: Array2<T>,

    /// Value projection
    value_proj: Array2<T>,

    /// Output projection
    output_proj: Array2<T>,

    /// Attention dropout
    dropout: f64,
}

/// Feed-forward network
#[derive(Debug, Clone)]
pub struct FeedForward<T: Float + Debug + Send + Sync + 'static> {
    /// First linear layer
    linear1: LinearLayer<T>,

    /// Second linear layer
    linear2: LinearLayer<T>,

    /// Activation function
    activation: ActivationType,

    /// Dropout
    dropout: f64,
}

/// Linear layer
#[derive(Debug, Clone)]
pub struct LinearLayer<T: Float + Debug + Send + Sync + 'static> {
    /// Weight matrix
    weight: Array2<T>,

    /// Bias vector
    bias: Array1<T>,
}

/// Positional encoding
#[derive(Debug, Clone)]
pub struct PositionalEncoding<T: Float + Debug + Send + Sync + 'static> {
    /// Encoding matrix
    encoding: Array2<T>,

    /// Maximum sequence length
    max_length: usize,

    /// Model dimension
    _model_dim: usize,
}

/// Random architecture controller (baseline)
pub struct RandomController<T: Float + Debug + Send + Sync + 'static> {
    /// Available component types
    component_types: Vec<ComponentType>,

    /// Search space
    search_space: Option<SearchSpaceConfig>,

    /// Controller statistics
    statistics: ControllerStatistics<T>,

    /// Random number generator seed
    seed: Option<u64>,

    /// Generation count
    generation_count: usize,
}

/// Evolutionary controller using genetic algorithms
pub struct EvolutionaryController<T: Float + Debug + Send + Sync + 'static> {
    /// Population of controllers
    population: Vec<ControllerGenome<T>>,

    /// Population size
    population_size: usize,

    /// Mutation rate
    mutation_rate: f64,

    /// Crossover rate
    crossover_rate: f64,

    /// Elite size
    elite_size: usize,

    /// Search space
    search_space: Option<SearchSpaceConfig>,

    /// Controller statistics
    statistics: ControllerStatistics<T>,

    /// Generation count
    generation_count: usize,
}

/// Controller genome for evolutionary approach
#[derive(Debug, Clone)]
pub struct ControllerGenome<T: Float + Debug + Send + Sync + 'static> {
    /// Genome encoding
    encoding: Vec<T>,

    /// Fitness score
    fitness: T,

    /// Age
    age: usize,

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

impl<
        T: Float
            + Default
            + Clone
            + Send
            + Sync
            + 'static
            + std::iter::Sum
            + for<'a> std::iter::Sum<&'a T>
            + scirs2_core::ndarray::ScalarOperand,
    > RNNController<T>
{
    /// Create new RNN controller
    pub fn new(hiddensize: usize, numlayers: usize, _vocab_size: usize) -> Result<Self> {
        let config = RNNControllerConfig {
            hiddensize,
            numlayers,
            rnn_type: RNNType::LSTM,
            dropout_rate: 0.1,
            learning_rate: 0.001,
            temperature: 1.0,
            use_attention: false,
            max_sequence_length: 50,
        };

        let mut rnn_layers = Vec::new();
        for i in 0..numlayers {
            let input_size = if i == 0 { hiddensize } else { hiddensize };
            rnn_layers.push(RNNLayer::new(RNNType::LSTM, input_size, hiddensize)?);
        }

        let output_layer = OutputLayer::new(hiddensize, _vocab_size, ActivationType::Softmax)?;
        let embedding_layer = EmbeddingLayer::new(_vocab_size, hiddensize)?;

        let hidden_states = vec![Array1::zeros(hiddensize); numlayers];
        let cell_states = vec![Array1::zeros(hiddensize); numlayers];

        Ok(Self {
            config,
            rnn_layers,
            output_layer,
            hidden_states,
            cell_states,
            embedding_layer,
            search_space: None,
            statistics: ControllerStatistics::default(),
            training_history: VecDeque::new(),
            action_space: ActionSpace::default(),
            generation_count: 0,
        })
    }

    /// Forward pass through RNN
    fn forward(&mut self, inputsequence: &[usize]) -> Result<Array2<T>> {
        let seq_len = inputsequence.len();
        let _vocab_size = self.action_space.component_types.len();
        let mut outputs = Array2::zeros((seq_len, _vocab_size));

        // Reset states at beginning of _sequence
        for state in &mut self.hidden_states {
            state.fill(T::zero());
        }
        for state in &mut self.cell_states {
            state.fill(T::zero());
        }

        for (t, &input_token) in inputsequence.iter().enumerate() {
            // Embed input token
            let embedded = self.embedding_layer.forward(input_token)?;

            // Forward through RNN layers
            let mut layer_input = embedded;
            for (layer_idx, layer) in self.rnn_layers.iter_mut().enumerate() {
                let (hidden_output, cell_output) = layer.forward(
                    &layer_input,
                    &self.hidden_states[layer_idx],
                    &self.cell_states[layer_idx],
                )?;

                self.hidden_states[layer_idx] = hidden_output.clone();
                self.cell_states[layer_idx] = cell_output;
                layer_input = hidden_output;
            }

            // Output projection
            let output = self.output_layer.forward(&layer_input)?;
            outputs.row_mut(t).assign(&output);
        }

        Ok(outputs)
    }

    /// Sample architecture from controller
    fn sample_architecture(&mut self) -> Result<OptimizerArchitecture<T>> {
        let mut components = Vec::new();
        let mut sequence = Vec::new();

        // Start token
        sequence.push(0);

        for _ in 0..self.config.max_sequence_length {
            let logits = self.forward(&sequence)?;
            let last_logits = logits.row(logits.nrows() - 1);

            // Sample from distribution
            let token = self.sample_from_logits(&last_logits.to_owned())?;

            // Check for end token
            if token == 1 {
                // Assuming 1 is end token
                break;
            }

            sequence.push(token);

            // Convert token to component if it represents a component type
            if token >= 2 && token < self.action_space.component_types.len() + 2 {
                let component_type = self.action_space.component_types[token - 2].clone();
                let component = self.create_component_from_type(component_type)?;
                components.push(component);
            }
        }

        if components.is_empty() {
            // Return default architecture if sampling failed
            let default_component = self.create_component_from_type(ComponentType::Adam)?;
            components.push(default_component);
        }

        Ok(OptimizerArchitecture {
            components,
            connections: Vec::new(),
            metadata: HashMap::new(),
        })
    }

    /// Sample from logits using temperature
    fn sample_from_logits(&self, logits: &Array1<T>) -> Result<usize> {
        // Apply temperature scaling
        let scaled_logits = logits.mapv(|x| x / scirs2_core::numeric::NumCast::from(self.config.temperature).unwrap_or_else(|| T::zero()));

        // Softmax
        let max_logit = scaled_logits
            .iter()
            .cloned()
            .fold(T::neg_infinity(), |a, b| if a > b { a } else { b });
        let exp_logits = scaled_logits.mapv(|x| (x - max_logit).exp());
        let sum_exp = exp_logits.sum();
        let probs = exp_logits / sum_exp;

        // Sample from categorical distribution
        let mut rng = scirs2_core::random::thread_rng();
        let rand_val = rng.random::<f64>();
        let mut cumulative = 0.0;

        for (i, &prob) in probs.iter().enumerate() {
            cumulative += prob.to_f64().unwrap_or(0.0);
            if cumulative >= rand_val {
                return Ok(i);
            }
        }

        Ok(probs.len() - 1) // Fallback to last token
    }

    /// Create component from component type
    fn create_component_from_type(
        &self,
        component_type: ComponentType,
    ) -> Result<OptimizerComponent<T>> {
        let mut hyperparameters = HashMap::new();

        // Set default hyperparameters based on component _type
        match component_type {
            ComponentType::Adam => {
                hyperparameters.insert("learning_rate".to_string(), scirs2_core::numeric::NumCast::from(0.001).unwrap_or_else(|| T::zero()));
                hyperparameters.insert("beta1".to_string(), scirs2_core::numeric::NumCast::from(0.9).unwrap_or_else(|| T::zero()));
                hyperparameters.insert("beta2".to_string(), scirs2_core::numeric::NumCast::from(0.999).unwrap_or_else(|| T::zero()));
                hyperparameters.insert("epsilon".to_string(), scirs2_core::numeric::NumCast::from(1e-8).unwrap_or_else(|| T::zero()));
            }
            ComponentType::SGD => {
                hyperparameters.insert("learning_rate".to_string(), scirs2_core::numeric::NumCast::from(0.01).unwrap_or_else(|| T::zero()));
                hyperparameters.insert("momentum".to_string(), scirs2_core::numeric::NumCast::from(0.9).unwrap_or_else(|| T::zero()));
                hyperparameters.insert("weight_decay".to_string(), scirs2_core::numeric::NumCast::from(0.0001).unwrap_or_else(|| T::zero()));
            }
            _ => {
                hyperparameters.insert("learning_rate".to_string(), scirs2_core::numeric::NumCast::from(0.001).unwrap_or_else(|| T::zero()));
            }
        }

        Ok(OptimizerComponent {
            component_type,
            hyperparameters,
            connections: Vec::new(),
        })
    }
}

impl<
        T: Float
            + Default
            + Clone
            + Send
            + Sync
            + 'static
            + std::iter::Sum
            + for<'a> std::iter::Sum<&'a T>
            + scirs2_core::ndarray::ScalarOperand,
    > ArchitectureController<T> for RNNController<T>
{
    fn initialize(&mut self, searchspace: &SearchSpaceConfig) -> Result<()> {
        self.search_space = Some(searchspace.clone());

        // Initialize action _space from search _space
        self.action_space = ActionSpace {
            component_types: searchspace
                .optimizer_components
                .iter()
                .map(|c| c.componenttype.clone())
                .collect(),
            connection_types: vec!["sequential".to_string(), "parallel".to_string()],
            hyperparameter_ranges: HashMap::new(),
            max_components: 10,
        };

        self.generation_count = 0;
        Ok(())
    }

    fn generate_architecture(
        &mut self,
        context: &SearchContext<T>,
    ) -> Result<OptimizerArchitecture<T>> {
        let architecture = self.sample_architecture()?;
        self.generation_count += 1;
        self.statistics.total_generated += 1;
        Ok(architecture)
    }

    fn update_with_results(&mut self, results: &[SearchResult<T>]) -> Result<()> {
        // Create training batch from results
        let mut batch = TrainingBatch {
            inputs: Vec::new(),
            targets: Vec::new(),
            rewards: Vec::new(),
            lengths: Vec::new(),
        };

        for result in results {
            // Extract performance as reward
            let reward = result
                .evaluation_results
                .metric_scores
                .get(&super::EvaluationMetric::FinalPerformance)
                .cloned()
                .unwrap_or(T::zero());

            batch.rewards.push(reward);

            // Simplified: create input sequence from architecture
            let input_seq = self.architecture_to_sequence(&result.architecture)?;
            let seq_len = input_seq.len();
            batch.inputs.push(Array1::from_vec(input_seq.clone()));
            batch.targets.push(Array1::from_vec(input_seq)); // Self-prediction for now
            batch.lengths.push(seq_len);
        }

        self.training_history.push_back(batch);

        // Keep limited history
        if self.training_history.len() > 100 {
            self.training_history.pop_front();
        }

        // Update statistics
        let avg_reward = if !results.is_empty() {
            results
                .iter()
                .filter_map(|r| {
                    r.evaluation_results
                        .metric_scores
                        .get(&super::EvaluationMetric::FinalPerformance)
                })
                .sum::<T>()
                / T::from(results.len()).expect("unwrap failed")
        } else {
            T::zero()
        };

        self.statistics
            .controller_metrics
            .insert("avg_reward".to_string(), avg_reward);

        Ok(())
    }

    fn get_statistics(&self) -> ControllerStatistics<T> {
        self.statistics.clone()
    }

    fn name(&self) -> &str {
        "RNNController"
    }

    fn reset(&mut self) -> Result<()> {
        for state in &mut self.hidden_states {
            state.fill(T::zero());
        }
        for state in &mut self.cell_states {
            state.fill(T::zero());
        }
        self.generation_count = 0;
        Ok(())
    }
}

impl<
        T: Float
            + Default
            + Clone
            + Send
            + Sync
            + 'static
            + std::iter::Sum
            + for<'a> std::iter::Sum<&'a T>
            + scirs2_core::ndarray::ScalarOperand,
    > RNNController<T>
{
    fn architecture_to_sequence(
        &self,
        architecture: &OptimizerArchitecture<T>,
    ) -> Result<Vec<usize>> {
        let mut sequence = vec![0]; // Start token

        for component in &architecture.components {
            // Find component type index
            if let Some(pos) = self
                .action_space
                .component_types
                .iter()
                .position(|x| *x == component.component_type)
            {
                sequence.push(pos + 2); // Offset by 2 (start and end tokens)
            }
        }

        sequence.push(1); // End token
        Ok(sequence)
    }
}

impl<T: Float + Debug + Default + Clone + Send + Sync + 'static + scirs2_core::ndarray::ScalarOperand>
    TransformerController<T>
{
    /// Create new Transformer controller
    pub fn new(_model_dim: usize, numheads: usize, numlayers: usize) -> Result<Self> {
        let config = TransformerConfig {
            _model_dim,
            numheads,
            numlayers,
            ffdim: _model_dim * 4,
            dropout_rate: 0.1,
            learning_rate: 0.001,
            temperature: 1.0,
            max_sequence_length: 50,
        };

        let mut layers = Vec::new();
        for _ in 0..numlayers {
            layers.push(TransformerLayer::new(_model_dim, numheads, _model_dim * 4)?);
        }

        let positional_encoding = PositionalEncoding::new(config.max_sequence_length, _model_dim)?;
        let input_embedding = EmbeddingLayer::new(1000, _model_dim)?; // Vocab size placeholder
        let output_projection = OutputLayer::new(_model_dim, 1000, ActivationType::Softmax)?;
        let layer_norm = LayerNorm::new(_model_dim)?;

        Ok(Self {
            config,
            layers,
            positional_encoding,
            input_embedding,
            output_projection,
            layer_norm,
            search_space: None,
            statistics: ControllerStatistics::default(),
            generation_count: 0,
        })
    }
}

impl<T: Float + Debug + Default + Clone + Send + Sync + std::iter::Sum> ArchitectureController<T>
    for TransformerController<T>
{
    fn initialize(&mut self, searchspace: &SearchSpaceConfig) -> Result<()> {
        self.search_space = Some(searchspace.clone());
        self.generation_count = 0;
        Ok(())
    }

    fn generate_architecture(
        &mut self,
        context: &SearchContext<T>,
    ) -> Result<OptimizerArchitecture<T>> {
        // Simplified architecture generation for Transformer
        // In practice, this would use the full transformer forward pass
        use super::architecture_space::{ComponentType, OptimizerComponent};

        let component = OptimizerComponent {
            component_type: ComponentType::Adam,
            hyperparameters: {
                let mut params = HashMap::new();
                params.insert("learning_rate".to_string(), scirs2_core::numeric::NumCast::from(0.001).unwrap_or_else(|| T::zero()));
                params.insert("beta1".to_string(), scirs2_core::numeric::NumCast::from(0.9).unwrap_or_else(|| T::zero()));
                params.insert("beta2".to_string(), scirs2_core::numeric::NumCast::from(0.999).unwrap_or_else(|| T::zero()));
                params
            },
            connections: Vec::new(),
        };

        self.generation_count += 1;
        self.statistics.total_generated += 1;

        Ok(OptimizerArchitecture {
            components: vec![component],
            connections: Vec::new(),
            metadata: HashMap::new(),
        })
    }

    fn update_with_results(&mut self, results: &[SearchResult<T>]) -> Result<()> {
        // Simplified update for Transformer controller
        Ok(())
    }

    fn get_statistics(&self) -> ControllerStatistics<T> {
        self.statistics.clone()
    }

    fn name(&self) -> &str {
        "TransformerController"
    }

    fn reset(&mut self) -> Result<()> {
        self.generation_count = 0;
        Ok(())
    }
}

impl<T: Float + Debug + Default + Clone + Send + Sync + std::iter::Sum> RandomController<T> {
    /// Create new random controller
    pub fn new(_vocabsize: usize) -> Result<Self> {
        Ok(Self {
            component_types: vec![
                ComponentType::Adam,
                ComponentType::SGD,
                ComponentType::AdaGrad,
            ],
            search_space: None,
            statistics: ControllerStatistics::default(),
            seed: None,
            generation_count: 0,
        })
    }
}

impl<T: Float + Debug + Default + Clone + Send + Sync + std::iter::Sum> ArchitectureController<T>
    for RandomController<T>
{
    fn initialize(&mut self, searchspace: &SearchSpaceConfig) -> Result<()> {
        self.search_space = Some(searchspace.clone());
        self.component_types = searchspace
            .optimizer_components
            .iter()
            .map(|c| c.componenttype.clone())
            .collect();
        self.generation_count = 0;
        Ok(())
    }

    fn generate_architecture(
        &mut self,
        context: &SearchContext<T>,
    ) -> Result<OptimizerArchitecture<T>> {
        use super::architecture_space::{ComponentType, OptimizerComponent};

        // Random selection of component type
        let mut rng = scirs2_core::random::thread_rng();
        let component_type =
            self.component_types[rng.gen_range(0..self.component_types.len())].clone();

        let mut hyperparameters = HashMap::new();
        match component_type {
            ComponentType::Adam => {
                hyperparameters.insert(
                    "learning_rate".to_string(),
                    T::from(rng.random::<f64>() * 0.01).expect("unwrap failed"),
                );
                hyperparameters.insert(
                    "beta1".to_string(),
                    T::from(0.8 + rng.random::<f64>() * 0.19).expect("unwrap failed"),
                );
                hyperparameters.insert(
                    "beta2".to_string(),
                    T::from(0.9 + rng.random::<f64>() * 0.099).expect("unwrap failed"),
                );
            }
            ComponentType::SGD => {
                hyperparameters.insert(
                    "learning_rate".to_string(),
                    T::from(rng.random::<f64>() * 0.1).expect("unwrap failed"),
                );
                hyperparameters.insert(
                    "momentum".to_string(),
                    T::from(rng.random::<f64>() * 0.99).expect("unwrap failed"),
                );
            }
            _ => {
                hyperparameters.insert(
                    "learning_rate".to_string(),
                    T::from(rng.random::<f64>() * 0.01).expect("unwrap failed"),
                );
            }
        }

        let component = OptimizerComponent {
            component_type,
            hyperparameters,
            connections: Vec::new(),
        };

        self.generation_count += 1;
        self.statistics.total_generated += 1;

        Ok(OptimizerArchitecture {
            components: vec![component],
            connections: Vec::new(),
            metadata: HashMap::new(),
        })
    }

    fn update_with_results(&mut self, results: &[SearchResult<T>]) -> Result<()> {
        // Random controller doesn't learn from _results
        Ok(())
    }

    fn get_statistics(&self) -> ControllerStatistics<T> {
        self.statistics.clone()
    }

    fn name(&self) -> &str {
        "RandomController"
    }

    fn reset(&mut self) -> Result<()> {
        self.generation_count = 0;
        Ok(())
    }
}

// Implementation helpers for layers
impl<T: Float + Debug + Default + Clone + 'static + scirs2_core::ndarray::ScalarOperand + Send + Sync> RNNLayer<T> {
    fn new(layer_type: RNNType, input_size: usize, hiddensize: usize) -> Result<Self> {
        let gate_size = match layer_type {
            RNNType::LSTM => hiddensize * 4,
            RNNType::GRU => hiddensize * 3,
            RNNType::RNN => hiddensize,
        };

        Ok(Self {
            layer_type,
            weight_ih: Array2::zeros((gate_size, input_size)),
            weight_hh: Array2::zeros((gate_size, hiddensize)),
            bias_ih: Array1::zeros(gate_size),
            bias_hh: Array1::zeros(gate_size),
            layer_norm: None,
            dropout_mask: None,
        })
    }

    fn forward(
        &self,
        input: &Array1<T>,
        hidden: &Array1<T>,
        cell: &Array1<T>,
    ) -> Result<(Array1<T>, Array1<T>)> {
        match self.layer_type {
            RNNType::LSTM => self.lstm_forward(input, hidden, cell),
            RNNType::GRU => self.gru_forward(input, hidden),
            RNNType::RNN => self.rnn_forward(input, hidden),
        }
    }

    fn lstm_forward(
        &self,
        input: &Array1<T>,
        hidden: &Array1<T>,
        cell: &Array1<T>,
    ) -> Result<(Array1<T>, Array1<T>)> {
        let hiddensize = hidden.len();

        let gi = self.weight_ih.dot(input) + &self.bias_ih;
        let gh = self.weight_hh.dot(hidden) + &self.bias_hh;
        let combined = gi + gh;

        // Split gates
        let input_gate = Self::sigmoid(&combined.slice(s![0..hiddensize]).to_owned());
        let forget_gate = Self::sigmoid(&combined.slice(s![hiddensize..2 * hiddensize]).to_owned());
        let cell_gate = Self::tanh(
            &combined
                .slice(s![2 * hiddensize..3 * hiddensize])
                .to_owned(),
        );
        let output_gate = Self::sigmoid(
            &combined
                .slice(s![3 * hiddensize..4 * hiddensize])
                .to_owned(),
        );

        let new_cell = &forget_gate * cell + &input_gate * &cell_gate;
        let new_hidden = &output_gate * &Self::tanh(&new_cell);

        Ok((new_hidden, new_cell))
    }

    fn gru_forward(&self, input: &Array1<T>, hidden: &Array1<T>) -> Result<(Array1<T>, Array1<T>)> {
        let hiddensize = hidden.len();

        let gi = self.weight_ih.dot(input) + &self.bias_ih;
        let gh = self.weight_hh.dot(hidden) + &self.bias_hh;

        let reset_gate = Self::sigmoid(
            &(gi.slice(s![0..hiddensize]).to_owned() + gh.slice(s![0..hiddensize]).to_owned()),
        );
        let update_gate = Self::sigmoid(
            &(gi.slice(s![hiddensize..2 * hiddensize]).to_owned()
                + gh.slice(s![hiddensize..2 * hiddensize]).to_owned()),
        );

        let new_gate = Self::tanh(
            &(gi.slice(s![2 * hiddensize..3 * hiddensize]).to_owned()
                + &reset_gate * &gh.slice(s![2 * hiddensize..3 * hiddensize]).to_owned()),
        );

        let one_minus_update = update_gate.mapv(|x| T::one() - x);
        let new_hidden = &update_gate * hidden + &one_minus_update * &new_gate;

        Ok((new_hidden.clone(), new_hidden))
    }

    fn rnn_forward(&self, input: &Array1<T>, hidden: &Array1<T>) -> Result<(Array1<T>, Array1<T>)> {
        let gi = self.weight_ih.dot(input) + &self.bias_ih;
        let gh = self.weight_hh.dot(hidden) + &self.bias_hh;
        let new_hidden = Self::tanh(&(gi + gh));

        Ok((new_hidden.clone(), new_hidden))
    }

    fn sigmoid(x: &Array1<T>) -> Array1<T> {
        x.mapv(|xi| T::one() / (T::one() + (-xi).exp()))
    }

    fn tanh(x: &Array1<T>) -> Array1<T> {
        x.mapv(|xi| xi.tanh())
    }
}

impl<T: Float + Debug + Default + Clone + 'static + scirs2_core::ndarray::ScalarOperand + Send + Sync> OutputLayer<T> {
    fn new(input_size: usize, outputsize: usize, activation: ActivationType) -> Result<Self> {
        Ok(Self {
            weight: Array2::zeros((outputsize, input_size)),
            bias: Array1::zeros(outputsize),
            activation,
        })
    }

    fn forward(&self, input: &Array1<T>) -> Result<Array1<T>> {
        let output = self.weight.dot(input) + &self.bias;

        match self.activation {
            ActivationType::Linear => Ok(output),
            ActivationType::Softmax => Ok(Self::softmax(&output)),
            ActivationType::Sigmoid => Ok(Self::sigmoid(&output)),
            ActivationType::Tanh => Ok(output.mapv(|x| x.tanh())),
            ActivationType::ReLU => Ok(output.mapv(|x| if x > T::zero() { x } else { T::zero() })),
        }
    }

    fn softmax(x: &Array1<T>) -> Array1<T> {
        let max_val = x
            .iter()
            .cloned()
            .fold(T::neg_infinity(), |a, b| if a > b { a } else { b });
        let exp_x = x.mapv(|xi| (xi - max_val).exp());
        let sum_exp = exp_x.sum();
        exp_x / sum_exp
    }

    fn sigmoid(x: &Array1<T>) -> Array1<T> {
        x.mapv(|xi| T::one() / (T::one() + (-xi).exp()))
    }
}

impl<T: Float + Debug + Send + Sync + 'static + Default + Clone> EmbeddingLayer<T> {
    fn new(_vocab_size: usize, embeddingdim: usize) -> Result<Self> {
        Ok(Self {
            embeddings: Array2::zeros((_vocab_size, embeddingdim)),
            _vocab_size,
            embeddingdim,
        })
    }

    fn forward(&self, token: usize) -> Result<Array1<T>> {
        if token >= self._vocab_size {
            return Err(OptimError::InvalidConfig(format!(
                "Token {} out of vocabulary",
                token
            )));
        }

        Ok(self.embeddings.row(token).to_owned())
    }
}

impl<T: Float + Debug + Send + Sync + 'static + Default + Clone> TransformerLayer<T> {
    fn new(_model_dim: usize, numheads: usize, ffdim: usize) -> Result<Self> {
        Ok(Self {
            attention: MultiHeadAttention::new(_model_dim, numheads)?,
            feed_forward: FeedForward::new(_model_dim, ffdim)?,
            norm1: LayerNorm::new(_model_dim)?,
            norm2: LayerNorm::new(_model_dim)?,
            dropout: 0.1,
        })
    }
}

impl<T: Float + Debug + Send + Sync + 'static + Default + Clone> MultiHeadAttention<T> {
    fn new(_model_dim: usize, numheads: usize) -> Result<Self> {
        let head_dim = _model_dim / numheads;

        Ok(Self {
            numheads,
            head_dim,
            query_proj: Array2::zeros((_model_dim, _model_dim)),
            key_proj: Array2::zeros((_model_dim, _model_dim)),
            value_proj: Array2::zeros((_model_dim, _model_dim)),
            output_proj: Array2::zeros((_model_dim, _model_dim)),
            dropout: 0.1,
        })
    }
}

impl<T: Float + Debug + Send + Sync + 'static + Default + Clone> FeedForward<T> {
    fn new(input_dim: usize, ffdim: usize) -> Result<Self> {
        Ok(Self {
            linear1: LinearLayer::new(input_dim, ffdim)?,
            linear2: LinearLayer::new(ffdim, input_dim)?,
            activation: ActivationType::ReLU,
            dropout: 0.1,
        })
    }
}

impl<T: Float + Debug + Send + Sync + 'static + Default + Clone> LinearLayer<T> {
    fn new(input_dim: usize, outputdim: usize) -> Result<Self> {
        Ok(Self {
            weight: Array2::zeros((outputdim, input_dim)),
            bias: Array1::zeros(outputdim),
        })
    }
}

impl<T: Float + Debug + Send + Sync + 'static + Default + Clone> LayerNorm<T> {
    fn new(dim: usize) -> Result<Self> {
        Ok(Self {
            scale: Array1::ones(dim),
            shift: Array1::zeros(dim),
            eps: scirs2_core::numeric::NumCast::from(1e-6).unwrap_or_else(|| T::zero()),
        })
    }
}

impl<T: Float + Debug + Send + Sync + 'static + Default + Clone> PositionalEncoding<T> {
    fn new(max_length: usize, _model_dim: usize) -> Result<Self> {
        let mut encoding = Array2::zeros((max_length, _model_dim));

        for pos in 0..max_length {
            for i in 0.._model_dim / 2 {
                let angle = pos as f64 / 10000_f64.powf(2.0 * i as f64 / _model_dim as f64);
                encoding[[pos, 2 * i]] = T::from(angle.sin()).expect("unwrap failed");
                encoding[[pos, 2 * i + 1]] = T::from(angle.cos()).expect("unwrap failed");
            }
        }

        Ok(Self {
            encoding,
            max_length,
            _model_dim,
        })
    }
}

impl Default for ActionSpace {
    fn default() -> Self {
        Self {
            component_types: vec![
                ComponentType::Adam,
                ComponentType::SGD,
                ComponentType::AdaGrad,
            ],
            connection_types: vec!["sequential".to_string(), "parallel".to_string()],
            hyperparameter_ranges: HashMap::new(),
            max_components: 10,
        }
    }
}

impl<T: Float + Debug + Default + Send + Sync> Default for ControllerStatistics<T> {
    fn default() -> Self {
        Self {
            total_generated: 0,
            success_rate: 0.0,
            avg_generation_time_ms: 0.0,
            diversity_score: T::zero(),
            controller_metrics: HashMap::new(),
        }
    }
}

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

    #[test]
    fn test_rnn_controller_creation() {
        let controller = RNNController::<f64>::new(256, 2, 100);
        assert!(controller.is_ok());

        let controller = controller.expect("unwrap failed");
        assert_eq!(controller.name(), "RNNController");
    }

    #[test]
    fn test_transformer_controller_creation() {
        let controller = TransformerController::<f64>::new(512, 8, 4);
        assert!(controller.is_ok());

        let controller = controller.expect("unwrap failed");
        assert_eq!(controller.name(), "TransformerController");
    }

    #[test]
    fn test_random_controller_creation() {
        let controller = RandomController::<f64>::new(100);
        assert!(controller.is_ok());

        let controller = controller.expect("unwrap failed");
        assert_eq!(controller.name(), "RandomController");
    }
}