fugue-evo 0.2.0

Evolutionary computation for Rust: classical EC algorithms plus evolutionary inference - evolutionary algorithms as probabilistic programs (tempered SMC in trace space, built on fugue-ppl)
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Selection operators
//!
//! This module provides various selection operators for genetic algorithms.

use rand::seq::SliceRandom;
use rand::Rng;
use rand_distr::{Distribution, WeightedIndex};

use crate::genome::traits::EvolutionaryGenome;
use crate::operators::traits::SelectionOperator;

/// Tournament selection operator
///
/// Selects the best individual from a randomly sampled subset (the
/// "tournament") of the population.
///
/// # Sampling with vs. without replacement
///
/// By default competitors are sampled **with replacement** (each of the `k`
/// competitors is drawn independently and uniformly from the whole
/// population). This is the textbook model, for which the probability that the
/// individual of rank `i` wins has the clean closed form and selection pressure
/// grows smoothly with `k`; the same individual may appear more than once in a
/// tournament. Crucially, `tournament_size >= population size` does **not**
/// make selection deterministic under this model — a draw of `k = n`
/// competitors with replacement includes the global best only with probability
/// `1 - ((n-1)/n)^k` (≈ 0.63 at `k = n`).
///
/// The without-replacement variant (constructed via
/// [`without_replacement`](Self::without_replacement)) instead draws `k`
/// *distinct* individuals; there the tournament size is capped at the
/// population size and `k >= n` degenerates to fully elitist selection (the
/// global best is chosen every call).
#[derive(Clone, Debug)]
pub struct TournamentSelection {
    /// Tournament size (number of individuals competing)
    pub tournament_size: usize,
    /// Whether competitors are sampled with replacement (canonical default) or
    /// as distinct individuals.
    pub with_replacement: bool,
}

impl TournamentSelection {
    /// Create a new tournament selection with the given size (sampling with
    /// replacement, the canonical model).
    pub fn new(tournament_size: usize) -> Self {
        assert!(tournament_size >= 1, "Tournament size must be at least 1");
        Self {
            tournament_size,
            with_replacement: true,
        }
    }

    /// Create binary tournament selection (size = 2, with replacement)
    pub fn binary() -> Self {
        Self::new(2)
    }

    /// Create a tournament selection that samples `tournament_size` **distinct**
    /// competitors (without replacement).
    ///
    /// Note that with this variant `tournament_size >= population size` selects
    /// the global best deterministically every call.
    pub fn without_replacement(tournament_size: usize) -> Self {
        assert!(tournament_size >= 1, "Tournament size must be at least 1");
        Self {
            tournament_size,
            with_replacement: false,
        }
    }
}

