evolutionary-planning 0.1.0

Evolutionary planning for no_std: genetic algorithms, evolution strategies, CMA-ES and multi-objective evolutionary search over plans, paths and trajectories
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
//! GLEAM's diffusion model (Jakob 2021, section 7): a structured population on a ring, where
//! every individual mates only within its deme of neighbours, so good genes spread slowly,
//! niches form and premature convergence is counteracted.
//!
//! ```mermaid
//! graph LR
//!     pick[next individual on the ring] --> partner[partner from its deme, linear ranking]
//!     partner --> breed[offspring: crossover, mutation, repair]
//!     breed --> best[best offspring, optional local search]
//!     best --> accept{acceptance rule}
//!     accept -->|accepted| replace[replace the individual]
//!     accept -->|rejected| pick
//!     replace --> pick
//! ```
//!
//! A generation visits every individual once, in ring order, updating in place. Two
//! stagnation counters serve as the termination criteria Jakob recommends: generations in
//! which no deme's best improved, and generations without any accepted offspring.
//!
//! ```
//! use evolutionary_planning::diffusion::{Config, Diffusion};
//! use evolutionary_planning::ga::Operators;
//! use evolutionary_planning::operators::{NoRepair, discrete::{BitFlip, RandomBits}, sequence::UniformCrossover};
//! use evolutionary_planning::operators::selection::LinearRanking;
//! use evolutionary_planning::problems::single::one_max;
//! use evolutionary_planning::{Limits, SplitMix64};
//!
//! let mut diffusion = Diffusion::new(Config::default(), Operators {
//!     init: RandomBits { len: 30 },
//!     selection: LinearRanking::default(),
//!     crossover: UniformCrossover::default(),
//!     mutation: BitFlip::default(),
//!     repair: NoRepair,
//! });
//! let mut objective = |b: &Vec<bool>| one_max(b);
//! let outcome = diffusion.run(&mut objective, &mut Limits::evaluations(20_000).with_target(0.0), &mut (), &mut SplitMix64::new(1));
//! assert_eq!(outcome.best_cost(), 0.0);
//! ```

use alloc::vec::Vec;

use rand_core::Rng;

use crate::evaluator::Evaluator;
use crate::ga::{NoRefine, Operators, Refine, population_best};
use crate::operators::{Crossover, Initializer, Mutation, Repair, Selection};
use crate::rng::chance;
use crate::{Budget, Individual, Objective, Observer, Outcome, Stats, Stop};

/// When the best offspring of a mating replaces the individual at the deme's centre.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Acceptance {
    /// Always.
    Always,
    /// If it is better than the worst individual of the deme ("local least").
    LocalLeast,
    /// If it is better than the individual it replaces.
    BetterParent,
    /// If it is better than the deme's worst, and, when the individual is the deme's best,
    /// also better than it ("local elitist"); the default.
    LocalElitist,
}

/// Parameters of a [`Diffusion`] run.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Config {
    /// The number of individuals on the ring, at least twice the deme size.
    pub population_size: usize,
    /// The deme size (the individual and its neighbours), odd, at least `3`; Jakob reports
    /// 7 to 11 as typical. An even value is rounded up.
    pub deme_size: usize,
    /// Offspring produced per mating; the best one competes for acceptance.
    pub offspring: usize,
    /// The probability that a mating uses crossover (otherwise the offspring are mutants of
    /// the individual).
    pub crossover_rate: f64,
    /// The acceptance rule.
    pub acceptance: Acceptance,
    /// Stop after this many consecutive generations in which no deme's best improved.
    pub max_without_improvement: Option<u64>,
    /// Stop after this many consecutive generations without an accepted offspring.
    pub max_without_acceptance: Option<u64>,
}

impl Default for Config {
    /// 60 individuals, demes of 9, two offspring per mating, crossover rate 0.6, local
    /// elitist acceptance, no stagnation limits.
    fn default() -> Self {
        Self {
            population_size: 60,
            deme_size: 9,
            offspring: 2,
            crossover_rate: 0.6,
            acceptance: Acceptance::LocalElitist,
            max_without_improvement: None,
            max_without_acceptance: None,
        }
    }
}

