quantrs2-anneal 0.1.3

Quantum annealing support for the QuantRS2 framework
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
//! Feature extraction system for meta-learning optimization

use std::collections::{BTreeMap, HashMap, VecDeque};
use std::time::Instant;

use super::config::{
    ActivationFunction, DimensionalityReduction, FeatureExtractionConfig, FeatureSelectionMethod,
    LayerType,
};
use crate::applications::ApplicationResult;
use crate::ising::IsingModel;

/// Problem features representation
#[derive(Debug, Clone)]
pub struct ProblemFeatures {
    /// Problem size (number of variables)
    pub size: usize,
    /// Graph density
    pub density: f64,
    /// Graph-based features
    pub graph_features: GraphFeatures,
    /// Statistical features
    pub statistical_features: StatisticalFeatures,
    /// Spectral features
    pub spectral_features: SpectralFeatures,
    /// Domain-specific features
    pub domain_features: HashMap<String, f64>,
}

/// Feature vector representation
#[derive(Debug, Clone)]
pub struct FeatureVector {
    /// Feature values
    pub values: Vec<f64>,
    /// Feature names
    pub names: Vec<String>,
    /// Normalization parameters
    pub normalization: Option<NormalizationParams>,
}

/// Normalization parameters
#[derive(Debug, Clone)]
pub struct NormalizationParams {
    /// Mean values
    pub mean: Vec<f64>,
    /// Standard deviation values
    pub std: Vec<f64>,
    /// Min values
    pub min: Vec<f64>,
    /// Max values
    pub max: Vec<f64>,
}

/// Distribution statistics
#[derive(Debug, Clone)]
pub struct DistributionStats {
    /// Mean value
    pub mean: f64,
    /// Standard deviation
    pub std_dev: f64,
    /// Minimum value
    pub min: f64,
    /// Maximum value
    pub max: f64,
    /// Skewness
    pub skewness: f64,
    /// Kurtosis
    pub kurtosis: f64,
}

impl Default for DistributionStats {
    fn default() -> Self {
        Self {
            mean: 0.0,
            std_dev: 1.0,
            min: 0.0,
            max: 1.0,
            skewness: 0.0,
            kurtosis: 3.0,
        }
    }
}

/// Graph-based features
#[derive(Debug, Clone)]
pub struct GraphFeatures {
    /// Number of vertices
    pub num_vertices: usize,
    /// Number of edges
    pub num_edges: usize,
    /// Average degree
    pub avg_degree: f64,
    /// Clustering coefficient
    pub clustering_coefficient: f64,
    /// Path length statistics
    pub path_length_stats: PathLengthStats,
    /// Centrality measures
    pub centrality_measures: CentralityMeasures,
}

impl Default for GraphFeatures {
    fn default() -> Self {
        Self {
            num_vertices: 0,
            num_edges: 0,
            avg_degree: 0.0,
            clustering_coefficient: 0.0,
            path_length_stats: PathLengthStats {
                avg_shortest_path: 0.0,
                diameter: 0,
                radius: 0,
                eccentricity_stats: DistributionStats::default(),
            },
            centrality_measures: CentralityMeasures {
                degree_centrality: DistributionStats::default(),
                betweenness_centrality: DistributionStats::default(),
                closeness_centrality: DistributionStats::default(),
                eigenvector_centrality: DistributionStats::default(),
            },
        }
    }
}

/// Path length statistics
#[derive(Debug, Clone)]
pub struct PathLengthStats {
    /// Average shortest path length
    pub avg_shortest_path: f64,
    /// Diameter
    pub diameter: usize,
    /// Radius
    pub radius: usize,
    /// Eccentricity statistics
    pub eccentricity_stats: DistributionStats,
}

