fugue-evo 0.3.1

An implementation of fugue for running evolutionary algorithms as Bayesian inference: priors and likelihoods as probabilistic programs, tempered SMC in trace space, annealed optimization, Pareto posteriors - plus a standalone classical EC toolkit
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
//! Diagnostics and statistics
//!
//! This module provides statistics collection and analysis for evolutionary runs.

pub mod convergence;

use std::time::Duration;

use serde::{Deserialize, Serialize};

use crate::fitness::traits::FitnessValue;
use crate::genome::traits::EvolutionaryGenome;
use crate::population::population::Population;

/// Statistics for a single generation
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GenerationStats {
    /// Generation number
    pub generation: usize,
    /// Total fitness evaluations so far
    pub evaluations: usize,
    /// Best fitness in this generation
    pub best_fitness: f64,
    /// Worst fitness in this generation
    pub worst_fitness: f64,
    /// Mean fitness
    pub mean_fitness: f64,
    /// Median fitness
    pub median_fitness: f64,
    /// Fitness standard deviation
    pub fitness_std: f64,
    /// Population diversity
    pub diversity: f64,
    /// Timing information
    pub timing: TimingStats,
}

/// Timing statistics
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct TimingStats {
    /// Time spent on fitness evaluation (ms)
    pub evaluation_ms: f64,
    /// Time spent on selection (ms)
    pub selection_ms: f64,
    /// Time spent on crossover (ms)
    pub crossover_ms: f64,
    /// Time spent on mutation (ms)
    pub mutation_ms: f64,
    /// Total generation time (ms)
    pub total_ms: f64,
}

impl TimingStats {
    /// Create new timing stats
    pub fn new() -> Self {
        Self::default()
    }

    /// Set evaluation time
    pub fn with_evaluation(mut self, duration: Duration) -> Self {
        self.evaluation_ms = duration.as_secs_f64() * 1000.0;
        self
    }

    /// Set selection time
    pub fn with_selection(mut self, duration: Duration) -> Self {
        self.selection_ms = duration.as_secs_f64() * 1000.0;
        self
    }

    /// Set crossover time
    pub fn with_crossover(mut self, duration: Duration) -> Self {
        self.crossover_ms = duration.as_secs_f64() * 1000.0;
        self
    }

    /// Set mutation time
    pub fn with_mutation(mut self, duration: Duration) -> Self {
        self.mutation_ms = duration.as_secs_f64() * 1000.0;
        self
    }

    /// Set total time
    pub fn with_total(mut self, duration: Duration) -> Self {
        self.total_ms = duration.as_secs_f64() * 1000.0;
        self
    }
}

impl GenerationStats {
    /// Compute statistics from a population
    pub fn from_population<G, F>(
        population: &Population<G, F>,
        generation: usize,
        evaluations: usize,
    ) -> Self
    where
        G: EvolutionaryGenome,
        F: FitnessValue,
    {
        let mut fitnesses: Vec<f64> = population
            .iter()
            .filter_map(|i| i.fitness.as_ref().map(|f| f.to_f64()))
            .collect();

        if fitnesses.is_empty() {
            return Self {
                generation,
                evaluations,
                best_fitness: f64::NEG_INFINITY,
                worst_fitness: f64::INFINITY,
                mean_fitness: 0.0,
                median_fitness: 0.0,
                fitness_std: 0.0,
                diversity: 0.0,
                timing: TimingStats::default(),
            };
        }

        fitnesses.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let best = fitnesses.last().copied().unwrap_or(f64::NEG_INFINITY);
        let worst = fitnesses.first().copied().unwrap_or(f64::INFINITY);
        let mean = fitnesses.iter().sum::<f64>() / fitnesses.len() as f64;
        let median = if fitnesses.len().is_multiple_of(2) {
            (fitnesses[fitnesses.len() / 2 - 1] + fitnesses[fitnesses.len() / 2]) / 2.0
        } else {
            fitnesses[fitnesses.len() / 2]
        };

        let variance = if fitnesses.len() > 1 {
            fitnesses.iter().map(|f| (f - mean).powi(2)).sum::<f64>() / (fitnesses.len() - 1) as f64
        } else {
            0.0
        };
        let std = variance.sqrt();

        let diversity = population.diversity();

        Self {
            generation,
            evaluations,
            best_fitness: best,
            worst_fitness: worst,
            mean_fitness: mean,
            median_fitness: median,
            fitness_std: std,
            diversity,
            timing: TimingStats::default(),
        }
    }

