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
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
//! Genetic path planning on graphs and grids: a genome is a simple path from start to goal,
//! initialised by randomised depth-first search, recombined at shared vertices, mutated by
//! rerouting a stretch, and repaired by cutting loops.
//!
//! ```
//! use evolutionary_planning::graph_path::{Config, Grid, dijkstra, solve};
//! use evolutionary_planning::{Limits, SplitMix64};
//!
//! // A 10 x 10 grid with a wall that has a gap at the bottom.
//! let mut grid = Grid::open(10, 10, false);
//! for y in 0..9 {
//!     grid.block(5, y);
//! }
//! let (start, goal) = (grid.node(0, 0), grid.node(9, 0));
//! let (path, cost) = solve(&grid, start, goal, &Config::default(), &mut Limits::evaluations(20_000), &mut SplitMix64::new(1)).expect("reachable");
//! assert_eq!(Some(cost), dijkstra(&grid, start, goal).map(|(_, c)| c));
//! assert_eq!((path[0], path[path.len() - 1]), (start, goal));
//! ```

use alloc::collections::BinaryHeap;
use alloc::vec::Vec;
use core::cmp::Ordering;

use rand_core::Rng;

use crate::ga::{Config as GaConfig, Ga, Operators, Scheme};
use crate::operators::selection::Tournament;
use crate::operators::{Crossover, Initializer, Mutation, Repair};
use crate::rng::{below, shuffle, two_sorted_indices};
use crate::{Budget, Objective, Stats};

/// A directed graph with non-negative edge costs over vertices `0..node_count()`.
pub trait Graph {
    /// The number of vertices.
    fn node_count(&self) -> usize;

    /// Appends the out-edges `(target, cost)` of `node` to `out`; costs are non-negative and
    /// finite.
    fn neighbours(&self, node: usize, out: &mut Vec<(usize, f64)>);

    /// The cost of the edge `from -> to`, if it exists (the cheapest of parallel edges).
    fn edge_cost(&self, from: usize, to: usize) -> Option<f64> {
        let mut out = Vec::new();
        self.neighbours(from, &mut out);

        out.into_iter()
            .filter(|&(v, _)| v == to)
            .map(|(_, c)| c)
            .min_by(f64::total_cmp)
    }
}

/// A graph given by adjacency lists.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct AdjacencyList {
    /// `edges[v]` lists the out-edges `(target, cost)` of `v`.
    pub edges: Vec<Vec<(usize, f64)>>,
}

impl Graph for AdjacencyList {
    fn node_count(&self) -> usize {
        self.edges.len()
    }

    fn neighbours(&self, node: usize, out: &mut Vec<(usize, f64)>) {
        out.extend_from_slice(&self.edges[node]);
    }

    fn edge_cost(&self, from: usize, to: usize) -> Option<f64> {
        self.edges[from]
            .iter()
            .filter(|&&(v, _)| v == to)
            .map(|&(_, c)| c)
            .min_by(f64::total_cmp)
    }
}

/// A 4- or 8-connected grid with blocked cells; straight moves cost `1`, diagonal moves
/// `√2` (and may not cut blocked corners). Vertex `y * width + x` is cell `(x, y)`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Grid {
    /// The number of columns.
    width: usize,
    /// The number of rows.
    height: usize,
    /// Whether each cell is blocked, row-major.
    blocked: Vec<bool>,
    /// Whether diagonal moves are allowed.
    diagonal: bool,
}

impl Grid {
    /// A grid without obstacles.
    #[must_use]
    pub fn open(width: usize, height: usize, diagonal: bool) -> Self {
        Self {
            width,
            height,
            blocked: alloc::vec![false; width * height],
            diagonal,
        }
    }

    /// Blocks cell `(x, y)`.
    ///
    /// # Panics
    ///
    /// Panics if the cell is outside the grid.
    pub fn block(&mut self, x: usize, y: usize) {
        assert!(x < self.width && y < self.height, "cell outside the grid");
        self.blocked[y * self.width + x] = true;
    }

