genesis-protocol 0.2.2

🧬 The first protocol for digital life - creating, evolving, and networking living digital organisms
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! 🧬 Evolution Engine - Biological Evolution for Digital Organisms
//!
//! This module implements the evolution engine that drives biological evolution
//! of TRON organisms through mutations, selection pressure, and fitness evaluation.

use crate::dna::{DigitalDNA, Mutation, DNAError};
use crate::tron::TRON;
use serde::{Deserialize, Serialize};
use std::time::{SystemTime, UNIX_EPOCH};

/// Evolution engine for managing organism evolution
#[derive(Debug, Clone)]
pub struct EvolutionEngine {
    /// Current evolution cycle
    pub current_cycle: u64,
    /// Selection pressure (0.0-1.0)
    pub selection_pressure: f64,
    /// Mutation rate (0.0-1.0)
    pub mutation_rate: f64,
    /// Evolution history
    pub evolution_history: Vec<EvolutionEvent>,
    /// Fitness statistics
    pub fitness_stats: FitnessStats,
    /// Evolution parameters
    pub parameters: EvolutionParameters,
}

/// Types of evolution mutations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MutationType {
    /// Beneficial mutation
    Beneficial,
    /// Neutral mutation
    Neutral,
    /// Harmful mutation
    Harmful,
    /// Adaptive mutation in response to environment
    Adaptive,
}

/// Selection pressure types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SelectionPressure {
    /// Natural selection based on fitness
    Natural,
    /// Sexual selection for reproduction
    Sexual,
    /// Environmental pressure
    Environmental,
    /// Competitive pressure from other organisms
    Competitive,
    /// Artificial selection by external forces
    Artificial,
}

/// Evolution event record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvolutionEvent {
    /// Event ID
    pub event_id: String,
    /// Organism that evolved
    pub organism_id: String,
    /// Evolution cycle when event occurred
    pub cycle: u64,
    /// Mutation applied
    pub mutation: Mutation,
    /// Fitness before evolution
    pub fitness_before: f64,
    /// Fitness after evolution
    pub fitness_after: f64,
    /// Selection pressure applied
    pub selection_pressure: f64,
    /// Timestamp
    pub timestamp: u64,
    /// Evolution outcome
    pub outcome: EvolutionOutcome,
}

/// Evolution outcome types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum EvolutionOutcome {
    /// Successful evolution
    Success,
    /// Evolution failed
    Failed,
    /// Organism became extinct
    Extinct,
    /// Organism created new species
    Speciation,
}

/// Evolution parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvolutionParameters {
    /// Base mutation rate
    pub base_mutation_rate: f64,
    /// Maximum mutations per cycle
    pub max_mutations_per_cycle: usize,
    /// Fitness threshold for survival
    pub survival_threshold: f64,
    /// Fitness threshold for reproduction
    pub reproduction_threshold: f64,
    /// Environmental adaptation factor
    pub adaptation_factor: f64,
    /// Sexual selection strength
    pub sexual_selection_strength: f64,
}

/// Fitness statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FitnessStats {
    /// Average fitness in population
    pub average_fitness: f64,
    /// Maximum fitness recorded
    pub max_fitness: f64,
    /// Minimum fitness recorded
    pub min_fitness: f64,
    /// Fitness variance
    pub fitness_variance: f64,
    /// Number of organisms tracked
    pub organism_count: usize,
}

impl EvolutionEngine {
    /// Create new evolution engine
    pub fn new() -> Result<Self, EvolutionError> {
        Ok(EvolutionEngine {
            current_cycle: 0,
            selection_pressure: 0.5,
            mutation_rate: 0.01,
            evolution_history: Vec::new(),
            fitness_stats: FitnessStats::new(),
            parameters: EvolutionParameters::default(),
        })
    }

    /// Evolve a single organism
    pub fn evolve_organism(&mut self, organism: &mut TRON) -> Result<EvolutionEvent, EvolutionError> {
        let fitness_before = organism.dna.fitness;
        
        // Apply selection pressure
        if fitness_before < self.parameters.survival_threshold {
            return Err(EvolutionError::InsufficientFitness(fitness_before));
        }
        
        // Generate mutation
        let mutation = self.generate_mutation(&organism.dna)?;
        
        // Apply mutation
        organism.dna.mutate(mutation.clone())
            .map_err(|e| EvolutionError::MutationFailed(e.to_string()))?;
        
        let fitness_after = organism.dna.fitness;
        
        // Record evolution event
        let event = EvolutionEvent {
            event_id: uuid::Uuid::new_v4().to_string(),
            organism_id: organism.id.clone(),
            cycle: self.current_cycle,
            mutation,
            fitness_before,
            fitness_after,
            selection_pressure: self.selection_pressure,
            timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(),
            outcome: if fitness_after > fitness_before {
                EvolutionOutcome::Success
            } else {
                EvolutionOutcome::Failed
            },
        };
        
        self.evolution_history.push(event.clone());
        self.fitness_stats.update_fitness(fitness_after);
        
        Ok(event)
    }

