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
// Optimizer ensemble management

use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::numeric::Float;
use std::fmt::Debug;
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};

use super::{
    AdvancedOptimizer, EnsembleStrategy, OptimizerSelectionAlgorithm, OptimizationContext,
    LandscapeFeatures, ResourceAllocation, MetaLearningGuidance, ResourceUtilization,
    OptimizerCapabilities,
};
use super::config::AdaptationType;
use super::resource_management::OptimizerResources;
use super::{ScalabilityInfo, ResourceRequirements, ComputationalComplexity, ConvergenceGuarantees};
use super::config::{ComplexityClass, ParallelScalability};
use crate::error::Result;

/// Optimizer ensemble manager
#[derive(Debug)]
pub struct OptimizerEnsemble<T: Float + Debug + Send + Sync + 'static> {
    /// Active optimizers
    optimizers: HashMap<String, Box<dyn AdvancedOptimizer<T>>>,

    /// Optimizer performance scores
    performance_scores: HashMap<String, T>,

    /// Ensemble weights
    ensemble_weights: HashMap<String, T>,

    /// Ensemble strategy
    ensemble_strategy: EnsembleStrategy,

    /// Selection algorithm
    selection_algorithm: OptimizerSelectionAlgorithm,

    /// Performance history for each optimizer
    performance_history: HashMap<String, VecDeque<T>>,

    /// Selection statistics
    selection_stats: SelectionStatistics<T>,

    /// Ensemble learning rate
    ensemble_learning_rate: T,

    /// Weight update mechanism
    weight_updater: WeightUpdateMechanism<T>,
}