impl<G: EvolutionaryGenome> SelectionOperator<G> for TournamentSelection {
    fn select<R: Rng>(&self, population: &[(G, f64)], rng: &mut R) -> usize {
        assert!(!population.is_empty(), "Population cannot be empty");

        let n = population.len();

        let tournament: Vec<usize> = if self.with_replacement {
            // Canonical: k competitors drawn i.i.d. with replacement.
            (0..self.tournament_size)
                .map(|_| rng.gen_range(0..n))
                .collect()
        } else {
            // Distinct competitors; cannot draw more than the population size.
            let k = self.tournament_size.min(n);
            (0..n)
                .collect::<Vec<usize>>()
                .choose_multiple(rng, k)
                .copied()
                .collect()
        };

        // Find the best in the tournament
        tournament
            .into_iter()
            .max_by(|&a, &b| {
                population[a]
                    .1
                    .partial_cmp(&population[b].1)
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
            .unwrap()
    }
}

/// Roulette wheel selection (fitness proportionate)
///
/// Selection probability is proportional to fitness.
#[derive(Clone, Debug)]
pub struct RouletteSelection {
    /// Offset to ensure all fitnesses are positive
    offset: f64,
}

impl RouletteSelection {
    /// Create a new roulette selection
    pub fn new() -> Self {
        Self { offset: 0.0 }
    }

    /// Create with a fitness offset (to handle negative fitness)
    pub fn with_offset(offset: f64) -> Self {
        Self { offset }
    }
}

impl Default for RouletteSelection {
    fn default() -> Self {
        Self::new()
    }
}

impl<G: EvolutionaryGenome> SelectionOperator<G> for RouletteSelection {
    fn select<R: Rng>(&self, population: &[(G, f64)], rng: &mut R) -> usize {
        assert!(!population.is_empty(), "Population cannot be empty");

        // Find minimum fitness and compute offset
        let min_fitness = population
            .iter()
            .map(|(_, f)| *f)
            .min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .unwrap();

        let offset = if min_fitness < 0.0 {
            -min_fitness + self.offset + 1.0
        } else {
            self.offset
        };

        // Compute weights
        let weights: Vec<f64> = population.iter().map(|(_, f)| f + offset).collect();

        // Handle case where all weights are zero
        let total: f64 = weights.iter().sum();
        if total <= 0.0 {
            return rng.gen_range(0..population.len());
        }

        // Create weighted distribution
        match WeightedIndex::new(&weights) {
            Ok(dist) => dist.sample(rng),
            Err(_) => rng.gen_range(0..population.len()),
        }
    }
}

/// Truncation selection
///
/// Selects only from the top percentage of the population.
#[derive(Clone, Debug)]
pub struct TruncationSelection {
    /// Fraction of population to select from (0.0 to 1.0)
    pub truncation_ratio: f64,
}

impl TruncationSelection {
    /// Create a new truncation selection
    pub fn new(truncation_ratio: f64) -> Self {
        assert!(
            truncation_ratio > 0.0 && truncation_ratio <= 1.0,
            "Truncation ratio must be in (0, 1]"
        );
        Self { truncation_ratio }
    }
}

impl<G: EvolutionaryGenome> SelectionOperator<G> for TruncationSelection {
    fn select<R: Rng>(&self, population: &[(G, f64)], rng: &mut R) -> usize {
        assert!(!population.is_empty(), "Population cannot be empty");

        // Sort indices by fitness (descending)
        let mut indices: Vec<usize> = (0..population.len()).collect();
        indices.sort_by(|&a, &b| {
            population[b]
                .1
                .partial_cmp(&population[a].1)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Select from top portion
        let cutoff = ((population.len() as f64) * self.truncation_ratio).ceil() as usize;
        let cutoff = cutoff.max(1);

        indices[rng.gen_range(0..cutoff)]
    }
}

/// Rank-based selection
///
/// Selection probability is based on rank rather than raw fitness.
#[derive(Clone, Debug)]
pub struct RankSelection {
    /// Selection pressure (1.0 = uniform, 2.0 = strong pressure)
    pub selection_pressure: f64,
}

impl RankSelection {
    /// Create a new rank selection
    pub fn new(selection_pressure: f64) -> Self {
        assert!(
            (1.0..=2.0).contains(&selection_pressure),
            "Selection pressure must be in [1.0, 2.0]"
        );
        Self { selection_pressure }
    }
}

impl Default for RankSelection {
    fn default() -> Self {
        Self::new(1.5)
    }
}

impl<G: EvolutionaryGenome> SelectionOperator<G> for RankSelection {
    fn select<R: Rng>(&self, population: &[(G, f64)], rng: &mut R) -> usize {
        assert!(!population.is_empty(), "Population cannot be empty");

        let n = population.len();
        let sp = self.selection_pressure;

        // Sort indices by fitness (ascending - worst first)
        let mut indices: Vec<usize> = (0..n).collect();
        indices.sort_by(|&a, &b| {
            population[a]
                .1
                .partial_cmp(&population[b].1)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Compute rank-based weights using Baker's linear ranking
        // weight(i) = 2 - sp + 2(sp - 1)(rank - 1)/(n - 1)
        let weights: Vec<f64> = (0..n)
            .map(|rank| {
                if n == 1 {
                    1.0
                } else {
                    2.0 - sp + 2.0 * (sp - 1.0) * (rank as f64) / ((n - 1) as f64)
                }
            })
            .collect();

        match WeightedIndex::new(&weights) {
            Ok(dist) => indices[dist.sample(rng)],
            Err(_) => indices[rng.gen_range(0..n)],
        }
    }
}

/// Boltzmann selection (temperature-based)
///
/// Uses softmax of fitness values scaled by temperature.
#[derive(Clone, Debug)]
pub struct BoltzmannSelection {
    /// Temperature parameter (higher = more uniform, lower = more greedy)
    pub temperature: f64,
}

impl BoltzmannSelection {
    /// Create a new Boltzmann selection
    pub fn new(temperature: f64) -> Self {
        assert!(temperature > 0.0, "Temperature must be positive");
        Self { temperature }
    }
}

impl<G: EvolutionaryGenome> SelectionOperator<G> for BoltzmannSelection {
    fn select<R: Rng>(&self, population: &[(G, f64)], rng: &mut R) -> usize {
        assert!(!population.is_empty(), "Population cannot be empty");

        // Use log-sum-exp trick for numerical stability
        let scaled: Vec<f64> = population
            .iter()
            .map(|(_, f)| f / self.temperature)
            .collect();
        let max_scaled = scaled.iter().cloned().fold(f64::NEG_INFINITY, f64::max);

        let weights: Vec<f64> = scaled.iter().map(|s| (s - max_scaled).exp()).collect();

        match WeightedIndex::new(&weights) {
            Ok(dist) => dist.sample(rng),
            Err(_) => rng.gen_range(0..population.len()),
        }
    }
}

/// Random selection (uniform)
#[derive(Clone, Debug, Default)]
pub struct RandomSelection;

impl RandomSelection {
    /// Create a new random selection
    pub fn new() -> Self {
        Self
    }
}

impl<G: EvolutionaryGenome> SelectionOperator<G> for RandomSelection {
    fn select<R: Rng>(&self, population: &[(G, f64)], rng: &mut R) -> usize {
        assert!(!population.is_empty(), "Population cannot be empty");
        rng.gen_range(0..population.len())
    }
}

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

    fn create_population(size: usize) -> Vec<(RealVector, f64)> {
        (0..size)
            .map(|i| (RealVector::new(vec![i as f64]), i as f64))
            .collect()
    }

    #[test]
    fn test_tournament_selection_selects_valid_index() {
        let mut rng = rand::thread_rng();
        let population = create_population(10);
        let selection = TournamentSelection::new(3);

        for _ in 0..100 {
            let idx = selection.select(&population, &mut rng);
            assert!(idx < population.len());
        }
    }

    #[test]
    fn test_tournament_selection_binary() {
        let selection = TournamentSelection::binary();
        assert_eq!(selection.tournament_size, 2);
    }

    #[test]
    fn test_tournament_selection_prefers_fitter() {
        let mut rng = rand::thread_rng();
        // Population with clear fitness difference
        let population: Vec<(RealVector, f64)> = vec![
            (RealVector::new(vec![0.0]), 0.0),
            (RealVector::new(vec![1.0]), 100.0), // Much fitter
            (RealVector::new(vec![2.0]), 0.0),
        ];

        // Without-replacement full tournament draws all distinct individuals,
        // so it selects the best deterministically.
        let selection = TournamentSelection::without_replacement(3);

        let mut best_count = 0;
        let trials = 100;
        for _ in 0..trials {
            let idx = selection.select(&population, &mut rng);
            if idx == 1 {
                best_count += 1;
            }
        }

        // With a full without-replacement tournament, should always select the best
        assert_eq!(best_count, trials);
    }

    #[test]
    fn test_tournament_with_replacement_is_not_deterministic_at_full_size() {
        // regression: EV-104 — the default (with-replacement) tournament must
        // NOT collapse to fully elitist selection when tournament_size ==
        // population size. Pre-fix, sampling was without replacement and
        // clamped to the population, so k >= n selected the global best on
        // every call (best_count would equal trials). With canonical
        // with-replacement sampling the global best is included only with
        // probability 1 - ((n-1)/n)^k, so it is selected only part of the time.
        use rand::SeedableRng;
        let mut rng = rand::rngs::StdRng::seed_from_u64(7);

        // Unique global maximum at index 4.
        let population: Vec<(RealVector, f64)> = (0..5)
            .map(|i| (RealVector::new(vec![i as f64]), i as f64))
            .collect();

        let selection = TournamentSelection::new(5); // k == n, with replacement
        assert!(selection.with_replacement);

        let trials = 300;
        let mut best_count = 0;
        for _ in 0..trials {
            if selection.select(&population, &mut rng) == 4 {
                best_count += 1;
            }
        }

        // Expected P(best selected) = 1 - (4/5)^5 ≈ 0.672, so the count must be
        // strictly below `trials` (the pre-fix without-replacement code would
        // give exactly `trials`).
        assert!(
            best_count < trials,
            "with-replacement tournament should not be deterministic at k == n (got {best_count}/{trials})"
        );
        // ...but the best should still win the clear majority of the time.
        assert!(
            best_count > trials / 2,
            "expected the fittest to win most tournaments (got {best_count}/{trials})"
        );
    }

    #[test]
    fn test_tournament_without_replacement_full_size_is_elitist() {
        // The retained without-replacement variant selects the global best
        // deterministically when tournament_size >= population size.
        let mut rng = rand::thread_rng();
        let population: Vec<(RealVector, f64)> = (0..5)
            .map(|i| (RealVector::new(vec![i as f64]), i as f64))
            .collect();

        let selection = TournamentSelection::without_replacement(5);
        for _ in 0..50 {
            assert_eq!(selection.select(&population, &mut rng), 4);
        }
    }

    #[test]
    fn test_roulette_selection_selects_valid_index() {
        let mut rng = rand::thread_rng();
        let population = create_population(10);
        let selection = RouletteSelection::new();

        for _ in 0..100 {
            let idx = selection.select(&population, &mut rng);
            assert!(idx < population.len());
        }
    }

    #[test]
    fn test_roulette_selection_handles_negative_fitness() {
        let mut rng = rand::thread_rng();
        let population: Vec<(RealVector, f64)> = vec![
            (RealVector::new(vec![0.0]), -10.0),
            (RealVector::new(vec![1.0]), -5.0),
            (RealVector::new(vec![2.0]), -1.0),
        ];

        let selection = RouletteSelection::new();

        for _ in 0..100 {
            let idx = selection.select(&population, &mut rng);
            assert!(idx < population.len());
        }
    }

    #[test]
    fn test_truncation_selection_selects_from_top() {
        let mut rng = rand::thread_rng();
        let population = create_population(10);
        let selection = TruncationSelection::new(0.2); // Top 20%

        for _ in 0..100 {
            let idx = selection.select(&population, &mut rng);
            // Top 20% of [0..9] should be indices 8 or 9
            assert!(idx >= 8);
        }
    }

    #[test]
    fn test_rank_selection_selects_valid_index() {
        let mut rng = rand::thread_rng();
        let population = create_population(10);
        let selection = RankSelection::new(1.5);

        for _ in 0..100 {
            let idx = selection.select(&population, &mut rng);
            assert!(idx < population.len());
        }
    }

    #[test]
    fn test_boltzmann_selection_selects_valid_index() {
        let mut rng = rand::thread_rng();
        let population = create_population(10);
        let selection = BoltzmannSelection::new(1.0);

        for _ in 0..100 {
            let idx = selection.select(&population, &mut rng);
            assert!(idx < population.len());
        }
    }

    #[test]
    fn test_boltzmann_selection_temperature_effect() {
        let mut rng = rand::thread_rng();
        // Population with clear fitness difference
        let population: Vec<(RealVector, f64)> = vec![
            (RealVector::new(vec![0.0]), 0.0),
            (RealVector::new(vec![1.0]), 10.0),
        ];

        // Low temperature = more greedy
        let low_temp = BoltzmannSelection::new(0.1);
        // High temperature = more uniform
        let high_temp = BoltzmannSelection::new(100.0);

        let mut low_best_count = 0;
        let mut high_best_count = 0;
        let trials = 1000;

        for _ in 0..trials {
            if low_temp.select(&population, &mut rng) == 1 {
                low_best_count += 1;
            }
            if high_temp.select(&population, &mut rng) == 1 {
                high_best_count += 1;
            }
        }

        // Low temperature should select best more often
        assert!(low_best_count > high_best_count);
    }

    #[test]
    fn test_random_selection_uniform() {
        let mut rng = rand::thread_rng();
        let population = create_population(2);
        let selection = RandomSelection::new();

        let mut counts = [0, 0];
        let trials = 1000;

        for _ in 0..trials {
            counts[selection.select(&population, &mut rng)] += 1;
        }

        // Should be roughly 50-50 (with some variance)
        let ratio = counts[0] as f64 / counts[1] as f64;
        assert!(ratio > 0.8 && ratio < 1.2);
    }

    #[test]
    fn test_select_many() {
        let mut rng = rand::thread_rng();
        let population = create_population(10);
        let selection = TournamentSelection::new(3);

        let indices = selection.select_many(&population, 5, &mut rng);
        assert_eq!(indices.len(), 5);
        for idx in indices {
            assert!(idx < population.len());
        }
    }

    #[test]
    #[should_panic(expected = "Tournament size must be at least 1")]
    fn test_tournament_size_zero() {
        TournamentSelection::new(0);
    }

    #[test]
    #[should_panic(expected = "Truncation ratio must be in (0, 1]")]
    fn test_truncation_ratio_zero() {
        TruncationSelection::new(0.0);
    }

    #[test]
    #[should_panic(expected = "Temperature must be positive")]
    fn test_boltzmann_temperature_zero() {
        BoltzmannSelection::new(0.0);
    }
}