radiate-selectors 1.3.0

Selection strategies for the Radiate genetic algorithm library.
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
#[cfg(test)]
mod selector_tests {
    use radiate_test::*;
    use radiate_core::*;
    use radiate_selectors::*;
    use rstest::*;

    fn fitness_improvement_metric(
        population: &Population<FloatChromosome<f32>>,
        selected: &Population<FloatChromosome<f32>>,
        objectives: &Objective,
    ) -> f32 {
        let population_avg: f32 = population
            .iter()
            .map(|ind| ind.genotype()[0].as_slice()[0].allele())
            .sum::<f32>()
            / population.len() as f32;

        let selected_avg: f32 = selected
            .iter()
            .map(|ind| ind.genotype()[0].as_slice()[0].allele())
            .sum::<f32>()
            / selected.len() as f32;

        if let Objective::Single(optimize) = objectives {
            match optimize {
                Optimize::Minimize => population_avg - selected_avg,
                Optimize::Maximize => selected_avg - population_avg,
            }
        } else {
            panic!("Objective must be single");
        }
    }

    #[rstest]
    #[case(10, Optimize::Minimize)]
    #[case(20, Optimize::Minimize)]
    #[case(30, Optimize::Minimize)]
    #[case(10, Optimize::Maximize)]
    #[case(20, Optimize::Maximize)]
    #[case(30, Optimize::Maximize)]
    fn elite_selector_selects(#[case] num: usize, #[case] optimize: Optimize) {
        let mut population = float_population(100);
        optimize.sort(&mut population);

        let selector = EliteSelector::new();
        let selected = selector.select(population.as_ref(), &Objective::Single(optimize), num);

        for i in 0..num {
            let original = population[i].score().unwrap().as_f32();
            let selected = population[selected[i]].score().unwrap().as_f32();

            assert_eq!(original, selected);
        }

        assert_eq!(selected.len(), num);
    }

    #[rstest]
    #[case(BoltzmannSelector::new(4.0), Optimize::Minimize, 80)]
    #[case(BoltzmannSelector::new(1.0), Optimize::Minimize, 80)]
    #[case(BoltzmannSelector::new(4.0), Optimize::Maximize, 80)]
    #[case(BoltzmannSelector::new(1.0), Optimize::Maximize, 80)]
    #[case(RouletteSelector::new(), Optimize::Minimize, 80)]
    #[case(RouletteSelector::new(), Optimize::Maximize, 80)]
    #[case(TournamentSelector::new(3), Optimize::Minimize, 80)]
    #[case(TournamentSelector::new(3), Optimize::Maximize, 80)]
    #[case(RankSelector::new(), Optimize::Minimize, 80)]
    #[case(RankSelector::new(), Optimize::Maximize, 80)]
    #[case(StochasticUniversalSamplingSelector::new(), Optimize::Minimize, 80)]
    #[case(StochasticUniversalSamplingSelector::new(), Optimize::Maximize, 80)]
    fn test_probability_selectors_better_than_random(
        #[case] selector: impl Select<FloatChromosome<f32>>,
        #[case] optimize: Optimize,
        #[case] count: usize,
    ) {
        let num_permutations = 1000;
        let objectives = Objective::Single(optimize);

        let mut population = random_float_population(100);
        optimize.sort(&mut population);

        let mut better_than_random = 0;

        let random_selector = RandomSelector::new();

        for _ in 0..num_permutations {
            let selected = selector
                .select(population.as_ref(), &objectives, count)
                .into_iter()
                .map(|idx| population[idx].clone())
                .collect::<Population<FloatChromosome<f32>>>();
            let random_selected = random_selector
                .select(population.as_ref(), &objectives, count)
                .into_iter()
                .map(|idx| population[idx].clone())
                .collect::<Population<FloatChromosome<f32>>>();

            assert_eq!(selected.len(), count);
            assert_eq!(random_selected.len(), count);

            let observed_metric = fitness_improvement_metric(&population, &selected, &objectives);
            let random_metric =
                fitness_improvement_metric(&population, &random_selected, &objectives);

            if random_metric < observed_metric {
                better_than_random += 1;
            }
        }

        let percent_better_than_random = better_than_random as f32 / num_permutations as f32;

        assert!(percent_better_than_random > 0.9);
    }