impl<T: Float + Debug + std::fmt::Debug + Send + Sync + std::default::Default + 'static> OptimizerEnsemble<T> {
    /// Create new optimizer ensemble
    pub fn new() -> Result<Self> {
        Ok(Self {
            optimizers: HashMap::new(),
            performance_scores: HashMap::new(),
            ensemble_weights: HashMap::new(),
            ensemble_strategy: EnsembleStrategy::WeightedVoting,
            selection_algorithm: OptimizerSelectionAlgorithm::MultiArmedBandit,
            performance_history: HashMap::new(),
            selection_stats: SelectionStatistics::new(),
            ensemble_learning_rate: scirs2_core::numeric::NumCast::from(0.01).unwrap_or_else(|| T::zero()),
            weight_updater: WeightUpdateMechanism::new(),
        })
    }

    /// Register a new optimizer in the ensemble
    pub fn register_optimizer(&mut self, id: String, optimizer: Box<dyn AdvancedOptimizer<T>>) -> Result<()> {
        self.optimizers.insert(id.clone(), optimizer);
        self.performance_scores.insert(id.clone(), T::zero());
        self.ensemble_weights.insert(id.clone(), T::from(1.0 / self.optimizers.len() as f64).expect("unwrap failed"));
        self.performance_history.insert(id, VecDeque::new());
        Ok(())
    }

    /// Register default optimizers
    pub fn register_default_optimizers(&mut self) -> Result<()> {
        // Register various learned optimizers
        self.register_optimizer("adam".to_string(), Box::new(AdamOptimizer::new()?))?;
        self.register_optimizer("rmsprop".to_string(), Box::new(RMSpropOptimizer::new()?))?;
        self.register_optimizer("sgd_momentum".to_string(), Box::new(SGDMomentumOptimizer::new()?))?;
        self.register_optimizer("adagrad".to_string(), Box::new(AdagradOptimizer::new()?))?;
        self.register_optimizer("learned_lstm".to_string(), Box::new(LearnedLSTMOptimizer::new()?))?;
        self.register_optimizer("adaptive_gradient".to_string(), Box::new(AdaptiveGradientOptimizer::new()?))?;

        Ok(())
    }

    /// Select optimizers based on current context
    pub fn select_optimizers(
        &mut self,
        landscape_features: &LandscapeFeatures<T>,
        context: &OptimizationContext<T>,
    ) -> Result<Vec<String>> {
        match self.selection_algorithm {
            OptimizerSelectionAlgorithm::MultiArmedBandit => {
                self.select_with_multi_armed_bandit(landscape_features, context)
            }
            OptimizerSelectionAlgorithm::UpperConfidenceBound => {
                self.select_with_ucb(landscape_features, context)
            }
            OptimizerSelectionAlgorithm::ThompsonSampling => {
                self.select_with_thompson_sampling(landscape_features, context)
            }
            OptimizerSelectionAlgorithm::EpsilonGreedy => {
                self.select_with_epsilon_greedy(landscape_features, context)
            }
            OptimizerSelectionAlgorithm::Portfolio => {
                self.select_with_portfolio(landscape_features, context)
            }
            OptimizerSelectionAlgorithm::ContextualBandits => {
                self.select_with_contextual_bandits(landscape_features, context)
            }
        }
    }

    /// Execute parallel optimization with selected optimizers
    pub fn execute_parallel_optimization(
        &mut self,
        parameters: &Array1<T>,
        gradients: &Array1<T>,
        selected_optimizers: &[String],
        resource_allocation: &ResourceAllocation<T>,
        meta_learning_guidance: &MetaLearningGuidance<T>,
        context: &OptimizationContext<T>,
    ) -> Result<HashMap<String, Array1<T>>> {
        let mut results = HashMap::new();
        let start_time = Instant::now();

        // Prepare shared context for parallel execution
        let shared_context = Arc::new(context.clone());
        let shared_guidance = Arc::new(meta_learning_guidance.clone());

        // Execute optimizers in parallel
        let handles: Vec<_> = selected_optimizers
            .iter()
            .map(|optimizer_id| {
                let optimizer_id = optimizer_id.clone();
                let parameters = parameters.clone();
                let gradients = gradients.clone();
                let context = Arc::clone(&shared_context);
                let guidance = Arc::clone(&shared_guidance);
                let resources = resource_allocation.get_allocation(&optimizer_id).unwrap_or_default();

                thread::spawn(move || {
                    // This would be the actual parallel execution
                    // For now, we'll simulate it
                    let result = Self::execute_single_optimizer(
                        &optimizer_id,
                        &parameters,
                        &gradients,
                        &*context,
                        &*guidance,
                        &resources,
                    );
                    (optimizer_id, result)
                })
            })
            .collect();

        // Collect results
        for handle in handles {
            let (optimizer_id, result) = handle.join().map_err(|_| {
                crate::error::OptimError::Other("Thread join failed".to_string())
            })?;

            match result {
                Ok(optimization_result) => {
                    results.insert(optimizer_id.clone(), optimization_result);
                    // Update performance tracking
                    self.update_optimizer_performance(&optimizer_id, start_time.elapsed())?;
                }
                Err(e) => {
                    // Log error and continue with other optimizers
                    eprintln!("Optimizer {} failed: {:?}", optimizer_id, e);
                }
            }
        }

        // Update selection statistics
        self.selection_stats.update_with_results(&results, selected_optimizers);

        Ok(results)
    }

    /// Ensemble optimization results
    pub fn ensemble_results(
        &mut self,
        optimization_results: &HashMap<String, Array1<T>>,
        performance_predictions: &HashMap<String, T>,
        context: &OptimizationContext<T>,
    ) -> Result<Array1<T>> {
        match self.ensemble_strategy {
            EnsembleStrategy::WeightedVoting => {
                self.weighted_voting_ensemble(optimization_results, performance_predictions)
            }
            EnsembleStrategy::Stacking => {
                self.stacking_ensemble(optimization_results, context)
            }
            EnsembleStrategy::Boosting => {
                self.boosting_ensemble(optimization_results, context)
            }
            EnsembleStrategy::Bagging => {
                self.bagging_ensemble(optimization_results)
            }
            EnsembleStrategy::DynamicSelection => {
                self.dynamic_selection_ensemble(optimization_results, context)
            }
            EnsembleStrategy::MixtureOfExperts => {
                self.mixture_of_experts_ensemble(optimization_results, context)
            }
        }
    }

    /// Weighted voting ensemble
    fn weighted_voting_ensemble(
        &self,
        results: &HashMap<String, Array1<T>>,
        performance_predictions: &HashMap<String, T>,
    ) -> Result<Array1<T>> {
        if results.is_empty() {
            return Err(crate::error::OptimError::Other("No results to ensemble".to_string()));
        }

        // Get the dimensionality from any result
        let first_result = results.values().next().expect("unwrap failed");
        let mut ensemble_result = Array1::zeros(first_result.len());
        let mut total_weight = T::zero();

        // Combine results using weights based on performance predictions
        for (optimizer_id, result) in results {
            let weight = performance_predictions.get(optimizer_id)
                .cloned()
                .unwrap_or_else(|| self.ensemble_weights.get(optimizer_id).cloned().unwrap_or(T::one()));

            for i in 0..ensemble_result.len() {
                ensemble_result[i] = ensemble_result[i] + weight * result[i];
            }
            total_weight = total_weight + weight;
        }

        // Normalize by total weight
        if total_weight > T::zero() {
            for i in 0..ensemble_result.len() {
                ensemble_result[i] = ensemble_result[i] / total_weight;
            }
        }

        Ok(ensemble_result)
    }

    /// Stacking ensemble with meta-learner
    fn stacking_ensemble(
        &self,
        results: &HashMap<String, Array1<T>>,
        _context: &OptimizationContext<T>,
    ) -> Result<Array1<T>> {
        // Simplified stacking - in practice, this would use a trained meta-learner
        self.weighted_voting_ensemble(results, &HashMap::new())
    }

    /// Boosting ensemble
    fn boosting_ensemble(
        &self,
        results: &HashMap<String, Array1<T>>,
        _context: &OptimizationContext<T>,
    ) -> Result<Array1<T>> {
        // Simplified boosting implementation
        if results.is_empty() {
            return Err(crate::error::OptimError::Other("No results to ensemble".to_string()));
        }

        // Weight optimizers based on their recent performance
        let mut weighted_predictions = HashMap::new();
        for (optimizer_id, _) in results {
            let recent_performance = self.get_recent_performance(optimizer_id);
            weighted_predictions.insert(optimizer_id.clone(), recent_performance);
        }

        self.weighted_voting_ensemble(results, &weighted_predictions)
    }

    /// Bagging ensemble
    fn bagging_ensemble(&self, results: &HashMap<String, Array1<T>>) -> Result<Array1<T>> {
        // Simple average for bagging
        if results.is_empty() {
            return Err(crate::error::OptimError::Other("No results to ensemble".to_string()));
        }

        let first_result = results.values().next().expect("unwrap failed");
        let mut ensemble_result = Array1::zeros(first_result.len());
        let count = T::from(results.len()).expect("unwrap failed");

        for result in results.values() {
            for i in 0..ensemble_result.len() {
                ensemble_result[i] = ensemble_result[i] + result[i] / count;
            }
        }

        Ok(ensemble_result)
    }

    /// Dynamic selection ensemble
    fn dynamic_selection_ensemble(
        &self,
        results: &HashMap<String, Array1<T>>,
        context: &OptimizationContext<T>,
    ) -> Result<Array1<T>> {
        // Select best optimizer based on current context
        let best_optimizer = self.select_best_optimizer_for_context(context)?;

        if let Some(result) = results.get(&best_optimizer) {
            Ok(result.clone())
        } else {
            // Fallback to weighted voting
            self.weighted_voting_ensemble(results, &HashMap::new())
        }
    }

    /// Mixture of experts ensemble
    fn mixture_of_experts_ensemble(
        &self,
        results: &HashMap<String, Array1<T>>,
        context: &OptimizationContext<T>,
    ) -> Result<Array1<T>> {
        // Calculate gating weights based on context
        let gating_weights = self.calculate_gating_weights(context)?;
        self.weighted_voting_ensemble(results, &gating_weights)
    }

    /// Multi-armed bandit selection
    fn select_with_multi_armed_bandit(
        &mut self,
        _landscape_features: &LandscapeFeatures<T>,
        context: &OptimizationContext<T>,
    ) -> Result<Vec<String>> {
        let num_select = (context.computational_budget.available_cores).min(self.optimizers.len());
        let mut selected: Vec<String> = Vec::new();

        // Simple epsilon-greedy for MAB
        let epsilon = scirs2_core::numeric::NumCast::from(0.1).unwrap_or_else(|| T::zero());

        for _ in 0..num_select {
            if scirs2_core::random::random::<f64>() < epsilon.to_f64().expect("unwrap failed") {
                // Exploration: random selection
                let available: Vec<String> = self.optimizers.keys()
                    .filter(|k| !selected.contains(k))
                    .cloned()
                    .collect();
                if !available.is_empty() {
                    use scirs2_core::random::Rng;
                    let mut rng = scirs2_core::random::thread_rng();
                    let idx = rng.gen_range(0..available.len());
                    selected.push(available[idx].clone());
                }
            } else {
                // Exploitation: select best performing
                let best = self.performance_scores.iter()
                    .filter(|(k, _)| !selected.contains(k))
                    .max_by(|(_, a), (_, b)| a.partial_cmp(b).expect("unwrap failed"))
                    .map(|(k, _)| k.clone());

                if let Some(best_optimizer) = best {
                    selected.push(best_optimizer);
                }
            }
        }

        Ok(selected)
    }

    /// Upper Confidence Bound selection
    fn select_with_ucb(
        &mut self,
        _landscape_features: &LandscapeFeatures<T>,
        context: &OptimizationContext<T>,
    ) -> Result<Vec<String>> {
        let num_select = (context.computational_budget.available_cores).min(self.optimizers.len());
        let mut selected = Vec::new();
        let total_selections = self.selection_stats.total_selections as f64;

        for _ in 0..num_select {
            let mut best_optimizer = None;
            let mut best_ucb_value = T::neg_infinity();

            for (optimizer_id, &avg_reward) in &self.performance_scores {
                if selected.contains(optimizer_id) {
                    continue;
                }

                let selections = self.selection_stats.optimizer_selections.get(optimizer_id).cloned().unwrap_or(1) as f64;
                let confidence = T::from(2.0 * (total_selections.ln() / selections).sqrt()).expect("unwrap failed");
                let ucb_value = avg_reward + confidence;

                if ucb_value > best_ucb_value {
                    best_ucb_value = ucb_value;
                    best_optimizer = Some(optimizer_id.clone());
                }
            }

            if let Some(optimizer) = best_optimizer {
                selected.push(optimizer);
            }
        }

        Ok(selected)
    }

    /// Thompson sampling selection
    fn select_with_thompson_sampling(
        &mut self,
        _landscape_features: &LandscapeFeatures<T>,
        context: &OptimizationContext<T>,
    ) -> Result<Vec<String>> {
        let num_select = (context.computational_budget.available_cores).min(self.optimizers.len());
        let mut selected = Vec::new();

        // Simple Thompson sampling using Beta distributions
        for _ in 0..num_select {
            let mut best_optimizer = None;
            let mut best_sample = T::neg_infinity();

            for (optimizer_id, _) in &self.optimizers {
                if selected.contains(optimizer_id) {
                    continue;
                }

                // Sample from posterior (simplified)
                let alpha = self.selection_stats.successes.get(optimizer_id).cloned().unwrap_or(1) as f64;
                let beta = self.selection_stats.failures.get(optimizer_id).cloned().unwrap_or(1) as f64;

                // Simplified sampling (in practice, would use proper Beta distribution)
                let sample = T::from(alpha / (alpha + beta) + scirs2_core::random::random::<f64>() * 0.1).expect("unwrap failed");

                if sample > best_sample {
                    best_sample = sample;
                    best_optimizer = Some(optimizer_id.clone());
                }
            }

            if let Some(optimizer) = best_optimizer {
                selected.push(optimizer);
            }
        }

        Ok(selected)
    }

    /// Epsilon-greedy selection
    fn select_with_epsilon_greedy(
        &mut self,
        _landscape_features: &LandscapeFeatures<T>,
        context: &OptimizationContext<T>,
    ) -> Result<Vec<String>> {
        // This is similar to multi-armed bandit but with fixed epsilon
        self.select_with_multi_armed_bandit(_landscape_features, context)
    }

    /// Portfolio selection
    fn select_with_portfolio(
        &mut self,
        landscape_features: &LandscapeFeatures<T>,
        context: &OptimizationContext<T>,
    ) -> Result<Vec<String>> {
        // Select diverse optimizers based on their capabilities and landscape features
        let num_select = (context.computational_budget.available_cores).min(self.optimizers.len());
        let mut selected = Vec::new();

        // Prioritize diversity in optimizer selection
        let optimizer_capabilities: HashMap<String, f64> = self.optimizers.iter()
            .map(|(id, optimizer)| {
                let capabilities = optimizer.get_capabilities();
                let suitability = self.calculate_suitability_score(&capabilities, landscape_features, context);
                (id.clone(), suitability)
            })
            .collect();

        // Sort by suitability and select top performers
        let mut sorted_optimizers: Vec<_> = optimizer_capabilities.iter().collect();
        sorted_optimizers.sort_by(|a, b| b.1.partial_cmp(a.1).expect("unwrap failed"));

        for (optimizer_id, _) in sorted_optimizers.iter().take(num_select) {
            selected.push((*optimizer_id).clone());
        }

        Ok(selected)
    }

    /// Contextual bandits selection
    fn select_with_contextual_bandits(
        &mut self,
        landscape_features: &LandscapeFeatures<T>,
        context: &OptimizationContext<T>,
    ) -> Result<Vec<String>> {
        // Use landscape features as context for selection
        let context_vector = self.extract_context_features(landscape_features, context);

        // Select optimizers based on context-aware predictions
        let num_select = (context.computational_budget.available_cores).min(self.optimizers.len());
        let mut selected = Vec::new();

        for optimizer_id in self.optimizers.keys() {
            if selected.len() >= num_select {
                break;
            }

            let predicted_reward = self.predict_contextual_reward(optimizer_id, &context_vector);
            if predicted_reward > scirs2_core::numeric::NumCast::from(0.5).unwrap_or_else(|| T::zero()) { // Threshold
                selected.push(optimizer_id.clone());
            }
        }

        // Fill remaining slots with best performers if needed
        while selected.len() < num_select {
            let remaining: Vec<_> = self.optimizers.keys()
                .filter(|k| !selected.contains(k))
                .collect();

            if remaining.is_empty() {
                break;
            }

            let mut best_optimizer: Option<String> = None;
            let mut best_score = T::zero();

            for &optimizer_id in &remaining {
                if let Some(score) = self.performance_scores.get(optimizer_id) {
                    if *score > best_score {
                        best_score = *score;
                        best_optimizer = Some(optimizer_id.clone());
                    }
                }
            }

            if let Some(optimizer) = best_optimizer {
                selected.push(optimizer);
            } else {
                break;
            }
        }

        Ok(selected)
    }

    /// Helper methods
    fn execute_single_optimizer(
        optimizer_id: &str,
        parameters: &Array1<T>,
        gradients: &Array1<T>,
        context: &OptimizationContext<T>,
        _guidance: &MetaLearningGuidance<T>,
        _resources: &OptimizerResources<T>,
    ) -> Result<Array1<T>> {
        // Simplified optimization step - in practice, this would call the actual optimizer
        let learning_rate = scirs2_core::numeric::NumCast::from(0.01).unwrap_or_else(|| T::zero()); // Would be determined by the optimizer
        let mut result = parameters.clone();

        for i in 0..result.len() {
            result[i] = result[i] - learning_rate * gradients[i];
        }

        Ok(result)
    }

    fn update_optimizer_performance(&mut self, optimizer_id: &str, _elapsed: Duration) -> Result<()> {
        // Update performance score (simplified)
        let current_score = self.performance_scores.get(optimizer_id).cloned().unwrap_or(T::zero());
        let improvement = scirs2_core::numeric::NumCast::from(0.01).unwrap_or_else(|| T::zero()); // Would be calculated based on actual performance
        let new_score = current_score + improvement;

        self.performance_scores.insert(optimizer_id.to_string(), new_score);

        // Update performance history
        if let Some(history) = self.performance_history.get_mut(optimizer_id) {
            history.push_back(new_score);
            if history.len() > 100 {
                history.pop_front();
            }
        }

        Ok(())
    }

    fn get_recent_performance(&self, optimizer_id: &str) -> T {
        self.performance_history.get(optimizer_id)
            .and_then(|history| history.back())
            .cloned()
            .unwrap_or(T::zero())
    }

    fn select_best_optimizer_for_context(&self, _context: &OptimizationContext<T>) -> Result<String> {
        self.performance_scores.iter()
            .max_by(|(_, a), (_, b)| a.partial_cmp(b).expect("unwrap failed"))
            .map(|(k, _)| k.clone())
            .ok_or_else(|| crate::error::OptimError::Other("No optimizers available".to_string()))
    }

    fn calculate_gating_weights(&self, _context: &OptimizationContext<T>) -> Result<HashMap<String, T>> {
        // Simplified gating weights based on performance scores
        Ok(self.performance_scores.clone())
    }

    fn calculate_suitability_score(
        &self,
        _capabilities: &OptimizerCapabilities,
        _landscape_features: &LandscapeFeatures<T>,
        _context: &OptimizationContext<T>,
    ) -> f64 {
        // Simplified suitability calculation
        scirs2_core::random::random::<f64>()
    }

    fn extract_context_features(&self, _landscape_features: &LandscapeFeatures<T>, _context: &OptimizationContext<T>) -> Vec<T> {
        // Extract relevant features for contextual bandits
        vec![scirs2_core::numeric::NumCast::from(0.5).unwrap_or_else(|| T::zero()); 10] // Placeholder
    }

    fn predict_contextual_reward(&self, _optimizer_id: &str, _context_vector: &[T]) -> T {
        // Predict reward based on context (simplified)
        scirs2_core::numeric::NumCast::from(0.6).unwrap_or_else(|| T::zero())
    }

    /// Reset ensemble state
    pub fn reset(&mut self) -> Result<()> {
        self.performance_scores.clear();
        self.ensemble_weights.clear();
        self.performance_history.clear();
        self.selection_stats = SelectionStatistics::new();
        Ok(())
    }
}

