Skip to main content

oxirs_embed/
evolutionary_nas_types.rs

1//! Evolutionary NAS — Types
2//!
3//! Chromosome/genome representation, mutation operators, fitness types, and population config.
4
5use chrono::{DateTime, Utc};
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8use std::time::Duration;
9use uuid::Uuid;
10
11// ── Config ────────────────────────────────────────────────────────────────────
12
13/// Configuration for evolutionary neural architecture search
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct EvolutionaryConfig {
16    pub population_size: usize,
17    pub max_generations: usize,
18    pub elite_percentage: f32,
19    pub tournament_size: usize,
20    pub crossover_probability: f32,
21    pub mutation_probability: f32,
22    pub diversity_strength: f32,
23    pub objective_weights: ObjectiveWeights,
24    pub target_hardware: HardwareTarget,
25    pub progressive_config: ProgressiveConfig,
26}
27
28impl Default for EvolutionaryConfig {
29    fn default() -> Self {
30        Self {
31            population_size: 50,
32            max_generations: 100,
33            elite_percentage: 0.1,
34            tournament_size: 5,
35            crossover_probability: 0.8,
36            mutation_probability: 0.1,
37            diversity_strength: 0.3,
38            objective_weights: ObjectiveWeights::default(),
39            target_hardware: HardwareTarget::default(),
40            progressive_config: ProgressiveConfig::default(),
41        }
42    }
43}
44
45/// Multi-objective optimization weights
46#[derive(Debug, Clone, Serialize, Deserialize)]
47pub struct ObjectiveWeights {
48    pub accuracy_weight: f32,
49    pub efficiency_weight: f32,
50    pub memory_weight: f32,
51    pub simplicity_weight: f32,
52    pub novelty_weight: f32,
53}
54
55impl Default for ObjectiveWeights {
56    fn default() -> Self {
57        Self {
58            accuracy_weight: 0.4,
59            efficiency_weight: 0.3,
60            memory_weight: 0.15,
61            simplicity_weight: 0.1,
62            novelty_weight: 0.05,
63        }
64    }
65}
66
67/// Target hardware configuration for optimization
68#[derive(Debug, Clone, Serialize, Deserialize)]
69pub enum HardwareTarget {
70    HighPerformanceGPU {
71        gpu_memory_gb: f32,
72        compute_capability: f32,
73        parallelism_factor: f32,
74    },
75    EdgeDevice {
76        cpu_cores: usize,
77        memory_mb: f32,
78        power_budget_watts: f32,
79    },
80    CloudDeployment {
81        instance_type: String,
82        cost_per_hour: f32,
83        scaling_factor: f32,
84    },
85    NeuromorphicChip {
86        neuron_count: usize,
87        synapse_count: usize,
88        spike_rate_khz: f32,
89    },
90}
91
92impl Default for HardwareTarget {
93    fn default() -> Self {
94        Self::HighPerformanceGPU {
95            gpu_memory_gb: 16.0,
96            compute_capability: 8.0,
97            parallelism_factor: 1.0,
98        }
99    }
100}
101
102/// Progressive complexification configuration
103#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct ProgressiveConfig {
105    pub start_complexity: usize,
106    pub max_complexity: usize,
107    pub complexity_increase_rate: f32,
108    pub enable_modular_building: bool,
109    pub enable_module_library: bool,
110}
111
112impl Default for ProgressiveConfig {
113    fn default() -> Self {
114        Self {
115            start_complexity: 3,
116            max_complexity: 20,
117            complexity_increase_rate: 0.1,
118            enable_modular_building: true,
119            enable_module_library: true,
120        }
121    }
122}
123
124// ── Genome ────────────────────────────────────────────────────────────────────
125
126/// Architecture candidate in the population
127#[derive(Debug, Clone, Serialize, Deserialize)]
128pub struct ArchitectureCandidate {
129    pub id: Uuid,
130    pub genome: ArchitectureGenome,
131    pub fitness: FitnessScores,
132    pub performance: Option<PerformanceMetrics>,
133    pub generation: usize,
134    pub parents: Vec<Uuid>,
135    pub novelty_score: f32,
136    pub hardware_metrics: HardwareMetrics,
137}
138
139/// Architecture genome representation using graph-based encoding
140#[derive(Debug, Clone, Serialize, Deserialize)]
141pub struct ArchitectureGenome {
142    pub nodes: Vec<NodeGene>,
143    pub connections: Vec<ConnectionGene>,
144    pub global_params: GlobalParameters,
145    pub modules: Vec<ModuleDefinition>,
146}
147
148/// Node gene representing a layer or operation
149#[derive(Debug, Clone, Serialize, Deserialize)]
150pub struct NodeGene {
151    pub id: usize,
152    pub operation: OperationType,
153    pub parameters: HashMap<String, f32>,
154    pub active: bool,
155    pub innovation_number: usize,
156}
157
158/// Connection gene representing data flow
159#[derive(Debug, Clone, Serialize, Deserialize)]
160pub struct ConnectionGene {
161    pub from_node: usize,
162    pub to_node: usize,
163    pub weight: f32,
164    pub active: bool,
165    pub innovation_number: usize,
166}
167
168/// Types of operations available for architecture building
169#[derive(Debug, Clone, Serialize, Deserialize)]
170pub enum OperationType {
171    Linear {
172        input_dim: usize,
173        output_dim: usize,
174    },
175    Convolution {
176        filters: usize,
177        kernel_size: usize,
178    },
179    GraphConv {
180        channels: usize,
181        aggregation: String,
182    },
183    Attention {
184        heads: usize,
185        embed_dim: usize,
186    },
187    Transformer {
188        layers: usize,
189        heads: usize,
190    },
191    Embedding {
192        vocab_size: usize,
193        embed_dim: usize,
194    },
195    Activation {
196        function: String,
197    },
198    Normalization {
199        method: String,
200    },
201    Dropout {
202        rate: f32,
203    },
204    SkipConnection,
205    Pooling {
206        method: String,
207        size: usize,
208    },
209    Custom {
210        operation_id: String,
211        params: HashMap<String, f32>,
212    },
213}
214
215/// Global parameters affecting the entire architecture
216#[derive(Debug, Clone, Serialize, Deserialize)]
217pub struct GlobalParameters {
218    pub learning_rate: f32,
219    pub optimizer: String,
220    pub regularization: f32,
221    pub batch_size: usize,
222    pub epochs: usize,
223}
224
225impl Default for GlobalParameters {
226    fn default() -> Self {
227        Self {
228            learning_rate: 0.001,
229            optimizer: "adam".to_string(),
230            regularization: 0.01,
231            batch_size: 32,
232            epochs: 100,
233        }
234    }
235}
236
237/// Module definition for modular architecture building
238#[derive(Debug, Clone, Serialize, Deserialize)]
239pub struct ModuleDefinition {
240    pub id: String,
241    pub nodes: Vec<NodeGene>,
242    pub connections: Vec<ConnectionGene>,
243    pub interface: ModuleInterface,
244    pub characteristics: ModuleCharacteristics,
245}
246
247/// Module interface specification
248#[derive(Debug, Clone, Serialize, Deserialize)]
249pub struct ModuleInterface {
250    pub input_dim: usize,
251    pub output_dim: usize,
252    pub input_types: Vec<String>,
253    pub output_types: Vec<String>,
254}
255
256/// Module performance characteristics
257#[derive(Debug, Clone, Serialize, Deserialize)]
258pub struct ModuleCharacteristics {
259    pub computational_cost: f64,
260    pub memory_cost: f64,
261    pub accuracy_contribution: f32,
262    pub stability: f32,
263}
264
265// ── Fitness & Performance ─────────────────────────────────────────────────────
266
267/// Fitness scores for multi-objective optimization
268#[derive(Debug, Clone, Serialize, Deserialize)]
269pub struct FitnessScores {
270    pub overall_fitness: f32,
271    pub accuracy: f32,
272    pub efficiency: f32,
273    pub memory_efficiency: f32,
274    pub simplicity: f32,
275    pub novelty: f32,
276    pub hardware_compatibility: f32,
277    pub pareto_rank: usize,
278    pub crowding_distance: f32,
279}
280
281impl Default for FitnessScores {
282    fn default() -> Self {
283        Self {
284            overall_fitness: 0.0,
285            accuracy: 0.0,
286            efficiency: 0.0,
287            memory_efficiency: 0.0,
288            simplicity: 0.0,
289            novelty: 0.0,
290            hardware_compatibility: 0.0,
291            pareto_rank: 0,
292            crowding_distance: 0.0,
293        }
294    }
295}
296
297/// Performance metrics for architecture evaluation
298#[derive(Debug, Clone, Serialize, Deserialize)]
299pub struct PerformanceMetrics {
300    pub training_accuracy: f32,
301    pub validation_accuracy: f32,
302    pub test_accuracy: Option<f32>,
303    pub training_time: f64,
304    pub inference_time_ms: f32,
305    pub memory_usage_mb: f32,
306    pub energy_consumption: Option<f32>,
307    pub model_size: usize,
308    pub flops: u64,
309}
310
311/// Hardware-specific metrics
312#[derive(Debug, Clone, Serialize, Deserialize)]
313pub struct HardwareMetrics {
314    pub gpu_utilization: f32,
315    pub memory_utilization: f32,
316    pub throughput: f32,
317    pub power_consumption: f32,
318    pub efficiency_score: f32,
319}
320
321impl Default for HardwareMetrics {
322    fn default() -> Self {
323        Self {
324            gpu_utilization: 0.0,
325            memory_utilization: 0.0,
326            throughput: 0.0,
327            power_consumption: 0.0,
328            efficiency_score: 0.0,
329        }
330    }
331}
332
333// ── History & Tracking ────────────────────────────────────────────────────────
334
335/// Statistics for each generation
336#[derive(Debug, Clone, Serialize, Deserialize)]
337pub struct GenerationStatistics {
338    pub generation: usize,
339    pub best_fitness: f32,
340    pub average_fitness: f32,
341    pub fitness_std: f32,
342    pub diversity_score: f32,
343    pub new_innovations: usize,
344    pub timestamp: DateTime<Utc>,
345}
346
347/// Innovation tracking for genetic operators
348pub struct InnovationTracker {
349    pub(crate) next_innovation: usize,
350    pub(crate) innovation_history: HashMap<String, usize>,
351    pub(crate) innovation_fitness: HashMap<usize, f32>,
352}
353
354impl InnovationTracker {
355    pub fn new() -> Self {
356        Self {
357            next_innovation: 1,
358            innovation_history: HashMap::new(),
359            innovation_fitness: HashMap::new(),
360        }
361    }
362
363    pub fn get_innovation_number(&mut self, innovation_key: &str) -> usize {
364        if let Some(&innovation) = self.innovation_history.get(innovation_key) {
365            innovation
366        } else {
367            let innovation = self.next_innovation;
368            self.next_innovation += 1;
369            self.innovation_history
370                .insert(innovation_key.to_string(), innovation);
371            innovation
372        }
373    }
374}
375
376impl Default for InnovationTracker {
377    fn default() -> Self {
378        Self::new()
379    }
380}
381
382/// Convergence metrics for evolution monitoring
383#[derive(Debug, Clone, Serialize, Deserialize)]
384pub struct ConvergenceMetrics {
385    pub improvement_rate: f32,
386    pub stagnation_count: usize,
387    pub diversity_trend: Vec<f32>,
388    pub convergence_probability: f32,
389}
390
391/// Diversity metrics for population analysis
392#[derive(Debug, Clone, Serialize, Deserialize)]
393pub struct DiversityMetrics {
394    pub genotypic_diversity: f32,
395    pub phenotypic_diversity: f32,
396    pub novelty_distribution: Vec<f32>,
397    pub population_entropy: f32,
398}
399
400// ── Evaluation support ────────────────────────────────────────────────────────
401
402/// Dataset for architecture evaluation
403#[derive(Debug, Clone)]
404pub struct EvaluationDataset {
405    pub name: String,
406    pub train_triples: Vec<(String, String, String)>,
407    pub val_triples: Vec<(String, String, String)>,
408    pub test_triples: Option<Vec<(String, String, String)>>,
409    pub entity_vocab: std::collections::HashSet<String>,
410    pub relation_vocab: std::collections::HashSet<String>,
411}
412
413/// Profiling result for hardware measurement
414#[derive(Debug, Clone, Serialize, Deserialize)]
415pub struct ProfilingResult {
416    pub architecture_id: Uuid,
417    pub hardware_metrics: HardwareMetrics,
418    pub timestamp: DateTime<Utc>,
419    pub duration: Duration,
420}
421
422/// Hardware optimization result
423#[derive(Debug, Clone, Serialize, Deserialize)]
424pub struct OptimizationResult {
425    pub performance_improvement: f32,
426    pub efficiency_gain: f32,
427    pub confidence: f32,
428    pub modifications: Vec<String>,
429}