    /// Every selector must return exactly `count` indices when `count <=
    /// pop.len()`.
    #[rstest]
    #[case(BoltzmannSelector::new(4.0))]
    #[case(RouletteSelector::new())]
    #[case(TournamentSelector::new(3))]
    #[case(RankSelector::new())]
    #[case(StochasticUniversalSamplingSelector::new())]
    #[case(RandomSelector::new())]
    fn selector_returns_requested_count(#[case] selector: impl Select<FloatChromosome<f32>>) {
        seeded(7, || {
            let population = random_float_population(50);
            let objective = Objective::Single(Optimize::Maximize);

            for &count in &[1, 5, 25, 50] {
                let selected = selector.select(population.as_ref(), &objective, count);
                assert_eq!(
                    selected.len(),
                    count,
                    "selector returned {} indices, expected {count}",
                    selected.len()
                );
            }
        });
    }

    #[rstest]
    #[case(BoltzmannSelector::new(4.0))]
    #[case(RouletteSelector::new())]
    #[case(TournamentSelector::new(3))]
    #[case(RankSelector::new())]
    #[case(StochasticUniversalSamplingSelector::new())]
    fn selector_indices_in_bounds(#[case] selector: impl Select<FloatChromosome<f32>>) {
        seeded(8, || {
            let population = random_float_population(50);
            let objective = Objective::Single(Optimize::Maximize);

            let selected = selector.select(population.as_ref(), &objective, 100);
            for idx in selected {
                assert!(
                    idx < population.len(),
                    "selector returned out-of-bounds idx {idx} for pop of size {}",
                    population.len()
                );
            }
        });
    }

    /// Tournament with k=1 degenerates to uniform random sampling — each
    /// "tournament" is a single random pick. Empirical frequency for each
    /// index should be close to 1/N. Catches bugs where k=1 accidentally
    /// short-circuits to "always pick idx 0".
    #[test]
    fn tournament_k1_approaches_uniform() {
        seeded(9, || {
            const POP_SIZE: usize = 10;
            const SAMPLES: usize = 20_000;
            const EXPECTED: f32 = SAMPLES as f32 / POP_SIZE as f32; // ~2000 per slot

            let mut population = float_population(POP_SIZE);
            Optimize::Maximize.sort(&mut population);

            let selector = TournamentSelector::new(1);
            let objective = Objective::Single(Optimize::Maximize);

            let mut counts = vec![0usize; POP_SIZE];
            for _ in 0..SAMPLES {
                let chosen = selector.select(population.as_ref(), &objective, 1);
                counts[chosen[0]] += 1;
            }

            // 5σ bound for binomial: σ = sqrt(N * p * (1-p)) ≈ sqrt(20000 * 0.1 * 0.9) ≈ 42.
            // 5σ ≈ 210; allow generous 400 to keep the test stable.
            for (i, &c) in counts.iter().enumerate() {
                let deviation = (c as f32 - EXPECTED).abs();
                assert!(
                    deviation < 400.0,
                    "tournament k=1 skewed at idx {i}: count {c}, expected ~{EXPECTED}, deviation {deviation}"
                );
            }
        });
    }

