optirs-core 0.3.1

OptiRS core optimization algorithms and utilities
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
// Curriculum optimization for adaptive training
//
// This module provides curriculum learning capabilities including task difficulty progression,
// sample importance weighting, and adversarial training support.

use crate::error::{OptimError, Result};
use scirs2_core::ndarray::{Array, Dimension, ScalarOperand, Zip};
use scirs2_core::numeric::Float;
use std::collections::{HashMap, VecDeque};
use std::fmt::Debug;
use std::marker::PhantomData;

/// Curriculum learning strategy
#[derive(Debug, Clone)]
pub enum CurriculumStrategy {
    /// Linear difficulty progression
    Linear {
        /// Starting difficulty (0.0 to 1.0)
        start_difficulty: f64,
        /// Ending difficulty (0.0 to 1.0)
        end_difficulty: f64,
        /// Number of steps to reach end difficulty
        num_steps: usize,
    },
    /// Exponential difficulty progression
    Exponential {
        /// Starting difficulty (0.0 to 1.0)
        start_difficulty: f64,
        /// Ending difficulty (0.0 to 1.0)
        end_difficulty: f64,
        /// Growth rate
        growth_rate: f64,
    },
    /// Performance-based curriculum
    PerformanceBased {
        /// Threshold for advancing difficulty
        advance_threshold: f64,
        /// Threshold for reducing difficulty
        reduce_threshold: f64,
        /// Difficulty adjustment step size
        adjustment_step: f64,
        /// Window size for performance averaging
        window_size: usize,
    },
    /// Custom curriculum with predefined schedule
    Custom {
        /// Difficulty schedule (step -> difficulty)
        schedule: HashMap<usize, f64>,
        /// Default difficulty for unspecified steps
        default_difficulty: f64,
    },
}

/// Sample importance weighting strategy
#[derive(Debug, Clone)]
pub enum ImportanceWeightingStrategy {
    /// Uniform weighting (all samples equal)
    Uniform,
    /// Loss-based weighting (higher loss = higher weight)
    LossBased {
        /// Temperature parameter for softmax weighting
        temperature: f64,
        /// Minimum weight to avoid zero weights
        min_weight: f64,
    },
    /// Gradient norm based weighting
    GradientNormBased {
        /// Temperature parameter
        temperature: f64,
        /// Minimum weight
        min_weight: f64,
    },
    /// Uncertainty-based weighting
    UncertaintyBased {
        /// Temperature parameter
        temperature: f64,
        /// Minimum weight
        min_weight: f64,
    },
    /// Age-based weighting (older samples get higher weight)
    AgeBased {
        /// Decay factor for age
        decayfactor: f64,
    },
}

/// Adversarial training configuration
#[derive(Debug, Clone)]
pub struct AdversarialConfig<A: Float> {
    /// Adversarial perturbation magnitude
    pub epsilon: A,
    /// Number of adversarial steps
    pub num_steps: usize,
    /// Step size for adversarial perturbation
    pub step_size: A,
    /// Type of adversarial attack
    pub attack_type: AdversarialAttack,
    /// Regularization weight for adversarial loss
    pub adversarial_weight: A,
}

/// Types of adversarial attacks
#[derive(Debug, Clone, Copy)]
pub enum AdversarialAttack {
    /// Fast Gradient Sign Method (FGSM)
    FGSM,
    /// Projected Gradient Descent (PGD)
    PGD,
    /// Basic Iterative Method (BIM)
    BIM,
    /// Momentum Iterative Method (MIM)
    MIM,
}

/// Curriculum learning manager
#[derive(Debug)]
pub struct CurriculumManager<A: Float, D: Dimension> {
    /// Curriculum strategy
    strategy: CurriculumStrategy,
    /// Current difficulty level
    current_difficulty: f64,
    /// Current step count
    step_count: usize,
    /// Performance history
    performance_history: VecDeque<A>,
    /// Sample difficulty scores
    sample_difficulties: HashMap<usize, f64>,
    /// Importance weighting strategy
    importance_strategy: ImportanceWeightingStrategy,
    /// Sample weights
    sample_weights: HashMap<usize, A>,
    /// Adversarial training configuration
    adversarial_config: Option<AdversarialConfig<A>>,
    /// Phantom data for dimension
    _phantom: PhantomData<D>,
}