    /// The vertex of cell `(x, y)`.
    #[must_use]
    pub fn node(&self, x: usize, y: usize) -> usize {
        y * self.width + x
    }

    /// Whether cell `(x, y)` is inside the grid and not blocked.
    #[must_use]
    pub fn is_free(&self, x: usize, y: usize) -> bool {
        x < self.width && y < self.height && !self.blocked[y * self.width + x]
    }

    /// The cell `(x, y)` of a vertex.
    #[must_use]
    pub fn cell(&self, node: usize) -> (usize, usize) {
        (node % self.width, node / self.width)
    }

    /// Whether cell `(x, y)` is free; cells outside the grid are not.
    fn free(&self, x: isize, y: isize) -> bool {
        let (Ok(x), Ok(y)) = (usize::try_from(x), usize::try_from(y)) else {
            return false;
        };

        x < self.width && y < self.height && !self.blocked[y * self.width + x]
    }
}

impl Graph for Grid {
    fn node_count(&self) -> usize {
        self.width * self.height
    }

    fn neighbours(&self, node: usize, out: &mut Vec<(usize, f64)>) {
        let (x, y) = self.cell(node);
        let (Ok(x), Ok(y)) = (isize::try_from(x), isize::try_from(y)) else {
            return;
        };

        if !self.free(x, y) {
            return;
        }

        for (dx, dy) in [
            (1, 0),
            (-1, 0),
            (0, 1),
            (0, -1),
            (1, 1),
            (1, -1),
            (-1, 1),
            (-1, -1),
        ] {
            let diagonal = dx != 0 && dy != 0;
            if diagonal && !(self.diagonal && self.free(x + dx, y) && self.free(x, y + dy)) {
                continue;
            }

            if self.free(x + dx, y + dy) {
                let target = usize::try_from(
                    (y + dy) * isize::try_from(self.width).unwrap_or(isize::MAX) + x + dx,
                )
                .expect("a free cell has a valid index");
                out.push((
                    target,
                    if diagonal {
                        core::f64::consts::SQRT_2
                    } else {
                        1.0
                    },
                ));
            }
        }
    }
}

/// The cost of `path` if every consecutive pair is an edge; `None` otherwise (or if empty).
#[must_use]
pub fn path_cost<G: Graph + ?Sized>(graph: &G, path: &[usize]) -> Option<f64> {
    if path.is_empty() {
        return None;
    }

    path.windows(2).map(|w| graph.edge_cost(w[0], w[1])).sum()
}

/// A shortest path by Dijkstra's algorithm, with its cost; `None` if `goal` is unreachable.
/// The reference for tests and the baseline for benches.
///
/// # Panics
///
/// Panics if `start` or `goal` is not a vertex of the graph.
#[must_use]
pub fn dijkstra<G: Graph + ?Sized>(
    graph: &G,
    start: usize,
    goal: usize,
) -> Option<(Vec<usize>, f64)> {
    /// A heap entry ordered by smallest distance first.
    #[derive(PartialEq)]
    struct Entry(f64, usize);
    impl Eq for Entry {}
    impl PartialOrd for Entry {
        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
            Some(self.cmp(other))
        }
    }
    impl Ord for Entry {
        fn cmp(&self, other: &Self) -> Ordering {
            other.0.total_cmp(&self.0).then(other.1.cmp(&self.1))
        }
    }

    let n = graph.node_count();
    let mut distance = alloc::vec![f64::INFINITY; n];
    let mut parent = alloc::vec![usize::MAX; n];
    let mut heap = BinaryHeap::new();
    let mut edges = Vec::new();
    distance[start] = 0.0;
    heap.push(Entry(0.0, start));
    while let Some(Entry(d, v)) = heap.pop() {
        if v == goal {
            break;
        }
        if d > distance[v] {
            continue;
        }

        edges.clear();
        graph.neighbours(v, &mut edges);
        for &(w, c) in &edges {
            if d + c < distance[w] {
                distance[w] = d + c;
                parent[w] = v;
                heap.push(Entry(d + c, w));
            }
        }
    }

    if !distance[goal].is_finite() {
        return None;
    }

    let mut path = alloc::vec![goal];
    while *path.last().expect("non-empty") != start {
        path.push(parent[*path.last().expect("non-empty")]);
    }
    path.reverse();

    Some((path, distance[goal]))
}