    /// Set timing information
    pub fn with_timing(mut self, timing: TimingStats) -> Self {
        self.timing = timing;
        self
    }
}

/// Statistics collector for an entire evolution run
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct EvolutionStats {
    /// Statistics per generation
    pub generations: Vec<GenerationStats>,
    /// Total runtime in milliseconds
    pub total_runtime_ms: f64,
    /// Reason for termination
    pub termination_reason: Option<String>,
}

impl EvolutionStats {
    /// Create a new stats collector
    pub fn new() -> Self {
        Self::default()
    }

    /// Record a generation's statistics
    pub fn record(&mut self, stats: GenerationStats) {
        self.generations.push(stats);
    }

    /// Get the number of generations recorded
    pub fn num_generations(&self) -> usize {
        self.generations.len()
    }

    /// Get the best fitness across all generations
    pub fn best_fitness(&self) -> Option<f64> {
        self.generations
            .iter()
            .map(|g| g.best_fitness)
            .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
    }

    /// Get the final best fitness
    pub fn final_best_fitness(&self) -> Option<f64> {
        self.generations.last().map(|g| g.best_fitness)
    }

    /// Get the history of best fitness values
    pub fn best_fitness_history(&self) -> Vec<f64> {
        self.generations.iter().map(|g| g.best_fitness).collect()
    }

    /// Get the history of mean fitness values
    pub fn mean_fitness_history(&self) -> Vec<f64> {
        self.generations.iter().map(|g| g.mean_fitness).collect()
    }

    /// Get the history of diversity values
    pub fn diversity_history(&self) -> Vec<f64> {
        self.generations.iter().map(|g| g.diversity).collect()
    }

    /// Set the termination reason
    pub fn set_termination_reason(&mut self, reason: &str) {
        self.termination_reason = Some(reason.to_string());
    }

    /// Set the total runtime
    pub fn set_runtime(&mut self, duration: Duration) {
        self.total_runtime_ms = duration.as_secs_f64() * 1000.0;
    }

    /// Get a summary of the evolution run
    pub fn summary(&self) -> String {
        let best = self.best_fitness().unwrap_or(f64::NEG_INFINITY);
        let final_best = self.final_best_fitness().unwrap_or(f64::NEG_INFINITY);
        let generations = self.num_generations();
        let runtime = self.total_runtime_ms;

        format!(
            "Evolution Summary:\n\
             - Generations: {}\n\
             - Best fitness: {:.6}\n\
             - Final best: {:.6}\n\
             - Runtime: {:.2}ms\n\
             - Termination: {}",
            generations,
            best,
            final_best,
            runtime,
            self.termination_reason.as_deref().unwrap_or("unknown")
        )
    }
}

/// Result of an evolution run
#[derive(Clone, Debug)]
pub struct EvolutionResult<G, F = f64>
where
    G: EvolutionaryGenome,
    F: FitnessValue,
{
    /// The best genome found
    pub best_genome: G,
    /// The best fitness value
    pub best_fitness: F,
    /// Number of generations completed
    pub generations: usize,
    /// Total fitness evaluations
    pub evaluations: usize,
    /// Statistics for the run
    pub stats: EvolutionStats,
}

impl<G, F> EvolutionResult<G, F>
where
    G: EvolutionaryGenome,
    F: FitnessValue,
{
    /// Create a new evolution result
    pub fn new(best_genome: G, best_fitness: F, generations: usize, evaluations: usize) -> Self {
        Self {
            best_genome,
            best_fitness,
            generations,
            evaluations,
            stats: EvolutionStats::new(),
        }
    }

    /// Add statistics to the result
    pub fn with_stats(mut self, stats: EvolutionStats) -> Self {
        self.stats = stats;
        self
    }
}

pub mod prelude {
    pub use super::convergence::{
        detect_stagnation, evolutionary_ess, evolutionary_ess_log, evolutionary_rhat,
        fitness_convergence, ConvergenceConfig, ConvergenceDetector, ConvergenceReason,
        ConvergenceStatus,
    };
    pub use super::{EvolutionResult, EvolutionStats, GenerationStats, TimingStats};
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::genome::real_vector::RealVector;
    use crate::population::individual::Individual;

