Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A* fails to find optimal solution with admissible but inconsistent heuristic #378

Closed
lillanes opened this issue Oct 13, 2020 · 1 comment

Comments

@lillanes
Copy link
Contributor

A* using an admissible but inconsistent heuristic should find optimal paths. The current implementation seems to assume that heuristics will always be consistent, and fails to find optimal paths in some cases.

Test Case

use petgraph::prelude::*;
use petgraph::algo::astar;

#[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])));
}

Steps To Reproduce

See above.

Actual Results

astar finds the path A->D with cost 10.

Expected Results

The algorithm should find the optimal path A->B->C->D with cost 9.

lillanes added a commit to lillanes/petgraph that referenced this issue Oct 13, 2020
The new test uses an admissible but inconsistent heuristic. The current
implementation of A* fails to find the optimal solution (petgraph#378).
lillanes added a commit to lillanes/petgraph that referenced this issue Oct 13, 2020
Fix for petgraph#378

Given an admissible but inconsistent heuristic (that is, one that for
every node underestimates the distance to the goal, but may overestimate
the distance from a neighbour of to the goal plus the cost of reaching
that neighbour), A* may need to re-visit some non-goal nodes as it finds
shorter paths to reach them.

This commit forces A* to do these re-expansions. It also simplifies the
implementation a bit, removing the unnecessary use of a visit map to
keep track of visited nodes (the tracking is already happening due to
having to keep track of the costs).
bluss pushed a commit that referenced this issue Jan 10, 2021
The new test uses an admissible but inconsistent heuristic. The current
implementation of A* fails to find the optimal solution (#378).
bluss pushed a commit that referenced this issue Jan 10, 2021
Fix for #378

Given an admissible but inconsistent heuristic (that is, one that for
every node underestimates the distance to the goal, but may overestimate
the distance from a neighbour of to the goal plus the cost of reaching
that neighbour), A* may need to re-visit some non-goal nodes as it finds
shorter paths to reach them.

This commit forces A* to do these re-expansions. It also simplifies the
implementation a bit, removing the unnecessary use of a visit map to
keep track of visited nodes (the tracking is already happening due to
having to keep track of the costs).
@bluss
Copy link
Member

bluss commented Jan 10, 2021

Fixed by #379

@bluss bluss closed this as completed Jan 10, 2021
teuron pushed a commit to teuron/petgraph that referenced this issue Oct 9, 2022
The new test uses an admissible but inconsistent heuristic. The current
implementation of A* fails to find the optimal solution (petgraph#378).
teuron pushed a commit to teuron/petgraph that referenced this issue Oct 9, 2022
Fix for petgraph#378

Given an admissible but inconsistent heuristic (that is, one that for
every node underestimates the distance to the goal, but may overestimate
the distance from a neighbour of to the goal plus the cost of reaching
that neighbour), A* may need to re-visit some non-goal nodes as it finds
shorter paths to reach them.

This commit forces A* to do these re-expansions. It also simplifies the
implementation a bit, removing the unnecessary use of a visit map to
keep track of visited nodes (the tracking is already happening due to
having to keep track of the costs).
teuron pushed a commit to teuron/petgraph that referenced this issue Oct 9, 2022
The new test uses an admissible but inconsistent heuristic. The current
implementation of A* fails to find the optimal solution (petgraph#378).
teuron pushed a commit to teuron/petgraph that referenced this issue Oct 9, 2022
Fix for petgraph#378

Given an admissible but inconsistent heuristic (that is, one that for
every node underestimates the distance to the goal, but may overestimate
the distance from a neighbour of to the goal plus the cost of reaching
that neighbour), A* may need to re-visit some non-goal nodes as it finds
shorter paths to reach them.

This commit forces A* to do these re-expansions. It also simplifies the
implementation a bit, removing the unnecessary use of a visit map to
keep track of visited nodes (the tracking is already happening due to
having to keep track of the costs).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants