Skip to main content

Module graph_path

Module graph_path 

Source
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§

AdjacencyList
A graph given by adjacency lists.
CommonVertex
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). Vertex y * width + x is cell (x, y).
PathLength
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.
RandomPath
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 +∞.
RemoveLoops
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; None if goal is unreachable. The reference for tests and the baseline for benches.
path_cost
The cost of path if every consecutive pair is an edge; None otherwise (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 None if no evaluated path reached the goal.