/// Selection statistics for tracking optimizer performance
#[derive(Debug)]
pub struct SelectionStatistics<T: Float + Debug + Send + Sync + 'static> {
    /// Total number of selections
    pub total_selections: usize,
    /// Selections per optimizer
    pub optimizer_selections: HashMap<String, usize>,
    /// Success counts per optimizer
    pub successes: HashMap<String, usize>,
    /// Failure counts per optimizer
    pub failures: HashMap<String, usize>,
    /// Average rewards per optimizer
    pub average_rewards: HashMap<String, T>,
}

impl<T: Float + Debug + Send + Sync + 'static> SelectionStatistics<T> {
    pub fn new() -> Self {
        Self {
            total_selections: 0,
            optimizer_selections: HashMap::new(),
            successes: HashMap::new(),
            failures: HashMap::new(),
            average_rewards: HashMap::new(),
        }
    }

    pub fn update_with_results(&mut self, results: &HashMap<String, Array1<T>>, selected: &[String]) {
        self.total_selections += selected.len();

        for optimizer_id in selected {
            *self.optimizer_selections.entry(optimizer_id.clone()).or_insert(0) += 1;

            if results.contains_key(optimizer_id) {
                *self.successes.entry(optimizer_id.clone()).or_insert(0) += 1;
            } else {
                *self.failures.entry(optimizer_id.clone()).or_insert(0) += 1;
            }
        }
    }
}

