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
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
use std::fmt::Debug;
// Architecture encoding and representation for neural architecture search
//
// This module provides methods to encode neural network architectures into
// various representations suitable for different search algorithms.

#[allow(dead_code)]

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

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

/// Configuration for architecture encoding
#[derive(Debug, Clone)]
pub struct ArchitectureEncodingConfig {
    /// Maximum sequence length for sequential encodings
    pub max_sequence_length: usize,
    
    /// Vocabulary size for categorical encodings
    pub vocabulary_size: usize,
    
    /// Embedding dimensions for different encoding types
    pub embedding_dimensions: HashMap<EncodingType, usize>,
    
    /// Whether to include positional encodings
    pub use_positional_encoding: bool,
    
    /// Normalization method
    pub normalization_method: NormalizationMethod,
    
    /// Supported operation types
    pub operation_vocabulary: Vec<String>,
    
    /// Maximum graph depth
    pub max_graph_depth: usize,
}

/// Types of architecture encodings
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EncodingType {
    /// Sequential encoding (list of operations)
    Sequential,
    
    /// Graph-based encoding (adjacency matrices)
    Graph,
    
    /// Tree-based encoding
    Tree,
    
    /// Path-based encoding
    PathBased,
    
    /// Continuous vector encoding
    ContinuousVector,
    
    /// Categorical encoding
    Categorical,
    
    /// Hybrid encoding (multiple representations)
    Hybrid,
}

/// Normalization methods for encodings
#[derive(Debug, Clone, Copy)]
pub enum NormalizationMethod {
    /// No normalization
    None,
    
    /// Min-max normalization
    MinMax,
    
    /// Z-score normalization
    ZScore,
    
    /// Layer normalization
    LayerNorm,
    
    /// Batch normalization
    BatchNorm,
}

/// Architecture encoder for different representation types
#[derive(Debug)]
pub struct ArchitectureEncoder<T: Float + Debug + Send + Sync + 'static> {
    /// Configuration
    config: ArchitectureEncodingConfig,
    
    /// Encoding mappings
    encodings: HashMap<EncodingType, Box<dyn EncodingStrategy<T>>>,
    
    /// Operation to index mapping
    operation_to_index: HashMap<String, usize>,
    
    /// Index to operation mapping
    index_to_operation: HashMap<usize, String>,
    
    /// Embedding matrices for different encodings
    embedding_matrices: HashMap<EncodingType, Array2<T>>,
    
    /// Normalization statistics
    normalization_stats: HashMap<String, NormalizationStats<T>>,
}

/// Trait for different encoding strategies
pub trait EncodingStrategy<T: Float + Debug + Send + Sync + 'static> {
    /// Encode architecture into the specific representation
    fn encode(&self, architecture: &ArchitectureDefinition<T>) -> Result<EncodedArchitecture<T>>;
    
    /// Decode representation back to architecture
    fn decode(&self, encoded: &EncodedArchitecture<T>) -> Result<ArchitectureDefinition<T>>;
    
    /// Get encoding dimensionality
    fn get_dimensions(&self) -> Vec<usize>;
    
    /// Validate encoding consistency
    fn validate(&self, encoded: &EncodedArchitecture<T>) -> Result<bool>;
}

/// Architecture definition structure
#[derive(Debug, Clone)]
pub struct ArchitectureDefinition<T: Float + Debug + Send + Sync + 'static> {
    /// Architecture identifier
    pub id: String,
    
    /// List of operations/layers
    pub operations: Vec<OperationSpec<T>>,
    
    /// Connection graph
    pub connections: ConnectionGraph,
    
    /// Global architecture parameters
    pub global_parameters: HashMap<String, T>,
    
    /// Input/output specifications
    pub io_specs: IOSpecifications,
    
    /// Architecture metadata
    pub metadata: ArchitectureMetadata,
}

/// Operation specification
#[derive(Debug, Clone)]
pub struct OperationSpec<T: Float + Debug + Send + Sync + 'static> {
    /// Operation identifier
    pub id: String,
    
    /// Operation type
    pub operation_type: String,
    
    /// Operation parameters
    pub parameters: HashMap<String, ParameterValue<T>>,
    
    /// Input shapes
    pub input_shapes: Vec<Vec<usize>>,
    
    /// Output shapes
    pub output_shapes: Vec<Vec<usize>>,
    
    /// Position in architecture
    pub position: Position,
}