impl<A: Float + ScalarOperand + Debug, D: Dimension + Send + Sync> CurriculumManager<A, D> {
    /// Create a new curriculum manager
    pub fn new(
        strategy: CurriculumStrategy,
        importance_strategy: ImportanceWeightingStrategy,
    ) -> Self {
        let initial_difficulty = match &strategy {
            CurriculumStrategy::Linear {
                start_difficulty, ..
            } => *start_difficulty,
            CurriculumStrategy::Exponential {
                start_difficulty, ..
            } => *start_difficulty,
            CurriculumStrategy::PerformanceBased { .. } => 0.1, // Start easy
            CurriculumStrategy::Custom {
                default_difficulty, ..
            } => *default_difficulty,
        };

        Self {
            strategy,
            current_difficulty: initial_difficulty,
            step_count: 0,
            performance_history: VecDeque::new(),
            sample_difficulties: HashMap::new(),
            importance_strategy,
            sample_weights: HashMap::new(),
            adversarial_config: None,
            _phantom: PhantomData,
        }
    }

    /// Enable adversarial training
    pub fn enable_adversarial_training(&mut self, config: AdversarialConfig<A>) {
        self.adversarial_config = Some(config);
    }

    /// Disable adversarial training
    pub fn disable_adversarial_training(&mut self) {
        self.adversarial_config = None;
    }

    /// Update curriculum based on performance
    pub fn update_curriculum(&mut self, performance: A) -> Result<()> {
        self.performance_history.push_back(performance);
        self.step_count += 1;

        // Update difficulty based on strategy
        match &self.strategy {
            CurriculumStrategy::Linear {
                start_difficulty,
                end_difficulty,
                num_steps,
            } => {
                let progress = (self.step_count as f64) / (*num_steps as f64);
                let progress = progress.min(1.0);
                self.current_difficulty =
                    start_difficulty + progress * (end_difficulty - start_difficulty);
            }
            CurriculumStrategy::Exponential {
                start_difficulty,
                end_difficulty,
                growth_rate,
            } => {
                let progress = 1.0 - (-growth_rate * self.step_count as f64).exp();
                self.current_difficulty =
                    start_difficulty + progress * (end_difficulty - start_difficulty);
            }
            CurriculumStrategy::PerformanceBased {
                advance_threshold,
                reduce_threshold,
                adjustment_step,
                window_size,
            } => {
                if self.performance_history.len() >= *window_size {
                    // Keep only recent performance
                    while self.performance_history.len() > *window_size {
                        self.performance_history.pop_front();
                    }

                    // Calculate average performance
                    let avg_performance = self
                        .performance_history
                        .iter()
                        .fold(A::zero(), |acc, &perf| acc + perf)
                        / A::from(self.performance_history.len()).expect("unwrap failed");

                    let avg_perf_f64 = avg_performance.to_f64().unwrap_or(0.0);

                    // Adjust difficulty based on performance
                    if avg_perf_f64 > *advance_threshold {
                        self.current_difficulty =
                            (self.current_difficulty + adjustment_step).min(1.0);
                    } else if avg_perf_f64 < *reduce_threshold {
                        self.current_difficulty =
                            (self.current_difficulty - adjustment_step).max(0.0);
                    }
                }
            }
            CurriculumStrategy::Custom {
                schedule,
                default_difficulty,
            } => {
                self.current_difficulty = schedule
                    .get(&self.step_count)
                    .copied()
                    .unwrap_or(*default_difficulty);
            }
        }

        Ok(())
    }

    /// Set difficulty score for a sample
    pub fn set_sample_difficulty(&mut self, sampleid: usize, difficulty: f64) {
        self.sample_difficulties.insert(sampleid, difficulty);
    }