/// Weight update mechanism for ensemble weights
#[derive(Debug)]
pub struct WeightUpdateMechanism<T: Float + Debug + Send + Sync + 'static> {
    /// Learning rate for weight updates
    learning_rate: T,
    /// Momentum factor
    momentum: T,
    /// Previous weight updates
    previous_updates: HashMap<String, T>,
}

impl<T: Float + Debug + Send + Sync + 'static> WeightUpdateMechanism<T> {
    pub fn new() -> Self {
        Self {
            learning_rate: scirs2_core::numeric::NumCast::from(0.01).unwrap_or_else(|| T::zero()),
            momentum: scirs2_core::numeric::NumCast::from(0.9).unwrap_or_else(|| T::zero()),
            previous_updates: HashMap::new(),
        }
    }

    pub fn update_weights(
        &mut self,
        current_weights: &mut HashMap<String, T>,
        performance_scores: &HashMap<String, T>,
    ) -> Result<()> {
        for (optimizer_id, current_weight) in current_weights.iter_mut() {
            if let Some(&score) = performance_scores.get(optimizer_id) {
                let gradient = score - *current_weight;
                let previous_update = self.previous_updates.get(optimizer_id).cloned().unwrap_or(T::zero());
                let update = self.learning_rate * gradient + self.momentum * previous_update;

                *current_weight = *current_weight + update;
                self.previous_updates.insert(optimizer_id.clone(), update);
            }
        }

        // Normalize weights
        let total_weight: T = current_weights.values().fold(T::zero(), |acc, &w| acc + w);
        if total_weight > T::zero() {
            for weight in current_weights.values_mut() {
                *weight = *weight / total_weight;
            }
        }

        Ok(())
    }
}