/// Centrality measures
#[derive(Debug, Clone)]
pub struct CentralityMeasures {
    /// Degree centrality
    pub degree_centrality: DistributionStats,
    /// Betweenness centrality
    pub betweenness_centrality: DistributionStats,
    /// Closeness centrality
    pub closeness_centrality: DistributionStats,
    /// Eigenvector centrality
    pub eigenvector_centrality: DistributionStats,
}

/// Statistical features
#[derive(Debug, Clone)]
pub struct StatisticalFeatures {
    /// Bias statistics
    pub bias_stats: DistributionStats,
    /// Coupling statistics
    pub coupling_stats: DistributionStats,
    /// Energy landscape features
    pub energy_landscape: EnergyLandscapeFeatures,
    /// Correlation features
    pub correlation_features: CorrelationFeatures,
}

impl Default for StatisticalFeatures {
    fn default() -> Self {
        Self {
            bias_stats: DistributionStats::default(),
            coupling_stats: DistributionStats::default(),
            energy_landscape: EnergyLandscapeFeatures {
                local_minima_estimate: 0,
                energy_barriers: Vec::new(),
                ruggedness: 0.0,
                basin_sizes: DistributionStats::default(),
            },
            correlation_features: CorrelationFeatures {
                autocorrelation: Vec::new(),
                cross_correlation: HashMap::new(),
                mutual_information: 0.0,
            },
        }
    }
}

/// Energy landscape features
#[derive(Debug, Clone)]
pub struct EnergyLandscapeFeatures {
    /// Number of local minima estimate
    pub local_minima_estimate: usize,
    /// Energy barrier estimates
    pub energy_barriers: Vec<f64>,
    /// Landscape ruggedness
    pub ruggedness: f64,
    /// Basin size distribution
    pub basin_sizes: DistributionStats,
}

/// Correlation features
#[derive(Debug, Clone)]
pub struct CorrelationFeatures {
    /// Autocorrelation function
    pub autocorrelation: Vec<f64>,
    /// Cross-correlation features
    pub cross_correlation: HashMap<String, f64>,
    /// Mutual information
    pub mutual_information: f64,
}

/// Spectral features
#[derive(Debug, Clone)]
pub struct SpectralFeatures {
    /// Eigenvalue statistics
    pub eigenvalue_stats: DistributionStats,
    /// Spectral gap
    pub spectral_gap: f64,
    /// Spectral radius
    pub spectral_radius: f64,
    /// Trace
    pub trace: f64,
    /// Condition number
    pub condition_number: f64,
}

impl Default for SpectralFeatures {
    fn default() -> Self {
        Self {
            eigenvalue_stats: DistributionStats::default(),
            spectral_gap: 0.0,
            spectral_radius: 0.0,
            trace: 0.0,
            condition_number: 1.0,
        }
    }
}

/// Feature extraction system
pub struct FeatureExtractor {
    /// Configuration
    pub config: FeatureExtractionConfig,
    /// Feature transformers
    pub transformers: Vec<FeatureTransformer>,
    /// Feature selectors
    pub selectors: Vec<FeatureSelector>,
    /// Dimensionality reducers
    pub reducers: Vec<DimensionalityReducer>,
}

impl FeatureExtractor {
    #[must_use]
    pub const fn new(config: FeatureExtractionConfig) -> Self {
        Self {
            config,
            transformers: Vec::new(),
            selectors: Vec::new(),
            reducers: Vec::new(),
        }
    }

    pub fn extract_features(&mut self, problem: &IsingModel) -> ApplicationResult<ProblemFeatures> {
        let graph_features = if self.config.enable_graph_features {
            self.extract_graph_features(problem)?
        } else {
            GraphFeatures::default()
        };

        let statistical_features = if self.config.enable_statistical_features {
            self.extract_statistical_features(problem)?
        } else {
            StatisticalFeatures::default()
        };

        let spectral_features = if self.config.enable_spectral_features {
            self.extract_spectral_features(problem)?
        } else {
            SpectralFeatures::default()
        };

        Ok(ProblemFeatures {
            size: problem.num_qubits,
            density: self.calculate_density(problem),
            graph_features,
            statistical_features,
            spectral_features,
            domain_features: HashMap::new(),
        })
    }

