optirs_core/streaming/adaptive_streaming/
meta_learning.rs

1// Meta-learning and experience management for adaptive streaming
2//
3// This module provides sophisticated meta-learning capabilities that learn from
4// optimization experiences to improve future adaptation decisions, including
5// experience replay, transfer learning, and adaptive strategy selection.
6
7use super::config::*;
8use super::optimizer::{Adaptation, AdaptationPriority, AdaptationType, StreamingDataPoint};
9use super::performance::{PerformanceSnapshot, PerformanceTracker};
10
11use scirs2_core::numeric::Float;
12use scirs2_core::random::{thread_rng, Rng};
13use serde::{Deserialize, Serialize};
14use std::collections::{HashMap, VecDeque};
15use std::time::{Duration, Instant};
16
17/// Type of meta-model used for decision making
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub enum MetaModelType {
20    NeuralNetwork,
21    LinearRegression,
22    RandomForest,
23    GradientBoosting,
24    SupportVectorMachine,
25}
26
27/// Meta-learning system for streaming optimization
28pub struct MetaLearner<A: Float + Send + Sync> {
29    /// Meta-learning configuration
30    config: MetaLearningConfig,
31    /// Experience buffer for learning
32    experience_buffer: ExperienceBuffer<A>,
33    /// Meta-model for decision making
34    meta_model: MetaModel<A>,
35    /// Strategy selection system
36    strategy_selector: StrategySelector<A>,
37    /// Transfer learning system
38    transfer_learning: TransferLearning<A>,
39    /// Meta-learning statistics
40    statistics: MetaLearningStatistics<A>,
41    /// Learning rate adaptation
42    learning_rate_adapter: LearningRateAdapter<A>,
43}
44
45/// Type alias for experience replay functionality
46pub type ExperienceReplay<A> = ExperienceBuffer<A>;
47
48/// Experience buffer for storing and managing learning experiences
49pub struct ExperienceBuffer<A: Float + Send + Sync> {
50    /// Buffer configuration
51    config: ExperienceReplayConfig,
52    /// Stored experiences
53    experiences: VecDeque<MetaExperience<A>>,
54    /// Priority queue for prioritized replay
55    priority_queue: VecDeque<(MetaExperience<A>, A)>,
56    /// Experience importance sampling
57    importance_weights: HashMap<usize, A>,
58    /// Experience diversity tracker
59    diversity_tracker: ExperienceDiversityTracker<A>,
60}
61
62/// Meta-learning experience representation
63#[derive(Debug, Clone)]
64pub struct MetaExperience<A: Float + Send + Sync> {
65    /// Unique experience ID
66    pub id: u64,
67    /// State when experience occurred
68    pub state: MetaState<A>,
69    /// Action taken
70    pub action: MetaAction<A>,
71    /// Reward received
72    pub reward: A,
73    /// Next state after action
74    pub next_state: Option<MetaState<A>>,
75    /// Experience timestamp
76    pub timestamp: Instant,
77    /// Episode context
78    pub episode_context: EpisodeContext<A>,
79    /// Experience priority for replay
80    pub priority: A,
81    /// Number of times replayed
82    pub replay_count: usize,
83}
84
85/// Meta-state representation
86#[derive(Debug, Clone)]
87pub struct MetaState<A: Float + Send + Sync> {
88    /// Performance metrics at this state
89    pub performance_metrics: Vec<A>,
90    /// Resource state
91    pub resource_state: Vec<A>,
92    /// Drift indicators
93    pub drift_indicators: Vec<A>,
94    /// Adaptation history length
95    pub adaptation_history: usize,
96    /// State timestamp
97    pub timestamp: Instant,
98}
99
100/// Meta-action representation
101#[derive(Debug, Clone)]
102pub struct MetaAction<A: Float + Send + Sync> {
103    /// Adaptation magnitudes applied
104    pub adaptation_magnitudes: Vec<A>,
105    /// Types of adaptations
106    pub adaptation_types: Vec<AdaptationType>,
107    /// Learning rate change
108    pub learning_rate_change: A,
109    /// Buffer size change
110    pub buffer_size_change: A,
111    /// Action timestamp
112    pub timestamp: Instant,
113}
114
115/// Episode context for meta-learning
116#[derive(Debug, Clone)]
117pub struct EpisodeContext<A: Float + Send + Sync> {
118    /// Episode ID
119    pub episode_id: u64,
120    /// Episode start time
121    pub start_time: Instant,
122    /// Episode duration
123    pub duration: Duration,
124    /// Initial performance
125    pub initial_performance: A,
126    /// Final performance
127    pub final_performance: A,
128    /// Number of adaptations in episode
129    pub adaptation_count: usize,
130    /// Episode outcome classification
131    pub outcome: EpisodeOutcome,
132}
133
134/// Episode outcome classifications
135#[derive(Debug, Clone, PartialEq, Eq)]
136pub enum EpisodeOutcome {
137    /// Significant improvement
138    Success,
139    /// Moderate improvement
140    PartialSuccess,
141    /// No significant change
142    Neutral,
143    /// Performance degradation
144    Failure,
145    /// Severe performance degradation
146    CriticalFailure,
147}
148
149/// Experience diversity tracking
150pub struct ExperienceDiversityTracker<A: Float + Send + Sync> {
151    /// State space clustering
152    state_clusters: Vec<StateCluster<A>>,
153    /// Action space clustering
154    action_clusters: Vec<ActionCluster<A>>,
155    /// Diversity metrics
156    diversity_metrics: DiversityMetrics<A>,
157    /// Novelty detection
158    novelty_detector: NoveltyDetector<A>,
159}
160
161/// State clustering for diversity
162#[derive(Debug, Clone)]
163pub struct StateCluster<A: Float + Send + Sync> {
164    /// Cluster center
165    pub center: MetaState<A>,
166    /// Cluster members
167    pub members: Vec<usize>,
168    /// Cluster radius
169    pub radius: A,
170    /// Last update time
171    pub last_update: Instant,
172}
173
174/// Action clustering for diversity
175#[derive(Debug, Clone)]
176pub struct ActionCluster<A: Float + Send + Sync> {
177    /// Cluster center
178    pub center: MetaAction<A>,
179    /// Cluster members
180    pub members: Vec<usize>,
181    /// Cluster effectiveness
182    pub effectiveness: A,
183    /// Usage frequency
184    pub usage_frequency: usize,
185}
186
187/// Diversity metrics for experience management
188#[derive(Debug, Clone)]
189pub struct DiversityMetrics<A: Float + Send + Sync> {
190    /// State space coverage
191    pub state_coverage: A,
192    /// Action space coverage
193    pub action_coverage: A,
194    /// Experience entropy
195    pub experience_entropy: A,
196    /// Temporal diversity
197    pub temporal_diversity: A,
198    /// Outcome diversity
199    pub outcome_diversity: A,
200}
201
202/// Novelty detection for new experiences
203pub struct NoveltyDetector<A: Float + Send + Sync> {
204    /// Reference experiences for comparison
205    reference_experiences: VecDeque<MetaExperience<A>>,
206    /// Novelty threshold
207    novelty_threshold: A,
208    /// Feature importance weights
209    feature_weights: Vec<A>,
210}
211
212/// Meta-model for decision making
213pub struct MetaModel<A: Float + Send + Sync> {
214    /// Model type
215    model_type: MetaModelType,
216    /// Model parameters
217    parameters: MetaModelParameters<A>,
218    /// Training history
219    training_history: VecDeque<TrainingEpisode<A>>,
220    /// Model performance metrics
221    performance_metrics: ModelPerformanceMetrics<A>,
222    /// Feature importance
223    feature_importance: Vec<A>,
224}
225
226/// Meta-model parameters
227#[derive(Debug, Clone)]
228pub struct MetaModelParameters<A: Float + Send + Sync> {
229    /// Weight matrices for neural networks
230    pub weights: Vec<Vec<A>>,
231    /// Bias vectors
232    pub biases: Vec<A>,
233    /// Learning rate
234    pub learning_rate: A,
235    /// Regularization parameters
236    pub regularization: RegularizationParams<A>,
237    /// Optimization parameters
238    pub optimization: OptimizationParams<A>,
239}
240
241/// Regularization parameters
242#[derive(Debug, Clone)]
243pub struct RegularizationParams<A: Float + Send + Sync> {
244    /// L1 regularization strength
245    pub l1_lambda: A,
246    /// L2 regularization strength
247    pub l2_lambda: A,
248    /// Dropout rate
249    pub dropout_rate: A,
250    /// Early stopping patience
251    pub early_stopping_patience: usize,
252}
253
254/// Optimization parameters
255#[derive(Debug, Clone)]
256pub struct OptimizationParams<A: Float + Send + Sync> {
257    /// Momentum coefficient
258    pub momentum: A,
259    /// Adam beta1 parameter
260    pub beta1: A,
261    /// Adam beta2 parameter
262    pub beta2: A,
263    /// Epsilon for numerical stability
264    pub epsilon: A,
265    /// Gradient clipping threshold
266    pub grad_clip_threshold: A,
267}
268
269/// Training episode for meta-model
270#[derive(Debug, Clone)]
271pub struct TrainingEpisode<A: Float + Send + Sync> {
272    /// Episode ID
273    pub episode_id: u64,
274    /// Training loss
275    pub training_loss: A,
276    /// Validation loss
277    pub validation_loss: A,
278    /// Training accuracy
279    pub training_accuracy: A,
280    /// Validation accuracy
281    pub validation_accuracy: A,
282    /// Episode duration
283    pub duration: Duration,
284    /// Timestamp
285    pub timestamp: Instant,
286}
287
288/// Model performance metrics
289#[derive(Debug, Clone)]
290pub struct ModelPerformanceMetrics<A: Float + Send + Sync> {
291    /// Prediction accuracy
292    pub prediction_accuracy: A,
293    /// Decision quality
294    pub decision_quality: A,
295    /// Adaptation effectiveness
296    pub adaptation_effectiveness: A,
297    /// Transfer learning success rate
298    pub transfer_success_rate: A,
299    /// Generalization performance
300    pub generalization_performance: A,
301}
302
303/// Strategy selection system
304pub struct StrategySelector<A: Float + Send + Sync> {
305    /// Available strategies
306    strategies: HashMap<String, AdaptationStrategy<A>>,
307    /// Strategy performance history
308    strategy_performance: HashMap<String, StrategyPerformance<A>>,
309    /// Strategy selection policy
310    selection_policy: SelectionPolicy,
311    /// Exploration parameters
312    exploration_params: ExplorationParams<A>,
313    /// Context-based selection
314    context_selector: ContextBasedSelector<A>,
315}
316
317/// Adaptation strategy representation
318#[derive(Debug, Clone)]
319pub struct AdaptationStrategy<A: Float + Send + Sync> {
320    /// Strategy name
321    pub name: String,
322    /// Strategy parameters
323    pub parameters: HashMap<String, A>,
324    /// Strategy type
325    pub strategy_type: StrategyType,
326    /// Applicability conditions
327    pub conditions: Vec<StrategyCondition<A>>,
328    /// Expected outcomes
329    pub expected_outcomes: Vec<A>,
330}
331
332/// Strategy types
333#[derive(Debug, Clone)]
334pub enum StrategyType {
335    /// Conservative strategy (small changes)
336    Conservative,
337    /// Aggressive strategy (large changes)
338    Aggressive,
339    /// Balanced strategy
340    Balanced,
341    /// Reactive strategy (responds to changes)
342    Reactive,
343    /// Proactive strategy (anticipates changes)
344    Proactive,
345    /// Custom strategy
346    Custom(String),
347}
348
349/// Strategy applicability conditions
350#[derive(Debug, Clone)]
351pub struct StrategyCondition<A: Float + Send + Sync> {
352    /// Condition type
353    pub condition_type: ConditionType,
354    /// Threshold value
355    pub threshold: A,
356    /// Operator for comparison
357    pub operator: ComparisonOperator,
358    /// Weight in decision making
359    pub weight: A,
360}
361
362/// Condition types for strategy selection
363#[derive(Debug, Clone)]
364pub enum ConditionType {
365    /// Performance threshold
366    Performance,
367    /// Resource utilization
368    ResourceUtilization,
369    /// Data quality
370    DataQuality,
371    /// Drift detection
372    DriftDetection,
373    /// Time-based condition
374    Temporal,
375    /// Custom condition
376    Custom(String),
377}
378
379/// Comparison operators
380#[derive(Debug, Clone)]
381pub enum ComparisonOperator {
382    /// Greater than
383    GreaterThan,
384    /// Less than
385    LessThan,
386    /// Equal to
387    EqualTo,
388    /// Between values
389    Between(f64, f64),
390    /// In set of values
391    InSet(Vec<f64>),
392}
393
394/// Strategy performance tracking
395#[derive(Debug, Clone)]
396pub struct StrategyPerformance<A: Float + Send + Sync> {
397    /// Number of times used
398    pub usage_count: usize,
399    /// Success rate
400    pub success_rate: A,
401    /// Average improvement
402    pub avg_improvement: A,
403    /// Best improvement achieved
404    pub best_improvement: A,
405    /// Worst outcome
406    pub worst_outcome: A,
407    /// Recent performance trend
408    pub recent_trend: TrendDirection,
409    /// Context-specific performance
410    pub context_performance: HashMap<String, A>,
411}
412
413/// Trend direction for strategy performance
414#[derive(Debug, Clone, PartialEq, Eq)]
415pub enum TrendDirection {
416    /// Improving trend
417    Improving,
418    /// Declining trend
419    Declining,
420    /// Stable trend
421    Stable,
422    /// Oscillating trend
423    Oscillating,
424}
425
426/// Strategy selection policies
427#[derive(Debug, Clone)]
428pub enum SelectionPolicy {
429    /// Epsilon-greedy selection
430    EpsilonGreedy { epsilon: f64 },
431    /// Upper Confidence Bound
432    UCB { confidence_parameter: f64 },
433    /// Thompson sampling
434    ThompsonSampling,
435    /// Softmax selection
436    Softmax { temperature: f64 },
437    /// Context-aware selection
438    ContextAware,
439    /// Multi-armed bandit
440    MultiArmedBandit,
441}
442
443/// Exploration parameters
444#[derive(Debug, Clone)]
445pub struct ExplorationParams<A: Float + Send + Sync> {
446    /// Exploration rate
447    pub exploration_rate: A,
448    /// Exploration decay
449    pub exploration_decay: A,
450    /// Minimum exploration rate
451    pub min_exploration_rate: A,
452    /// Curiosity bonus weight
453    pub curiosity_weight: A,
454    /// Novelty bonus weight
455    pub novelty_weight: A,
456}
457
458/// Context-based strategy selector
459pub struct ContextBasedSelector<A: Float + Send + Sync> {
460    /// Context features
461    context_features: Vec<ContextFeature<A>>,
462    /// Context clustering
463    context_clusters: Vec<ContextCluster<A>>,
464    /// Strategy mappings per context
465    context_strategies: HashMap<String, Vec<String>>,
466    /// Context recognition model
467    context_model: ContextModel<A>,
468}
469
470/// Context feature for strategy selection
471#[derive(Debug, Clone)]
472pub struct ContextFeature<A: Float + Send + Sync> {
473    /// Feature name
474    pub name: String,
475    /// Feature value
476    pub value: A,
477    /// Feature importance
478    pub importance: A,
479    /// Feature stability
480    pub stability: A,
481}
482
483/// Context clustering for similar situations
484#[derive(Debug, Clone)]
485pub struct ContextCluster<A: Float + Send + Sync> {
486    /// Cluster ID
487    pub id: String,
488    /// Cluster center features
489    pub center: Vec<A>,
490    /// Cluster radius
491    pub radius: A,
492    /// Associated strategies
493    pub strategies: Vec<String>,
494    /// Cluster performance
495    pub performance: A,
496}
497
498/// Context recognition model
499pub struct ContextModel<A: Float + Send + Sync> {
500    /// Model parameters
501    parameters: Vec<A>,
502    /// Feature weights
503    feature_weights: Vec<A>,
504    /// Classification threshold
505    threshold: A,
506    /// Model accuracy
507    accuracy: A,
508}
509
510/// Transfer learning system
511pub struct TransferLearning<A: Float + Send + Sync> {
512    /// Source domain experiences
513    source_experiences: HashMap<String, Vec<MetaExperience<A>>>,
514    /// Transfer learning strategies
515    transfer_strategies: Vec<TransferStrategy>,
516    /// Domain adaptation methods
517    domain_adaptation: DomainAdaptation<A>,
518    /// Transfer learning metrics
519    transfer_metrics: TransferMetrics<A>,
520}
521
522/// Transfer learning strategies
523#[derive(Debug, Clone)]
524pub enum TransferStrategy {
525    /// Direct parameter transfer
526    ParameterTransfer,
527    /// Feature transfer
528    FeatureTransfer,
529    /// Instance transfer
530    InstanceTransfer,
531    /// Relational transfer
532    RelationalTransfer,
533    /// Meta-transfer learning
534    MetaTransfer,
535}
536
537/// Domain adaptation methods
538pub struct DomainAdaptation<A: Float + Send + Sync> {
539    /// Source domain characteristics
540    source_characteristics: Vec<A>,
541    /// Target domain characteristics
542    target_characteristics: Vec<A>,
543    /// Adaptation weights
544    adaptation_weights: Vec<A>,
545    /// Domain similarity measure
546    domain_similarity: A,
547}
548
549/// Transfer learning metrics
550#[derive(Debug, Clone)]
551pub struct TransferMetrics<A: Float + Send + Sync> {
552    /// Transfer success rate
553    pub success_rate: A,
554    /// Improvement from transfer
555    pub improvement: A,
556    /// Transfer efficiency
557    pub efficiency: A,
558    /// Negative transfer incidents
559    pub negative_transfer_count: usize,
560}
561
562/// Learning rate adaptation system
563pub struct LearningRateAdapter<A: Float + Send + Sync> {
564    /// Current learning rate
565    current_rate: A,
566    /// Learning rate history
567    rate_history: VecDeque<A>,
568    /// Performance feedback
569    performance_feedback: VecDeque<A>,
570    /// Adaptation strategy
571    adaptation_strategy: LearningRateStrategy,
572    /// Rate bounds
573    min_rate: A,
574    max_rate: A,
575}
576
577/// Learning rate adaptation strategies
578#[derive(Debug, Clone)]
579pub enum LearningRateStrategy {
580    /// Fixed learning rate
581    Fixed,
582    /// Step decay
583    StepDecay { decay_factor: f64, step_size: usize },
584    /// Exponential decay
585    ExponentialDecay { decay_rate: f64 },
586    /// Performance-based adaptation
587    PerformanceBased,
588    /// Cyclical learning rates
589    Cyclical {
590        min_lr: f64,
591        max_lr: f64,
592        cycle_length: usize,
593    },
594    /// Adaptive learning rate (Adam-style)
595    Adaptive,
596}
597
598/// Meta-learning statistics
599#[derive(Debug, Clone)]
600pub struct MetaLearningStatistics<A: Float + Send + Sync> {
601    /// Total experiences collected
602    pub total_experiences: usize,
603    /// Model training episodes
604    pub training_episodes: usize,
605    /// Average reward per episode
606    pub avg_reward_per_episode: A,
607    /// Best episode reward
608    pub best_episode_reward: A,
609    /// Learning progress
610    pub learning_progress: A,
611    /// Strategy selection accuracy
612    pub strategy_selection_accuracy: A,
613    /// Transfer learning success rate
614    pub transfer_success_rate: A,
615    /// Experience replay effectiveness
616    pub replay_effectiveness: A,
617}
618
619impl<A: Float + Default + Clone + std::iter::Sum + Send + Sync + std::fmt::Debug> MetaLearner<A> {
620    /// Creates a new meta-learner
621    pub fn new(config: &StreamingConfig) -> Result<Self, String> {
622        let meta_config = config.meta_learning_config.clone();
623
624        let experience_buffer = ExperienceBuffer::new(&meta_config.replay_config);
625        let meta_model = MetaModel::new(meta_config.model_complexity.clone())?;
626        let strategy_selector = StrategySelector::new();
627        let transfer_learning = TransferLearning::new();
628        let learning_rate_adapter = LearningRateAdapter::new(meta_config.meta_learning_rate);
629
630        let statistics = MetaLearningStatistics {
631            total_experiences: 0,
632            training_episodes: 0,
633            avg_reward_per_episode: A::zero(),
634            best_episode_reward: A::zero(),
635            learning_progress: A::zero(),
636            strategy_selection_accuracy: A::zero(),
637            transfer_success_rate: A::zero(),
638            replay_effectiveness: A::zero(),
639        };
640
641        Ok(Self {
642            config: meta_config,
643            experience_buffer,
644            meta_model,
645            strategy_selector,
646            transfer_learning,
647            statistics,
648            learning_rate_adapter,
649        })
650    }
651
652    /// Updates the meta-learner with new experience
653    pub fn update_experience(
654        &mut self,
655        state: MetaState<A>,
656        action: MetaAction<A>,
657        reward: A,
658    ) -> Result<(), String> {
659        let experience = MetaExperience {
660            id: self.generate_experience_id(),
661            state,
662            action,
663            reward,
664            next_state: None, // Will be filled in next update
665            timestamp: Instant::now(),
666            episode_context: self.create_episode_context(reward)?,
667            priority: self.calculate_experience_priority(reward),
668            replay_count: 0,
669        };
670
671        // Add to experience buffer
672        self.experience_buffer.add_experience(experience)?;
673
674        // Update statistics
675        self.statistics.total_experiences += 1;
676
677        // Trigger learning if enough experiences collected
678        if self
679            .statistics
680            .total_experiences
681            .is_multiple_of(self.config.update_frequency)
682        {
683            self.trigger_learning()?;
684        }
685
686        Ok(())
687    }
688
689    /// Generates unique experience ID
690    fn generate_experience_id(&self) -> u64 {
691        self.statistics.total_experiences as u64 + 1
692    }
693
694    /// Creates episode context for experience
695    fn create_episode_context(&self, reward: A) -> Result<EpisodeContext<A>, String> {
696        let outcome = if reward > A::from(0.8).unwrap() {
697            EpisodeOutcome::Success
698        } else if reward > A::from(0.5).unwrap() {
699            EpisodeOutcome::PartialSuccess
700        } else if reward > A::from(0.2).unwrap() {
701            EpisodeOutcome::Neutral
702        } else if reward > A::from(-0.2).unwrap() {
703            EpisodeOutcome::Failure
704        } else {
705            EpisodeOutcome::CriticalFailure
706        };
707
708        Ok(EpisodeContext {
709            episode_id: self.statistics.training_episodes as u64,
710            start_time: Instant::now(),
711            duration: Duration::from_secs(60), // Simplified
712            initial_performance: A::from(0.5).unwrap(), // Simplified
713            final_performance: reward,
714            adaptation_count: 1, // Simplified
715            outcome,
716        })
717    }
718
719    /// Calculates priority for experience replay
720    fn calculate_experience_priority(&self, reward: A) -> A {
721        // Higher priority for experiences with extreme rewards (positive or negative)
722        let abs_reward = reward.abs();
723        if abs_reward > A::from(0.8).unwrap() {
724            A::from(1.0).unwrap()
725        } else {
726            abs_reward
727        }
728    }
729
730    /// Triggers meta-learning update
731    fn trigger_learning(&mut self) -> Result<(), String> {
732        // Sample experiences for training
733        let training_batch = self
734            .experience_buffer
735            .sample_batch(self.config.replay_config.batch_size)?;
736
737        // Train meta-model
738        self.meta_model.train_on_batch(&training_batch)?;
739
740        // Update strategy selection
741        self.strategy_selector
742            .update_from_experiences(&training_batch)?;
743
744        // Update statistics
745        self.statistics.training_episodes += 1;
746
747        Ok(())
748    }
749
750    /// Recommends adaptations based on current state
751    pub fn recommend_adaptations(
752        &mut self,
753        current_data: &[StreamingDataPoint<A>],
754        performance_tracker: &PerformanceTracker<A>,
755    ) -> Result<Vec<Adaptation<A>>, String> {
756        // Extract current meta-state
757        let current_state = self.extract_meta_state(current_data, performance_tracker)?;
758
759        // Use meta-model to predict best action
760        let predicted_action = self.meta_model.predict_action(&current_state)?;
761
762        // Select appropriate strategy
763        let strategy = self.strategy_selector.select_strategy(&current_state)?;
764
765        // Generate adaptations based on prediction and strategy
766        let adaptations =
767            self.generate_adaptations_from_prediction(&predicted_action, &strategy)?;
768
769        Ok(adaptations)
770    }
771
772    /// Extracts meta-state from current situation
773    fn extract_meta_state(
774        &self,
775        current_data: &[StreamingDataPoint<A>],
776        performance_tracker: &PerformanceTracker<A>,
777    ) -> Result<MetaState<A>, String> {
778        // Get recent performance
779        let recent_performance = performance_tracker.get_recent_performance(5);
780        let performance_metrics = if !recent_performance.is_empty() {
781            vec![
782                recent_performance[0].loss,
783                recent_performance[0].accuracy.unwrap_or(A::zero()),
784                recent_performance[0].convergence_rate.unwrap_or(A::zero()),
785            ]
786        } else {
787            vec![A::zero(), A::zero(), A::zero()]
788        };
789
790        // Extract resource state (simplified)
791        let resource_state = vec![A::from(0.5).unwrap(), A::from(0.3).unwrap()];
792
793        // Extract drift indicators (simplified)
794        let drift_indicators = vec![A::from(0.1).unwrap()];
795
796        Ok(MetaState {
797            performance_metrics,
798            resource_state,
799            drift_indicators,
800            adaptation_history: self.statistics.total_experiences,
801            timestamp: Instant::now(),
802        })
803    }
804
805    /// Generates adaptations from model prediction
806    fn generate_adaptations_from_prediction(
807        &self,
808        predicted_action: &MetaAction<A>,
809        _strategy: &AdaptationStrategy<A>,
810    ) -> Result<Vec<Adaptation<A>>, String> {
811        let mut adaptations = Vec::new();
812
813        // Generate adaptations based on predicted action
814        for (i, &magnitude) in predicted_action.adaptation_magnitudes.iter().enumerate() {
815            if magnitude.abs() > A::from(0.05).unwrap() {
816                // Minimum threshold
817                let adaptation_type = if i < predicted_action.adaptation_types.len() {
818                    predicted_action.adaptation_types[i].clone()
819                } else {
820                    AdaptationType::LearningRate // Default
821                };
822
823                let adaptation = Adaptation {
824                    adaptation_type,
825                    magnitude,
826                    target_component: "meta_learner".to_string(),
827                    parameters: std::collections::HashMap::new(),
828                    priority: if magnitude.abs() > A::from(0.3).unwrap() {
829                        AdaptationPriority::High
830                    } else {
831                        AdaptationPriority::Normal
832                    },
833                    timestamp: Instant::now(),
834                };
835
836                adaptations.push(adaptation);
837            }
838        }
839
840        Ok(adaptations)
841    }
842
843    /// Applies adaptation to meta-learning system
844    pub fn apply_adaptation(&mut self, adaptation: &Adaptation<A>) -> Result<(), String> {
845        match adaptation.adaptation_type {
846            AdaptationType::MetaLearning => {
847                // Adjust meta-learning parameters
848                let new_rate = self.learning_rate_adapter.current_rate + adaptation.magnitude;
849                self.learning_rate_adapter.update_rate(new_rate)?;
850            }
851            _ => {
852                // Handle other adaptation types
853            }
854        }
855
856        Ok(())
857    }
858
859    /// Gets meta-learning effectiveness score
860    pub fn get_effectiveness_score(&self) -> f32 {
861        self.statistics.learning_progress.to_f32().unwrap_or(0.0)
862    }
863
864    /// Gets diagnostic information
865    pub fn get_diagnostics(&self) -> MetaLearningDiagnostics {
866        MetaLearningDiagnostics {
867            total_experiences: self.statistics.total_experiences,
868            training_episodes: self.statistics.training_episodes,
869            current_learning_rate: self
870                .learning_rate_adapter
871                .current_rate
872                .to_f64()
873                .unwrap_or(0.0),
874            model_accuracy: self
875                .meta_model
876                .performance_metrics
877                .prediction_accuracy
878                .to_f64()
879                .unwrap_or(0.0),
880            strategy_count: self.strategy_selector.strategies.len(),
881            transfer_success_rate: self
882                .statistics
883                .transfer_success_rate
884                .to_f64()
885                .unwrap_or(0.0),
886        }
887    }
888}
889
890impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> ExperienceBuffer<A> {
891    fn new(config: &ExperienceReplayConfig) -> Self {
892        Self {
893            config: config.clone(),
894            experiences: VecDeque::with_capacity(10000),
895            priority_queue: VecDeque::new(),
896            importance_weights: HashMap::new(),
897            diversity_tracker: ExperienceDiversityTracker::new(),
898        }
899    }
900
901    fn add_experience(&mut self, experience: MetaExperience<A>) -> Result<(), String> {
902        // Add to main buffer
903        if self.experiences.len() >= 10000 {
904            self.experiences.pop_front();
905        }
906        self.experiences.push_back(experience.clone());
907
908        // Add to priority queue if using prioritized replay
909        if self.config.enable_prioritized_replay {
910            let priority = experience.priority;
911            self.priority_queue.push_back((experience, priority));
912
913            // Sort by priority
914            self.priority_queue
915                .make_contiguous()
916                .sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
917
918            // Limit priority queue size
919            if self.priority_queue.len() > 1000 {
920                self.priority_queue.pop_front();
921            }
922        }
923
924        Ok(())
925    }
926
927    fn sample_batch(&mut self, batch_size: usize) -> Result<Vec<MetaExperience<A>>, String> {
928        if self.experiences.is_empty() {
929            return Ok(Vec::new());
930        }
931
932        let mut batch = Vec::with_capacity(batch_size);
933
934        if self.config.enable_prioritized_replay && !self.priority_queue.is_empty() {
935            // Sample from priority queue
936            for _ in 0..batch_size.min(self.priority_queue.len()) {
937                if let Some((experience, _)) = self.priority_queue.pop_front() {
938                    batch.push(experience);
939                }
940            }
941        } else {
942            // Random sampling
943            for _ in 0..batch_size.min(self.experiences.len()) {
944                let idx = thread_rng().gen_range(0..self.experiences.len());
945                if let Some(experience) = self.experiences.get(idx) {
946                    batch.push(experience.clone());
947                }
948            }
949        }
950
951        Ok(batch)
952    }
953}
954
955impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> ExperienceDiversityTracker<A> {
956    fn new() -> Self {
957        Self {
958            state_clusters: Vec::new(),
959            action_clusters: Vec::new(),
960            diversity_metrics: DiversityMetrics {
961                state_coverage: A::zero(),
962                action_coverage: A::zero(),
963                experience_entropy: A::zero(),
964                temporal_diversity: A::zero(),
965                outcome_diversity: A::zero(),
966            },
967            novelty_detector: NoveltyDetector::new(),
968        }
969    }
970}
971
972impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> NoveltyDetector<A> {
973    fn new() -> Self {
974        Self {
975            reference_experiences: VecDeque::with_capacity(1000),
976            novelty_threshold: A::from(0.5).unwrap(),
977            feature_weights: Vec::new(),
978        }
979    }
980}
981
982impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> MetaModel<A> {
983    fn new(complexity: MetaModelComplexity) -> Result<Self, String> {
984        let parameters = match complexity {
985            MetaModelComplexity::Low => MetaModelParameters {
986                weights: vec![vec![A::from(0.1).unwrap(); 10]; 2],
987                biases: vec![A::zero(); 10],
988                learning_rate: A::from(0.01).unwrap(),
989                regularization: RegularizationParams {
990                    l1_lambda: A::from(0.001).unwrap(),
991                    l2_lambda: A::from(0.001).unwrap(),
992                    dropout_rate: A::from(0.1).unwrap(),
993                    early_stopping_patience: 10,
994                },
995                optimization: OptimizationParams {
996                    momentum: A::from(0.9).unwrap(),
997                    beta1: A::from(0.9).unwrap(),
998                    beta2: A::from(0.999).unwrap(),
999                    epsilon: A::from(1e-8).unwrap(),
1000                    grad_clip_threshold: A::from(1.0).unwrap(),
1001                },
1002            },
1003            _ => MetaModelParameters {
1004                weights: vec![vec![A::from(0.1).unwrap(); 50]; 3],
1005                biases: vec![A::zero(); 50],
1006                learning_rate: A::from(0.001).unwrap(),
1007                regularization: RegularizationParams {
1008                    l1_lambda: A::from(0.0001).unwrap(),
1009                    l2_lambda: A::from(0.0001).unwrap(),
1010                    dropout_rate: A::from(0.2).unwrap(),
1011                    early_stopping_patience: 20,
1012                },
1013                optimization: OptimizationParams {
1014                    momentum: A::from(0.9).unwrap(),
1015                    beta1: A::from(0.9).unwrap(),
1016                    beta2: A::from(0.999).unwrap(),
1017                    epsilon: A::from(1e-8).unwrap(),
1018                    grad_clip_threshold: A::from(1.0).unwrap(),
1019                },
1020            },
1021        };
1022
1023        Ok(Self {
1024            model_type: MetaModelType::NeuralNetwork,
1025            parameters,
1026            training_history: VecDeque::with_capacity(1000),
1027            performance_metrics: ModelPerformanceMetrics {
1028                prediction_accuracy: A::from(0.5).unwrap(),
1029                decision_quality: A::from(0.5).unwrap(),
1030                adaptation_effectiveness: A::from(0.5).unwrap(),
1031                transfer_success_rate: A::from(0.5).unwrap(),
1032                generalization_performance: A::from(0.5).unwrap(),
1033            },
1034            feature_importance: Vec::new(),
1035        })
1036    }
1037
1038    fn train_on_batch(&mut self, batch: &[MetaExperience<A>]) -> Result<(), String> {
1039        if batch.is_empty() {
1040            return Ok(());
1041        }
1042
1043        // Simplified training - in practice would implement proper neural network training
1044        let avg_reward = batch.iter().map(|e| e.reward).sum::<A>() / A::from(batch.len()).unwrap();
1045
1046        // Update learning rate based on performance
1047        if avg_reward > A::from(0.5).unwrap() {
1048            self.parameters.learning_rate = self.parameters.learning_rate * A::from(1.01).unwrap();
1049        } else {
1050            self.parameters.learning_rate = self.parameters.learning_rate * A::from(0.99).unwrap();
1051        }
1052
1053        // Update performance metrics
1054        self.performance_metrics.prediction_accuracy = avg_reward;
1055
1056        Ok(())
1057    }
1058
1059    fn predict_action(&self, state: &MetaState<A>) -> Result<MetaAction<A>, String> {
1060        // Simplified prediction - in practice would use trained neural network
1061        let action = MetaAction {
1062            adaptation_magnitudes: vec![A::from(0.1).unwrap(), A::from(-0.05).unwrap()],
1063            adaptation_types: vec![AdaptationType::LearningRate, AdaptationType::BufferSize],
1064            learning_rate_change: A::from(0.01).unwrap(),
1065            buffer_size_change: A::from(5.0).unwrap(),
1066            timestamp: Instant::now(),
1067        };
1068
1069        Ok(action)
1070    }
1071}
1072
1073impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> StrategySelector<A> {
1074    fn new() -> Self {
1075        let mut strategies = HashMap::new();
1076
1077        // Add default strategies
1078        strategies.insert(
1079            "conservative".to_string(),
1080            AdaptationStrategy {
1081                name: "conservative".to_string(),
1082                parameters: HashMap::new(),
1083                strategy_type: StrategyType::Conservative,
1084                conditions: Vec::new(),
1085                expected_outcomes: vec![A::from(0.05).unwrap()],
1086            },
1087        );
1088
1089        strategies.insert(
1090            "aggressive".to_string(),
1091            AdaptationStrategy {
1092                name: "aggressive".to_string(),
1093                parameters: HashMap::new(),
1094                strategy_type: StrategyType::Aggressive,
1095                conditions: Vec::new(),
1096                expected_outcomes: vec![A::from(0.2).unwrap()],
1097            },
1098        );
1099
1100        Self {
1101            strategies,
1102            strategy_performance: HashMap::new(),
1103            selection_policy: SelectionPolicy::EpsilonGreedy { epsilon: 0.1 },
1104            exploration_params: ExplorationParams {
1105                exploration_rate: A::from(0.1).unwrap(),
1106                exploration_decay: A::from(0.99).unwrap(),
1107                min_exploration_rate: A::from(0.01).unwrap(),
1108                curiosity_weight: A::from(0.1).unwrap(),
1109                novelty_weight: A::from(0.1).unwrap(),
1110            },
1111            context_selector: ContextBasedSelector::new(),
1112        }
1113    }
1114
1115    fn select_strategy(&self, _state: &MetaState<A>) -> Result<AdaptationStrategy<A>, String> {
1116        // Simple strategy selection - in practice would be more sophisticated
1117        if let Some(strategy) = self.strategies.get("balanced") {
1118            Ok(strategy.clone())
1119        } else if let Some(strategy) = self.strategies.values().next() {
1120            Ok(strategy.clone())
1121        } else {
1122            Err("No strategies available".to_string())
1123        }
1124    }
1125
1126    fn update_from_experiences(
1127        &mut self,
1128        _experiences: &[MetaExperience<A>],
1129    ) -> Result<(), String> {
1130        // Update strategy performance based on experiences
1131        Ok(())
1132    }
1133}
1134
1135impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> ContextBasedSelector<A> {
1136    fn new() -> Self {
1137        Self {
1138            context_features: Vec::new(),
1139            context_clusters: Vec::new(),
1140            context_strategies: HashMap::new(),
1141            context_model: ContextModel {
1142                parameters: Vec::new(),
1143                feature_weights: Vec::new(),
1144                threshold: A::from(0.5).unwrap(),
1145                accuracy: A::from(0.5).unwrap(),
1146            },
1147        }
1148    }
1149}
1150
1151impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> TransferLearning<A> {
1152    fn new() -> Self {
1153        Self {
1154            source_experiences: HashMap::new(),
1155            transfer_strategies: vec![TransferStrategy::ParameterTransfer],
1156            domain_adaptation: DomainAdaptation {
1157                source_characteristics: Vec::new(),
1158                target_characteristics: Vec::new(),
1159                adaptation_weights: Vec::new(),
1160                domain_similarity: A::from(0.5).unwrap(),
1161            },
1162            transfer_metrics: TransferMetrics {
1163                success_rate: A::from(0.5).unwrap(),
1164                improvement: A::from(0.1).unwrap(),
1165                efficiency: A::from(0.7).unwrap(),
1166                negative_transfer_count: 0,
1167            },
1168        }
1169    }
1170}
1171
1172impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> LearningRateAdapter<A> {
1173    fn new(initial_rate: f64) -> Self {
1174        Self {
1175            current_rate: A::from(initial_rate).unwrap(),
1176            rate_history: VecDeque::with_capacity(100),
1177            performance_feedback: VecDeque::with_capacity(100),
1178            adaptation_strategy: LearningRateStrategy::PerformanceBased,
1179            min_rate: A::from(1e-6).unwrap(),
1180            max_rate: A::from(0.1).unwrap(),
1181        }
1182    }
1183
1184    fn update_rate(&mut self, new_rate: A) -> Result<(), String> {
1185        self.current_rate = new_rate.max(self.min_rate).min(self.max_rate);
1186
1187        if self.rate_history.len() >= 100 {
1188            self.rate_history.pop_front();
1189        }
1190        self.rate_history.push_back(self.current_rate);
1191
1192        Ok(())
1193    }
1194}
1195
1196/// Diagnostic information for meta-learning
1197#[derive(Debug, Clone)]
1198pub struct MetaLearningDiagnostics {
1199    pub total_experiences: usize,
1200    pub training_episodes: usize,
1201    pub current_learning_rate: f64,
1202    pub model_accuracy: f64,
1203    pub strategy_count: usize,
1204    pub transfer_success_rate: f64,
1205}