Skip to content

Commit

Permalink
Add additional test for A*
Browse files Browse the repository at this point in the history
The new test uses an admissible but inconsistent heuristic. The current
implementation of A* fails to find the optimal solution (petgraph#378).
  • Loading branch information
lillanes authored and teuron committed Oct 9, 2022
1 parent b3dbf1c commit fc89171
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,30 @@ fn test_astar_manhattan_heuristic() {
}
}

#[test]
fn test_astar_admissible_inconsistent() {
let mut g = Graph::new();
let a = g.add_node("A");
let b = g.add_node("B");
let c = g.add_node("C");
let d = g.add_node("D");
g.add_edge(a, b, 3);
g.add_edge(b, c, 3);
g.add_edge(c, d, 3);
g.add_edge(a, c, 8);
g.add_edge(a, d, 10);

let admissible_inconsistent = |n: NodeIndex| match g[n] {
"A" => 9,
"B" => 6,
"C" => 0,
&_ => 0,
};

let optimal = astar(&g, a, |n| n == d, |e| *e.weight(), admissible_inconsistent);
assert_eq!(optimal, Some((9, vec![a, b, c, d])));
}

#[cfg(feature = "generate")]
#[test]
fn test_generate_undirected() {
Expand Down

0 comments on commit fc89171

Please sign in to comment.