    fn extract_graph_features(&self, problem: &IsingModel) -> ApplicationResult<GraphFeatures> {
        let num_vertices = problem.num_qubits;
        let mut num_edges = 0;

        // Count edges (non-zero couplings)
        for i in 0..problem.num_qubits {
            for j in (i + 1)..problem.num_qubits {
                if problem.get_coupling(i, j).unwrap_or(0.0).abs() > 1e-10 {
                    num_edges += 1;
                }
            }
        }

        let avg_degree = if num_vertices > 0 {
            2.0 * num_edges as f64 / num_vertices as f64
        } else {
            0.0
        };

        Ok(GraphFeatures {
            num_vertices,
            num_edges,
            avg_degree,
            clustering_coefficient: 0.1, // Simplified
            path_length_stats: PathLengthStats {
                avg_shortest_path: avg_degree.ln().max(1.0),
                diameter: num_vertices / 2,
                radius: num_vertices / 4,
                eccentricity_stats: DistributionStats::default(),
            },
            centrality_measures: CentralityMeasures {
                degree_centrality: DistributionStats::default(),
                betweenness_centrality: DistributionStats::default(),
                closeness_centrality: DistributionStats::default(),
                eigenvector_centrality: DistributionStats::default(),
            },
        })
    }

    fn extract_statistical_features(
        &self,
        problem: &IsingModel,
    ) -> ApplicationResult<StatisticalFeatures> {
        let mut bias_values = Vec::new();
        let mut coupling_values = Vec::new();

        // Collect bias values
        for i in 0..problem.num_qubits {
            bias_values.push(problem.get_bias(i).unwrap_or(0.0));
        }

        // Collect coupling values
        for i in 0..problem.num_qubits {
            for j in (i + 1)..problem.num_qubits {
                let coupling = problem.get_coupling(i, j).unwrap_or(0.0);
                if coupling.abs() > 1e-10 {
                    coupling_values.push(coupling);
                }
            }
        }

        Ok(StatisticalFeatures {
            bias_stats: self.calculate_distribution_stats(&bias_values),
            coupling_stats: self.calculate_distribution_stats(&coupling_values),
            energy_landscape: EnergyLandscapeFeatures {
                local_minima_estimate: (problem.num_qubits as f64).sqrt() as usize,
                energy_barriers: vec![1.0, 2.0, 3.0],
                ruggedness: 0.5,
                basin_sizes: DistributionStats::default(),
            },
            correlation_features: CorrelationFeatures {
                autocorrelation: vec![1.0, 0.8, 0.6, 0.4, 0.2],
                cross_correlation: HashMap::new(),
                mutual_information: 0.3,
            },
        })
    }

    fn extract_spectral_features(
        &self,
        problem: &IsingModel,
    ) -> ApplicationResult<SpectralFeatures> {
        // Simplified spectral analysis
        let n = problem.num_qubits as f64;
        let spectral_gap_estimate = 1.0 / n.sqrt();

        Ok(SpectralFeatures {
            eigenvalue_stats: DistributionStats {
                mean: 0.0,
                std_dev: 1.0,
                min: -n,
                max: n,
                skewness: 0.0,
                kurtosis: 3.0,
            },
            spectral_gap: spectral_gap_estimate,
            spectral_radius: n,
            trace: 0.0,
            condition_number: n,
        })
    }

    fn calculate_density(&self, problem: &IsingModel) -> f64 {
        let mut num_edges = 0;
        let max_edges = problem.num_qubits * (problem.num_qubits - 1) / 2;

        for i in 0..problem.num_qubits {
            for j in (i + 1)..problem.num_qubits {
                if problem.get_coupling(i, j).unwrap_or(0.0).abs() > 1e-10 {
                    num_edges += 1;
                }
            }
        }

        if max_edges > 0 {
            f64::from(num_edges) / max_edges as f64
        } else {
            0.0
        }
    }