/// Ensemble optimization results
#[derive(Debug)]
pub struct EnsembleOptimizationResults<T: Float + Debug + Send + Sync + 'static> {
    pub updated_parameters: Array1<T>,
    pub performance_score: T,
    pub individual_results: HashMap<String, T>,
    pub adaptation_events: Vec<AdaptationEvent<T>>,
    pub resource_usage: ResourceUtilization<T>,
}

/// Adaptation event
#[derive(Debug, Clone)]
pub struct AdaptationEvent<T: Float + Debug + Send + Sync + 'static> {
    /// Event timestamp
    pub timestamp: std::time::SystemTime,
    /// Adaptation type
    pub adaptation_type: AdaptationType,
    /// Trigger that caused adaptation
    pub trigger: String,
    /// Performance before adaptation
    pub performance_before: T,
    /// Performance after adaptation
    pub performance_after: T,
    /// Adaptation cost
    pub adaptation_cost: T,
}

/// Supporting types and placeholder implementations


// Placeholder optimizer implementations
#[derive(Debug)]
pub struct AdamOptimizer<T: Float + Debug + Send + Sync + 'static> {
    learning_rate: T,
}

impl<T: Float + Debug + Send + Sync + 'static> AdamOptimizer<T> {
    pub fn new() -> Result<Self> {
        Ok(Self {
            learning_rate: scirs2_core::numeric::NumCast::from(0.001).unwrap_or_else(|| T::zero()),
        })
    }
}