/// Parameter values for operations
#[derive(Debug, Clone)]
pub enum ParameterValue<T: Float + Debug + Send + Sync + 'static> {
    /// Integer parameter
    Integer(i64),
    
    /// Float parameter
    Float(T),
    
    /// Boolean parameter
    Boolean(bool),
    
    /// String parameter
    String(String),
    
    /// Array parameter
    Array(Vec<T>),
    
    /// Tensor parameter
    Tensor(Array3<T>),
}

/// Connection graph representation
#[derive(Debug, Clone)]
pub struct ConnectionGraph {
    /// Adjacency matrix
    pub adjacency_matrix: Array2<f32>,
    
    /// Edge types
    pub edge_types: HashMap<(usize, usize), EdgeType>,
    
    /// Node properties
    pub node_properties: HashMap<usize, NodeProperties>,
    
    /// Graph topology information
    pub topology: GraphTopology,
}

/// Edge types in connection graph
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EdgeType {
    /// Standard data flow edge
    DataFlow,
    
    /// Skip connection
    Skip,
    
    /// Residual connection
    Residual,
    
    /// Attention connection
    Attention,
    
    /// Dense connection
    Dense,
    
    /// Custom connection type
    Custom(u8),
}

/// Properties of graph nodes
#[derive(Debug, Clone)]
pub struct NodeProperties {
    /// Node type
    pub node_type: NodeType,
    
    /// Node depth in graph
    pub depth: usize,
    
    /// Input degree
    pub in_degree: usize,
    
    /// Output degree
    pub out_degree: usize,
    
    /// Node attributes
    pub attributes: HashMap<String, String>,
}

/// Types of nodes in graph
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NodeType {
    /// Input node
    Input,
    
    /// Output node
    Output,
    
    /// Operation node
    Operation,
    
    /// Concat/merge node
    Merge,
    
    /// Split node
    Split,
}

/// Graph topology information
#[derive(Debug, Clone)]
pub struct GraphTopology {
    /// Strongly connected components
    pub scc: Vec<Vec<usize>>,
    
    /// Topological ordering
    pub topological_order: Vec<usize>,
    
    /// Longest paths
    pub longest_paths: HashMap<usize, usize>,
    
    /// Graph diameter
    pub diameter: usize,
    
    /// Clustering coefficient
    pub clustering_coefficient: f64,
}

/// Position information for operations
#[derive(Debug, Clone)]
pub struct Position {
    /// Layer index
    pub layer_index: usize,
    
    /// Block index within layer
    pub block_index: usize,
    
    /// Operation index within block
    pub operation_index: usize,
    
    /// Spatial coordinates (if applicable)
    pub coordinates: Option<(f64, f64)>,
}

/// Input/output specifications
#[derive(Debug, Clone)]
pub struct IOSpecifications {
    /// Input shapes
    pub input_shapes: Vec<Vec<usize>>,
    
    /// Output shapes
    pub output_shapes: Vec<Vec<usize>>,
    
    /// Data types
    pub data_types: Vec<String>,
    
    /// Input/output constraints
    pub constraints: Vec<IOConstraint>,
}

/// Input/output constraints
#[derive(Debug, Clone)]
pub struct IOConstraint {
    /// Constraint type
    pub constraint_type: String,
    
    /// Constraint parameters
    pub parameters: HashMap<String, f64>,
}

/// Architecture metadata
#[derive(Debug, Clone)]
pub struct ArchitectureMetadata {
    /// Creation timestamp
    pub created_at: u64,
    
    /// Last modified timestamp
    pub modified_at: u64,
    
    /// Architecture tags
    pub tags: Vec<String>,
    
    /// Performance estimates
    pub performance_estimates: HashMap<String, f64>,
    
    /// Complexity metrics
    pub complexity_metrics: ComplexityMetrics,
}

/// Complexity metrics for architectures
#[derive(Debug, Clone)]
pub struct ComplexityMetrics {
    /// Total parameters
    pub total_parameters: usize,
    
    /// FLOPS estimate
    pub flops: u64,
    
    /// Memory requirement
    pub memory_mb: f64,
    
    /// Graph complexity measures
    pub graph_complexity: f64,
    
    /// Architectural diversity
    pub diversity_score: f64,
}

