Skip to main content

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).expect("unwrap failed") {
697            EpisodeOutcome::Success
698        } else if reward > A::from(0.5).expect("unwrap failed") {
699            EpisodeOutcome::PartialSuccess
700        } else if reward > A::from(0.2).expect("unwrap failed") {
701            EpisodeOutcome::Neutral
702        } else if reward > A::from(-0.2).expect("unwrap failed") {
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).expect("unwrap failed"), // 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).expect("unwrap failed") {
724            A::from(1.0).expect("unwrap failed")
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![
792            A::from(0.5).expect("unwrap failed"),
793            A::from(0.3).expect("unwrap failed"),
794        ];
795
796        // Extract drift indicators (simplified)
797        let drift_indicators = vec![A::from(0.1).expect("unwrap failed")];
798
799        Ok(MetaState {
800            performance_metrics,
801            resource_state,
802            drift_indicators,
803            adaptation_history: self.statistics.total_experiences,
804            timestamp: Instant::now(),
805        })
806    }
807
808    /// Generates adaptations from model prediction
809    fn generate_adaptations_from_prediction(
810        &self,
811        predicted_action: &MetaAction<A>,
812        _strategy: &AdaptationStrategy<A>,
813    ) -> Result<Vec<Adaptation<A>>, String> {
814        let mut adaptations = Vec::new();
815
816        // Generate adaptations based on predicted action
817        for (i, &magnitude) in predicted_action.adaptation_magnitudes.iter().enumerate() {
818            if magnitude.abs() > A::from(0.05).expect("unwrap failed") {
819                // Minimum threshold
820                let adaptation_type = if i < predicted_action.adaptation_types.len() {
821                    predicted_action.adaptation_types[i].clone()
822                } else {
823                    AdaptationType::LearningRate // Default
824                };
825
826                let adaptation = Adaptation {
827                    adaptation_type,
828                    magnitude,
829                    target_component: "meta_learner".to_string(),
830                    parameters: std::collections::HashMap::new(),
831                    priority: if magnitude.abs() > A::from(0.3).expect("unwrap failed") {
832                        AdaptationPriority::High
833                    } else {
834                        AdaptationPriority::Normal
835                    },
836                    timestamp: Instant::now(),
837                };
838
839                adaptations.push(adaptation);
840            }
841        }
842
843        Ok(adaptations)
844    }
845
846    /// Applies adaptation to meta-learning system
847    pub fn apply_adaptation(&mut self, adaptation: &Adaptation<A>) -> Result<(), String> {
848        match adaptation.adaptation_type {
849            AdaptationType::MetaLearning => {
850                // Adjust meta-learning parameters
851                let new_rate = self.learning_rate_adapter.current_rate + adaptation.magnitude;
852                self.learning_rate_adapter.update_rate(new_rate)?;
853            }
854            _ => {
855                // Handle other adaptation types
856            }
857        }
858
859        Ok(())
860    }
861
862    /// Gets meta-learning effectiveness score
863    pub fn get_effectiveness_score(&self) -> f32 {
864        self.statistics.learning_progress.to_f32().unwrap_or(0.0)
865    }
866
867    /// Gets diagnostic information
868    pub fn get_diagnostics(&self) -> MetaLearningDiagnostics {
869        MetaLearningDiagnostics {
870            total_experiences: self.statistics.total_experiences,
871            training_episodes: self.statistics.training_episodes,
872            current_learning_rate: self
873                .learning_rate_adapter
874                .current_rate
875                .to_f64()
876                .unwrap_or(0.0),
877            model_accuracy: self
878                .meta_model
879                .performance_metrics
880                .prediction_accuracy
881                .to_f64()
882                .unwrap_or(0.0),
883            strategy_count: self.strategy_selector.strategies.len(),
884            transfer_success_rate: self
885                .statistics
886                .transfer_success_rate
887                .to_f64()
888                .unwrap_or(0.0),
889        }
890    }
891}
892
893impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> ExperienceBuffer<A> {
894    fn new(config: &ExperienceReplayConfig) -> Self {
895        Self {
896            config: config.clone(),
897            experiences: VecDeque::with_capacity(10000),
898            priority_queue: VecDeque::new(),
899            importance_weights: HashMap::new(),
900            diversity_tracker: ExperienceDiversityTracker::new(),
901        }
902    }
903
904    fn add_experience(&mut self, experience: MetaExperience<A>) -> Result<(), String> {
905        // Add to main buffer
906        if self.experiences.len() >= 10000 {
907            self.experiences.pop_front();
908        }
909        self.experiences.push_back(experience.clone());
910
911        // Add to priority queue if using prioritized replay
912        if self.config.enable_prioritized_replay {
913            let priority = experience.priority;
914            self.priority_queue.push_back((experience, priority));
915
916            // Sort by priority
917            self.priority_queue
918                .make_contiguous()
919                .sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
920
921            // Limit priority queue size
922            if self.priority_queue.len() > 1000 {
923                self.priority_queue.pop_front();
924            }
925        }
926
927        Ok(())
928    }
929
930    fn sample_batch(&mut self, batch_size: usize) -> Result<Vec<MetaExperience<A>>, String> {
931        if self.experiences.is_empty() {
932            return Ok(Vec::new());
933        }
934
935        let mut batch = Vec::with_capacity(batch_size);
936
937        if self.config.enable_prioritized_replay && !self.priority_queue.is_empty() {
938            // Sample from priority queue
939            for _ in 0..batch_size.min(self.priority_queue.len()) {
940                if let Some((experience, _)) = self.priority_queue.pop_front() {
941                    batch.push(experience);
942                }
943            }
944        } else {
945            // Random sampling
946            for _ in 0..batch_size.min(self.experiences.len()) {
947                let idx = thread_rng().gen_range(0..self.experiences.len());
948                if let Some(experience) = self.experiences.get(idx) {
949                    batch.push(experience.clone());
950                }
951            }
952        }
953
954        Ok(batch)
955    }
956}
957
958impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> ExperienceDiversityTracker<A> {
959    fn new() -> Self {
960        Self {
961            state_clusters: Vec::new(),
962            action_clusters: Vec::new(),
963            diversity_metrics: DiversityMetrics {
964                state_coverage: A::zero(),
965                action_coverage: A::zero(),
966                experience_entropy: A::zero(),
967                temporal_diversity: A::zero(),
968                outcome_diversity: A::zero(),
969            },
970            novelty_detector: NoveltyDetector::new(),
971        }
972    }
973}
974
975impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> NoveltyDetector<A> {
976    fn new() -> Self {
977        Self {
978            reference_experiences: VecDeque::with_capacity(1000),
979            novelty_threshold: A::from(0.5).expect("unwrap failed"),
980            feature_weights: Vec::new(),
981        }
982    }
983}
984
985impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> MetaModel<A> {
986    fn new(complexity: MetaModelComplexity) -> Result<Self, String> {
987        let parameters = match complexity {
988            MetaModelComplexity::Low => MetaModelParameters {
989                weights: vec![vec![A::from(0.1).expect("unwrap failed"); 10]; 2],
990                biases: vec![A::zero(); 10],
991                learning_rate: A::from(0.01).expect("unwrap failed"),
992                regularization: RegularizationParams {
993                    l1_lambda: A::from(0.001).expect("unwrap failed"),
994                    l2_lambda: A::from(0.001).expect("unwrap failed"),
995                    dropout_rate: A::from(0.1).expect("unwrap failed"),
996                    early_stopping_patience: 10,
997                },
998                optimization: OptimizationParams {
999                    momentum: A::from(0.9).expect("unwrap failed"),
1000                    beta1: A::from(0.9).expect("unwrap failed"),
1001                    beta2: A::from(0.999).expect("unwrap failed"),
1002                    epsilon: A::from(1e-8).expect("unwrap failed"),
1003                    grad_clip_threshold: A::from(1.0).expect("unwrap failed"),
1004                },
1005            },
1006            _ => MetaModelParameters {
1007                weights: vec![vec![A::from(0.1).expect("unwrap failed"); 50]; 3],
1008                biases: vec![A::zero(); 50],
1009                learning_rate: A::from(0.001).expect("unwrap failed"),
1010                regularization: RegularizationParams {
1011                    l1_lambda: A::from(0.0001).expect("unwrap failed"),
1012                    l2_lambda: A::from(0.0001).expect("unwrap failed"),
1013                    dropout_rate: A::from(0.2).expect("unwrap failed"),
1014                    early_stopping_patience: 20,
1015                },
1016                optimization: OptimizationParams {
1017                    momentum: A::from(0.9).expect("unwrap failed"),
1018                    beta1: A::from(0.9).expect("unwrap failed"),
1019                    beta2: A::from(0.999).expect("unwrap failed"),
1020                    epsilon: A::from(1e-8).expect("unwrap failed"),
1021                    grad_clip_threshold: A::from(1.0).expect("unwrap failed"),
1022                },
1023            },
1024        };
1025
1026        Ok(Self {
1027            model_type: MetaModelType::NeuralNetwork,
1028            parameters,
1029            training_history: VecDeque::with_capacity(1000),
1030            performance_metrics: ModelPerformanceMetrics {
1031                prediction_accuracy: A::from(0.5).expect("unwrap failed"),
1032                decision_quality: A::from(0.5).expect("unwrap failed"),
1033                adaptation_effectiveness: A::from(0.5).expect("unwrap failed"),
1034                transfer_success_rate: A::from(0.5).expect("unwrap failed"),
1035                generalization_performance: A::from(0.5).expect("unwrap failed"),
1036            },
1037            feature_importance: Vec::new(),
1038        })
1039    }
1040
1041    fn train_on_batch(&mut self, batch: &[MetaExperience<A>]) -> Result<(), String> {
1042        if batch.is_empty() {
1043            return Ok(());
1044        }
1045
1046        // Simplified training - in practice would implement proper neural network training
1047        let avg_reward = batch.iter().map(|e| e.reward).sum::<A>()
1048            / A::from(batch.len()).expect("unwrap failed");
1049
1050        // Update learning rate based on performance
1051        if avg_reward > A::from(0.5).expect("unwrap failed") {
1052            self.parameters.learning_rate =
1053                self.parameters.learning_rate * A::from(1.01).expect("unwrap failed");
1054        } else {
1055            self.parameters.learning_rate =
1056                self.parameters.learning_rate * A::from(0.99).expect("unwrap failed");
1057        }
1058
1059        // Update performance metrics
1060        self.performance_metrics.prediction_accuracy = avg_reward;
1061
1062        Ok(())
1063    }
1064
1065    fn predict_action(&self, state: &MetaState<A>) -> Result<MetaAction<A>, String> {
1066        // Simplified prediction - in practice would use trained neural network
1067        let action = MetaAction {
1068            adaptation_magnitudes: vec![
1069                A::from(0.1).expect("unwrap failed"),
1070                A::from(-0.05).expect("unwrap failed"),
1071            ],
1072            adaptation_types: vec![AdaptationType::LearningRate, AdaptationType::BufferSize],
1073            learning_rate_change: A::from(0.01).expect("unwrap failed"),
1074            buffer_size_change: A::from(5.0).expect("unwrap failed"),
1075            timestamp: Instant::now(),
1076        };
1077
1078        Ok(action)
1079    }
1080}
1081
1082impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> StrategySelector<A> {
1083    fn new() -> Self {
1084        let mut strategies = HashMap::new();
1085
1086        // Add default strategies
1087        strategies.insert(
1088            "conservative".to_string(),
1089            AdaptationStrategy {
1090                name: "conservative".to_string(),
1091                parameters: HashMap::new(),
1092                strategy_type: StrategyType::Conservative,
1093                conditions: Vec::new(),
1094                expected_outcomes: vec![A::from(0.05).expect("unwrap failed")],
1095            },
1096        );
1097
1098        strategies.insert(
1099            "aggressive".to_string(),
1100            AdaptationStrategy {
1101                name: "aggressive".to_string(),
1102                parameters: HashMap::new(),
1103                strategy_type: StrategyType::Aggressive,
1104                conditions: Vec::new(),
1105                expected_outcomes: vec![A::from(0.2).expect("unwrap failed")],
1106            },
1107        );
1108
1109        Self {
1110            strategies,
1111            strategy_performance: HashMap::new(),
1112            selection_policy: SelectionPolicy::EpsilonGreedy { epsilon: 0.1 },
1113            exploration_params: ExplorationParams {
1114                exploration_rate: A::from(0.1).expect("unwrap failed"),
1115                exploration_decay: A::from(0.99).expect("unwrap failed"),
1116                min_exploration_rate: A::from(0.01).expect("unwrap failed"),
1117                curiosity_weight: A::from(0.1).expect("unwrap failed"),
1118                novelty_weight: A::from(0.1).expect("unwrap failed"),
1119            },
1120            context_selector: ContextBasedSelector::new(),
1121        }
1122    }
1123
1124    fn select_strategy(&self, _state: &MetaState<A>) -> Result<AdaptationStrategy<A>, String> {
1125        // Simple strategy selection - in practice would be more sophisticated
1126        if let Some(strategy) = self.strategies.get("balanced") {
1127            Ok(strategy.clone())
1128        } else if let Some(strategy) = self.strategies.values().next() {
1129            Ok(strategy.clone())
1130        } else {
1131            Err("No strategies available".to_string())
1132        }
1133    }
1134
1135    fn update_from_experiences(
1136        &mut self,
1137        _experiences: &[MetaExperience<A>],
1138    ) -> Result<(), String> {
1139        // Update strategy performance based on experiences
1140        Ok(())
1141    }
1142}
1143
1144impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> ContextBasedSelector<A> {
1145    fn new() -> Self {
1146        Self {
1147            context_features: Vec::new(),
1148            context_clusters: Vec::new(),
1149            context_strategies: HashMap::new(),
1150            context_model: ContextModel {
1151                parameters: Vec::new(),
1152                feature_weights: Vec::new(),
1153                threshold: A::from(0.5).expect("unwrap failed"),
1154                accuracy: A::from(0.5).expect("unwrap failed"),
1155            },
1156        }
1157    }
1158}
1159
1160impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> TransferLearning<A> {
1161    fn new() -> Self {
1162        Self {
1163            source_experiences: HashMap::new(),
1164            transfer_strategies: vec![TransferStrategy::ParameterTransfer],
1165            domain_adaptation: DomainAdaptation {
1166                source_characteristics: Vec::new(),
1167                target_characteristics: Vec::new(),
1168                adaptation_weights: Vec::new(),
1169                domain_similarity: A::from(0.5).expect("unwrap failed"),
1170            },
1171            transfer_metrics: TransferMetrics {
1172                success_rate: A::from(0.5).expect("unwrap failed"),
1173                improvement: A::from(0.1).expect("unwrap failed"),
1174                efficiency: A::from(0.7).expect("unwrap failed"),
1175                negative_transfer_count: 0,
1176            },
1177        }
1178    }
1179}
1180
1181impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> LearningRateAdapter<A> {
1182    fn new(initial_rate: f64) -> Self {
1183        Self {
1184            current_rate: A::from(initial_rate).expect("unwrap failed"),
1185            rate_history: VecDeque::with_capacity(100),
1186            performance_feedback: VecDeque::with_capacity(100),
1187            adaptation_strategy: LearningRateStrategy::PerformanceBased,
1188            min_rate: A::from(1e-6).expect("unwrap failed"),
1189            max_rate: A::from(0.1).expect("unwrap failed"),
1190        }
1191    }
1192
1193    fn update_rate(&mut self, new_rate: A) -> Result<(), String> {
1194        self.current_rate = new_rate.max(self.min_rate).min(self.max_rate);
1195
1196        if self.rate_history.len() >= 100 {
1197            self.rate_history.pop_front();
1198        }
1199        self.rate_history.push_back(self.current_rate);
1200
1201        Ok(())
1202    }
1203}
1204
1205/// Diagnostic information for meta-learning
1206#[derive(Debug, Clone)]
1207pub struct MetaLearningDiagnostics {
1208    pub total_experiences: usize,
1209    pub training_episodes: usize,
1210    pub current_learning_rate: f64,
1211    pub model_accuracy: f64,
1212    pub strategy_count: usize,
1213    pub transfer_success_rate: f64,
1214}