    fn calculate_distribution_stats(&self, values: &[f64]) -> DistributionStats {
        if values.is_empty() {
            return DistributionStats::default();
        }

        let mean = values.iter().sum::<f64>() / values.len() as f64;
        let variance = values.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / values.len() as f64;
        let std_dev = variance.sqrt();
        let min = values.iter().fold(f64::INFINITY, |a, &b| a.min(b));
        let max = values.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));

        DistributionStats {
            mean,
            std_dev,
            min,
            max,
            skewness: 0.0, // Simplified
            kurtosis: 3.0, // Simplified
        }
    }

    #[must_use]
    pub fn vectorize_features(&self, features: &ProblemFeatures) -> FeatureVector {
        let mut values = Vec::new();
        let mut names = Vec::new();

        // Basic features
        values.push(features.size as f64);
        names.push("size".to_string());
        values.push(features.density);
        names.push("density".to_string());

        // Graph features
        if self.config.enable_graph_features {
            values.push(features.graph_features.num_vertices as f64);
            names.push("num_vertices".to_string());
            values.push(features.graph_features.num_edges as f64);
            names.push("num_edges".to_string());
            values.push(features.graph_features.avg_degree);
            names.push("avg_degree".to_string());
            values.push(features.graph_features.clustering_coefficient);
            names.push("clustering_coefficient".to_string());
        }

        // Statistical features
        if self.config.enable_statistical_features {
            values.push(features.statistical_features.bias_stats.mean);
            names.push("bias_mean".to_string());
            values.push(features.statistical_features.bias_stats.std_dev);
            names.push("bias_std".to_string());
            values.push(features.statistical_features.coupling_stats.mean);
            names.push("coupling_mean".to_string());
            values.push(features.statistical_features.coupling_stats.std_dev);
            names.push("coupling_std".to_string());
        }

        // Spectral features
        if self.config.enable_spectral_features {
            values.push(features.spectral_features.spectral_gap);
            names.push("spectral_gap".to_string());
            values.push(features.spectral_features.spectral_radius);
            names.push("spectral_radius".to_string());
            values.push(features.spectral_features.condition_number);
            names.push("condition_number".to_string());
        }

        FeatureVector {
            values,
            names,
            normalization: None,
        }
    }
}

/// Feature transformer
#[derive(Debug)]
pub struct FeatureTransformer {
    /// Transformer type
    pub transformer_type: TransformerType,
    /// Parameters
    pub parameters: HashMap<String, f64>,
    /// Fitted state
    pub is_fitted: bool,
}

/// Transformer types
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TransformerType {
    /// Polynomial features
    Polynomial,
    /// Interaction features
    Interaction,
    /// Logarithmic transform
    Logarithmic,
    /// Box-Cox transform
    BoxCox,
    /// Custom transform
    Custom(String),
}

/// Feature selector
#[derive(Debug)]
pub struct FeatureSelector {
    /// Selection method
    pub method: FeatureSelectionMethod,
    /// Selected features
    pub selected_features: Vec<usize>,
    /// Feature importance scores
    pub importance_scores: Vec<f64>,
}

/// Dimensionality reducer
#[derive(Debug)]
pub struct DimensionalityReducer {
    /// Reduction method
    pub method: DimensionalityReduction,
    /// Target dimensions
    pub target_dims: usize,
    /// Transformation matrix
    pub transformation_matrix: Option<Vec<Vec<f64>>>,
    /// Explained variance
    pub explained_variance: Vec<f64>,
}

/// Experience database for storing optimization experiences
pub struct ExperienceDatabase {
    /// Stored experiences
    pub experiences: VecDeque<OptimizationExperience>,
    /// Index for fast retrieval
    pub index: ExperienceIndex,
    /// Similarity cache
    pub similarity_cache: HashMap<String, Vec<(String, f64)>>,
    /// Statistics
    pub statistics: DatabaseStatistics,
}