/// A randomised depth-first search from `from` to `to` avoiding `forbidden` vertices,
/// expanding at most `budget` vertices; returns the path (both ends included).
fn random_dfs<G: Graph + ?Sized, R: Rng + ?Sized>(
    graph: &G,
    from: usize,
    to: usize,
    forbidden: &[bool],
    budget: usize,
    rng: &mut R,
) -> Option<Vec<usize>> {
    let mut visited = forbidden.to_vec();
    let mut stack: Vec<(usize, Vec<usize>)> = Vec::new();
    let mut edges = Vec::new();
    let mut path = alloc::vec![from];
    visited[from] = true;
    let mut expansions = 0;
    let frontier = |v: usize, visited: &[bool], rng: &mut R, edges: &mut Vec<(usize, f64)>| {
        edges.clear();
        graph.neighbours(v, edges);
        let mut next: Vec<usize> = edges
            .iter()
            .map(|e| e.0)
            .filter(|&w| w == to || !visited[w])
            .collect();
        shuffle(rng, &mut next);
        next
    };
    stack.push((from, frontier(from, &visited, rng, &mut edges)));
    while let Some((_, options)) = stack.last_mut() {
        let Some(next) = options.pop() else {
            stack.pop();
            path.pop();
            continue;
        };

        if next == to {
            path.push(to);
            return Some(path);
        }
        if visited[next] {
            continue;
        }

        expansions += 1;
        if expansions > budget {
            return None;
        }

        visited[next] = true;
        path.push(next);
        let options = frontier(next, &visited, rng, &mut edges);
        stack.push((next, options));
    }

    None
}

/// Random simple paths from `start` to `goal` by randomised depth-first search. If the goal
/// is unreachable the genome is just `[start]`, which [`PathLength`] scores `+∞`.
#[derive(Debug, Clone, Copy)]
pub struct RandomPath<'g, G: ?Sized> {
    /// The graph.
    pub graph: &'g G,
    /// The first vertex.
    pub start: usize,
    /// The last vertex.
    pub goal: usize,
}

impl<G: Graph + ?Sized> Initializer<Vec<usize>> for RandomPath<'_, G> {
    fn initialize<R: Rng + ?Sized>(&mut self, rng: &mut R) -> Vec<usize> {
        let forbidden = alloc::vec![false; self.graph.node_count()];
        random_dfs(
            self.graph,
            self.start,
            self.goal,
            &forbidden,
            usize::MAX,
            rng,
        )
        .unwrap_or_else(|| alloc::vec![self.start])
    }
}

/// Crossover at a shared vertex: both parents are cut at a vertex they have in common and
/// the tails exchanged; loops are cut afterwards. Parents without a shared interior vertex
/// are returned unchanged.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct CommonVertex;

impl Crossover<Vec<usize>> for CommonVertex {
    fn crossover<R: Rng + ?Sized>(
        &mut self,
        a: &Vec<usize>,
        b: &Vec<usize>,
        rng: &mut R,
    ) -> [Vec<usize>; 2] {
        let shared: Vec<(usize, usize)> = a
            .iter()
            .enumerate()
            .skip(1)
            .take(a.len().saturating_sub(2))
            .filter_map(|(i, v)| {
                b.iter()
                    .position(|w| w == v)
                    .filter(|&j| j > 0 && j + 1 < b.len())
                    .map(|j| (i, j))
            })
            .collect();
        if shared.is_empty() {
            return [a.clone(), b.clone()];
        }

        let (i, j) = shared[below(rng, shared.len())];
        let mut c1: Vec<usize> = a[..=i].iter().chain(&b[j + 1..]).copied().collect();
        let mut c2: Vec<usize> = b[..=j].iter().chain(&a[i + 1..]).copied().collect();
        remove_loops(&mut c1);
        remove_loops(&mut c2);

        [c1, c2]
    }
}