    /// Check if sample should be included based on current difficulty
    pub fn should_include_sample(&self, sampleid: usize) -> bool {
        if let Some(&sample_difficulty) = self.sample_difficulties.get(&sampleid) {
            sample_difficulty <= self.current_difficulty
        } else {
            true // Include unknown samples
        }
    }

    /// Get current difficulty level
    pub fn get_current_difficulty(&self) -> f64 {
        self.current_difficulty
    }

    /// Compute importance weights for samples
    pub fn compute_sample_weights(
        &mut self,
        sampleids: &[usize],
        losses: &[A],
        gradient_norms: Option<&[A]>,
        uncertainties: Option<&[A]>,
    ) -> Result<()> {
        if sampleids.len() != losses.len() {
            return Err(OptimError::DimensionMismatch(
                "Sample IDs and losses must have same length".to_string(),
            ));
        }

        match &self.importance_strategy {
            ImportanceWeightingStrategy::Uniform => {
                let uniform_weight = A::one();
                for &sampleid in sampleids {
                    self.sample_weights.insert(sampleid, uniform_weight);
                }
            }
            ImportanceWeightingStrategy::LossBased {
                temperature,
                min_weight,
            } => {
                self.compute_loss_based_weights(sampleids, losses, *temperature, *min_weight)?;
            }
            ImportanceWeightingStrategy::GradientNormBased {
                temperature,
                min_weight,
            } => {
                if let Some(grad_norms) = gradient_norms {
                    self.compute_gradient_norm_weights(
                        sampleids,
                        grad_norms,
                        *temperature,
                        *min_weight,
                    )?;
                } else {
                    // Fall back to uniform weights
                    for &sampleid in sampleids {
                        self.sample_weights.insert(sampleid, A::one());
                    }
                }
            }
            ImportanceWeightingStrategy::UncertaintyBased {
                temperature,
                min_weight,
            } => {
                if let Some(uncertainties_array) = uncertainties {
                    self.compute_uncertainty_weights(
                        sampleids,
                        uncertainties_array,
                        *temperature,
                        *min_weight,
                    )?;
                } else {
                    // Fall back to uniform weights
                    for &sampleid in sampleids {
                        self.sample_weights.insert(sampleid, A::one());
                    }
                }
            }
            ImportanceWeightingStrategy::AgeBased { decayfactor } => {
                self.compute_age_based_weights(sampleids, *decayfactor)?;
            }
        }

        Ok(())
    }

    /// Compute loss-based importance weights
    fn compute_loss_based_weights(
        &mut self,
        sampleids: &[usize],
        losses: &[A],
        temperature: f64,
        min_weight: f64,
    ) -> Result<()> {
        // Compute softmax weights based on losses
        let temp = A::from(temperature).expect("unwrap failed");
        let min_w = A::from(min_weight).expect("unwrap failed");

        // Find max loss for numerical stability
        let max_loss = losses.iter().fold(A::neg_infinity(), |a, &b| A::max(a, b));

        // Compute unnormalized weights
        let mut unnormalized_weights = Vec::new();
        for &loss in losses {
            let normalized_loss = (loss - max_loss) / temp;
            unnormalized_weights.push(A::exp(normalized_loss));
        }

        // Normalize weights
        let sum_weights: A = unnormalized_weights
            .iter()
            .fold(A::zero(), |acc, &w| acc + w);

        for (i, &sampleid) in sampleids.iter().enumerate() {
            let weight = A::max(min_w, unnormalized_weights[i] / sum_weights);
            self.sample_weights.insert(sampleid, weight);
        }

        Ok(())
    }