/// The diffusion-model GA; the [`Operators::selection`] picks the partner within the deme
/// (linear ranking is GLEAM's choice).
#[derive(Debug, Clone, PartialEq)]
pub struct Diffusion<I, S, X, M, P> {
    /// The run parameters.
    pub config: Config,
    /// The breeding operators.
    pub operators: Operators<I, S, X, M, P>,
}

/// What one generation changed.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
struct Progress {
    /// Some accepted offspring beat its deme's best.
    improved: bool,
    /// Some offspring was accepted.
    accepted: bool,
}

impl<I, S, X, M, P> Diffusion<I, S, X, M, P> {
    /// Creates a diffusion-model run.
    #[must_use]
    pub fn new(config: Config, operators: Operators<I, S, X, M, P>) -> Self {
        Self { config, operators }
    }

    /// Runs until the budget or a stagnation limit stops it.
    ///
    /// # Panics
    ///
    /// Panics if the population is smaller than twice the deme size.
    pub fn run<G, O, B, Obs, R>(
        &mut self,
        objective: &mut O,
        budget: &mut B,
        observer: &mut Obs,
        rng: &mut R,
    ) -> Outcome<G>
    where
        G: Clone,
        I: Initializer<G>,
        S: Selection,
        X: Crossover<G>,
        M: Mutation<G>,
        P: Repair<G>,
        O: Objective<G> + ?Sized,
        B: Budget<Stats> + ?Sized,
        Obs: Observer + ?Sized,
        R: Rng + ?Sized,
    {
        let mut evaluator = Evaluator::new(objective, budget, observer);

        self.evolve(&mut evaluator, &mut NoRefine, rng)
    }

    /// Runs HyGLEAM: the diffusion model with `memetic` local search on the best offspring of
    /// every mating.
    ///
    /// # Panics
    ///
    /// Panics if the population is smaller than twice the deme size.
    pub fn run_memetic<G, L, O, B, Obs, R>(
        &mut self,
        memetic: &mut crate::memetic::Memetic<L>,
        objective: &mut O,
        budget: &mut B,
        observer: &mut Obs,
        rng: &mut R,
    ) -> Outcome<G>
    where
        G: Clone,
        I: Initializer<G>,
        S: Selection,
        X: Crossover<G>,
        M: Mutation<G>,
        P: Repair<G>,
        L: crate::memetic::LocalSearch<G>,
        O: Objective<G> + ?Sized,
        B: Budget<Stats> + ?Sized,
        Obs: Observer + ?Sized,
        R: Rng + ?Sized,
    {
        let mut evaluator = Evaluator::new(objective, budget, observer);

        self.evolve(&mut evaluator, memetic, rng)
    }

    /// The generation loop with its stagnation counters.
    fn evolve<G, O, B, Obs, F, R>(
        &mut self,
        evaluator: &mut Evaluator<'_, G, O, B, Obs>,
        refine: &mut F,
        rng: &mut R,
    ) -> Outcome<G>
    where
        G: Clone,
        I: Initializer<G>,
        S: Selection,
        X: Crossover<G>,
        M: Mutation<G>,
        P: Repair<G>,
        O: Objective<G> + ?Sized,
        B: Budget<Stats> + ?Sized,
        Obs: Observer + ?Sized,
        F: Refine<G>,
        R: Rng + ?Sized,
    {
        let radius = self.config.deme_size.max(3) / 2;
        assert!(
            self.config.population_size >= 2 * (2 * radius + 1),
            "the ring must hold at least two demes"
        );

        let mut population = Vec::with_capacity(self.config.population_size);
        for _ in 0..self.config.population_size {
            let Some(member) = evaluator.individual(self.operators.init.initialize(rng)) else {
                return evaluator.outcome(population, Stop::Budget);
            };
            population.push(member);
        }

        evaluator.end_generation(population_best(&population));
        let (mut without_improvement, mut without_acceptance) = (0, 0);
        loop {
            let Some(progress) = self.generation(&mut population, radius, evaluator, refine, rng)
            else {
                return evaluator.outcome(population, Stop::Budget);
            };

            evaluator.end_generation(population_best(&population));
            without_improvement = if progress.improved {
                0
            } else {
                without_improvement + 1
            };
            without_acceptance = if progress.accepted {
                0
            } else {
                without_acceptance + 1
            };
            let stagnated = self
                .config
                .max_without_improvement
                .is_some_and(|m| without_improvement >= m)
                || self
                    .config
                    .max_without_acceptance
                    .is_some_and(|m| without_acceptance >= m);
            if stagnated {
                return evaluator.outcome(population, Stop::Stagnation);
            }
        }
    }