/// Encoded architecture representation
#[derive(Debug, Clone)]
pub struct EncodedArchitecture<T: Float + Debug + Send + Sync + 'static> {
    /// Encoding type used
    pub encoding_type: EncodingType,
    
    /// Primary encoding (main representation)
    pub primary_encoding: Array2<T>,
    
    /// Secondary encodings (auxiliary representations)
    pub secondary_encodings: HashMap<String, Array2<T>>,
    
    /// Categorical features
    pub categorical_features: HashMap<String, Vec<usize>>,
    
    /// Continuous features
    pub continuous_features: HashMap<String, Array1<T>>,
    
    /// Sequence lengths (for variable-length encodings)
    pub sequence_lengths: Vec<usize>,
    
    /// Mask for padding (if applicable)
    pub padding_mask: Option<Array2<bool>>,
    
    /// Encoding metadata
    pub metadata: EncodingMetadata<T>,
}

/// Metadata for encodings
#[derive(Debug, Clone)]
pub struct EncodingMetadata<T: Float + Debug + Send + Sync + 'static> {
    /// Original architecture ID
    pub architecture_id: String,
    
    /// Encoding timestamp
    pub encoded_at: u64,
    
    /// Encoding quality metrics
    pub quality_metrics: EncodingQualityMetrics<T>,
    
    /// Reconstruction error
    pub reconstruction_error: Option<T>,
    
    /// Encoding version
    pub version: String,
}

/// Quality metrics for encodings
#[derive(Debug, Clone)]
pub struct EncodingQualityMetrics<T: Float + Debug + Send + Sync + 'static> {
    /// Information preservation score
    pub information_preservation: T,
    
    /// Compression ratio
    pub compression_ratio: T,
    
    /// Encoding stability
    pub stability: T,
    
    /// Semantic consistency
    pub semantic_consistency: T,
}

/// Normalization statistics
#[derive(Debug, Clone)]
pub struct NormalizationStats<T: Float + Debug + Send + Sync + 'static> {
    /// Mean values
    pub mean: Array1<T>,
    
    /// Standard deviations
    pub std: Array1<T>,
    
    /// Minimum values
    pub min: Array1<T>,
    
    /// Maximum values
    pub max: Array1<T>,
    
    /// Number of samples used for statistics
    pub sample_count: usize,
}

/// Sequential encoding strategy
#[derive(Debug)]
pub struct SequentialEncoder<T: Float + Debug + Send + Sync + 'static> {
    /// Maximum sequence length
    max_length: usize,
    
    /// Operation vocabulary
    vocabulary: HashMap<String, usize>,
    
    /// Embedding dimension
    embedding_dim: usize,
    
    /// Position encoding
    positional_encoding: Option<Array2<T>>,
}

/// Graph-based encoding strategy
#[derive(Debug)]
pub struct GraphEncoder<T: Float + Debug + Send + Sync + 'static> {
    /// Maximum number of nodes
    max_nodes: usize,
    
    /// Node feature dimension
    node_feature_dim: usize,
    
    /// Edge feature dimension
    edge_feature_dim: usize,
    
    /// Graph normalization method
    normalization: GraphNormalization,
}

/// Graph normalization methods
#[derive(Debug, Clone, Copy)]
pub enum GraphNormalization {
    /// No normalization
    None,
    
    /// Symmetric normalization
    Symmetric,
    
    /// Random walk normalization
    RandomWalk,
    
    /// Spectral normalization
    Spectral,
}

/// Tree-based encoding strategy
#[derive(Debug)]
pub struct TreeEncoder<T: Float + Debug + Send + Sync + 'static> {
    /// Maximum tree depth
    max_depth: usize,
    
    /// Maximum branching factor
    max_branching: usize,
    
    /// Node embedding dimension
    node_dim: usize,
    
    /// Tree traversal order
    traversal_order: TreeTraversalOrder,
}

/// Tree traversal orders
#[derive(Debug, Clone, Copy)]
pub enum TreeTraversalOrder {
    /// Depth-first search
    DepthFirst,
    
    /// Breadth-first search
    BreadthFirst,
    
    /// Pre-order traversal
    PreOrder,
    
    /// Post-order traversal
    PostOrder,
    
    /// In-order traversal
    InOrder,
}

/// Path-based encoding strategy
#[derive(Debug)]
pub struct PathEncoder<T: Float + Debug + Send + Sync + 'static> {
    /// Maximum path length
    max_path_length: usize,
    
    /// Number of paths to encode
    num_paths: usize,
    
    /// Path sampling strategy
    sampling_strategy: PathSamplingStrategy,
    
    /// Path embedding dimension
    path_dim: usize,
}