/// Optimization experience record
#[derive(Debug, Clone)]
pub struct OptimizationExperience {
    /// Unique experience identifier
    pub id: String,
    /// Problem characteristics
    pub problem_features: ProblemFeatures,
    /// Configuration used
    pub configuration: OptimizationConfiguration,
    /// Results achieved
    pub results: OptimizationResults,
    /// Timestamp
    pub timestamp: Instant,
    /// Problem domain
    pub domain: ProblemDomain,
    /// Success metrics
    pub success_metrics: SuccessMetrics,
}

use std::time::Duration;

/// Optimization configuration
#[derive(Debug, Clone)]
pub struct OptimizationConfiguration {
    /// Algorithm used
    pub algorithm: AlgorithmType,
    /// Hyperparameters
    pub hyperparameters: HashMap<String, f64>,
    /// Architecture specification
    pub architecture: Option<ArchitectureSpec>,
    /// Resource allocation
    pub resources: ResourceAllocation,
}

/// Algorithm types
#[derive(Debug, Clone, PartialEq)]
pub enum AlgorithmType {
    /// Simulated annealing
    SimulatedAnnealing,
    /// Quantum annealing
    QuantumAnnealing,
    /// Tabu search
    TabuSearch,
    /// Genetic algorithm
    GeneticAlgorithm,
    /// Particle swarm optimization
    ParticleSwarm,
    /// Ant colony optimization
    AntColony,
    /// Variable neighborhood search
    VariableNeighborhood,
    /// Hybrid algorithm
    Hybrid(Vec<Self>),
}

/// Architecture specification
#[derive(Debug, Clone)]
pub struct ArchitectureSpec {
    /// Layer specifications
    pub layers: Vec<LayerSpec>,
    /// Connection pattern
    pub connections: ConnectionPattern,
    /// Optimization settings
    pub optimization: OptimizationSettings,
}

/// Layer specification
#[derive(Debug, Clone)]
pub struct LayerSpec {
    /// Layer type
    pub layer_type: LayerType,
    /// Input dimension
    pub input_dim: usize,
    /// Output dimension
    pub output_dim: usize,
    /// Activation function
    pub activation: ActivationFunction,
    /// Dropout rate
    pub dropout: f64,
    /// Additional parameters
    pub parameters: HashMap<String, f64>,
}

/// Connection patterns
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConnectionPattern {
    /// Sequential connections
    Sequential,
    /// Skip connections
    SkipConnections,
    /// Dense connections
    DenseConnections,
    /// Residual connections
    ResidualConnections,
    /// Custom pattern
    Custom(Vec<(usize, usize)>),
}

/// Optimization settings
#[derive(Debug, Clone)]
pub struct OptimizationSettings {
    /// Optimizer type
    pub optimizer: OptimizerType,
    /// Learning rate
    pub learning_rate: f64,
    /// Batch size
    pub batch_size: usize,
    /// Number of epochs
    pub epochs: usize,
    /// Regularization
    pub regularization: RegularizationConfig,
}

/// Optimizer types
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OptimizerType {
    SGD,
    Adam,
    AdamW,
    RMSprop,
    Adagrad,
    Adadelta,
    LBFGS,
}

/// Regularization configuration
#[derive(Debug, Clone)]
pub struct RegularizationConfig {
    /// L1 regularization weight
    pub l1_weight: f64,
    /// L2 regularization weight
    pub l2_weight: f64,
    /// Dropout rate
    pub dropout: f64,
    /// Batch normalization
    pub batch_norm: bool,
    /// Early stopping
    pub early_stopping: bool,
}

/// Resource allocation
#[derive(Debug, Clone)]
pub struct ResourceAllocation {
    /// CPU allocation
    pub cpu: f64,
    /// Memory allocation (MB)
    pub memory: usize,
    /// GPU allocation
    pub gpu: f64,
    /// Time allocation
    pub time: Duration,
}