    /// One pass over the ring; `None` if the budget ran out.
    fn generation<G, O, B, Obs, F, R>(
        &mut self,
        population: &mut [Individual<G>],
        radius: usize,
        evaluator: &mut Evaluator<'_, G, O, B, Obs>,
        refine: &mut F,
        rng: &mut R,
    ) -> Option<Progress>
    where
        G: Clone,
        S: Selection,
        X: Crossover<G>,
        M: Mutation<G>,
        P: Repair<G>,
        O: Objective<G> + ?Sized,
        B: Budget<Stats> + ?Sized,
        Obs: Observer + ?Sized,
        F: Refine<G>,
        R: Rng + ?Sized,
    {
        let n = population.len();
        let mut progress = Progress::default();
        let mut picked = Vec::with_capacity(1);
        for centre in 0..n {
            let neighbours: Vec<usize> = (1..=radius)
                .flat_map(|d| [(centre + n - d) % n, (centre + d) % n])
                .collect();
            let costs: Vec<f64> = neighbours.iter().map(|&k| population[k].cost).collect();
            picked.clear();
            self.operators.selection.select(&costs, 1, rng, &mut picked);
            let partner = neighbours[picked[0]];
            let child = self.best_offspring(
                &population[centre].genome,
                &population[partner].genome,
                evaluator,
                rng,
            )?;
            let child = refine.refine(child, evaluator, rng);
            let deme_worst = costs
                .iter()
                .copied()
                .fold(population[centre].cost, f64::max);
            let deme_best = costs
                .iter()
                .copied()
                .fold(population[centre].cost, f64::min);
            if self.accepts(&child, &population[centre], deme_worst, deme_best) {
                progress.accepted = true;
                progress.improved |= child.cost < deme_best;
                population[centre] = child;
            }
        }

        Some(progress)
    }

    /// Breeds `offspring` children of the individual and its partner and returns the best.
    fn best_offspring<G, O, B, Obs, R>(
        &mut self,
        own: &G,
        partner: &G,
        evaluator: &mut Evaluator<'_, G, O, B, Obs>,
        rng: &mut R,
    ) -> Option<Individual<G>>
    where
        G: Clone,
        X: Crossover<G>,
        M: Mutation<G>,
        P: Repair<G>,
        O: Objective<G> + ?Sized,
        B: Budget<Stats> + ?Sized,
        Obs: Observer + ?Sized,
        R: Rng + ?Sized,
    {
        let mut best: Option<Individual<G>> = None;
        let mut produced = 0;
        while produced < self.config.offspring.max(1) {
            let children = if chance(rng, self.config.crossover_rate) {
                self.operators.crossover.crossover(own, partner, rng)
            } else {
                [own.clone(), own.clone()]
            };

            for mut child in children {
                if produced == self.config.offspring.max(1) {
                    break;
                }

                self.operators.mutation.mutate(&mut child, rng);
                self.operators.repair.repair(&mut child, rng);
                let individual = evaluator.individual(child)?;
                produced += 1;
                if best.as_ref().is_none_or(|b| individual.cost < b.cost) {
                    best = Some(individual);
                }
            }
        }

        best
    }