impl<T: Float + Debug + std::fmt::Debug + Send + Sync + 'static> AdvancedOptimizer<T> for AdamOptimizer<T> {
    fn optimize_step_with_context(
        &mut self,
        parameters: &Array1<T>,
        gradients: &Array1<T>,
        _context: &OptimizationContext<T>,
    ) -> Result<Array1<T>> {
        let mut result = parameters.clone();
        for i in 0..result.len() {
            result[i] = result[i] - self.learning_rate * gradients[i];
        }
        Ok(result)
    }

    fn adapt_to_landscape(&mut self, _features: &LandscapeFeatures<T>) -> Result<()> {
        Ok(())
    }

    fn get_capabilities(&self) -> OptimizerCapabilities {
        OptimizerCapabilities {
            supported_problems: vec![],
            scalability: ScalabilityInfo {
                max_parameters: 1000,
                memory_complexity: ComplexityClass::Linear,
                time_complexity: ComplexityClass::Linear,
                parallel_scalability: ParallelScalability {
                    max_threads: 8,
                    efficiency: 0.8,
                    communication_overhead: 0.1,
                },
            },
            memory_requirements: ResourceRequirements {
                cpu_cores: 1,
                memory_mb: 1000,
                gpu_memory_mb: 0,
                network_bandwidth: 100,
            },
            computational_complexity: ComputationalComplexity {
                time_complexity: "O(n)".to_string(),
                space_complexity: "O(n)".to_string(),
                parallelism_factor: 0.8,
            },
            convergence_guarantees: ConvergenceGuarantees {
                guarantees_convergence: false,
                convergence_rate: None,
                conditions: vec![],
            },
        }
    }

    fn get_performance_score(&self) -> T {
        scirs2_core::numeric::NumCast::from(0.8).unwrap_or_else(|| T::zero())
    }

    fn clone_optimizer(&self) -> Box<dyn AdvancedOptimizer<T>> {
        Box::new(AdamOptimizer {
            learning_rate: self.learning_rate,
        })
    }
}