/// Optimization results
#[derive(Debug, Clone)]
pub struct OptimizationResults {
    /// Final objective values
    pub objective_values: Vec<f64>,
    /// Execution time
    pub execution_time: Duration,
    /// Resource usage
    pub resource_usage: ResourceUsage,
    /// Convergence metrics
    pub convergence: ConvergenceMetrics,
    /// Solution quality metrics
    pub quality_metrics: QualityMetrics,
}

/// Resource usage tracking
#[derive(Debug, Clone)]
pub struct ResourceUsage {
    /// Peak CPU usage
    pub peak_cpu: f64,
    /// Peak memory usage (MB)
    pub peak_memory: usize,
    /// GPU utilization
    pub gpu_utilization: f64,
    /// Energy consumption
    pub energy_consumption: f64,
}

/// Convergence metrics
#[derive(Debug, Clone)]
pub struct ConvergenceMetrics {
    /// Number of iterations
    pub iterations: usize,
    /// Final convergence rate
    pub convergence_rate: f64,
    /// Plateau detection
    pub plateau_detected: bool,
    /// Convergence confidence
    pub confidence: f64,
}

/// Solution quality metrics
#[derive(Debug, Clone)]
pub struct QualityMetrics {
    /// Objective function value
    pub objective_value: f64,
    /// Constraint violation
    pub constraint_violation: f64,
    /// Robustness score
    pub robustness: f64,
    /// Diversity score
    pub diversity: f64,
}

/// Problem domains
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ProblemDomain {
    /// Combinatorial optimization
    Combinatorial,
    /// Portfolio optimization
    Portfolio,
    /// Scheduling
    Scheduling,
    /// Graph problems
    Graph,
    /// Machine learning
    MachineLearning,
    /// Physics simulation
    Physics,
    /// Chemistry
    Chemistry,
    /// Custom domain
    Custom(String),
}

/// Success metrics
#[derive(Debug, Clone)]
pub struct SuccessMetrics {
    /// Overall success score
    pub success_score: f64,
    /// Performance relative to baseline
    pub relative_performance: f64,
    /// User satisfaction score
    pub user_satisfaction: f64,
    /// Recommendation confidence
    pub recommendation_confidence: f64,
}

/// Experience indexing system
#[derive(Debug)]
pub struct ExperienceIndex {
    /// Domain-based index
    pub domain_index: HashMap<ProblemDomain, Vec<String>>,
    /// Size-based index
    pub size_index: BTreeMap<usize, Vec<String>>,
    /// Performance-based index
    pub performance_index: BTreeMap<String, Vec<String>>,
    /// Feature-based index
    pub feature_index: HashMap<String, Vec<String>>,
}

/// Database statistics
#[derive(Debug, Clone)]
pub struct DatabaseStatistics {
    /// Total experiences
    pub total_experiences: usize,
    /// Experiences per domain
    pub domain_distribution: HashMap<ProblemDomain, usize>,
    /// Average performance
    pub avg_performance: f64,
    /// Coverage statistics
    pub coverage_stats: CoverageStatistics,
}

/// Coverage statistics
#[derive(Debug, Clone)]
pub struct CoverageStatistics {
    /// Feature space coverage
    pub feature_coverage: f64,
    /// Problem size coverage
    pub size_coverage: (usize, usize),
    /// Domain coverage
    pub domain_coverage: f64,
    /// Performance range coverage
    pub performance_range: (f64, f64),
}