    /// Compute gradient norm based weights
    fn compute_gradient_norm_weights(
        &mut self,
        sampleids: &[usize],
        gradient_norms: &[A],
        temperature: f64,
        min_weight: f64,
    ) -> Result<()> {
        let temp = A::from(temperature).expect("unwrap failed");
        let min_w = A::from(min_weight).expect("unwrap failed");

        // Find max gradient norm for numerical stability
        let max_norm = gradient_norms
            .iter()
            .fold(A::neg_infinity(), |a, &b| A::max(a, b));

        // Compute softmax weights
        let mut unnormalized_weights = Vec::new();
        for &norm in gradient_norms {
            let normalized_norm = (norm - max_norm) / temp;
            unnormalized_weights.push(A::exp(normalized_norm));
        }

        let sum_weights: A = unnormalized_weights
            .iter()
            .fold(A::zero(), |acc, &w| acc + w);

        for (i, &sampleid) in sampleids.iter().enumerate() {
            let weight = A::max(min_w, unnormalized_weights[i] / sum_weights);
            self.sample_weights.insert(sampleid, weight);
        }

        Ok(())
    }

    /// Compute uncertainty-based weights
    fn compute_uncertainty_weights(
        &mut self,
        sampleids: &[usize],
        uncertainties: &[A],
        temperature: f64,
        min_weight: f64,
    ) -> Result<()> {
        let temp = A::from(temperature).expect("unwrap failed");
        let min_w = A::from(min_weight).expect("unwrap failed");

        // Find max uncertainty for numerical stability
        let max_uncertainty = uncertainties
            .iter()
            .fold(A::neg_infinity(), |a, &b| A::max(a, b));

        // Compute softmax weights (higher uncertainty = higher weight)
        let mut unnormalized_weights = Vec::new();
        for &uncertainty in uncertainties {
            let normalized_uncertainty = (uncertainty - max_uncertainty) / temp;
            unnormalized_weights.push(A::exp(normalized_uncertainty));
        }

        let sum_weights: A = unnormalized_weights
            .iter()
            .fold(A::zero(), |acc, &w| acc + w);

        for (i, &sampleid) in sampleids.iter().enumerate() {
            let weight = A::max(min_w, unnormalized_weights[i] / sum_weights);
            self.sample_weights.insert(sampleid, weight);
        }

        Ok(())
    }

    /// Compute age-based weights
    fn compute_age_based_weights(&mut self, sampleids: &[usize], decayfactor: f64) -> Result<()> {
        let decay = A::from(decayfactor).expect("unwrap failed");

        for &sampleid in sampleids {
            // Simple age-based weighting (older samples get exponentially higher weight)
            let age = A::from(self.step_count.saturating_sub(sampleid)).expect("unwrap failed");
            let weight = A::exp(decay * age);
            self.sample_weights.insert(sampleid, weight);
        }

        Ok(())
    }

    /// Get importance weight for a sample
    pub fn get_sample_weight(&self, sampleid: usize) -> A {
        self.sample_weights
            .get(&sampleid)
            .copied()
            .unwrap_or_else(|| A::one())
    }

    /// Generate adversarial examples
    pub fn generate_adversarial_examples(
        &self,
        inputs: &Array<A, D>,
        gradients: &Array<A, D>,
    ) -> Result<Array<A, D>> {
        if let Some(config) = &self.adversarial_config {
            match config.attack_type {
                AdversarialAttack::FGSM => self.fgsm_attack(inputs, gradients, config),
                AdversarialAttack::PGD => self.pgd_attack(inputs, gradients, config),
                AdversarialAttack::BIM => self.bim_attack(inputs, gradients, config),
                AdversarialAttack::MIM => self.mim_attack(inputs, gradients, config),
            }
        } else {
            Ok(inputs.clone()) // No adversarial training
        }
    }

    /// Fast Gradient Sign Method (FGSM)
    fn fgsm_attack(
        &self,
        inputs: &Array<A, D>,
        gradients: &Array<A, D>,
        config: &AdversarialConfig<A>,
    ) -> Result<Array<A, D>> {
        let mut adversarial = inputs.clone();

        // Sign of gradients
        let sign_gradients = gradients.mapv(|x| if x >= A::zero() { A::one() } else { -A::one() });

        // Add perturbation
        Zip::from(&mut adversarial)
            .and(&sign_gradients)
            .for_each(|x, &sign| {
                *x = *x + config.epsilon * sign;
            });

        Ok(adversarial)
    }