/// Path sampling strategies
#[derive(Debug, Clone, Copy)]
pub enum PathSamplingStrategy {
    /// Sample shortest paths
    Shortest,
    
    /// Sample longest paths
    Longest,
    
    /// Random path sampling
    Random,
    
    /// Important paths (based on centrality)
    Important,
    
    /// All simple paths
    AllSimple,
}

/// Continuous vector encoding strategy
#[derive(Debug)]
pub struct ContinuousEncoder<T: Float + Debug + Send + Sync + 'static> {
    /// Vector dimension
    vector_dim: usize,
    
    /// Encoding neural network
    encoder_network: Vec<Array2<T>>,
    
    /// Decoder neural network
    decoder_network: Vec<Array2<T>>,
    
    /// Variational encoding parameters
    variational_params: Option<VariationalParams<T>>,
}

/// Variational encoding parameters
#[derive(Debug, Clone)]
pub struct VariationalParams<T: Float + Debug + Send + Sync + 'static> {
    /// Latent dimension
    latent_dim: usize,
    
    /// KL divergence weight
    kl_weight: T,
    
    /// Reconstruction loss weight
    reconstruction_weight: T,
}

impl<T: Float + Debug + Default + Clone + Send + Sync + 'static> ArchitectureEncoder<T> {
    /// Create new architecture encoder
    pub fn new(config: ArchitectureEncodingConfig) -> Result<Self> {
        let mut encoder = Self {
            config: config.clone(),
            encodings: HashMap::new(),
            operation_to_index: HashMap::new(),
            index_to_operation: HashMap::new(),
            embedding_matrices: HashMap::new(),
            normalization_stats: HashMap::new(),
        };
        
        // Initialize operation vocabulary
        encoder.initialize_vocabulary()?;
        
        // Initialize encoding strategies
        encoder.initialize_encoders()?;
        
        // Initialize embedding matrices
        encoder.initialize_embeddings()?;
        
        Ok(encoder)
    }
    
    /// Initialize operation vocabulary
    fn initialize_vocabulary(&mut self) -> Result<()> {
        for (i, op) in self.config.operation_vocabulary.iter().enumerate() {
            self.operation_to_index.insert(op.clone(), i);
            self.index_to_operation.insert(i, op.clone());
        }
        Ok(())
    }
    
    /// Initialize encoding strategies
    fn initialize_encoders(&mut self) -> Result<()> {
        // Initialize sequential encoder
        if let Some(&dim) = self.config.embedding_dimensions.get(&EncodingType::Sequential) {
            let seq_encoder = SequentialEncoder::new(
                self.config.max_sequence_length,
                self.operation_to_index.clone(),
                dim,
                self.config.use_positional_encoding,
            )?;
            self.encodings.insert(EncodingType::Sequential, Box::new(seq_encoder));
        }
        
        // Initialize graph encoder
        if let Some(&dim) = self.config.embedding_dimensions.get(&EncodingType::Graph) {
            let graph_encoder = GraphEncoder::new(
                self.config.max_sequence_length, // max nodes
                dim,
                dim / 2, // edge feature dim
                GraphNormalization::Symmetric,
            )?;
            self.encodings.insert(EncodingType::Graph, Box::new(graph_encoder));
        }
        
        // Initialize other encoders...
        
        Ok(())
    }
    
    /// Initialize embedding matrices
    fn initialize_embeddings(&mut self) -> Result<()> {
        use scirs2_core::random::Rng;
        let mut rng = scirs2_core::random::thread_rng();
        
        for (&encoding_type, &dim) in &self.config.embedding_dimensions {
            let vocab_size = self.config.vocabulary_size;
            let mut embedding = Array2::zeros((vocab_size, dim));
            
            // Initialize with random values
            for i in 0..vocab_size {
                for j in 0..dim {
                    embedding[[i, j]] = T::from(rng.gen_range(-0.1..0.1)).expect("unwrap failed");
                }
            }
            
            self.embedding_matrices.insert(encoding_type, embedding);
        }
        
        Ok(())
    }
    
    /// Encode architecture using specified encoding type
    pub fn encode(&self, architecture: &ArchitectureDefinition<T>, encoding_type: EncodingType) -> Result<EncodedArchitecture<T>> {
        if let Some(encoder) = self.encodings.get(&encoding_type) {
            let mut encoded = encoder.encode(architecture)?;
            
            // Apply normalization if configured
            if let NormalizationMethod::MinMax | NormalizationMethod::ZScore = self.config.normalization_method {
                self.apply_normalization(&mut encoded)?;
            }
            
            Ok(encoded)
        } else {
            Err(OptimError::UnsupportedOperation(
                format!("Encoding type {:?} not supported", encoding_type)
            ))
        }
    }
    
    /// Decode encoded architecture back to definition
    pub fn decode(&self, encoded: &EncodedArchitecture<T>) -> Result<ArchitectureDefinition<T>> {
        if let Some(encoder) = self.encodings.get(&encoded.encoding_type) {
            encoder.decode(encoded)
        } else {
            Err(OptimError::UnsupportedOperation(
                format!("Encoding type {:?} not supported", encoded.encoding_type)
            ))
        }
    }
    
    /// Encode architecture using multiple encoding types
    pub fn encode_multi(&self, architecture: &ArchitectureDefinition<T>, 
                       encoding_types: &[EncodingType]) -> Result<HashMap<EncodingType, EncodedArchitecture<T>>> {
        let mut results = HashMap::new();
        
        for &encoding_type in encoding_types {
            let encoded = self.encode(architecture, encoding_type)?;
            results.insert(encoding_type, encoded);
        }
        
        Ok(results)
    }
    
    /// Apply normalization to encoded representation
    fn apply_normalization(&self, encoded: &mut EncodedArchitecture<T>) -> Result<()> {
        match self.config.normalization_method {
            NormalizationMethod::MinMax => {
                self.apply_minmax_normalization(&mut encoded.primary_encoding)?;
            }
            NormalizationMethod::ZScore => {
                self.apply_zscore_normalization(&mut encoded.primary_encoding)?;
            }
            _ => {} // Other normalization methods not implemented
        }
        
        Ok(())
    }
    
    /// Apply min-max normalization
    fn apply_minmax_normalization(&self, data: &mut Array2<T>) -> Result<()> {
        for j in 0..data.ncols() {
            let mut col = data.column_mut(j);
            let min_val = col.iter().cloned().fold(scirs2_core::numeric::NumCast::from(f64::INFINITY).unwrap_or_else(|| T::zero()), T::min);
            let max_val = col.iter().cloned().fold(scirs2_core::numeric::NumCast::from(f64::NEG_INFINITY).unwrap_or_else(|| T::zero()), T::max);
            
            if max_val > min_val {
                let range = max_val - min_val;
                for val in col.iter_mut() {
                    *val = (*val - min_val) / range;
                }
            }
        }
        
        Ok(())
    }
    
    /// Apply z-score normalization
    fn apply_zscore_normalization(&self, data: &mut Array2<T>) -> Result<()> {
        for j in 0..data.ncols() {
            let mut col = data.column_mut(j);
            let n = T::from(col.len() as f64).expect("unwrap failed");
            
            // Compute mean
            let mean = col.iter().cloned().fold(T::zero(), |acc, x| acc + x) / n;
            
            // Compute standard deviation
            let variance = col.iter().map(|&x| (x - mean) * (x - mean)).fold(T::zero(), |acc, x| acc + x) / n;
            let std_dev = variance.sqrt();
            
            if std_dev > T::zero() {
                for val in col.iter_mut() {
                    *val = (*val - mean) / std_dev;
                }
            }
        }
        
        Ok(())
    }
    
    /// Compute similarity between two encoded architectures
    pub fn compute_similarity(&self, encoded1: &EncodedArchitecture<T>, 
                             encoded2: &EncodedArchitecture<T>) -> Result<T> {
        if encoded1.encoding_type != encoded2.encoding_type {
            return Err(OptimError::InvalidInput(
                "Cannot compute similarity between different encoding types".to_string()
            ));
        }
        
        // Cosine similarity
        let flat1 = encoded1.primary_encoding.as_slice().expect("unwrap failed");
        let flat2 = encoded2.primary_encoding.as_slice().expect("unwrap failed");
        
        if flat1.len() != flat2.len() {
            return Err(OptimError::InvalidInput(
                "Encoded architectures have different dimensions".to_string()
            ));
        }
        
        let dot_product = flat1.iter().zip(flat2.iter()).map(|(&a, &b)| a * b).fold(T::zero(), |acc, x| acc + x);
        let norm1 = flat1.iter().map(|&x| x * x).fold(T::zero(), |acc, x| acc + x).sqrt();
        let norm2 = flat2.iter().map(|&x| x * x).fold(T::zero(), |acc, x| acc + x).sqrt();
        
        if norm1 > T::zero() && norm2 > T::zero() {
            Ok(dot_product / (norm1 * norm2))
        } else {
            Ok(T::zero())
        }
    }
    
    /// Update normalization statistics
    pub fn update_normalization_stats(&mut self, encoded: &EncodedArchitecture<T>) -> Result<()> {
        let stats_key = format!("{:?}", encoded.encoding_type);
        
        // Update running statistics (simplified implementation)
        let stats = self.normalization_stats.entry(stats_key).or_insert_with(|| NormalizationStats {
            mean: Array1::zeros(encoded.primary_encoding.ncols()),
            std: Array1::ones(encoded.primary_encoding.ncols()),
            min: Array1::from_elem(encoded.primary_encoding.ncols(), scirs2_core::numeric::NumCast::from(f64::INFINITY).unwrap_or_else(|| T::zero())),
            max: Array1::from_elem(encoded.primary_encoding.ncols(), scirs2_core::numeric::NumCast::from(f64::NEG_INFINITY).unwrap_or_else(|| T::zero())),
            sample_count: 0,
        });
        
        // Update statistics
        for j in 0..encoded.primary_encoding.ncols() {
            let col = encoded.primary_encoding.column(j);
            let col_min = col.iter().cloned().fold(scirs2_core::numeric::NumCast::from(f64::INFINITY).unwrap_or_else(|| T::zero()), T::min);
            let col_max = col.iter().cloned().fold(scirs2_core::numeric::NumCast::from(f64::NEG_INFINITY).unwrap_or_else(|| T::zero()), T::max);
            
            stats.min[j] = stats.min[j].min(col_min);
            stats.max[j] = stats.max[j].max(col_max);
        }
        
        stats.sample_count += 1;
        
        Ok(())
    }
    
    /// Get supported encoding types
    pub fn get_supported_encoding_types(&self) -> Vec<EncodingType> {
        self.encodings.keys().copied().collect()
    }
    
    /// Get encoding dimensions
    pub fn get_encoding_dimensions(&self, encoding_type: EncodingType) -> Option<Vec<usize>> {
        self.encodings.get(&encoding_type).map(|encoder| encoder.get_dimensions())
    }
    
    /// Validate encoded architecture
    pub fn validate_encoding(&self, encoded: &EncodedArchitecture<T>) -> Result<bool> {
        if let Some(encoder) = self.encodings.get(&encoded.encoding_type) {
            encoder.validate(encoded)
        } else {
            Ok(false)
        }
    }
}