    /// Boltzmann at very high temperature should concentrate sampling
    /// on the best individual — `temperature` controls the sharpness of
    /// the softmax over scores. Catches sign-error and weight-computation
    /// bugs that "beats random" would miss because a slightly-wrong
    /// distribution still beats random.
    #[test]
    fn boltzmann_high_temp_concentrates_on_best() {
        seeded(10, || {
            const POP_SIZE: usize = 10;
            const SAMPLES: usize = 5_000;

            let mut population = float_population(POP_SIZE);
            // Sort descending by score so idx 0 is best for Maximize.
            Optimize::Maximize.sort(&mut population);

            let selector = BoltzmannSelector::new(50.0); // very peaked
            let objective = Objective::Single(Optimize::Maximize);

            let mut counts = vec![0usize; POP_SIZE];
            for _ in 0..SAMPLES {
                let chosen = selector.select(population.as_ref(), &objective, 1);
                counts[chosen[0]] += 1;
            }

            // The best (idx 0) should be picked >50% of the time at this temperature.
            let best_share = counts[0] as f32 / SAMPLES as f32;
            assert!(
                best_share > 0.5,
                "high-temp Boltzmann should concentrate on best; got {best_share:.3} at idx 0"
            );
        });
    }

    /// Boltzmann monotonicity: better-scoring phenotypes get sampled at
    /// least as often as worse ones
    #[test]
    fn boltzmann_monotone_in_score() {
        seeded(11, || {
            const POP_SIZE: usize = 10;
            const SAMPLES: usize = 5_000;

            let mut population = float_population(POP_SIZE);
            // Score equals index. Sort descending so idx 0 is highest.
            Optimize::Maximize.sort(&mut population);

            let selector = BoltzmannSelector::new(2.0);
            let objective = Objective::Single(Optimize::Maximize);

            let mut counts = vec![0f32; POP_SIZE];
            for _ in 0..SAMPLES {
                let chosen = selector.select(population.as_ref(), &objective, 1);
                counts[chosen[0]] += 1.0;
            }

            // Top half (best 5) should be sampled more than bottom half.
            let top: f32 = counts[..POP_SIZE / 2].iter().sum();
            let bot: f32 = counts[POP_SIZE / 2..].iter().sum();
            assert!(
                top > bot,
                "Boltzmann not monotone: top half count {top} <= bottom half count {bot}"
            );
        });
    }

    /// Selection pressure must increase with `k`. With pop sorted
    /// descending by score, larger tournaments pick lower (better)
    /// indices on average. Catches regressions where the inner tournament
    /// loop doesn't actually reduce `best` (e.g. wrong comparison sign,
    /// off-by-one in the loop bound).
    #[test]
    fn tournament_pressure_grows_with_k() {
        seeded(12, || {
            const POP_SIZE: usize = 20;
            const SAMPLES: usize = 5_000;

            let mut population = float_population(POP_SIZE);
            Optimize::Maximize.sort(&mut population);
            let objective = Objective::Single(Optimize::Maximize);

            let mean_idx = |k: usize| -> f32 {
                let selector = TournamentSelector::new(k);
                let total: usize = (0..SAMPLES)
                    .map(|_| selector.select(population.as_ref(), &objective, 1)[0])
                    .sum();
                total as f32 / SAMPLES as f32
            };

            let k1 = mean_idx(1);
            let k5 = mean_idx(5);
            let k10 = mean_idx(10);

            // Each step up in k should pull the mean idx lower (toward 0 = best).
            assert!(
                k5 < k1 - 1.0 && k10 < k5 - 0.5,
                "tournament not selective enough: k=1 mean {k1:.2}, k=5 mean {k5:.2}, k=10 mean {k10:.2}"
            );
        });
    }

    /// Roulette monotonicity: parallel to `boltzmann_monotone_in_score`
    /// but exercises the roulette weight transform (`scale_l1_affine_sorted`)
    /// rather than the softmax. Catches sign-error in the affine-rescale
    /// step that's specific to roulette.
    #[test]
    fn roulette_monotone_in_score() {
        seeded(13, || {
            const POP_SIZE: usize = 10;
            const SAMPLES: usize = 5_000;

            let mut population = float_population(POP_SIZE);
            Optimize::Maximize.sort(&mut population);

            let selector = RouletteSelector::new();
            let objective = Objective::Single(Optimize::Maximize);

            let mut counts = vec![0f32; POP_SIZE];
            for _ in 0..SAMPLES {
                let chosen = selector.select(population.as_ref(), &objective, 1);
                counts[chosen[0]] += 1.0;
            }

            let top: f32 = counts[..POP_SIZE / 2].iter().sum();
            let bot: f32 = counts[POP_SIZE / 2..].iter().sum();
            assert!(
                top > bot,
                "Roulette not monotone: top half count {top} <= bottom half count {bot}"
            );
        });
    }

