scirs2_ndimage/ai_driven_adaptive_processing/
strategies.rs1use scirs2_core::ndarray::Array1;
7use std::collections::HashMap;
8
9use super::config::{AlgorithmType, ImagePattern};
10use super::learning::PerformanceMetrics;
11
12#[derive(Debug, Clone)]
14pub struct ProcessingStrategy {
15 pub algorithm_sequence: Vec<AlgorithmStep>,
17 pub parameters: HashMap<String, f64>,
19 pub expected_performance: PerformanceMetrics,
21 pub confidence: f64,
23 pub usage_count: usize,
25 pub success_rate: f64,
27}
28
29#[derive(Debug, Clone)]
31pub struct AlgorithmStep {
32 pub algorithm: AlgorithmType,
34 pub parameters: HashMap<String, f64>,
36 pub quality_contribution: f64,
38 pub computational_cost: f64,
40}
41
42#[derive(Debug, Clone)]
44pub enum ProcessingAlgorithm {
45 AdaptiveGaussianFilter,
46 IntelligentEdgeDetection,
47 AIEnhancedMedianFilter,
48 SmartBilateralFilter,
49 ContextAwareNoiseReduction,
50 AdaptiveMorphology,
51 IntelligentSegmentation,
52 AIFeatureExtraction,
53}
54
55#[derive(Debug, Clone)]
57pub struct AdaptationStrategy {
58 pub name: String,
60 pub parameters: Array1<f64>,
62 pub speed: f64,
64 pub effectiveness: f64,
66}
67
68#[derive(Debug, Clone)]
70pub struct PerformanceRecord {
71 pub timestamp: u64,
73 pub input_characteristics: Array1<f64>,
75 pub strategy_used: ProcessingStrategy,
77 pub achievedmetrics: PerformanceMetrics,
79 pub context: String,
81}
82
83impl ProcessingStrategy {
84 pub fn new() -> Self {
86 Self {
87 algorithm_sequence: Vec::new(),
88 parameters: HashMap::new(),
89 expected_performance: PerformanceMetrics {
90 speed: 0.0,
91 quality: 0.0,
92 memory_usage: 0.0,
93 energy_consumption: 0.0,
94 user_satisfaction: None,
95 },
96 confidence: 0.0,
97 usage_count: 0,
98 success_rate: 0.0,
99 }
100 }
101
102 pub fn add_step(&mut self, step: AlgorithmStep) {
104 self.algorithm_sequence.push(step);
105 }
106
107 pub fn update_performance(&mut self, metrics: &PerformanceMetrics, success: bool) {
109 self.usage_count += 1;
110
111 if success {
112 let total_attempts = self.usage_count as f64;
114 let previous_successes = (total_attempts - 1.0) * self.success_rate;
115 self.success_rate = (previous_successes + 1.0) / total_attempts;
116
117 let alpha = 0.1; self.expected_performance.speed =
120 (1.0 - alpha) * self.expected_performance.speed + alpha * metrics.speed;
121 self.expected_performance.quality =
122 (1.0 - alpha) * self.expected_performance.quality + alpha * metrics.quality;
123 self.expected_performance.memory_usage = (1.0 - alpha)
124 * self.expected_performance.memory_usage
125 + alpha * metrics.memory_usage;
126 }
127 }
128
129 pub fn calculate_score(&self, pattern: &ImagePattern) -> f64 {
131 let performance_score = self.expected_performance.quality;
133 let confidence_score = self.confidence;
134 let usage_score = (self.usage_count as f64).ln().max(0.0) / 10.0; let success_score = self.success_rate;
136
137 0.4 * performance_score + 0.3 * confidence_score + 0.1 * usage_score + 0.2 * success_score
139 }
140}
141
142impl AlgorithmStep {
143 pub fn new(algorithm: AlgorithmType) -> Self {
145 Self {
146 algorithm,
147 parameters: HashMap::new(),
148 quality_contribution: 0.0,
149 computational_cost: 1.0,
150 }
151 }
152
153 pub fn set_parameter(&mut self, name: &str, value: f64) {
155 self.parameters.insert(name.to_string(), value);
156 }
157
158 pub fn estimated_time(&self) -> f64 {
160 let base_time = match self.algorithm {
162 AlgorithmType::GaussianFilter => 0.1,
163 AlgorithmType::MedianFilter => 0.3,
164 AlgorithmType::BilateralFilter => 0.5,
165 AlgorithmType::EdgeDetection => 0.2,
166 AlgorithmType::MorphologyOperation => 0.25,
167 AlgorithmType::QuantumProcessing => 1.0,
168 AlgorithmType::NeuromorphicProcessing => 0.8,
169 AlgorithmType::ConsciousnessSimulation => 1.5,
170 AlgorithmType::AdvancedFusion => 1.2,
171 AlgorithmType::CustomAI => 0.7,
172 };
173
174 base_time * self.computational_cost
175 }
176}