/// Replaces the stretch between two path vertices by a random detour (a bounded randomised
/// depth-first search that avoids the rest of the path).
#[derive(Debug, Clone, Copy)]
pub struct Reroute<'g, G: ?Sized> {
    /// The graph.
    pub graph: &'g G,
    /// The largest number of vertices one detour search may expand.
    pub max_expansions: usize,
}

impl<G: Graph + ?Sized> Mutation<Vec<usize>> for Reroute<'_, G> {
    fn mutate<R: Rng + ?Sized>(&mut self, genome: &mut Vec<usize>, rng: &mut R) {
        if genome.len() < 2 {
            return;
        }

        let (i, j) = two_sorted_indices(rng, genome.len());
        let mut forbidden = alloc::vec![false; self.graph.node_count()];
        for &v in genome[..i].iter().chain(&genome[j + 1..]) {
            forbidden[v] = true;
        }

        if let Some(detour) = random_dfs(
            self.graph,
            genome[i],
            genome[j],
            &forbidden,
            self.max_expansions,
            rng,
        ) {
            genome.splice(i..=j, detour);
        }
    }
}

/// Cuts every loop: when a vertex reappears, the stretch between its occurrences goes.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct RemoveLoops;

impl Repair<Vec<usize>> for RemoveLoops {
    fn repair<R: Rng + ?Sized>(&mut self, genome: &mut Vec<usize>, _rng: &mut R) {
        remove_loops(genome);
    }
}

/// Removes loops in place, keeping the first occurrence of each vertex: `O(length)` with a
/// position table over the vertex range of the path.
fn remove_loops(path: &mut Vec<usize>) {
    let Some(&largest) = path.iter().max() else {
        return;
    };

    let mut position = alloc::vec![usize::MAX; largest + 1];
    let mut out: Vec<usize> = Vec::with_capacity(path.len());
    for &v in path.iter() {
        let at = position[v];
        if at < out.len() && out[at] == v {
            for &dropped in &out[at + 1..] {
                position[dropped] = usize::MAX;
            }
            out.truncate(at + 1);
        } else {
            position[v] = out.len();
            out.push(v);
        }
    }

    *path = out;
}

/// Path length as an objective: the sum of edge costs, `+∞` for paths that are not walks in
/// the graph or do not run from `start` to `goal`.
#[derive(Debug, Clone, Copy)]
pub struct PathLength<'g, G: ?Sized> {
    /// The graph.
    pub graph: &'g G,
    /// The required first vertex.
    pub start: usize,
    /// The required last vertex.
    pub goal: usize,
}

impl<G: Graph + ?Sized> Objective<Vec<usize>> for PathLength<'_, G> {
    fn evaluate(&mut self, genome: &Vec<usize>) -> f64 {
        if genome.first() != Some(&self.start) || genome.last() != Some(&self.goal) {
            return f64::INFINITY;
        }

        path_cost(self.graph, genome).unwrap_or(f64::INFINITY)
    }
}

/// Parameters of [`solve`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Config {
    /// The GA population size.
    pub population_size: usize,
    /// The largest number of vertices a reroute may expand.
    pub max_expansions: usize,
}

impl Default for Config {
    /// 50 paths, reroutes of at most 64 expansions.
    fn default() -> Self {
        Self {
            population_size: 50,
            max_expansions: 64,
        }
    }
}