    /// Projected Gradient Descent (PGD)
    fn pgd_attack(
        &self,
        inputs: &Array<A, D>,
        gradients: &Array<A, D>,
        config: &AdversarialConfig<A>,
    ) -> Result<Array<A, D>> {
        let mut adversarial = inputs.clone();

        // Multiple PGD steps
        for _ in 0..config.num_steps {
            // Gradient step
            let sign_gradients =
                gradients.mapv(|x| if x >= A::zero() { A::one() } else { -A::one() });

            Zip::from(&mut adversarial)
                .and(&sign_gradients)
                .for_each(|x, &sign| {
                    *x = *x + config.step_size * sign;
                });

            // Project back to epsilon ball
            Zip::from(&mut adversarial)
                .and(inputs)
                .for_each(|adv, &orig| {
                    let diff = *adv - orig;
                    let clamped_diff = A::max(-config.epsilon, A::min(config.epsilon, diff));
                    *adv = orig + clamped_diff;
                });
        }

        Ok(adversarial)
    }

    /// Basic Iterative Method (BIM)
    fn bim_attack(
        &self,
        inputs: &Array<A, D>,
        gradients: &Array<A, D>,
        config: &AdversarialConfig<A>,
    ) -> Result<Array<A, D>> {
        // BIM is similar to PGD but with smaller steps
        let mut modified_config = config.clone();
        modified_config.step_size =
            config.epsilon / A::from(config.num_steps).expect("unwrap failed");

        self.pgd_attack(inputs, gradients, &modified_config)
    }

    /// Momentum Iterative Method (MIM)
    fn mim_attack(
        &self,
        inputs: &Array<A, D>,
        gradients: &Array<A, D>,
        config: &AdversarialConfig<A>,
    ) -> Result<Array<A, D>> {
        let mut adversarial = inputs.clone();
        let mut momentum = Array::zeros(inputs.raw_dim());
        let decayfactor = A::from(1.0).expect("unwrap failed"); // Momentum decay factor

        for _ in 0..config.num_steps {
            // Update momentum
            let grad_norm = gradients.mapv(|x| x * x).sum().sqrt();
            let normalized_gradients = if grad_norm > A::zero() {
                gradients.mapv(|x| x / grad_norm)
            } else {
                gradients.clone()
            };

            Zip::from(&mut momentum)
                .and(&normalized_gradients)
                .for_each(|m, &g| {
                    *m = decayfactor * *m + g;
                });

            // Apply momentum-based update
            let momentum_signs =
                momentum.mapv(|x| if x >= A::zero() { A::one() } else { -A::one() });

            Zip::from(&mut adversarial)
                .and(&momentum_signs)
                .for_each(|x, &sign| {
                    *x = *x + config.step_size * sign;
                });

            // Project back to epsilon ball
            Zip::from(&mut adversarial)
                .and(inputs)
                .for_each(|adv, &orig| {
                    let diff = *adv - orig;
                    let clamped_diff = A::max(-config.epsilon, A::min(config.epsilon, diff));
                    *adv = orig + clamped_diff;
                });
        }

        Ok(adversarial)
    }

    /// Get filtered samples based on current curriculum
    pub fn filter_samples(&self, sampleids: &[usize]) -> Vec<usize> {
        sampleids
            .iter()
            .copied()
            .filter(|&id| self.should_include_sample(id))
            .collect()
    }

    /// Get performance history
    pub fn get_performance_history(&self) -> &VecDeque<A> {
        &self.performance_history
    }

    /// Get step count
    pub fn step_count(&self) -> usize {
        self.step_count
    }

    /// Reset curriculum state
    pub fn reset(&mut self) {
        self.step_count = 0;
        self.performance_history.clear();
        self.sample_weights.clear();
        self.current_difficulty = match &self.strategy {
            CurriculumStrategy::Linear {
                start_difficulty, ..
            } => *start_difficulty,
            CurriculumStrategy::Exponential {
                start_difficulty, ..
            } => *start_difficulty,
            CurriculumStrategy::PerformanceBased { .. } => 0.1,
            CurriculumStrategy::Custom {
                default_difficulty, ..
            } => *default_difficulty,
        };
    }