// Implementations for specific encoding strategies

impl<T: Float + Debug + Default + Clone + Send + Sync + 'static> SequentialEncoder<T> {
    pub fn new(max_length: usize, vocabulary: HashMap<String, usize>, 
              embedding_dim: usize, use_positional: bool) -> Result<Self> {
        let positional_encoding = if use_positional {
            Some(Self::create_positional_encoding(max_length, embedding_dim)?)
        } else {
            None
        };
        
        Ok(Self {
            max_length,
            vocabulary,
            embedding_dim,
            positional_encoding,
        })
    }
    
    fn create_positional_encoding(max_length: usize, embedding_dim: usize) -> Result<Array2<T>> {
        let mut pos_encoding = Array2::zeros((max_length, embedding_dim));
        
        for pos in 0..max_length {
            for i in 0..(embedding_dim / 2) {
                let angle = scirs2_core::numeric::NumCast::from(pos as f64).unwrap_or_else(|| T::zero()) / 
                           T::from(10000.0_f64.powf(2.0 * i as f64 / embedding_dim as f64)).expect("unwrap failed");
                pos_encoding[[pos, 2 * i]] = angle.sin();
                pos_encoding[[pos, 2 * i + 1]] = angle.cos();
            }
        }
        
        Ok(pos_encoding)
    }
}