    fn create_test_population() -> Population<RealVector> {
        let individuals = vec![
            Individual::with_fitness(RealVector::new(vec![1.0]), 10.0),
            Individual::with_fitness(RealVector::new(vec![2.0]), 20.0),
            Individual::with_fitness(RealVector::new(vec![3.0]), 30.0),
            Individual::with_fitness(RealVector::new(vec![4.0]), 40.0),
            Individual::with_fitness(RealVector::new(vec![5.0]), 50.0),
        ];
        Population::from_individuals(individuals)
    }

    #[test]
    fn test_generation_stats_from_population() {
        let pop = create_test_population();
        let stats = GenerationStats::from_population(&pop, 10, 100);

        assert_eq!(stats.generation, 10);
        assert_eq!(stats.evaluations, 100);
        assert_eq!(stats.best_fitness, 50.0);
        assert_eq!(stats.worst_fitness, 10.0);
        assert_eq!(stats.mean_fitness, 30.0);
        assert_eq!(stats.median_fitness, 30.0);
        assert!(stats.fitness_std > 15.0 && stats.fitness_std < 16.0);
    }

    #[test]
    fn test_generation_stats_empty_population() {
        let pop: Population<RealVector> = Population::new();
        let stats = GenerationStats::from_population(&pop, 0, 0);

        assert_eq!(stats.best_fitness, f64::NEG_INFINITY);
        assert_eq!(stats.worst_fitness, f64::INFINITY);
    }

    #[test]
    fn test_evolution_stats_record() {
        let mut stats = EvolutionStats::new();
        let pop = create_test_population();

        for i in 0..5 {
            let gen_stats = GenerationStats::from_population(&pop, i, i * 10);
            stats.record(gen_stats);
        }

        assert_eq!(stats.num_generations(), 5);
        assert_eq!(stats.best_fitness(), Some(50.0));
    }

    #[test]
    fn test_evolution_stats_history() {
        let mut stats = EvolutionStats::new();

        for i in 0..5 {
            stats.record(GenerationStats {
                generation: i,
                evaluations: i * 10,
                best_fitness: (i + 1) as f64 * 10.0,
                worst_fitness: 0.0,
                mean_fitness: (i + 1) as f64 * 5.0,
                median_fitness: 0.0,
                fitness_std: 0.0,
                diversity: 1.0 / (i + 1) as f64,
                timing: TimingStats::default(),
            });
        }

        let best_history = stats.best_fitness_history();
        assert_eq!(best_history, vec![10.0, 20.0, 30.0, 40.0, 50.0]);

        let mean_history = stats.mean_fitness_history();
        assert_eq!(mean_history, vec![5.0, 10.0, 15.0, 20.0, 25.0]);
    }

    #[test]
    fn test_evolution_stats_summary() {
        let mut stats = EvolutionStats::new();
        stats.record(GenerationStats {
            generation: 0,
            evaluations: 100,
            best_fitness: 50.0,
            worst_fitness: 10.0,
            mean_fitness: 30.0,
            median_fitness: 30.0,
            fitness_std: 15.0,
            diversity: 0.5,
            timing: TimingStats::default(),
        });
        stats.set_termination_reason("Target reached");
        stats.set_runtime(Duration::from_millis(1234));

        let summary = stats.summary();
        assert!(summary.contains("Generations: 1"));
        assert!(summary.contains("Best fitness: 50"));
        assert!(summary.contains("Target reached"));
    }

    #[test]
    fn test_timing_stats() {
        let timing = TimingStats::new()
            .with_evaluation(Duration::from_millis(100))
            .with_selection(Duration::from_millis(20))
            .with_crossover(Duration::from_millis(30))
            .with_mutation(Duration::from_millis(10))
            .with_total(Duration::from_millis(160));

        assert!((timing.evaluation_ms - 100.0).abs() < 0.1);
        assert!((timing.selection_ms - 20.0).abs() < 0.1);
        assert!((timing.crossover_ms - 30.0).abs() < 0.1);
        assert!((timing.mutation_ms - 10.0).abs() < 0.1);
        assert!((timing.total_ms - 160.0).abs() < 0.1);
    }

    #[test]
    fn test_evolution_result() {
        let genome = RealVector::new(vec![1.0, 2.0, 3.0]);
        let result = EvolutionResult::new(genome, 42.0, 100, 1000);

        assert_eq!(result.best_fitness, 42.0);
        assert_eq!(result.generations, 100);
        assert_eq!(result.evaluations, 1000);
    }
}