evolutionary-planning 0.1.1

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
488
489
490
491
492
493
494
495
496
497
//! Genetic algorithms: generational with elitism, and steady state.
//!
//! ```mermaid
//! graph LR
//!     init[initialise and evaluate] --> select[select parents]
//!     select --> cross[crossover with rate]
//!     cross --> mutate[mutate with rate]
//!     mutate --> repair[repair]
//!     repair --> eval[evaluate]
//!     eval --> refine[optional local search]
//!     refine --> replace[replace: generational with elites, or steady state]
//!     replace --> select
//! ```
//!
//! # Examples
//!
//! OneMax on 32 bits:
//!
//! ```
//! use evolutionary_planning::ga::{Config, Ga, Operators};
//! use evolutionary_planning::operators::{discrete::{BitFlip, RandomBits}, selection::Tournament, sequence::UniformCrossover, NoRepair};
//! use evolutionary_planning::{Limits, SplitMix64};
//!
//! let mut ga = Ga::new(Config::default(), Operators {
//!     init: RandomBits { len: 32 },
//!     selection: Tournament { size: 2 },
//!     crossover: UniformCrossover::default(),
//!     mutation: BitFlip::default(),
//!     repair: NoRepair,
//! });
//! let mut zeros = |bits: &Vec<bool>| bits.iter().filter(|&&b| !b).count() as f64;
//! let outcome = ga.run(&mut zeros, &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::operators::{Crossover, Initializer, Mutation, Repair, Selection};
use crate::rng::chance;
use crate::{Budget, Individual, Objective, Observer, Outcome, Stats, Stop};

/// How offspring enter the population.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Scheme {
    /// Each generation replaces the whole population except the
    /// [`Config::elitism`] best, which are copied unchanged.
    Generational,
    /// Each step breeds two offspring; each replaces the current worst individual. With
    /// `only_if_better`, only if it is strictly cheaper. One generation is counted per
    /// `population_size` offspring.
    SteadyState {
        /// Replace only when the offspring is strictly cheaper than the worst.
        only_if_better: bool,
    },
}

/// Parameters of a [`Ga`] run.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Config {
    /// The number of individuals, at least `2`.
    pub population_size: usize,
    /// Generational scheme: the number of best individuals copied unchanged into the next
    /// generation (clamped to `population_size - 1`, so every generation breeds). Ignored by
    /// the steady-state scheme, which is elitist by construction.
    pub elitism: usize,
    /// The probability that a pair of parents is recombined; otherwise the children are
    /// copies.
    pub crossover_rate: f64,
    /// The probability that a child is mutated at all; the mutation operator decides how
    /// much (for example a per-gene rate).
    pub mutation_rate: f64,
    /// Generational or steady state.
    pub scheme: Scheme,
}

impl Default for Config {
    /// 100 individuals, one elite, crossover rate 0.9, every child mutated, generational.
    fn default() -> Self {
        Self {
            population_size: 100,
            elitism: 1,
            crossover_rate: 0.9,
            mutation_rate: 1.0,
            scheme: Scheme::Generational,
        }
    }
}

/// The operators a [`Ga`] breeds with.
#[derive(Debug, Clone, PartialEq)]
pub struct Operators<I, S, X, M, P> {
    /// Creates the initial population.
    pub init: I,
    /// Picks parents.
    pub selection: S,
    /// Recombines two parents.
    pub crossover: X,
    /// Mutates a child.
    pub mutation: M,
    /// Repairs a child after variation; [`NoRepair`](crate::operators::NoRepair) if none.
    pub repair: P,
}

/// A genetic algorithm over any genome type.
#[derive(Debug, Clone, PartialEq)]
pub struct Ga<I, S, X, M, P> {
    /// The run parameters.
    pub config: Config,
    /// The breeding operators.
    pub operators: Operators<I, S, X, M, P>,
}