    /// Export curriculum state for analysis
    pub fn export_state(&self) -> CurriculumState<A> {
        CurriculumState {
            current_difficulty: self.current_difficulty,
            step_count: self.step_count,
            performance_history: self.performance_history.clone(),
            sample_weights: self.sample_weights.clone(),
            has_adversarial: self.adversarial_config.is_some(),
        }
    }
}

/// Curriculum state for analysis and visualization
#[derive(Debug, Clone)]
pub struct CurriculumState<A: Float> {
    /// Current difficulty level
    pub current_difficulty: f64,
    /// Current step count
    pub step_count: usize,
    /// Performance history
    pub performance_history: VecDeque<A>,
    /// Sample weights
    pub sample_weights: HashMap<usize, A>,
    /// Whether adversarial training is enabled
    pub has_adversarial: bool,
}

/// Adaptive curriculum that automatically adjusts strategy
#[derive(Debug)]
pub struct AdaptiveCurriculum<A: Float, D: Dimension> {
    /// Collection of curriculum managers with different strategies
    curricula: Vec<CurriculumManager<A, D>>,
    /// Current active curriculum index
    active_curriculum: usize,
    /// Performance tracking for each curriculum
    curriculum_performance: Vec<VecDeque<A>>,
    /// Switch threshold for changing curriculum
    switchthreshold: A,
    /// Minimum steps before switching
    min_steps_before_switch: usize,
    /// Steps since last switch
    steps_since_switch: usize,
    /// Phantom data for dimension
    _phantom: PhantomData<D>,
}

impl<A: Float + ScalarOperand + Debug, D: Dimension + Send + Sync> AdaptiveCurriculum<A, D> {
    /// Create a new adaptive curriculum
    pub fn new(curricula: Vec<CurriculumManager<A, D>>, switchthreshold: A) -> Self {
        let num_curricula = curricula.len();
        Self {
            curricula,
            active_curriculum: 0,
            curriculum_performance: vec![VecDeque::new(); num_curricula],
            switchthreshold,
            min_steps_before_switch: 100,
            steps_since_switch: 0,
            _phantom: PhantomData,
        }
    }

    /// Update with performance and potentially switch curriculum
    pub fn update(&mut self, performance: A) -> Result<()> {
        // Update current curriculum
        self.curricula[self.active_curriculum].update_curriculum(performance)?;
        self.curriculum_performance[self.active_curriculum].push_back(performance);
        self.steps_since_switch += 1;

        // Consider switching if enough steps have passed
        if self.steps_since_switch >= self.min_steps_before_switch {
            self.consider_curriculum_switch()?;
        }

        Ok(())
    }

    /// Consider switching to a better performing curriculum
    fn consider_curriculum_switch(&mut self) -> Result<()> {
        let current_performance = self.get_average_performance(self.active_curriculum);
        let mut best_curriculum = self.active_curriculum;
        let mut best_performance = current_performance;

        // Find best performing curriculum
        for (i, _) in self.curricula.iter().enumerate() {
            if i != self.active_curriculum {
                let perf = self.get_average_performance(i);
                if perf > best_performance + self.switchthreshold {
                    best_performance = perf;
                    best_curriculum = i;
                }
            }
        }

        // Switch if a better curriculum is found
        if best_curriculum != self.active_curriculum {
            self.active_curriculum = best_curriculum;
            self.steps_since_switch = 0;
        }

        Ok(())
    }

    /// Get average performance for a curriculum
    fn get_average_performance(&self, curriculumidx: usize) -> A {
        let perf_history = &self.curriculum_performance[curriculumidx];
        if perf_history.is_empty() {
            A::zero()
        } else {
            let sum = perf_history.iter().fold(A::zero(), |acc, &perf| acc + perf);
            sum / A::from(perf_history.len()).expect("unwrap failed")
        }
    }