    /// Generate appropriate mutation based on organism state
    fn generate_mutation(&self, dna: &DigitalDNA) -> Result<Mutation, EvolutionError> {
        // Determine mutation type based on fitness and environment
        let mutation_type = if dna.fitness > 0.8 {
            MutationType::Beneficial
        } else if dna.fitness < 0.3 {
            MutationType::Adaptive
        } else {
            MutationType::Neutral
        };
        
        // Generate mutation based on type
        match mutation_type {
            MutationType::Beneficial => {
                // Beneficial mutations are rare but powerful
                if rand::random::<f64>() < 0.1 {
                    Ok(Mutation::KeyEvolution {
                        old_generation: dna.keypair.key_generation,
                        new_generation: dna.keypair.key_generation + 1,
                        timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(),
                    })
                } else {
                    Ok(dna.generate_random_mutation())
                }
            },
            MutationType::Adaptive => {
                // Adaptive mutations respond to environmental pressure
                Ok(Mutation::Duplication {
                    start: rand::random::<usize>() % dna.sequence.len(),
                    end: rand::random::<usize>() % dna.sequence.len(),
                    insert_at: rand::random::<usize>() % dna.sequence.len(),
                    timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(),
                })
            },
            _ => Ok(dna.generate_random_mutation()),
        }
    }

    /// Apply selection pressure to population
    pub fn apply_selection_pressure(&mut self, organisms: &mut Vec<TRON>) -> Result<Vec<String>, EvolutionError> {
        let mut eliminated = Vec::new();
        
        // Sort by fitness
        organisms.sort_by(|a, b| b.dna.fitness.partial_cmp(&a.dna.fitness).unwrap());
        
        // Apply selection pressure
        let elimination_threshold = self.selection_pressure;
        let mut to_eliminate = Vec::new();
        
        for organism in organisms.iter() {
            if organism.dna.fitness < elimination_threshold {
                to_eliminate.push(organism.id.clone());
            }
        }
        
        // Remove eliminated organisms
        organisms.retain(|o| !to_eliminate.contains(&o.id));
        eliminated.extend(to_eliminate);
        
        // Update fitness statistics
        self.update_population_stats(organisms);
        
        Ok(eliminated)
    }

    /// Update population fitness statistics
    fn update_population_stats(&mut self, organisms: &[TRON]) {
        if organisms.is_empty() {
            return;
        }
        
        let fitnesses: Vec<f64> = organisms.iter().map(|o| o.dna.fitness).collect();
        
        self.fitness_stats.organism_count = organisms.len();
        self.fitness_stats.average_fitness = fitnesses.iter().sum::<f64>() / organisms.len() as f64;
        self.fitness_stats.max_fitness = fitnesses.iter().cloned().fold(0.0, f64::max);
        self.fitness_stats.min_fitness = fitnesses.iter().cloned().fold(f64::INFINITY, f64::min);
        
        // Calculate variance
        let mean = self.fitness_stats.average_fitness;
        let variance = fitnesses.iter()
            .map(|f| (f - mean).powi(2))
            .sum::<f64>() / organisms.len() as f64;
        self.fitness_stats.fitness_variance = variance;
    }

    /// Get evolution statistics
    pub fn get_stats(&self) -> EvolutionStats {
        EvolutionStats {
            current_cycle: self.current_cycle,
            total_events: self.evolution_history.len(),
            successful_evolutions: self.evolution_history.iter()
                .filter(|e| matches!(e.outcome, EvolutionOutcome::Success))
                .count(),
            failed_evolutions: self.evolution_history.iter()
                .filter(|e| matches!(e.outcome, EvolutionOutcome::Failed))
                .count(),
            average_fitness: self.fitness_stats.average_fitness,
            max_fitness: self.fitness_stats.max_fitness,
            min_fitness: self.fitness_stats.min_fitness,
            selection_pressure: self.selection_pressure,
            mutation_rate: self.mutation_rate,
        }
    }

    /// Advance evolution cycle
    pub fn advance_cycle(&mut self) {
        self.current_cycle += 1;
        
        // Adjust mutation rate based on population health
        if self.fitness_stats.average_fitness > 0.8 {
            self.mutation_rate *= 0.9; // Reduce mutation rate in healthy populations
        } else if self.fitness_stats.average_fitness < 0.3 {
            self.mutation_rate *= 1.1; // Increase mutation rate in struggling populations
        }
        
        // Cap mutation rate
        self.mutation_rate = self.mutation_rate.max(0.001).min(0.1);
    }
}

/// Evolution statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvolutionStats {
    pub current_cycle: u64,
    pub total_events: usize,
    pub successful_evolutions: usize,
    pub failed_evolutions: usize,
    pub average_fitness: f64,
    pub max_fitness: f64,
    pub min_fitness: f64,
    pub selection_pressure: f64,
    pub mutation_rate: f64,
}