impl ExperienceDatabase {
    #[must_use]
    pub fn new() -> Self {
        Self {
            experiences: VecDeque::new(),
            index: ExperienceIndex {
                domain_index: HashMap::new(),
                size_index: BTreeMap::new(),
                performance_index: BTreeMap::new(),
                feature_index: HashMap::new(),
            },
            similarity_cache: HashMap::new(),
            statistics: DatabaseStatistics {
                total_experiences: 0,
                domain_distribution: HashMap::new(),
                avg_performance: 0.0,
                coverage_stats: CoverageStatistics {
                    feature_coverage: 0.0,
                    size_coverage: (0, 0),
                    domain_coverage: 0.0,
                    performance_range: (0.0, 1.0),
                },
            },
        }
    }

    pub fn add_experience(&mut self, experience: OptimizationExperience) {
        self.experiences.push_back(experience.clone());
        self.update_index(&experience);
        self.update_statistics();

        // Limit buffer size
        if self.experiences.len() > 10_000 {
            if let Some(removed) = self.experiences.pop_front() {
                self.remove_from_index(&removed);
            }
        }
    }

    fn update_index(&mut self, experience: &OptimizationExperience) {
        // Update domain index
        self.index
            .domain_index
            .entry(experience.domain.clone())
            .or_insert_with(Vec::new)
            .push(experience.id.clone());

        // Update size index
        self.index
            .size_index
            .entry(experience.problem_features.size)
            .or_insert_with(Vec::new)
            .push(experience.id.clone());
    }

    fn remove_from_index(&mut self, experience: &OptimizationExperience) {
        // Remove from domain index
        if let Some(ids) = self.index.domain_index.get_mut(&experience.domain) {
            ids.retain(|id| id != &experience.id);
        }

        // Remove from size index
        if let Some(ids) = self
            .index
            .size_index
            .get_mut(&experience.problem_features.size)
        {
            ids.retain(|id| id != &experience.id);
        }
    }

    fn update_statistics(&mut self) {
        self.statistics.total_experiences = self.experiences.len();

        if !self.experiences.is_empty() {
            let total_performance: f64 = self
                .experiences
                .iter()
                .map(|exp| exp.results.quality_metrics.objective_value)
                .sum();
            self.statistics.avg_performance = total_performance / self.experiences.len() as f64;
        }

        // Update domain distribution
        self.statistics.domain_distribution.clear();
        for experience in &self.experiences {
            *self
                .statistics
                .domain_distribution
                .entry(experience.domain.clone())
                .or_insert(0) += 1;
        }
    }

    pub fn find_similar_experiences(
        &self,
        features: &ProblemFeatures,
        limit: usize,
    ) -> ApplicationResult<Vec<OptimizationExperience>> {
        let mut similarities = Vec::new();

        for experience in &self.experiences {
            let similarity = self.calculate_similarity(features, &experience.problem_features);
            similarities.push((experience.clone(), similarity));
        }

        // Sort by similarity (descending)
        similarities.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        Ok(similarities
            .into_iter()
            .take(limit)
            .map(|(exp, _)| exp)
            .collect())
    }

    fn calculate_similarity(
        &self,
        features1: &ProblemFeatures,
        features2: &ProblemFeatures,
    ) -> f64 {
        // Simple similarity calculation based on size and density
        let size_diff = (features1.size as f64 - features2.size as f64).abs()
            / features1.size.max(features2.size) as f64;
        let density_diff = (features1.density - features2.density).abs();

        let size_similarity = 1.0 - size_diff;
        let density_similarity = 1.0 - density_diff;

        f64::midpoint(size_similarity, density_similarity)
    }
}

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

    #[test]
    fn test_feature_extractor_creation() {
        let config = FeatureExtractionConfig::default();
        let extractor = FeatureExtractor::new(config);
        assert!(extractor.config.enable_graph_features);
    }

    #[test]
    fn test_distribution_stats() {
        let stats = DistributionStats::default();
        assert_eq!(stats.mean, 0.0);
        assert_eq!(stats.std_dev, 1.0);
    }

    #[test]
    fn test_experience_database() {
        let db = ExperienceDatabase::new();
        assert_eq!(db.statistics.total_experiences, 0);
    }
}