    /// The acceptance rule.
    fn accepts<G>(
        &self,
        child: &Individual<G>,
        parent: &Individual<G>,
        deme_worst: f64,
        deme_best: f64,
    ) -> bool {
        match self.config.acceptance {
            Acceptance::Always => true,
            Acceptance::LocalLeast => child.cost < deme_worst,
            Acceptance::BetterParent => child.cost < parent.cost,
            Acceptance::LocalElitist => {
                let parent_is_best = parent.cost <= deme_best;
                child.cost < deme_worst && (!parent_is_best || child.cost < parent.cost)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::memetic::{Memetic, Rosenbrock};
    use crate::operators::NoRepair;
    use crate::operators::real::{Bounds, Polynomial, Sbx};
    use crate::operators::selection::LinearRanking;
    use crate::problems::single::{rastrigin, rosenbrock};
    use crate::{Limits, SplitMix64};

    /// A real-coded diffusion model.
    fn real(
        acceptance: Acceptance,
        bounds: &Bounds,
    ) -> Diffusion<Bounds, LinearRanking, Sbx, Polynomial, NoRepair> {
        Diffusion::new(
            Config {
                acceptance,
                ..Config::default()
            },
            Operators {
                init: bounds.clone(),
                selection: LinearRanking::default(),
                crossover: Sbx {
                    bounds: bounds.clone(),
                    eta: 15.0,
                },
                mutation: Polynomial {
                    bounds: bounds.clone(),
                    eta: 20.0,
                    rate: None,
                },
                repair: NoRepair,
            },
        )
    }

    /// Every acceptance rule makes progress on Rastrigin; the elitist ones (better parent,
    /// local elitist) never lose the best individual.
    #[test]
    fn acceptance_rules() {
        let bounds = Bounds::uniform(4, -5.12, 5.12);
        for acceptance in [
            Acceptance::Always,
            Acceptance::LocalLeast,
            Acceptance::BetterParent,
            Acceptance::LocalElitist,
        ] {
            let mut objective = |x: &Vec<f64>| rastrigin(x);
            let outcome = real(acceptance, &bounds).run(
                &mut objective,
                &mut Limits::evaluations(40_000),
                &mut (),
                &mut SplitMix64::new(2),
            );
            assert!(
                outcome.best_cost() < 3.0,
                "{acceptance:?}: {}",
                outcome.best_cost()
            );
            if matches!(
                acceptance,
                Acceptance::BetterParent | Acceptance::LocalElitist
            ) {
                assert_eq!(
                    outcome.population[0].cost,
                    outcome.best_cost(),
                    "{acceptance:?}"
                );
            }
        }
    }

    /// The stagnation criteria end a converged run before the budget.
    #[test]
    fn stagnation_stops() {
        let bounds = Bounds::uniform(2, -1.0, 1.0);
        let mut diffusion = real(Acceptance::BetterParent, &bounds);
        diffusion.config.max_without_acceptance = Some(5);
        let mut flat = |_: &Vec<f64>| 1.0;
        let outcome = diffusion.run(
            &mut flat,
            &mut Limits::evaluations(1_000_000),
            &mut (),
            &mut SplitMix64::new(1),
        );

        assert_eq!(outcome.stop, Stop::Stagnation);
        assert!(outcome.stats.evaluations < 1_000);
    }

    /// HyGLEAM (diffusion model plus adaptive Rosenbrock search) solves Rosenbrock in 5D.
    #[test]
    fn hygleam_solves_rosenbrock() {
        let bounds = Bounds::uniform(5, -2.0, 2.0);
        let mut diffusion = real(Acceptance::LocalElitist, &bounds);
        diffusion.config.population_size = 20;
        diffusion.config.deme_size = 5;
        let mut memetic = Memetic::adaptive(Rosenbrock::default());
        let mut objective = |x: &Vec<f64>| rosenbrock(x);
        let outcome = diffusion.run_memetic(
            &mut memetic,
            &mut objective,
            &mut Limits::evaluations(40_000),
            &mut (),
            &mut SplitMix64::new(3),
        );

        assert!(outcome.best_cost() < 1e-6, "{}", outcome.best_cost());
    }
}