Expand description
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));Structs§
- Adjacency
List - A graph given by adjacency lists.
- Common
Vertex - 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.
- Config
- Parameters of
solve. - Grid
- A 4- or 8-connected grid with blocked cells; straight moves cost
1, diagonal moves√2(and may not cut blocked corners). Vertexy * width + xis cell(x, y). - Path
Length - Path length as an objective: the sum of edge costs,
+∞for paths that are not walks in the graph or do not run fromstarttogoal. - Random
Path - Random simple paths from
starttogoalby randomised depth-first search. If the goal is unreachable the genome is just[start], whichPathLengthscores+∞. - Remove
Loops - Cuts every loop: when a vertex reappears, the stretch between its occurrences goes.
- Reroute
- Replaces the stretch between two path vertices by a random detour (a bounded randomised depth-first search that avoids the rest of the path).
Traits§
- Graph
- A directed graph with non-negative edge costs over vertices
0..node_count().
Functions§
- dijkstra
- A shortest path by Dijkstra’s algorithm, with its cost;
Noneifgoalis unreachable. The reference for tests and the baseline for benches. - path_
cost - The cost of
pathif every consecutive pair is an edge;Noneotherwise (or if empty). - solve
- 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
Noneif no evaluated path reached the goal.