// Additional placeholder optimizers
macro_rules! impl_placeholder_optimizer {
    ($name:ident, $lr:expr) => {
        #[derive(Debug)]
        pub struct $name<T: Float + Debug + Send + Sync + 'static> {
            learning_rate: T,
        }

        impl<T: Float + Debug + Send + Sync + 'static> $name<T> {
            pub fn new() -> Result<Self> {
                Ok(Self {
                    learning_rate: scirs2_core::numeric::NumCast::from($lr).unwrap_or_else(|| T::zero()),
                })
            }
        }

        impl<T: Float + Debug + std::fmt::Debug + Send + Sync + 'static> AdvancedOptimizer<T> for $name<T> {
            fn optimize_step_with_context(
                &mut self,
                parameters: &Array1<T>,
                gradients: &Array1<T>,
                _context: &OptimizationContext<T>,
            ) -> Result<Array1<T>> {
                let mut result = parameters.clone();
                for i in 0..result.len() {
                    result[i] = result[i] - self.learning_rate * gradients[i];
                }
                Ok(result)
            }

            fn adapt_to_landscape(&mut self, _features: &LandscapeFeatures<T>) -> Result<()> {
                Ok(())
            }

            fn get_capabilities(&self) -> OptimizerCapabilities {
                OptimizerCapabilities {
                        supported_problems: vec![],
                        scalability: ScalabilityInfo {
                            max_parameters: 1000,
                            memory_complexity: ComplexityClass::Linear,
                            time_complexity: ComplexityClass::Linear,
                            parallel_scalability: ParallelScalability {
                    max_threads: 8,
                    efficiency: 0.8,
                    communication_overhead: 0.1,
                },
                        },
                        memory_requirements: ResourceRequirements {
                            cpu_cores: 1,
                            memory_mb: 1000,
                            gpu_memory_mb: 0,
                            network_bandwidth: 100,
                        },
                        computational_complexity: ComputationalComplexity {
                            time_complexity: "O(n)".to_string(),
                            space_complexity: "O(n)".to_string(),
                            parallelism_factor: 0.8,
                        },
                        convergence_guarantees: ConvergenceGuarantees {
                            guarantees_convergence: false,
                            convergence_rate: None,
                            conditions: vec![],
                        },
                }
            }

            fn get_performance_score(&self) -> T {
                scirs2_core::numeric::NumCast::from($lr).unwrap_or_else(|| T::zero())
            }

            fn clone_optimizer(&self) -> Box<dyn AdvancedOptimizer<T>> {
                Box::new($name {
                    learning_rate: self.learning_rate,
                })
            }
        }
    };
}

impl_placeholder_optimizer!(RMSpropOptimizer, 0.01);
impl_placeholder_optimizer!(SGDMomentumOptimizer, 0.1);
impl_placeholder_optimizer!(AdagradOptimizer, 0.01);
impl_placeholder_optimizer!(LearnedLSTMOptimizer, 0.001);
impl_placeholder_optimizer!(AdaptiveGradientOptimizer, 0.005);

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

    #[test]
    fn test_ensemble_creation() {
        let ensemble = OptimizerEnsemble::<f32>::new();
        assert!(ensemble.is_ok());
    }

    #[test]
    fn test_optimizer_registration() {
        let mut ensemble = OptimizerEnsemble::<f32>::new().expect("unwrap failed");
        let optimizer = Box::new(AdamOptimizer::new().expect("unwrap failed"));
        assert!(ensemble.register_optimizer("test".to_string(), optimizer).is_ok());
    }

    #[test]
    fn test_default_optimizers_registration() {
        let mut ensemble = OptimizerEnsemble::<f32>::new().expect("unwrap failed");
        assert!(ensemble.register_default_optimizers().is_ok());
        assert!(ensemble.optimizers.len() > 0);
    }
}