impl<T: Float + Debug + Default + Clone + Send + Sync + 'static> EncodingStrategy<T> for SequentialEncoder<T> {
    fn encode(&self, architecture: &ArchitectureDefinition<T>) -> Result<EncodedArchitecture<T>> {
        let seq_len = architecture.operations.len().min(self.max_length);
        let mut encoding = Array2::zeros((seq_len, self.embedding_dim));
        
        // Encode operations as sequence
        for (i, operation) in architecture.operations.iter().enumerate().take(seq_len) {
            if let Some(&op_idx) = self.vocabulary.get(&operation.operation_type) {
                // Simple one-hot encoding (in practice would use learned embeddings)
                if op_idx < self.embedding_dim {
                    encoding[[i, op_idx]] = T::one();
                }
            }
        }
        
        // Add positional encoding if enabled
        if let Some(ref pos_enc) = self.positional_encoding {
            for i in 0..seq_len {
                for j in 0..self.embedding_dim.min(pos_enc.ncols()) {
                    encoding[[i, j]] = encoding[[i, j]] + pos_enc[[i, j]];
                }
            }
        }
        
        Ok(EncodedArchitecture {
            encoding_type: EncodingType::Sequential,
            primary_encoding: encoding,
            secondary_encodings: HashMap::new(),
            categorical_features: HashMap::new(),
            continuous_features: HashMap::new(),
            sequence_lengths: vec![seq_len],
            padding_mask: None,
            metadata: EncodingMetadata {
                architecture_id: architecture.id.clone(),
                encoded_at: std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_secs(),
                quality_metrics: EncodingQualityMetrics {
                    information_preservation: scirs2_core::numeric::NumCast::from(0.9).unwrap_or_else(|| T::zero()),
                    compression_ratio: T::from(seq_len as f64 / architecture.operations.len() as f64).expect("unwrap failed"),
                    stability: scirs2_core::numeric::NumCast::from(0.8).unwrap_or_else(|| T::zero()),
                    semantic_consistency: scirs2_core::numeric::NumCast::from(0.85).unwrap_or_else(|| T::zero()),
                },
                reconstruction_error: None,
                version: "1.0".to_string(),
            },
        })
    }
    
    fn decode(&self, encoded: &EncodedArchitecture<T>) -> Result<ArchitectureDefinition<T>> {
        // Simplified decoding - in practice would be more sophisticated
        let mut operations = Vec::new();
        
        for i in 0..encoded.primary_encoding.nrows() {
            // Find the operation with highest activation
            let row = encoded.primary_encoding.row(i);
            if let Some((max_idx, _)) = row.indexed_iter().max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)) {
                if let Some(op_name) = self.vocabulary.iter().find(|(_, &idx)| idx == max_idx).map(|(name, _)| name) {
                    operations.push(OperationSpec {
                        id: format!("op_{}", i),
                        operation_type: op_name.clone(),
                        parameters: HashMap::new(),
                        input_shapes: vec![vec![224, 224, 3]], // Default
                        output_shapes: vec![vec![224, 224, 3]], // Default
                        position: Position {
                            layer_index: i,
                            block_index: 0,
                            operation_index: 0,
                            coordinates: None,
                        },
                    });
                }
            }
        }
        
        Ok(ArchitectureDefinition {
            id: encoded.metadata.architecture_id.clone(),
            operations,
            connections: ConnectionGraph {
                adjacency_matrix: Array2::eye(operations.len()),
                edge_types: HashMap::new(),
                node_properties: HashMap::new(),
                topology: GraphTopology {
                    scc: vec![vec![0]],
                    topological_order: (0..operations.len()).collect(),
                    longest_paths: HashMap::new(),
                    diameter: operations.len(),
                    clustering_coefficient: 0.0,
                },
            },
            global_parameters: HashMap::new(),
            io_specs: IOSpecifications {
                input_shapes: vec![vec![224, 224, 3]],
                output_shapes: vec![vec![1000]],
                data_types: vec!["float32".to_string()],
                constraints: Vec::new(),
            },
            metadata: ArchitectureMetadata {
                created_at: encoded.metadata.encoded_at,
                modified_at: encoded.metadata.encoded_at,
                tags: Vec::new(),
                performance_estimates: HashMap::new(),
                complexity_metrics: ComplexityMetrics {
                    total_parameters: operations.len() * 1000, // Estimate
                    flops: operations.len() as u64 * 1000000, // Estimate
                    memory_mb: operations.len() as f64 * 10.0, // Estimate
                    graph_complexity: operations.len() as f64,
                    diversity_score: 0.5,
                },
            },
        })
    }
    
    fn get_dimensions(&self) -> Vec<usize> {
        vec![self.max_length, self.embedding_dim]
    }
    
    fn validate(&self, encoded: &EncodedArchitecture<T>) -> Result<bool> {
        Ok(encoded.primary_encoding.nrows() <= self.max_length &&
           encoded.primary_encoding.ncols() == self.embedding_dim)
    }
}