impl FitnessStats {
    pub fn new() -> Self {
        FitnessStats {
            average_fitness: 0.0,
            max_fitness: 0.0,
            min_fitness: f64::INFINITY,
            fitness_variance: 0.0,
            organism_count: 0,
        }
    }
    
    pub fn update_fitness(&mut self, fitness: f64) {
        self.organism_count += 1;
        
        if fitness > self.max_fitness {
            self.max_fitness = fitness;
        }
        
        if fitness < self.min_fitness {
            self.min_fitness = fitness;
        }
        
        // Update running average
        self.average_fitness = ((self.average_fitness * (self.organism_count - 1) as f64) + fitness) / self.organism_count as f64;
    }
}

impl Default for EvolutionParameters {
    fn default() -> Self {
        EvolutionParameters {
            base_mutation_rate: 0.01,
            max_mutations_per_cycle: 3,
            survival_threshold: 0.1,
            reproduction_threshold: 0.6,
            adaptation_factor: 0.8,
            sexual_selection_strength: 0.5,
        }
    }
}

/// Evolution-related errors
#[derive(Debug, thiserror::Error)]
pub enum EvolutionError {
    #[error("Insufficient fitness for evolution: {0}")]
    InsufficientFitness(f64),
    #[error("Mutation failed: {0}")]
    MutationFailed(String),
    #[error("Selection pressure too high: {0}")]
    SelectionPressureTooHigh(f64),
    #[error("Population extinct")]
    PopulationExtinct,
    #[error("Evolution cycle limit reached")]
    CycleLimitReached,
    #[error("DNA error: {0}")]
    DNA(#[from] DNAError),
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dna::DigitalDNA;

    #[test]
    fn test_evolution_engine_creation() {
        let engine = EvolutionEngine::new().unwrap();
        assert_eq!(engine.current_cycle, 0);
        assert_eq!(engine.selection_pressure, 0.5);
        assert_eq!(engine.mutation_rate, 0.01);
    }

    #[test]
    fn test_organism_evolution() {
        let mut engine = EvolutionEngine::new().unwrap();
        let mut organism = TRON::create_new().unwrap();
        
        let initial_fitness = organism.dna.fitness;
        let initial_generation = organism.dna.generation;
        
        let event = engine.evolve_organism(&mut organism).unwrap();
        
        assert_eq!(event.organism_id, organism.id);
        assert_eq!(event.fitness_before, initial_fitness);
        assert_eq!(organism.dna.generation, initial_generation + 1);
        assert_eq!(engine.evolution_history.len(), 1);
    }

    #[test]
    fn test_selection_pressure() {
        let mut engine = EvolutionEngine::new().unwrap();
        let mut organisms = Vec::new();
        
        // Create organisms with different fitness levels
        for i in 0..10 {
            let mut organism = TRON::create_new().unwrap();
            organism.dna.fitness = i as f64 / 10.0;
            organisms.push(organism);
        }
        
        engine.selection_pressure = 0.5;
        let eliminated = engine.apply_selection_pressure(&mut organisms).unwrap();
        
        // Low fitness organisms should be eliminated
        assert!(eliminated.len() > 0);
        assert!(organisms.len() < 10);
        
        // Remaining organisms should have higher fitness
        for organism in &organisms {
            assert!(organism.dna.fitness >= 0.5);
        }
    }

    #[test]
    fn test_fitness_stats() {
        let mut stats = FitnessStats::new();
        
        stats.update_fitness(0.8);
        stats.update_fitness(0.6);
        stats.update_fitness(0.9);
        
        assert_eq!(stats.organism_count, 3);
        assert_eq!(stats.max_fitness, 0.9);
        assert_eq!(stats.min_fitness, 0.6);
        assert!((stats.average_fitness - 0.7667).abs() < 0.01);
    }

    #[test]
    fn test_evolution_cycle_advance() {
        let mut engine = EvolutionEngine::new().unwrap();
        let initial_cycle = engine.current_cycle;
        
        engine.advance_cycle();
        
        assert_eq!(engine.current_cycle, initial_cycle + 1);
    }

    #[test]
    fn test_mutation_rate_adjustment() {
        let mut engine = EvolutionEngine::new().unwrap();
        
        // High fitness should reduce mutation rate
        engine.fitness_stats.average_fitness = 0.9;
        let initial_rate = engine.mutation_rate;
        
        engine.advance_cycle();
        
        assert!(engine.mutation_rate < initial_rate);
        
        // Low fitness should increase mutation rate
        engine.fitness_stats.average_fitness = 0.2;
        let current_rate = engine.mutation_rate;
        
        engine.advance_cycle();
        
        assert!(engine.mutation_rate > current_rate);
    }
}