/// Something applied to every freshly evaluated offspring: nothing for a plain GA, a local
/// search for the memetic variant.
pub(crate) trait Refine<G> {
    /// Returns the (possibly improved) offspring; evaluations go through `evaluator`.
    fn refine<O, B, Obs, R>(
        &mut self,
        child: Individual<G>,
        evaluator: &mut Evaluator<'_, G, O, B, Obs>,
        rng: &mut R,
    ) -> Individual<G>
    where
        G: Clone,
        O: Objective<G> + ?Sized,
        B: Budget<Stats> + ?Sized,
        Obs: Observer + ?Sized,
        R: Rng + ?Sized;
}

/// The refinement that changes nothing.
pub(crate) struct NoRefine;

impl<G> Refine<G> for NoRefine {
    fn refine<O, B, Obs, R>(
        &mut self,
        child: Individual<G>,
        _evaluator: &mut Evaluator<'_, G, O, B, Obs>,
        _rng: &mut R,
    ) -> Individual<G>
    where
        G: Clone,
        O: Objective<G> + ?Sized,
        B: Budget<Stats> + ?Sized,
        Obs: Observer + ?Sized,
        R: Rng + ?Sized,
    {
        child
    }
}

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

    /// Runs the GA from a random initial population until the budget stops it.
    ///
    /// The returned [`Outcome::best`] is the best genome ever evaluated; the population is the
    /// last complete one.
    ///
    /// # Panics
    ///
    /// Panics if [`Config::population_size`] is below `2`.
    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,
    {
        self.run_from(Vec::new(), objective, budget, observer, rng)
    }

    /// Like [`Ga::run`], but the initial population starts with `seeds` (truncated to the
    /// population size) and is filled up by the initialiser.
    ///
    /// # Panics
    ///
    /// Panics if [`Config::population_size`] is below `2`.
    pub fn run_from<G, O, B, Obs, R>(
        &mut self,
        seeds: Vec<G>,
        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(seeds, &mut evaluator, &mut NoRefine, rng)
    }

    /// Runs the GA as a memetic algorithm: every evaluated offspring (and initial member)
    /// goes through `memetic`'s local search according to its schedule. Local search
    /// evaluations count against the same budget.
    ///
    /// # Panics
    ///
    /// Panics if [`Config::population_size`] is below `2`.
    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(Vec::new(), &mut evaluator, memetic, rng)
    }

    /// The shared driver of the plain and the memetic GA.
    pub(crate) fn evolve<G, O, B, Obs, F, R>(
        &mut self,
        seeds: Vec<G>,
        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,
    {
        assert!(
            self.config.population_size >= 2,
            "a GA needs at least two individuals"
        );

        let Some(mut population) = self.initial_population(seeds, evaluator, refine, rng) else {
            return finish(evaluator, Vec::new());
        };

        evaluator.end_generation(population_best(&population));
        match self.config.scheme {
            Scheme::Generational => self.generational(&mut population, evaluator, refine, rng),
            Scheme::SteadyState { only_if_better } => {
                self.steady_state(&mut population, only_if_better, evaluator, refine, rng);
            }
        }

        finish(evaluator, population)
    }

    /// Creates and evaluates the initial population; `None` if the budget ran out first.
    fn initial_population<G, O, B, Obs, F, R>(
        &mut self,
        seeds: Vec<G>,
        evaluator: &mut Evaluator<'_, G, O, B, Obs>,
        refine: &mut F,
        rng: &mut R,
    ) -> Option<Vec<Individual<G>>>
    where
        G: Clone,
        I: Initializer<G>,
        O: Objective<G> + ?Sized,
        B: Budget<Stats> + ?Sized,
        Obs: Observer + ?Sized,
        F: Refine<G>,
        R: Rng + ?Sized,
    {
        let size = self.config.population_size;
        let mut genomes = seeds;
        genomes.truncate(size);
        while genomes.len() < size {
            genomes.push(self.operators.init.initialize(rng));
        }

        let mut population = Vec::with_capacity(size);
        for genome in genomes {
            let individual = evaluator.individual(genome)?;
            population.push(refine.refine(individual, evaluator, rng));
        }

        Some(population)
    }

    /// Runs generations until the budget is exhausted.
    fn generational<G, O, B, Obs, F, R>(
        &mut self,
        population: &mut Vec<Individual<G>>,
        evaluator: &mut Evaluator<'_, G, O, B, Obs>,
        refine: &mut F,
        rng: &mut R,
    ) 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 size = self.config.population_size;
        let mut parents = Vec::with_capacity(2);
        loop {
            crate::cost::sort_population(population);
            let costs: Vec<f64> = population.iter().map(|i| i.cost).collect();
            let mut next: Vec<Individual<G>> =
                population[..self.config.elitism.min(size - 1)].to_vec();
            while next.len() < size {
                parents.clear();
                self.operators
                    .selection
                    .select(&costs, 2, rng, &mut parents);
                for child in self.breed(
                    &population[parents[0]].genome,
                    &population[parents[1]].genome,
                    rng,
                ) {
                    if next.len() == size {
                        break;
                    }

                    let Some(individual) = evaluator.individual(child) else {
                        return;
                    };
                    next.push(refine.refine(individual, evaluator, rng));
                }
            }

            *population = next;
            evaluator.end_generation(population_best(population));
        }
    }

    /// Runs steady-state steps until the budget is exhausted.
    fn steady_state<G, O, B, Obs, F, R>(
        &mut self,
        population: &mut [Individual<G>],
        only_if_better: bool,
        evaluator: &mut Evaluator<'_, G, O, B, Obs>,
        refine: &mut F,
        rng: &mut R,
    ) 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 mut parents = Vec::with_capacity(2);
        let mut offspring_in_generation = 0;
        loop {
            let costs: Vec<f64> = population.iter().map(|i| i.cost).collect();
            parents.clear();
            self.operators
                .selection
                .select(&costs, 2, rng, &mut parents);
            let children = self.breed(
                &population[parents[0]].genome,
                &population[parents[1]].genome,
                rng,
            );
            for child in children {
                let Some(individual) = evaluator.individual(child) else {
                    return;
                };

                let individual = refine.refine(individual, evaluator, rng);
                replace_worst(population, individual, only_if_better);
                offspring_in_generation += 1;
                if offspring_in_generation == population.len() {
                    offspring_in_generation = 0;
                    evaluator.end_generation(population_best(population));
                }
            }
        }
    }

    /// Produces two children of `a` and `b`: crossover, mutation and repair.
    fn breed<G, R>(&mut self, a: &G, b: &G, rng: &mut R) -> [G; 2]
    where
        G: Clone,
        X: Crossover<G>,
        M: Mutation<G>,
        P: Repair<G>,
        R: Rng + ?Sized,
    {
        let mut children = if chance(rng, self.config.crossover_rate) {
            self.operators.crossover.crossover(a, b, rng)
        } else {
            [a.clone(), b.clone()]
        };

        for child in &mut children {
            if chance(rng, self.config.mutation_rate) {
                self.operators.mutation.mutate(child, rng);
            }

            self.operators.repair.repair(child, rng);
        }

        children
    }
}

/// Replaces the worst individual (the last among equals) by `newcomer`, if allowed.
fn replace_worst<G>(
    population: &mut [Individual<G>],
    newcomer: Individual<G>,
    only_if_better: bool,
) {
    let worst = population
        .iter()
        .enumerate()
        .max_by(|a, b| a.1.cost.total_cmp(&b.1.cost).then(a.0.cmp(&b.0)))
        .map_or(0, |(index, _)| index);

    if !only_if_better || newcomer.cost < population[worst].cost {
        population[worst] = newcomer;
    }
}

/// The cheapest cost in a population.
pub(crate) fn population_best<G>(population: &[Individual<G>]) -> f64 {
    population
        .iter()
        .map(|i| i.cost)
        .fold(f64::INFINITY, f64::min)
}

/// Ends a run stopped by the budget.
fn finish<G: Clone, O, B, Obs>(
    evaluator: &mut Evaluator<'_, G, O, B, Obs>,
    population: Vec<Individual<G>>,
) -> Outcome<G>
where
    O: Objective<G> + ?Sized,
    B: Budget<Stats> + ?Sized,
    Obs: Observer + ?Sized,
{
    evaluator.outcome(population, Stop::Budget)
}

#[cfg(test)]
mod tests;