/// Searches a short path with a generational GA (elitism two, tournament three, shared-vertex
/// crossover, reroute mutation, loop removal); returns the best path and its cost, or `None`
/// if no evaluated path reached the goal.
pub fn solve<G, B, R>(
    graph: &G,
    start: usize,
    goal: usize,
    config: &Config,
    budget: &mut B,
    rng: &mut R,
) -> Option<(Vec<usize>, f64)>
where
    G: Graph + ?Sized,
    B: Budget<Stats> + ?Sized,
    R: Rng + ?Sized,
{
    let mut ga = Ga::new(
        GaConfig {
            population_size: config.population_size,
            elitism: 2,
            crossover_rate: 0.7,
            mutation_rate: 0.8,
            scheme: Scheme::Generational,
        },
        Operators {
            init: RandomPath { graph, start, goal },
            selection: Tournament { size: 3 },
            crossover: CommonVertex,
            mutation: Reroute {
                graph,
                max_expansions: config.max_expansions,
            },
            repair: RemoveLoops,
        },
    );
    let mut objective = PathLength { graph, start, goal };
    let outcome = ga.run(&mut objective, budget, &mut (), rng);

    outcome
        .best
        .filter(|b| b.cost.is_finite())
        .map(|b| (b.genome, b.cost))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Limits, SplitMix64};

    /// A grid with a few walls, 4- or 8-connected.
    fn maze(diagonal: bool) -> Grid {
        let mut grid = Grid::open(16, 12, diagonal);
        for y in 0..9 {
            grid.block(4, y);
        }
        for y in 3..12 {
            grid.block(9, y);
        }
        for x in 11..15 {
            grid.block(x, 6);
        }

        grid
    }

    /// The GA finds an optimal path on 4- and 8-connected mazes, and every path it returns
    /// is a valid simple walk.
    #[test]
    fn matches_dijkstra() {
        for diagonal in [false, true] {
            let grid = maze(diagonal);
            let (start, goal) = (grid.node(0, 0), grid.node(15, 11));
            let (_, optimum) = dijkstra(&grid, start, goal).expect("reachable");
            let (path, cost) = solve(
                &grid,
                start,
                goal,
                &Config::default(),
                &mut Limits::evaluations(30_000),
                &mut SplitMix64::new(2),
            )
            .expect("found");

            assert_eq!(path_cost(&grid, &path), Some(cost));
            assert!(
                (cost - optimum).abs() < 1e-9,
                "diagonal {diagonal}: {cost} vs {optimum}"
            );
            let mut sorted = path.clone();
            sorted.sort_unstable();
            sorted.dedup();
            assert_eq!(sorted.len(), path.len(), "the path is simple");
        }
    }

    /// Operators keep paths valid from start to goal.
    #[test]
    fn operators_keep_paths_valid() {
        let grid = maze(true);
        let (start, goal) = (grid.node(0, 0), grid.node(15, 11));
        let mut rng = SplitMix64::new(5);
        let mut init = RandomPath {
            graph: &grid,
            start,
            goal,
        };
        let mut reroute = Reroute {
            graph: &grid,
            max_expansions: 50,
        };
        for _ in 0..100 {
            let a = init.initialize(&mut rng);
            let b = init.initialize(&mut rng);
            for mut child in CommonVertex.crossover(&a, &b, &mut rng) {
                reroute.mutate(&mut child, &mut rng);
                assert_eq!((child[0], child[child.len() - 1]), (start, goal));
                assert!(path_cost(&grid, &child).is_some());
            }
        }
    }

    /// Loop removal keeps the first occurrence and cuts the cycles in between.
    #[test]
    fn loops_are_cut() {
        let mut path = alloc::vec![0, 1, 2, 1, 3, 4, 3, 5, 0, 6];
        remove_loops(&mut path);

        assert_eq!(path, [0, 6]);
        let mut path = alloc::vec![0, 1, 2, 3, 2, 4];
        remove_loops(&mut path);
        assert_eq!(path, [0, 1, 2, 4]);
    }

    /// An unreachable goal yields no path.
    #[test]
    fn unreachable_goal() {
        let mut grid = Grid::open(5, 5, false);
        for y in 0..5 {
            grid.block(2, y);
        }

        assert!(dijkstra(&grid, 0, 4).is_none());
        assert!(
            solve(
                &grid,
                0,
                4,
                &Config::default(),
                &mut Limits::evaluations(500),
                &mut SplitMix64::new(1)
            )
            .is_none()
        );
    }
}