    /// Rank selection's defining property: distribution depends on
    /// *order*, not on absolute scores. Two pops with identical rank but
    /// wildly different score magnitudes must produce statistically
    /// indistinguishable selection counts. Catches regressions where the
    /// rank selector accidentally uses raw scores in the wheel.
    #[test]
    fn rank_ignores_score_magnitude() {
        const POP_SIZE: usize = 10;
        const SAMPLES: usize = 5_000;

        let sample = |scale: f32, seed: u64| -> Vec<usize> {
            seeded(seed, || {
                // Pop with linearly increasing scores, scaled by `scale`.
                // Both runs see the SAME rank order; only the magnitudes differ.
                let phenotypes: Vec<_> = (0..POP_SIZE)
                    .map(|i| {
                        let chrom = FloatChromosome::new(vec![FloatGene::from(0.0..1.0)]);
                        let mut p = Phenotype::from((vec![chrom], 0));
                        p.set_score(Some(Score::from((i as f32) * scale)));
                        p
                    })
                    .collect();
                let mut population = Population::new(phenotypes);
                Optimize::Maximize.sort(&mut population);

                let selector = RankSelector::new();
                let objective = Objective::Single(Optimize::Maximize);
                let mut counts = vec![0usize; POP_SIZE];
                for _ in 0..SAMPLES {
                    let chosen = selector.select(population.as_ref(), &objective, 1);
                    counts[chosen[0]] += 1;
                }
                counts
            })
        };

        // Use the same seed so the random draws are identical; only
        // the scaling differs. With identical RNG and rank-only
        // selection, counts must match exactly.
        let counts_small = sample(1e-3, 42);
        let counts_large = sample(1e3, 42);

        assert_eq!(
            counts_small, counts_large,
            "rank selector is not magnitude-invariant: {counts_small:?} vs {counts_large:?}"
        );
    }

    /// All-tied scores -> no preference for any phenotype. Selectors that
    /// silently bias toward idx 0 (e.g. tournament that always returns
    /// `best = 0` initially) skew the distribution. Each phenotype should
    /// appear in roughly `SAMPLES / POP_SIZE` of the picks.
    #[rstest]
    #[case(BoltzmannSelector::new(2.0))]
    #[case(RouletteSelector::new())]
    #[case(TournamentSelector::new(3))]
    fn equal_scores_produce_balanced_selection(
        #[case] selector: impl Select<FloatChromosome<f32>>,
    ) {
        seeded(14, || {
            const POP_SIZE: usize = 10;
            const SAMPLES: usize = 10_000;
            const EXPECTED: f32 = SAMPLES as f32 / POP_SIZE as f32;

            // Every phenotype gets the same score.
            let phenotypes: Vec<_> = (0..POP_SIZE)
                .map(|_| {
                    let chrom = FloatChromosome::new(vec![FloatGene::from(0.0..1.0)]);
                    let mut p = Phenotype::from((vec![chrom], 0));
                    p.set_score(Some(Score::from(5.0)));
                    p
                })
                .collect();
            let population = Population::new(phenotypes);
            let objective = Objective::Single(Optimize::Maximize);

            let mut counts = vec![0usize; POP_SIZE];
            for _ in 0..SAMPLES {
                let chosen = selector.select(population.as_ref(), &objective, 1);
                counts[chosen[0]] += 1;
            }

            // No bin should be more than ~3x the expected uniform count
            // — generous bound that still catches "always idx 0" bugs.
            for (i, &c) in counts.iter().enumerate() {
                assert!(
                    (c as f32) < EXPECTED * 3.0,
                    "tied-score selection skewed at idx {i}: count {c}, expected ~{EXPECTED}"
                );
            }
        });
    }
}