impl<T: Float + Debug + Default + Clone + Send + Sync + 'static> GraphEncoder<T> {
    pub fn new(max_nodes: usize, node_feature_dim: usize, 
              edge_feature_dim: usize, normalization: GraphNormalization) -> Result<Self> {
        Ok(Self {
            max_nodes,
            node_feature_dim,
            edge_feature_dim,
            normalization,
        })
    }
}

impl<T: Float + Debug + Default + Clone + Send + Sync + 'static> EncodingStrategy<T> for GraphEncoder<T> {
    fn encode(&self, architecture: &ArchitectureDefinition<T>) -> Result<EncodedArchitecture<T>> {
        let num_nodes = architecture.operations.len().min(self.max_nodes);
        let mut node_features = Array2::zeros((self.max_nodes, self.node_feature_dim));
        
        // Encode node features
        for (i, operation) in architecture.operations.iter().enumerate().take(num_nodes) {
            // Simple encoding - in practice would be more sophisticated
            node_features[[i, 0]] = scirs2_core::numeric::NumCast::from(i as f64).unwrap_or_else(|| T::zero()); // Node index
            node_features[[i, 1]] = T::from(operation.parameters.len() as f64).expect("unwrap failed"); // Parameter count
        }
        
        Ok(EncodedArchitecture {
            encoding_type: EncodingType::Graph,
            primary_encoding: node_features,
            secondary_encodings: {
                let mut secondary = HashMap::new();
                secondary.insert("adjacency_matrix".to_string(), 
                               architecture.connections.adjacency_matrix.mapv(|x| scirs2_core::numeric::NumCast::from(x).unwrap_or_else(|| T::zero())));
                secondary
            },
            categorical_features: HashMap::new(),
            continuous_features: HashMap::new(),
            sequence_lengths: vec![num_nodes],
            padding_mask: None,
            metadata: EncodingMetadata {
                architecture_id: architecture.id.clone(),
                encoded_at: std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_secs(),
                quality_metrics: EncodingQualityMetrics {
                    information_preservation: scirs2_core::numeric::NumCast::from(0.95).unwrap_or_else(|| T::zero()),
                    compression_ratio: T::from(num_nodes as f64 / architecture.operations.len() as f64).expect("unwrap failed"),
                    stability: scirs2_core::numeric::NumCast::from(0.9).unwrap_or_else(|| T::zero()),
                    semantic_consistency: scirs2_core::numeric::NumCast::from(0.9).unwrap_or_else(|| T::zero()),
                },
                reconstruction_error: None,
                version: "1.0".to_string(),
            },
        })
    }
    
    fn decode(&self, _encoded: &EncodedArchitecture<T>) -> Result<ArchitectureDefinition<T>> {
        // Simplified decoding implementation
        Err(OptimError::UnsupportedOperation("Graph decoding not implemented".to_string()))
    }
    
    fn get_dimensions(&self) -> Vec<usize> {
        vec![self.max_nodes, self.node_feature_dim]
    }
    
    fn validate(&self, encoded: &EncodedArchitecture<T>) -> Result<bool> {
        Ok(encoded.primary_encoding.nrows() <= self.max_nodes &&
           encoded.primary_encoding.ncols() == self.node_feature_dim)
    }
}

impl Default for ArchitectureEncodingConfig {
    fn default() -> Self {
        let mut embedding_dimensions = HashMap::new();
        embedding_dimensions.insert(EncodingType::Sequential, 128);
        embedding_dimensions.insert(EncodingType::Graph, 64);
        embedding_dimensions.insert(EncodingType::ContinuousVector, 256);
        
        Self {
            max_sequence_length: 50,
            vocabulary_size: 1000,
            embedding_dimensions,
            use_positional_encoding: true,
            normalization_method: NormalizationMethod::MinMax,
            operation_vocabulary: vec![
                "conv2d".to_string(),
                "dense".to_string(),
                "batch_norm".to_string(),
                "relu".to_string(),
                "max_pool".to_string(),
                "avg_pool".to_string(),
                "dropout".to_string(),
                "attention".to_string(),
                "lstm".to_string(),
                "gru".to_string(),
            ],
            max_graph_depth: 20,
        }
    }
}