    /// Get active curriculum manager
    pub fn active_curriculum(&self) -> &CurriculumManager<A, D> {
        &self.curricula[self.active_curriculum]
    }

    /// Get mutable active curriculum manager
    pub fn active_curriculum_mut(&mut self) -> &mut CurriculumManager<A, D> {
        &mut self.curricula[self.active_curriculum]
    }

    /// Get active curriculum index
    pub fn active_curriculum_index(&self) -> usize {
        self.active_curriculum
    }

    /// Get performance comparison across curricula
    pub fn get_curriculum_comparison(&self) -> Vec<(usize, A)> {
        (0..self.curricula.len())
            .map(|i| (i, self.get_average_performance(i)))
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;
    use scirs2_core::ndarray::Array1;

    #[test]
    fn test_linear_curriculum() {
        let strategy = CurriculumStrategy::Linear {
            start_difficulty: 0.1,
            end_difficulty: 1.0,
            num_steps: 10,
        };

        let importance_strategy = ImportanceWeightingStrategy::Uniform;
        let mut curriculum =
            CurriculumManager::<f64, scirs2_core::ndarray::Ix1>::new(strategy, importance_strategy);

        // Test initial difficulty
        assert_relative_eq!(curriculum.get_current_difficulty(), 0.1, epsilon = 1e-6);

        // Update curriculum multiple times
        for _ in 0..5 {
            curriculum.update_curriculum(0.8).expect("unwrap failed");
        }

        // Difficulty should have increased
        assert!(curriculum.get_current_difficulty() > 0.1);
        assert!(curriculum.get_current_difficulty() <= 1.0);
    }

    #[test]
    fn test_performance_based_curriculum() {
        let strategy = CurriculumStrategy::PerformanceBased {
            advance_threshold: 0.8,
            reduce_threshold: 0.4,
            adjustment_step: 0.1,
            window_size: 3,
        };

        let importance_strategy = ImportanceWeightingStrategy::Uniform;
        let mut curriculum =
            CurriculumManager::<f64, scirs2_core::ndarray::Ix1>::new(strategy, importance_strategy);

        let initial_difficulty = curriculum.get_current_difficulty();

        // Simulate good performance (should increase difficulty)
        for _ in 0..5 {
            curriculum.update_curriculum(0.9).expect("unwrap failed");
        }

        assert!(curriculum.get_current_difficulty() > initial_difficulty);
    }

    #[test]
    fn test_sample_filtering() {
        let strategy = CurriculumStrategy::Linear {
            start_difficulty: 0.5,
            end_difficulty: 0.5,
            num_steps: 10,
        };

        let importance_strategy = ImportanceWeightingStrategy::Uniform;
        let mut curriculum =
            CurriculumManager::<f64, scirs2_core::ndarray::Ix1>::new(strategy, importance_strategy);

        // Set sample difficulties
        curriculum.set_sample_difficulty(1, 0.3); // Easy
        curriculum.set_sample_difficulty(2, 0.7); // Hard
        curriculum.set_sample_difficulty(3, 0.5); // Medium

        let sampleids = vec![1, 2, 3, 4]; // 4 has no difficulty set
        let filtered = curriculum.filter_samples(&sampleids);

        // Should include samples 1, 3, 4 (difficulty <= 0.5 or unknown)
        assert_eq!(filtered.len(), 3);
        assert!(filtered.contains(&1));
        assert!(filtered.contains(&3));
        assert!(filtered.contains(&4));
        assert!(!filtered.contains(&2));
    }

    #[test]
    fn test_loss_based_importance_weighting() {
        let strategy = CurriculumStrategy::Linear {
            start_difficulty: 0.5,
            end_difficulty: 0.5,
            num_steps: 10,
        };

        let importance_strategy = ImportanceWeightingStrategy::LossBased {
            temperature: 1.0,
            min_weight: 0.1,
        };

        let mut curriculum =
            CurriculumManager::<f64, scirs2_core::ndarray::Ix1>::new(strategy, importance_strategy);

        let sampleids = vec![1, 2, 3];
        let losses = vec![0.1, 1.0, 0.5]; // Low, high, medium loss

        curriculum
            .compute_sample_weights(&sampleids, &losses, None, None)
            .expect("unwrap failed");

        // Sample with highest loss should have highest weight
        let weight1 = curriculum.get_sample_weight(1);
        let weight2 = curriculum.get_sample_weight(2);
        let weight3 = curriculum.get_sample_weight(3);

        assert!(weight2 > weight3); // High loss > medium loss
        assert!(weight3 > weight1); // Medium loss > low loss
    }

    #[test]
    fn test_adversarial_config() {
        let strategy = CurriculumStrategy::Linear {
            start_difficulty: 0.5,
            end_difficulty: 0.5,
            num_steps: 10,
        };

        let importance_strategy = ImportanceWeightingStrategy::Uniform;
        let mut curriculum =
            CurriculumManager::<f64, scirs2_core::ndarray::Ix1>::new(strategy, importance_strategy);

        let adversarial_config = AdversarialConfig {
            epsilon: 0.1,
            num_steps: 5,
            step_size: 0.02,
            attack_type: AdversarialAttack::FGSM,
            adversarial_weight: 0.5,
        };

        curriculum.enable_adversarial_training(adversarial_config);

        let inputs = Array1::from_vec(vec![1.0, 2.0, 3.0]);
        let gradients = Array1::from_vec(vec![0.1, -0.2, 0.3]);

        let adversarial = curriculum
            .generate_adversarial_examples(&inputs, &gradients)
            .expect("unwrap failed");

        // Adversarial examples should be different from original
        assert_ne!(
            adversarial.as_slice().expect("unwrap failed"),
            inputs.as_slice().expect("unwrap failed")
        );

        // Check that perturbation is bounded
        for (orig, adv) in inputs.iter().zip(adversarial.iter()) {
            assert!((adv - orig).abs() <= 0.1 + 1e-6); // epsilon + small tolerance
        }
    }

    #[test]
    fn test_adaptive_curriculum() {
        let strategy1 = CurriculumStrategy::Linear {
            start_difficulty: 0.1,
            end_difficulty: 0.5,
            num_steps: 100,
        };

        let strategy2 = CurriculumStrategy::Linear {
            start_difficulty: 0.2,
            end_difficulty: 0.8,
            num_steps: 100,
        };

        let importance_strategy = ImportanceWeightingStrategy::Uniform;
        let curriculum1 = CurriculumManager::<f64, scirs2_core::ndarray::Ix1>::new(
            strategy1,
            importance_strategy.clone(),
        );
        let curriculum2 = CurriculumManager::<f64, scirs2_core::ndarray::Ix1>::new(
            strategy2,
            importance_strategy,
        );

        let mut adaptive = AdaptiveCurriculum::new(vec![curriculum1, curriculum2], 0.1);

        assert_eq!(adaptive.active_curriculum_index(), 0);

        // Update with some performance values
        for _ in 0..150 {
            adaptive.update(0.7).expect("unwrap failed");
        }

        // Should potentially have switched curriculum
        let comparison = adaptive.get_curriculum_comparison();
        assert_eq!(comparison.len(), 2);
    }

    #[test]
    fn test_curriculum_state_export() {
        let strategy = CurriculumStrategy::Linear {
            start_difficulty: 0.1,
            end_difficulty: 1.0,
            num_steps: 10,
        };

        let importance_strategy = ImportanceWeightingStrategy::Uniform;
        let mut curriculum =
            CurriculumManager::<f64, scirs2_core::ndarray::Ix1>::new(strategy, importance_strategy);

        curriculum.update_curriculum(0.8).expect("unwrap failed");
        let state = curriculum.export_state();

        assert_eq!(state.step_count, 1);
        assert_eq!(state.performance_history.len(), 1);
        assert!(!state.has_adversarial);
    }
}