1use super::{
7 ActivationType, LearnedOptimizationConfig, LearnedOptimizer, MetaOptimizerState,
8 OptimizationProblem, TrainingTask,
9};
10use crate::error::OptimizeResult;
11use crate::result::OptimizeResults;
12use ndarray::{Array1, Array2, ArrayView1};
13use rand::Rng;
14use statrs::statistics::Statistics;
15use std::collections::{HashMap, VecDeque};
16
17#[derive(Debug, Clone)]
19pub struct NeuralAdaptiveOptimizer {
20 config: LearnedOptimizationConfig,
22 optimization_network: OptimizationNetwork,
24 adaptation_controller: AdaptationController,
26 performance_predictor: PerformancePredictor,
28 meta_state: MetaOptimizerState,
30 adaptive_stats: AdaptiveOptimizationStats,
32 computation_cache: ComputationCache,
34}
35
36#[derive(Debug, Clone)]
38pub struct ComputationCache {
39 gradient_buffer: Array1<f64>,
41 feature_buffer: Array1<f64>,
43 param_buffer: Array1<f64>,
45 network_output_buffer: Array1<f64>,
47 temp_buffer: Array1<f64>,
49 max_buffer_size: usize,
51}
52
53#[derive(Debug, Clone)]
55pub struct BoundedHistory<T> {
56 pub(crate) data: VecDeque<T>,
58 max_capacity: usize,
60}
61
62impl<T> BoundedHistory<T> {
63 pub fn new(capacity: usize) -> Self {
65 Self {
66 data: VecDeque::with_capacity(capacity),
67 max_capacity: capacity,
68 }
69 }
70
71 pub fn push(&mut self, item: T) {
73 if self.data.len() >= self.max_capacity {
74 self.data.pop_front();
75 }
76 self.data.push_back(item);
77 }
78
79 pub fn back(&self) -> Option<&T> {
81 self.data.back()
82 }
83
84 pub fn clear(&mut self) {
86 self.data.clear();
87 }
88
89 pub fn len(&self) -> usize {
91 self.data.len()
92 }
93
94 pub fn is_empty(&self) -> bool {
96 self.data.is_empty()
97 }
98}
99
100impl ComputationCache {
101 pub fn new(max_size: usize) -> Self {
103 Self {
104 gradient_buffer: Array1::zeros(max_size),
105 feature_buffer: Array1::zeros(max_size),
106 param_buffer: Array1::zeros(max_size),
107 network_output_buffer: Array1::zeros(max_size),
108 temp_buffer: Array1::zeros(max_size),
109 max_buffer_size: max_size,
110 }
111 }
112
113 pub fn get_gradient_buffer(&mut self, size: usize) -> &mut Array1<f64> {
115 if self.gradient_buffer.len() < size {
116 self.gradient_buffer = Array1::zeros(size);
117 }
118 &mut self.gradient_buffer
119 }
120
121 pub fn get_feature_buffer(&mut self, size: usize) -> &mut Array1<f64> {
123 if self.feature_buffer.len() < size {
124 self.feature_buffer = Array1::zeros(size);
125 }
126 &mut self.feature_buffer
127 }
128
129 pub fn get_param_buffer(&mut self, size: usize) -> &mut Array1<f64> {
131 if self.param_buffer.len() < size {
132 self.param_buffer = Array1::zeros(size);
133 }
134 &mut self.param_buffer
135 }
136
137 pub fn get_network_output_buffer(&mut self, size: usize) -> &mut Array1<f64> {
139 if self.network_output_buffer.len() < size {
140 self.network_output_buffer = Array1::zeros(size);
141 }
142 &mut self.network_output_buffer
143 }
144
145 pub fn get_temp_buffer(&mut self, size: usize) -> &mut Array1<f64> {
147 if self.temp_buffer.len() < size {
148 self.temp_buffer = Array1::zeros(size);
149 }
150 &mut self.temp_buffer
151 }
152
153 pub fn get_gradient_and_param_buffers(
155 &mut self,
156 gradient_size: usize,
157 param_size: usize,
158 ) -> (&mut Array1<f64>, &mut Array1<f64>) {
159 if self.gradient_buffer.len() < gradient_size {
160 self.gradient_buffer = Array1::zeros(gradient_size);
161 }
162 if self.param_buffer.len() < param_size {
163 self.param_buffer = Array1::zeros(param_size);
164 }
165 (&mut self.gradient_buffer, &mut self.param_buffer)
166 }
167
168 fn resize_buffer(&mut self, buffer: &mut Array1<f64>, requested_size: usize) {
170 let size = requested_size.min(self.max_buffer_size);
171 if buffer.len() != size {
172 *buffer = Array1::zeros(size);
173 } else {
174 buffer.fill(0.0);
175 }
176 }
177}
178
179#[derive(Debug, Clone)]
181pub struct OptimizationNetwork {
182 input_layer: NeuralLayer,
184 hidden_layers: Vec<NeuralLayer>,
186 output_layer: NeuralLayer,
188 recurrent_connections: RecurrentConnections,
190 architecture: NetworkArchitecture,
192}
193
194#[derive(Debug, Clone)]
196pub struct NeuralLayer {
197 weights: Array2<f64>,
199 biases: Array1<f64>,
201 activation: ActivationType,
203 size: usize,
205 dropout_rate: f64,
207 layer_norm: Option<LayerNormalization>,
209}
210
211#[derive(Debug, Clone)]
213pub struct LayerNormalization {
214 gamma: Array1<f64>,
216 beta: Array1<f64>,
218 running_mean: Array1<f64>,
220 running_var: Array1<f64>,
222 momentum: f64,
224 epsilon: f64,
226}
227
228#[derive(Debug, Clone)]
230pub struct RecurrentConnections {
231 hidden_state: Array1<f64>,
233 cell_state: Array1<f64>,
235 recurrent_weights: Array2<f64>,
237 input_gate_weights: Array2<f64>,
239 forget_gate_weights: Array2<f64>,
241 output_gate_weights: Array2<f64>,
243}
244
245#[derive(Debug, Clone)]
247pub struct NetworkArchitecture {
248 input_size: usize,
250 hidden_sizes: Vec<usize>,
252 output_size: usize,
254 activations: Vec<ActivationType>,
256 use_recurrent: bool,
258 use_attention: bool,
260}
261
262#[derive(Debug, Clone)]
264pub struct AdaptationController {
265 strategy_selector: StrategySelector,
267 adaptation_rate_controller: AdaptationRateController,
269 progress_monitor: ProgressMonitor,
271 strategy_history: BoundedHistory<OptimizationStrategy>,
273}
274
275#[derive(Debug, Clone)]
277pub struct StrategySelector {
278 selection_network: Array2<f64>,
280 strategy_embeddings: Array2<f64>,
282 strategy_weights: Array1<f64>,
284 available_strategies: Vec<OptimizationStrategy>,
286}
287
288#[derive(Debug, Clone)]
290pub struct OptimizationStrategy {
291 id: String,
293 parameters: Array1<f64>,
295 expected_performance: f64,
297 computational_cost: f64,
299 robustness: f64,
301}
302
303#[derive(Debug, Clone)]
305pub struct AdaptationRateController {
306 controller_network: Array2<f64>,
308 current_rate: f64,
310 rate_history: BoundedHistory<f64>,
312 performance_correlation: f64,
314}
315
316#[derive(Debug, Clone)]
318pub struct ProgressMonitor {
319 progress_indicators: Vec<ProgressIndicator>,
321 monitoring_network: Array2<f64>,
323 alert_thresholds: HashMap<String, f64>,
325 current_state: ProgressState,
327}
328
329#[derive(Debug, Clone)]
331pub struct ProgressIndicator {
332 name: String,
334 value: f64,
336 history: BoundedHistory<f64>,
338 trend: f64,
340 importance: f64,
342}
343
344#[derive(Debug, Clone)]
346pub enum ProgressState {
347 Improving,
348 Stagnating,
349 Deteriorating,
350 Converged,
351 Diverging,
352}
353
354#[derive(Debug, Clone)]
356pub struct PerformancePredictor {
357 prediction_network: Array2<f64>,
359 feature_extractor: FeatureExtractor,
361 prediction_horizon: usize,
363 prediction_accuracy: f64,
365 confidence_estimator: ConfidenceEstimator,
367}
368
369#[derive(Debug, Clone)]
371pub struct FeatureExtractor {
372 extraction_layers: Vec<Array2<f64>>,
374 feature_dim: usize,
376 temporal_features: TemporalFeatures,
378}
379
380#[derive(Debug, Clone)]
382pub struct TemporalFeatures {
383 time_embeddings: Array2<f64>,
385 trend_analyzer: TrendAnalyzer,
387 seasonality_detector: SeasonalityDetector,
389}
390
391#[derive(Debug, Clone)]
393pub struct TrendAnalyzer {
394 trend_coefficients: Array1<f64>,
396 window_size: usize,
398 trend_strength: f64,
400}
401
402#[derive(Debug, Clone)]
404pub struct SeasonalityDetector {
405 seasonal_patterns: Array2<f64>,
407 pattern_strength: Array1<f64>,
409 detection_threshold: f64,
411}
412
413#[derive(Debug, Clone)]
415pub struct ConfidenceEstimator {
416 confidence_network: Array2<f64>,
418 uncertainty_quantifier: UncertaintyQuantifier,
420 calibration_params: Array1<f64>,
422}
423
424#[derive(Debug, Clone)]
426pub struct UncertaintyQuantifier {
427 epistemic_uncertainty: f64,
429 aleatoric_uncertainty: f64,
431 method: UncertaintyMethod,
433}
434
435#[derive(Debug, Clone)]
437pub enum UncertaintyMethod {
438 Dropout,
439 Ensemble,
440 Bayesian,
441 Evidential,
442}
443
444#[derive(Debug, Clone)]
446pub struct AdaptiveOptimizationStats {
447 strategy_switches: usize,
449 avg_adaptation_rate: f64,
451 prediction_accuracy: f64,
453 computational_efficiency: f64,
455 robustness_score: f64,
457}
458
459impl NeuralAdaptiveOptimizer {
460 pub fn new(config: LearnedOptimizationConfig) -> Self {
462 let architecture = NetworkArchitecture {
463 input_size: config.max_parameters.min(100),
464 hidden_sizes: vec![config.hidden_size, config.hidden_size / 2],
465 output_size: 32, activations: vec![
467 ActivationType::GELU,
468 ActivationType::GELU,
469 ActivationType::Tanh,
470 ],
471 use_recurrent: true,
472 use_attention: config.use_transformer,
473 };
474
475 let optimization_network = OptimizationNetwork::new(architecture);
476 let adaptation_controller = AdaptationController::new(config.hidden_size);
477 let performance_predictor = PerformancePredictor::new(config.hidden_size);
478 let hidden_size = config.hidden_size;
479 let max_buffer_size = config.max_parameters.max(1000); Self {
482 config,
483 optimization_network,
484 adaptation_controller,
485 performance_predictor,
486 meta_state: MetaOptimizerState {
487 meta_params: Array1::zeros(hidden_size),
488 network_weights: Array2::zeros((hidden_size, hidden_size)),
489 performance_history: Vec::new(),
490 adaptation_stats: super::AdaptationStatistics::default(),
491 episode: 0,
492 },
493 adaptive_stats: AdaptiveOptimizationStats::default(),
494 computation_cache: ComputationCache::new(max_buffer_size),
495 }
496 }
497
498 pub fn adaptive_optimization_step<F>(
500 &mut self,
501 objective: &F,
502 current_params: &ArrayView1<f64>,
503 step_number: usize,
504 ) -> OptimizeResult<AdaptiveOptimizationStep>
505 where
506 F: Fn(&ArrayView1<f64>) -> f64,
507 {
508 let state_features = self.extract_state_features(objective, current_params, step_number)?;
510
511 let network_output = self.optimization_network.forward(&state_features.view())?;
513
514 let performance_prediction = self.performance_predictor.predict(&state_features)?;
516
517 let strategy = self
519 .adaptation_controller
520 .select_strategy(&network_output, &performance_prediction)?;
521
522 self.adaptation_controller
524 .monitor_and_adapt(&performance_prediction)?;
525
526 let step = AdaptiveOptimizationStep {
528 strategy: strategy.clone(),
529 predicted_performance: performance_prediction,
530 confidence: self
531 .performance_predictor
532 .confidence_estimator
533 .estimate_confidence(&state_features)?,
534 adaptation_signal: self.adaptation_controller.get_adaptation_signal(),
535 network_output: network_output.clone(),
536 };
537
538 self.update_adaptive_stats(&step)?;
540
541 Ok(step)
542 }
543
544 fn extract_state_features<F>(
546 &mut self,
547 objective: &F,
548 current_params: &ArrayView1<f64>,
549 step_number: usize,
550 ) -> OptimizeResult<Array1<f64>>
551 where
552 F: Fn(&ArrayView1<f64>) -> f64,
553 {
554 let mut features = Array1::zeros(self.optimization_network.architecture.input_size);
555 let feature_idx = 0;
556
557 let param_features = self.extract_parameter_features(current_params);
559 self.copy_features(&mut features, ¶m_features, feature_idx);
560
561 let obj_features = self.extract_objective_features(objective, current_params)?;
563 self.copy_features(
564 &mut features,
565 &obj_features,
566 feature_idx + param_features.len(),
567 );
568
569 let temporal_features = self.extract_temporal_features(step_number);
571 self.copy_features(
572 &mut features,
573 &temporal_features,
574 feature_idx + param_features.len() + obj_features.len(),
575 );
576
577 Ok(features)
578 }
579
580 fn extract_parameter_features(&self, params: &ArrayView1<f64>) -> Array1<f64> {
582 let mut features = Array1::zeros(20);
583
584 if !params.is_empty() {
585 features[0] = params.view().mean().tanh();
586 features[1] = params.view().variance().sqrt().tanh();
587 features[2] = params.fold(-f64::INFINITY, |a, &b| a.max(b)).tanh();
588 features[3] = params.fold(f64::INFINITY, |a, &b| a.min(b)).tanh();
589 features[4] = (params.len() as f64).ln().tanh();
590
591 let mean = features[0];
593 let std = features[1];
594 if std > 1e-8 {
595 let skewness = params
596 .iter()
597 .map(|&x| ((x - mean) / std).powi(3))
598 .sum::<f64>()
599 / params.len() as f64;
600 features[5] = skewness.tanh();
601
602 let kurtosis = params
603 .iter()
604 .map(|&x| ((x - mean) / std).powi(4))
605 .sum::<f64>()
606 / params.len() as f64
607 - 3.0;
608 features[6] = kurtosis.tanh();
609 }
610
611 features[7] =
613 (params.iter().map(|&x| x.abs()).sum::<f64>() / params.len() as f64).tanh(); features[8] = (params.iter().map(|&x| x * x).sum::<f64>()).sqrt().tanh(); let zero_count = params.iter().filter(|&&x| x.abs() < 1e-8).count();
618 features[9] = (zero_count as f64 / params.len() as f64).tanh();
619 }
620
621 features
622 }
623
624 fn extract_objective_features<F>(
626 &mut self,
627 objective: &F,
628 params: &ArrayView1<f64>,
629 ) -> OptimizeResult<Array1<f64>>
630 where
631 F: Fn(&ArrayView1<f64>) -> f64,
632 {
633 let mut features = Array1::zeros(15);
634
635 let f0 = objective(params);
636 features[0] = f0.abs().ln().tanh();
637
638 let h = 1e-6;
640 let gradient_sample_size = params.len().min(10); let (gradient_buffer, param_buffer) = self
642 .computation_cache
643 .get_gradient_and_param_buffers(gradient_sample_size, params.len());
644
645 for (i, &val) in params.iter().enumerate() {
647 if i < param_buffer.len() {
648 param_buffer[i] = val;
649 }
650 }
651
652 for i in 0..gradient_sample_size {
654 let original_val = param_buffer[i];
655 param_buffer[i] = original_val + h;
656 let f_plus = objective(¶m_buffer.view());
657 param_buffer[i] = original_val; gradient_buffer[i] = (f_plus - f0) / h;
660 }
661
662 let gradient_norm = (gradient_buffer
663 .iter()
664 .take(gradient_sample_size)
665 .map(|&g| g * g)
666 .sum::<f64>())
667 .sqrt();
668 features[1] = gradient_norm.ln().tanh();
669
670 if gradient_sample_size > 0 {
671 let grad_mean = gradient_buffer
672 .iter()
673 .take(gradient_sample_size)
674 .sum::<f64>()
675 / gradient_sample_size as f64;
676 let grad_var = gradient_buffer
677 .iter()
678 .take(gradient_sample_size)
679 .map(|&g| (g - grad_mean).powi(2))
680 .sum::<f64>()
681 / gradient_sample_size as f64;
682
683 features[2] = grad_mean.tanh();
684 features[3] = grad_var.sqrt().tanh();
685 }
686
687 if params.len() > 1 {
689 param_buffer[0] += h;
691 param_buffer[1] += h;
692 let f_plus_plus = objective(¶m_buffer.view());
693
694 param_buffer[1] -= 2.0 * h; let f_plus_minus = objective(¶m_buffer.view());
696
697 param_buffer[0] -= h;
699 param_buffer[1] += h;
700
701 let mixed_partial = (f_plus_plus - f_plus_minus) / (2.0 * h);
702 features[4] = mixed_partial.tanh();
703 }
704
705 Ok(features)
706 }
707
708 fn extract_temporal_features(&self, step_number: usize) -> Array1<f64> {
710 let mut features = Array1::zeros(10);
711
712 features[0] = (step_number as f64).ln().tanh();
713 features[1] = (step_number as f64 / 1000.0).tanh(); if self.meta_state.performance_history.len() > 1 {
717 let recent_performance = &self.meta_state.performance_history
718 [self.meta_state.performance_history.len().saturating_sub(5)..];
719
720 if recent_performance.len() > 1 {
721 let trend = (recent_performance[recent_performance.len() - 1]
722 - recent_performance[0])
723 / recent_performance.len() as f64;
724 features[2] = trend.tanh();
725
726 let variance = recent_performance.iter().map(|&x| x * x).sum::<f64>()
727 / recent_performance.len() as f64
728 - (recent_performance.iter().sum::<f64>() / recent_performance.len() as f64)
729 .powi(2);
730 features[3] = variance.sqrt().tanh();
731 }
732 }
733
734 features
735 }
736
737 fn copy_features(&self, target: &mut Array1<f64>, source: &Array1<f64>, start_idx: usize) {
739 for (i, &value) in source.iter().enumerate() {
740 if start_idx + i < target.len() {
741 target[start_idx + i] = value;
742 }
743 }
744 }
745
746 fn update_adaptive_stats(&mut self, step: &AdaptiveOptimizationStep) -> OptimizeResult<()> {
748 if let Some(last_strategy) = self.adaptation_controller.strategy_history.back() {
750 if last_strategy.id != step.strategy.id {
751 self.adaptive_stats.strategy_switches += 1;
752 }
753 }
754
755 self.adaptive_stats.avg_adaptation_rate =
757 0.9 * self.adaptive_stats.avg_adaptation_rate + 0.1 * step.adaptation_signal;
758
759 self.adaptive_stats.prediction_accuracy =
761 0.95 * self.adaptive_stats.prediction_accuracy + 0.05 * step.confidence;
762
763 Ok(())
764 }
765
766 pub fn train_networks(
768 &mut self,
769 training_data: &[OptimizationTrajectory],
770 ) -> OptimizeResult<()> {
771 for trajectory in training_data {
772 self.train_optimization_network(trajectory)?;
774
775 self.train_performance_predictor(trajectory)?;
777
778 self.update_adaptation_controller(trajectory)?;
780 }
781
782 Ok(())
783 }
784
785 fn train_optimization_network(
787 &mut self,
788 trajectory: &OptimizationTrajectory,
789 ) -> OptimizeResult<()> {
790 let learning_rate = self.config.meta_learning_rate;
792
793 for (i, state) in trajectory.states.iter().enumerate() {
794 if i + 1 < trajectory.actions.len() {
795 let target_action = &trajectory.actions[i + 1];
796 let predicted_action = self.optimization_network.forward(&state.view())?;
797
798 let mut loss_gradient = Array1::zeros(predicted_action.len());
800 for j in 0..loss_gradient.len().min(target_action.len()) {
801 loss_gradient[j] = 2.0 * (predicted_action[j] - target_action[j]);
802 }
803
804 self.optimization_network
806 .backward(&loss_gradient, learning_rate)?;
807 }
808 }
809
810 Ok(())
811 }
812
813 fn train_performance_predictor(
815 &mut self,
816 trajectory: &OptimizationTrajectory,
817 ) -> OptimizeResult<()> {
818 let learning_rate = self.config.meta_learning_rate * 0.5;
820
821 for (i, state) in trajectory.states.iter().enumerate() {
822 if i + self.performance_predictor.prediction_horizon
823 < trajectory.performance_values.len()
824 {
825 let target_performance = trajectory.performance_values
826 [i + self.performance_predictor.prediction_horizon];
827 let predicted_performance = self.performance_predictor.predict(state)?;
828
829 let error = target_performance - predicted_performance;
830
831 for row in self.performance_predictor.prediction_network.rows_mut() {
833 for weight in row {
834 *weight += learning_rate * error * rand::rng().random::<f64>() * 0.01;
835 }
836 }
837 }
838 }
839
840 Ok(())
841 }
842
843 fn update_adaptation_controller(
845 &mut self,
846 trajectory: &OptimizationTrajectory,
847 ) -> OptimizeResult<()> {
848 if trajectory.performance_values.len() > 2 {
850 let performance_trend =
851 trajectory.performance_values.last().unwrap() - trajectory.performance_values[0];
852
853 if performance_trend > 0.0 {
855 self.adaptation_controller.reinforce_current_strategy(0.1)?;
857 } else {
858 self.adaptation_controller.encourage_exploration(0.1)?;
860 }
861 }
862
863 Ok(())
864 }
865
866 pub fn get_adaptive_stats(&self) -> &AdaptiveOptimizationStats {
868 &self.adaptive_stats
869 }
870}
871
872#[derive(Debug, Clone)]
874pub struct OptimizationTrajectory {
875 pub states: Vec<Array1<f64>>,
877 pub actions: Vec<Array1<f64>>,
879 pub performance_values: Vec<f64>,
881 pub rewards: Vec<f64>,
883}
884
885#[derive(Debug, Clone)]
887pub struct AdaptiveOptimizationStep {
888 pub strategy: OptimizationStrategy,
890 pub predicted_performance: f64,
892 pub confidence: f64,
894 pub adaptation_signal: f64,
896 pub network_output: Array1<f64>,
898}
899
900impl OptimizationNetwork {
901 pub fn new(architecture: NetworkArchitecture) -> Self {
903 let mut hidden_layers = Vec::new();
904
905 let mut prev_size = architecture.input_size;
907 for (i, &hidden_size) in architecture.hidden_sizes.iter().enumerate() {
908 let activation = architecture
909 .activations
910 .get(i)
911 .copied()
912 .unwrap_or(ActivationType::ReLU);
913
914 hidden_layers.push(NeuralLayer::new(prev_size, hidden_size, activation));
915 prev_size = hidden_size;
916 }
917
918 let input_activation = architecture
920 .activations
921 .first()
922 .copied()
923 .unwrap_or(ActivationType::ReLU);
924 let output_activation = architecture
925 .activations
926 .last()
927 .copied()
928 .unwrap_or(ActivationType::Tanh);
929
930 let input_layer = NeuralLayer::new(
931 architecture.input_size,
932 architecture.input_size,
933 input_activation,
934 );
935 let output_layer = NeuralLayer::new(prev_size, architecture.output_size, output_activation);
936
937 let recurrent_connections = if architecture.use_recurrent {
938 RecurrentConnections::new(prev_size)
939 } else {
940 RecurrentConnections::empty()
941 };
942
943 Self {
944 input_layer,
945 hidden_layers,
946 output_layer,
947 recurrent_connections,
948 architecture,
949 }
950 }
951
952 pub fn forward(&mut self, input: &ArrayView1<f64>) -> OptimizeResult<Array1<f64>> {
954 let mut current = self.input_layer.forward(input)?;
956
957 for layer in &mut self.hidden_layers {
959 current = layer.forward(¤t.view())?;
960 }
961
962 if self.architecture.use_recurrent {
964 current = self.recurrent_connections.apply(¤t)?;
965 }
966
967 let output = self.output_layer.forward(¤t.view())?;
969
970 Ok(output)
971 }
972
973 pub fn backward(&mut self, gradient: &Array1<f64>, learning_rate: f64) -> OptimizeResult<()> {
975 for i in 0..self.output_layer.weights.nrows() {
980 for j in 0..self.output_layer.weights.ncols() {
981 let grad = if i < gradient.len() { gradient[i] } else { 0.0 };
982 self.output_layer.weights[[i, j]] -= learning_rate * grad * 0.01;
983 }
984 }
985
986 for layer in &mut self.hidden_layers {
988 for i in 0..layer.weights.nrows() {
989 for j in 0..layer.weights.ncols() {
990 layer.weights[[i, j]] -= learning_rate * rand::rng().random::<f64>() * 0.001;
991 }
992 }
993 }
994
995 Ok(())
996 }
997}
998
999impl NeuralLayer {
1000 pub fn new(input_size: usize, output_size: usize, activation: ActivationType) -> Self {
1002 let xavier_scale = (2.0 / (input_size + output_size) as f64).sqrt();
1003
1004 Self {
1005 weights: Array2::from_shape_fn((output_size, input_size), |_| {
1006 (rand::rng().random::<f64>() - 0.5) * 2.0 * xavier_scale
1007 }),
1008 biases: Array1::zeros(output_size),
1009 size: output_size,
1010 dropout_rate: 0.1,
1011 layer_norm: Some(LayerNormalization::new(output_size)),
1012 activation: ActivationType::ReLU,
1013 }
1014 }
1015
1016 pub fn forward(&mut self, input: &ArrayView1<f64>) -> OptimizeResult<Array1<f64>> {
1018 let mut output = Array1::zeros(self.size);
1019
1020 for i in 0..self.size {
1022 for j in 0..input.len().min(self.weights.ncols()) {
1023 output[i] += self.weights[[i, j]] * input[j];
1024 }
1025 output[i] += self.biases[i];
1026 }
1027
1028 if let Some(ref mut layer_norm) = self.layer_norm {
1030 output = layer_norm.normalize(&output)?;
1031 }
1032
1033 output.mapv_inplace(|x| self.activation.apply(x));
1035
1036 if self.dropout_rate > 0.0 {
1038 output *= 1.0 - self.dropout_rate;
1039 }
1040
1041 Ok(output)
1042 }
1043}
1044
1045impl LayerNormalization {
1046 pub fn new(size: usize) -> Self {
1048 Self {
1049 gamma: Array1::ones(size),
1050 beta: Array1::zeros(size),
1051 running_mean: Array1::zeros(size),
1052 running_var: Array1::ones(size),
1053 momentum: 0.9,
1054 epsilon: 1e-6,
1055 }
1056 }
1057
1058 pub fn normalize(&mut self, input: &Array1<f64>) -> OptimizeResult<Array1<f64>> {
1060 let mean = input.mean().unwrap_or(0.0);
1061 let var = input.variance();
1062 let std = (var + self.epsilon).sqrt();
1063
1064 self.running_mean = &self.running_mean * self.momentum
1066 + &(Array1::from_elem(input.len(), mean) * (1.0 - self.momentum));
1067 self.running_var = &self.running_var * self.momentum
1068 + &(Array1::from_elem(input.len(), var) * (1.0 - self.momentum));
1069
1070 let mut normalized = Array1::zeros(input.len());
1072 for i in 0..input.len().min(self.gamma.len()) {
1073 normalized[i] = self.gamma[i] * (input[i] - mean) / std + self.beta[i];
1074 }
1075
1076 Ok(normalized)
1077 }
1078}
1079
1080impl RecurrentConnections {
1081 pub fn new(size: usize) -> Self {
1083 Self {
1084 hidden_state: Array1::zeros(size),
1085 cell_state: Array1::zeros(size),
1086 recurrent_weights: Array2::from_shape_fn((size, size), |_| {
1087 (rand::rng().random::<f64>() - 0.5) * 0.1
1088 }),
1089 input_gate_weights: Array2::from_shape_fn((size, size), |_| {
1090 (rand::rng().random::<f64>() - 0.5) * 0.1
1091 }),
1092 forget_gate_weights: Array2::from_shape_fn((size, size), |_| {
1093 (rand::rng().random::<f64>() - 0.5) * 0.1
1094 }),
1095 output_gate_weights: Array2::from_shape_fn((size, size), |_| {
1096 (rand::rng().random::<f64>() - 0.5) * 0.1
1097 }),
1098 }
1099 }
1100
1101 pub fn empty() -> Self {
1103 Self {
1104 hidden_state: Array1::zeros(0),
1105 cell_state: Array1::zeros(0),
1106 recurrent_weights: Array2::zeros((0, 0)),
1107 input_gate_weights: Array2::zeros((0, 0)),
1108 forget_gate_weights: Array2::zeros((0, 0)),
1109 output_gate_weights: Array2::zeros((0, 0)),
1110 }
1111 }
1112
1113 pub fn apply(&mut self, input: &Array1<f64>) -> OptimizeResult<Array1<f64>> {
1115 if self.hidden_state.is_empty() {
1116 return Ok(input.clone());
1117 }
1118
1119 let size = self.hidden_state.len().min(input.len());
1120 let mut output = Array1::zeros(size);
1121
1122 for i in 0..size {
1124 let mut input_gate = 0.0;
1126 for j in 0..size {
1127 input_gate += self.input_gate_weights[[i, j]] * input[j];
1128 }
1129 input_gate = (input_gate).tanh();
1130
1131 let mut forget_gate = 0.0;
1133 for j in 0..size {
1134 forget_gate += self.forget_gate_weights[[i, j]] * self.hidden_state[j];
1135 }
1136 forget_gate = (forget_gate).tanh();
1137
1138 self.cell_state[i] = forget_gate * self.cell_state[i] + input_gate * input[i];
1140
1141 let mut output_gate = 0.0;
1143 for j in 0..size {
1144 output_gate += self.output_gate_weights[[i, j]] * input[j];
1145 }
1146 output_gate = (output_gate).tanh();
1147
1148 self.hidden_state[i] = output_gate * self.cell_state[i].tanh();
1150 output[i] = self.hidden_state[i];
1151 }
1152
1153 Ok(output)
1154 }
1155}
1156
1157impl AdaptationController {
1158 pub fn new(hidden_size: usize) -> Self {
1160 Self {
1161 strategy_selector: StrategySelector::new(hidden_size),
1162 adaptation_rate_controller: AdaptationRateController::new(),
1163 progress_monitor: ProgressMonitor::new(),
1164 strategy_history: BoundedHistory::new(100),
1165 }
1166 }
1167
1168 pub fn select_strategy(
1170 &mut self,
1171 network_output: &Array1<f64>,
1172 performance_prediction: &f64,
1173 ) -> OptimizeResult<OptimizationStrategy> {
1174 let strategy = self
1175 .strategy_selector
1176 .select(network_output, *performance_prediction)?;
1177 self.strategy_history.push(strategy.clone());
1178
1179 Ok(strategy)
1180 }
1181
1182 pub fn monitor_and_adapt(&mut self, performance_prediction: &f64) -> OptimizeResult<()> {
1184 self.progress_monitor.update(*performance_prediction)?;
1185
1186 match self.progress_monitor.current_state {
1187 ProgressState::Stagnating | ProgressState::Deteriorating => {
1188 self.adaptation_rate_controller.increase_rate()?;
1189 }
1190 ProgressState::Improving => {
1191 self.adaptation_rate_controller.maintain_rate()?;
1192 }
1193 _ => {}
1194 }
1195
1196 Ok(())
1197 }
1198
1199 pub fn get_adaptation_signal(&self) -> f64 {
1201 self.adaptation_rate_controller.current_rate
1202 }
1203
1204 pub fn reinforce_current_strategy(&mut self, strength: f64) -> OptimizeResult<()> {
1206 self.strategy_selector.reinforce_current(strength)
1207 }
1208
1209 pub fn encourage_exploration(&mut self, strength: f64) -> OptimizeResult<()> {
1211 self.strategy_selector.encourage_exploration(strength)
1212 }
1213}
1214
1215impl StrategySelector {
1216 pub fn new(hidden_size: usize) -> Self {
1218 let num_strategies = 5;
1219
1220 Self {
1221 selection_network: Array2::from_shape_fn((num_strategies, hidden_size), |_| {
1222 (rand::rng().random::<f64>() - 0.5) * 0.1
1223 }),
1224 strategy_embeddings: Array2::from_shape_fn((num_strategies, hidden_size), |_| {
1225 (rand::rng().random::<f64>() - 0.5) * 0.1
1226 }),
1227 strategy_weights: Array1::from_elem(num_strategies, 1.0 / num_strategies as f64),
1228 available_strategies: vec![
1229 OptimizationStrategy::gradient_descent(),
1230 OptimizationStrategy::momentum(),
1231 OptimizationStrategy::adaptive(),
1232 OptimizationStrategy::quasi_newton(),
1233 OptimizationStrategy::trust_region(),
1234 ],
1235 }
1236 }
1237
1238 pub fn select(
1240 &self,
1241 network_output: &Array1<f64>,
1242 performance_prediction: f64,
1243 ) -> OptimizeResult<OptimizationStrategy> {
1244 let mut strategy_scores = Array1::zeros(self.available_strategies.len());
1245
1246 for i in 0..strategy_scores.len() {
1248 for j in 0..network_output.len().min(self.selection_network.ncols()) {
1249 strategy_scores[i] += self.selection_network[[i, j]] * network_output[j];
1250 }
1251
1252 strategy_scores[i] += performance_prediction * 0.1;
1254
1255 strategy_scores[i] += self.strategy_weights[i];
1257 }
1258
1259 let max_score = strategy_scores.fold(-f64::INFINITY, |a, &b| a.max(b));
1261 strategy_scores.mapv_inplace(|x| (x - max_score).exp());
1262 let sum_scores = strategy_scores.sum();
1263 if sum_scores > 0.0 {
1264 strategy_scores /= sum_scores;
1265 }
1266
1267 let selected_idx = strategy_scores
1269 .iter()
1270 .enumerate()
1271 .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
1272 .map(|(i, _)| i)
1273 .unwrap_or(0);
1274
1275 Ok(self.available_strategies[selected_idx].clone())
1276 }
1277
1278 pub fn reinforce_current(&mut self, strength: f64) -> OptimizeResult<()> {
1280 if let Some((best_idx, _)) = self
1282 .strategy_weights
1283 .iter()
1284 .enumerate()
1285 .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
1286 {
1287 self.strategy_weights[best_idx] += strength;
1288 }
1289
1290 let sum = self.strategy_weights.sum();
1292 if sum > 0.0 {
1293 self.strategy_weights /= sum;
1294 }
1295
1296 Ok(())
1297 }
1298
1299 pub fn encourage_exploration(&mut self, strength: f64) -> OptimizeResult<()> {
1301 for weight in &mut self.strategy_weights {
1303 *weight += strength * rand::rng().random::<f64>();
1304 }
1305
1306 let sum = self.strategy_weights.sum();
1308 if sum > 0.0 {
1309 self.strategy_weights /= sum;
1310 }
1311
1312 Ok(())
1313 }
1314}
1315
1316impl OptimizationStrategy {
1317 pub fn gradient_descent() -> Self {
1319 Self {
1320 id: "gradient_descent".to_string(),
1321 parameters: Array1::from(vec![0.01, 0.0, 0.0]), expected_performance: 0.7,
1323 computational_cost: 0.3,
1324 robustness: 0.8,
1325 }
1326 }
1327
1328 pub fn momentum() -> Self {
1330 Self {
1331 id: "momentum".to_string(),
1332 parameters: Array1::from(vec![0.01, 0.9, 0.0]),
1333 expected_performance: 0.8,
1334 computational_cost: 0.4,
1335 robustness: 0.7,
1336 }
1337 }
1338
1339 pub fn adaptive() -> Self {
1341 Self {
1342 id: "adaptive".to_string(),
1343 parameters: Array1::from(vec![0.001, 0.0, 0.9]),
1344 expected_performance: 0.85,
1345 computational_cost: 0.6,
1346 robustness: 0.9,
1347 }
1348 }
1349
1350 pub fn quasi_newton() -> Self {
1352 Self {
1353 id: "quasi_newton".to_string(),
1354 parameters: Array1::from(vec![0.1, 0.0, 0.5]),
1355 expected_performance: 0.9,
1356 computational_cost: 0.8,
1357 robustness: 0.6,
1358 }
1359 }
1360
1361 pub fn trust_region() -> Self {
1363 Self {
1364 id: "trust_region".to_string(),
1365 parameters: Array1::from(vec![0.1, 0.0, 0.7]),
1366 expected_performance: 0.95,
1367 computational_cost: 0.9,
1368 robustness: 0.95,
1369 }
1370 }
1371}
1372
1373impl AdaptationRateController {
1374 pub fn new() -> Self {
1376 Self {
1377 controller_network: Array2::from_shape_fn((1, 10), |_| {
1378 (rand::rng().random::<f64>() - 0.5) * 0.1
1379 }),
1380 current_rate: 0.1,
1381 rate_history: BoundedHistory::new(100),
1382 performance_correlation: 0.0,
1383 }
1384 }
1385
1386 pub fn increase_rate(&mut self) -> OptimizeResult<()> {
1388 self.current_rate = (self.current_rate * 1.2).min(1.0);
1389 self.rate_history.push(self.current_rate);
1390
1391 Ok(())
1392 }
1393
1394 pub fn maintain_rate(&mut self) -> OptimizeResult<()> {
1396 self.rate_history.push(self.current_rate);
1397
1398 Ok(())
1399 }
1400}
1401
1402impl ProgressMonitor {
1403 pub fn new() -> Self {
1405 Self {
1406 progress_indicators: vec![
1407 ProgressIndicator::new("objective_improvement".to_string()),
1408 ProgressIndicator::new("gradient_norm".to_string()),
1409 ProgressIndicator::new("step_size".to_string()),
1410 ],
1411 monitoring_network: Array2::from_shape_fn((4, 10), |_| {
1412 (rand::rng().random::<f64>() - 0.5) * 0.1
1413 }),
1414 alert_thresholds: HashMap::new(),
1415 current_state: ProgressState::Improving,
1416 }
1417 }
1418
1419 pub fn update(&mut self, performance_value: f64) -> OptimizeResult<()> {
1421 for indicator in &mut self.progress_indicators {
1423 indicator.update(performance_value)?;
1424 }
1425
1426 self.current_state = self.determine_progress_state()?;
1428
1429 Ok(())
1430 }
1431
1432 fn determine_progress_state(&self) -> OptimizeResult<ProgressState> {
1434 let mut improvement_count = 0;
1435 let mut stagnation_count = 0;
1436
1437 for indicator in &self.progress_indicators {
1438 if indicator.trend > 0.1 {
1439 improvement_count += 1;
1440 } else if indicator.trend.abs() < 0.01 {
1441 stagnation_count += 1;
1442 }
1443 }
1444
1445 if improvement_count >= 2 {
1446 Ok(ProgressState::Improving)
1447 } else if stagnation_count >= 2 {
1448 Ok(ProgressState::Stagnating)
1449 } else {
1450 Ok(ProgressState::Deteriorating)
1451 }
1452 }
1453}
1454
1455impl ProgressIndicator {
1456 pub fn new(name: String) -> Self {
1458 Self {
1459 name,
1460 value: 0.0,
1461 history: BoundedHistory::new(50),
1462 trend: 0.0,
1463 importance: 1.0,
1464 }
1465 }
1466
1467 pub fn update(&mut self, new_value: f64) -> OptimizeResult<()> {
1469 self.value = new_value;
1470 self.history.push(new_value);
1471
1472 if self.history.len() > 2 {
1474 let first = self.history.data.front().copied().unwrap_or(new_value);
1476 let last = self.history.data.back().copied().unwrap_or(new_value);
1477 self.trend = (last - first) / self.history.len() as f64;
1478 }
1479
1480 Ok(())
1481 }
1482}
1483
1484impl PerformancePredictor {
1485 pub fn new(hidden_size: usize) -> Self {
1487 Self {
1488 prediction_network: Array2::from_shape_fn((1, hidden_size), |_| {
1489 (rand::rng().random::<f64>() - 0.5) * 0.1
1490 }),
1491 feature_extractor: FeatureExtractor::new(hidden_size),
1492 prediction_horizon: 5,
1493 prediction_accuracy: 0.5,
1494 confidence_estimator: ConfidenceEstimator::new(hidden_size),
1495 }
1496 }
1497
1498 pub fn predict(&self, state_features: &Array1<f64>) -> OptimizeResult<f64> {
1500 let prediction_features = self.feature_extractor.extract(state_features)?;
1502
1503 let mut prediction = 0.0;
1505 for j in 0..prediction_features
1506 .len()
1507 .min(self.prediction_network.ncols())
1508 {
1509 prediction += self.prediction_network[[0, j]] * prediction_features[j];
1510 }
1511
1512 Ok(prediction.tanh()) }
1514}
1515
1516impl FeatureExtractor {
1517 pub fn new(feature_dim: usize) -> Self {
1519 Self {
1520 extraction_layers: vec![Array2::from_shape_fn((feature_dim, feature_dim), |_| {
1521 (rand::rng().random::<f64>() - 0.5) * 0.1
1522 })],
1523 feature_dim,
1524 temporal_features: TemporalFeatures::new(feature_dim),
1525 }
1526 }
1527
1528 pub fn extract(&self, input: &Array1<f64>) -> OptimizeResult<Array1<f64>> {
1530 let mut features = input.clone();
1531
1532 for layer in &self.extraction_layers {
1534 let output_dim = layer.nrows().min(features.len());
1535 let input_dim = layer.ncols().min(features.len());
1536 let mut new_features = Array1::zeros(output_dim);
1537
1538 for i in 0..output_dim {
1539 for j in 0..input_dim {
1540 new_features[i] += layer[[i, j]] * features[j];
1541 }
1542 }
1543 features = new_features;
1544 }
1545
1546 Ok(features)
1547 }
1548}
1549
1550impl TemporalFeatures {
1551 pub fn new(dim: usize) -> Self {
1553 Self {
1554 time_embeddings: Array2::from_shape_fn((dim, 100), |_| {
1555 (rand::rng().random::<f64>() - 0.5) * 0.1
1556 }),
1557 trend_analyzer: TrendAnalyzer::new(),
1558 seasonality_detector: SeasonalityDetector::new(dim),
1559 }
1560 }
1561}
1562
1563impl TrendAnalyzer {
1564 pub fn new() -> Self {
1566 Self {
1567 trend_coefficients: Array1::from(vec![1.0, 0.5, 0.1]),
1568 window_size: 10,
1569 trend_strength: 0.0,
1570 }
1571 }
1572}
1573
1574impl SeasonalityDetector {
1575 pub fn new(dim: usize) -> Self {
1577 Self {
1578 seasonal_patterns: Array2::zeros((dim, 12)),
1579 pattern_strength: Array1::zeros(12),
1580 detection_threshold: 0.1,
1581 }
1582 }
1583}
1584
1585impl ConfidenceEstimator {
1586 pub fn new(hidden_size: usize) -> Self {
1588 Self {
1589 confidence_network: Array2::from_shape_fn((1, hidden_size), |_| {
1590 (rand::rng().random::<f64>() - 0.5) * 0.1
1591 }),
1592 uncertainty_quantifier: UncertaintyQuantifier::new(),
1593 calibration_params: Array1::from(vec![1.0, 0.0, 0.1]),
1594 }
1595 }
1596
1597 pub fn estimate_confidence(&self, features: &Array1<f64>) -> OptimizeResult<f64> {
1599 let mut confidence = 0.0;
1600 for j in 0..features.len().min(self.confidence_network.ncols()) {
1601 confidence += self.confidence_network[[0, j]] * features[j];
1602 }
1603
1604 Ok(1.0 / (1.0 + (-confidence).exp()))
1606 }
1607}
1608
1609impl UncertaintyQuantifier {
1610 pub fn new() -> Self {
1612 Self {
1613 epistemic_uncertainty: 0.1,
1614 aleatoric_uncertainty: 0.1,
1615 method: UncertaintyMethod::Dropout,
1616 }
1617 }
1618}
1619
1620impl Default for AdaptiveOptimizationStats {
1621 fn default() -> Self {
1622 Self {
1623 strategy_switches: 0,
1624 avg_adaptation_rate: 0.1,
1625 prediction_accuracy: 0.5,
1626 computational_efficiency: 0.5,
1627 robustness_score: 0.5,
1628 }
1629 }
1630}
1631
1632impl LearnedOptimizer for NeuralAdaptiveOptimizer {
1633 fn meta_train(&mut self, training_tasks: &[TrainingTask]) -> OptimizeResult<()> {
1634 let mut trajectories = Vec::new();
1636
1637 for task in training_tasks {
1638 let trajectory = self.create_trajectory_from_task(task)?;
1639 trajectories.push(trajectory);
1640 }
1641
1642 self.train_networks(&trajectories)?;
1644
1645 Ok(())
1646 }
1647
1648 fn adapt_to_problem(
1649 &mut self,
1650 _problem: &OptimizationProblem,
1651 _params: &ArrayView1<f64>,
1652 ) -> OptimizeResult<()> {
1653 Ok(())
1655 }
1656
1657 fn optimize<F>(
1658 &mut self,
1659 objective: F,
1660 initial_params: &ArrayView1<f64>,
1661 ) -> OptimizeResult<OptimizeResults<f64>>
1662 where
1663 F: Fn(&ArrayView1<f64>) -> f64,
1664 {
1665 let mut current_params = initial_params.to_owned();
1666 let mut best_value = objective(initial_params);
1667 let mut iterations = 0;
1668
1669 for step_number in 0..1000 {
1670 iterations = step_number;
1671
1672 let adaptive_step =
1674 self.adaptive_optimization_step(&objective, ¤t_params.view(), step_number)?;
1675
1676 let direction = self.compute_direction_for_strategy(
1678 &objective,
1679 ¤t_params,
1680 &adaptive_step.strategy,
1681 )?;
1682 let step_size = self.compute_step_size_for_strategy(&adaptive_step.strategy);
1683
1684 for i in 0..current_params.len().min(direction.len()) {
1686 current_params[i] -= step_size * direction[i];
1687 }
1688
1689 let current_value = objective(¤t_params.view());
1690
1691 if current_value < best_value {
1692 best_value = current_value;
1693 }
1694
1695 self.meta_state.performance_history.push(current_value);
1697
1698 if adaptive_step.confidence > 0.95 && step_size < 1e-8 {
1700 break;
1701 }
1702 }
1703
1704 Ok(OptimizeResults::<f64> {
1705 x: current_params,
1706 fun: best_value,
1707 success: true,
1708 nit: iterations,
1709 message: "Neural adaptive optimization completed".to_string(),
1710 jac: None,
1711 hess: None,
1712 constr: None,
1713 nfev: iterations * 5, njev: 0,
1715 nhev: 0,
1716 maxcv: 0,
1717 status: 0,
1718 })
1719 }
1720
1721 fn get_state(&self) -> &MetaOptimizerState {
1722 &self.meta_state
1723 }
1724
1725 fn reset(&mut self) {
1726 self.adaptive_stats = AdaptiveOptimizationStats::default();
1727 self.meta_state.performance_history.clear();
1728 self.adaptation_controller.strategy_history.clear();
1729 self.computation_cache.gradient_buffer.fill(0.0);
1731 self.computation_cache.feature_buffer.fill(0.0);
1732 self.computation_cache.param_buffer.fill(0.0);
1733 self.computation_cache.network_output_buffer.fill(0.0);
1734 self.computation_cache.temp_buffer.fill(0.0);
1735 }
1736}
1737
1738impl NeuralAdaptiveOptimizer {
1739 fn create_trajectory_from_task(
1740 &self,
1741 task: &TrainingTask,
1742 ) -> OptimizeResult<OptimizationTrajectory> {
1743 let num_steps = 10;
1745 let mut states = Vec::new();
1746 let mut actions = Vec::new();
1747 let mut performance_values = Vec::new();
1748 let mut rewards = Vec::new();
1749
1750 for i in 0..num_steps {
1751 states.push(Array1::from_shape_fn(
1752 self.optimization_network.architecture.input_size,
1753 |_| rand::rng().random::<f64>(),
1754 ));
1755
1756 actions.push(Array1::from_shape_fn(
1757 self.optimization_network.architecture.output_size,
1758 |_| rand::rng().random::<f64>(),
1759 ));
1760
1761 performance_values.push(1.0 - i as f64 / num_steps as f64);
1762 rewards.push(if i > 0 {
1763 performance_values[i - 1] - performance_values[i]
1764 } else {
1765 0.0
1766 });
1767 }
1768
1769 Ok(OptimizationTrajectory {
1770 states,
1771 actions,
1772 performance_values,
1773 rewards,
1774 })
1775 }
1776
1777 fn compute_direction_for_strategy<F>(
1778 &mut self,
1779 objective: &F,
1780 params: &Array1<f64>,
1781 strategy: &OptimizationStrategy,
1782 ) -> OptimizeResult<Array1<f64>>
1783 where
1784 F: Fn(&ArrayView1<f64>) -> f64,
1785 {
1786 let h = 1e-6;
1788 let f0 = objective(¶ms.view());
1789 let (gradient_buffer, param_buffer) = self
1790 .computation_cache
1791 .get_gradient_and_param_buffers(params.len(), params.len());
1792
1793 for (i, &val) in params.iter().enumerate() {
1795 if i < param_buffer.len() {
1796 param_buffer[i] = val;
1797 }
1798 }
1799
1800 for i in 0..params.len().min(gradient_buffer.len()) {
1801 let original_val = param_buffer[i];
1802 param_buffer[i] = original_val + h;
1803 let f_plus = objective(¶m_buffer.view());
1804 param_buffer[i] = original_val; gradient_buffer[i] = (f_plus - f0) / h;
1806 }
1807
1808 let mut gradient = Array1::zeros(params.len());
1810 for i in 0..params.len().min(gradient_buffer.len()) {
1811 gradient[i] = gradient_buffer[i];
1812 }
1813
1814 match strategy.id.as_str() {
1816 "momentum" => {
1817 gradient *= strategy.parameters[1]; }
1820 "adaptive" => {
1821 let adaptivity = strategy.parameters[2];
1823 gradient.mapv_inplace(|g| g / (1.0 + adaptivity * g.abs()));
1824 }
1825 _ => {
1826 }
1828 }
1829
1830 Ok(gradient)
1831 }
1832
1833 fn compute_step_size_for_strategy(&self, strategy: &OptimizationStrategy) -> f64 {
1834 strategy.parameters[0] }
1836}
1837
1838#[allow(dead_code)]
1840pub fn neural_adaptive_optimize<F>(
1841 objective: F,
1842 initial_params: &ArrayView1<f64>,
1843 config: Option<LearnedOptimizationConfig>,
1844) -> OptimizeResult<OptimizeResults<f64>>
1845where
1846 F: Fn(&ArrayView1<f64>) -> f64,
1847{
1848 let config = config.unwrap_or_default();
1849 let mut optimizer = NeuralAdaptiveOptimizer::new(config);
1850 optimizer.optimize(objective, initial_params)
1851}
1852
1853#[cfg(test)]
1854mod tests {
1855 use super::*;
1856
1857 #[test]
1858 fn test_neural_adaptive_optimizer_creation() {
1859 let config = LearnedOptimizationConfig::default();
1860 let optimizer = NeuralAdaptiveOptimizer::new(config);
1861
1862 assert_eq!(optimizer.adaptive_stats.strategy_switches, 0);
1863 }
1864
1865 #[test]
1866 fn test_optimization_network() {
1867 let architecture = NetworkArchitecture {
1868 input_size: 10,
1869 hidden_sizes: vec![16, 8],
1870 output_size: 4,
1871 activations: vec![
1872 ActivationType::ReLU,
1873 ActivationType::ReLU,
1874 ActivationType::Tanh,
1875 ],
1876 use_recurrent: false,
1877 use_attention: false,
1878 };
1879
1880 let mut network = OptimizationNetwork::new(architecture);
1881 let input = Array1::from(vec![1.0; 10]);
1882
1883 let output = network.forward(&input.view()).unwrap();
1884
1885 assert_eq!(output.len(), 4);
1886 assert!(output.iter().all(|&x| x.is_finite()));
1887 }
1888
1889 #[test]
1890 fn test_neural_layer() {
1891 let mut layer = NeuralLayer::new(5, 3, ActivationType::ReLU);
1892 let input = Array1::from(vec![1.0, 2.0, 3.0, 4.0, 5.0]);
1893
1894 let output = layer.forward(&input.view()).unwrap();
1895
1896 assert_eq!(output.len(), 3);
1897 assert!(output.iter().all(|&x| x.is_finite()));
1898 }
1899
1900 #[test]
1901 fn test_strategy_selector() {
1902 let selector = StrategySelector::new(16);
1903 let network_output = Array1::from(vec![0.5; 16]);
1904
1905 let strategy = selector.select(&network_output, 0.8).unwrap();
1906
1907 assert!(!strategy.id.is_empty());
1908 assert!(strategy.expected_performance >= 0.0);
1909 }
1910
1911 #[test]
1912 fn test_performance_predictor() {
1913 let predictor = PerformancePredictor::new(32);
1914 let features = Array1::from(vec![0.1; 32]);
1915
1916 let prediction = predictor.predict(&features).unwrap();
1917
1918 assert!(prediction >= -1.0 && prediction <= 1.0);
1919 }
1920
1921 #[test]
1922 fn test_neural_adaptive_optimization() {
1923 let objective = |x: &ArrayView1<f64>| x[0].powi(2) + x[1].powi(2);
1924 let initial = Array1::from(vec![2.0, 2.0]);
1925
1926 let config = LearnedOptimizationConfig {
1927 hidden_size: 32,
1928 max_parameters: 50,
1929 ..Default::default()
1930 };
1931
1932 let result = neural_adaptive_optimize(objective, &initial.view(), Some(config)).unwrap();
1933
1934 assert!(result.fun >= 0.0);
1935 assert_eq!(result.x.len(), 2);
1936 assert!(result.success);
1937 }
1938}
1939
1940#[allow(dead_code)]
1941pub fn placeholder() {
1942 }