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

AStar add #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

AStar add #45

wants to merge 2 commits into from

Conversation

mitchellri
Copy link

@mitchellri mitchellri commented Sep 22, 2019

Not sure if you want this included in the project, but it's a simple addition that can be very useful

  • Added extensions for AStar
  • Added additional comparison and private variable in NodeComparer for heuristic
  • Duplicated Dijkstra to AStar file
    • Added heuristic parameter
    • Changed NodeComparer to include heuristic

@matiii
Copy link
Owner

matiii commented Mar 10, 2020

What are pros of AStar method ?
Why we need heuristic method ?

@mitchellri
Copy link
Author

mitchellri commented Mar 11, 2020

AStar can increase performance of the pathfinding algorithm (Video example)
Heuristic is a cost function that helps determine which node to evaluate next (Stackoverflow)

Note: Since I made this request I have made additional commits to my master branch. If you wish to add AStar to your master, make sure to either reject the unrelated changes if possible, I can submit a new pull request omitting the unrelated commits, or change my master branch to only include the AStar add commit

@matiii
Copy link
Owner

matiii commented Mar 22, 2020

Hi, really nice feature. Could you provide more refinement to your contribution ? I prefer to keep graph data stracture still immutable. Instead modify NodeComparer would be better just extend it by inheritance. Why we need to expose herusitc method to be public ? Is it posible to have default implementation ?

@mitchellri
Copy link
Author

Thanks! Dijkstra's algorithm is a special case of the AStar algorithm when it has null heuristic, so the default implementation of AStar is Dijkstra's algorithm.

  • Removed commits up to original AStar implementation (Graph remains mutable)
  • NodeComparer original implementation with modified access for child classes
  • Added HeuristicNodeComparer extending NodeComparer to include heuristic

In this implementation AStar requires a heuristic, because Dijkstra should be used for Dijkstra.

Optionally, I could allow null heuristic in AStar which would perform Dijkstra's algorithm. In that case it might be worth considering removing Dijkstra if you do not want redundancy, but it might be confusing to have no Dijkstra in Dijkstra.NET
Here's what that might look like:

public static ShortestPathResult GetShortestPath(IDijkstraGraph graph, uint from, uint to, int depth, Func<uint, uint, int> heuristic = null)
{
        var path = new Dictionary<uint, uint>();
        var distance = new Dictionary<uint, int> {[from] = 0};
        var d = new Dictionary<uint, int> {[from] = 0};
        var comparer = heuristic is null ? new NodeComparer(distance) : new HeuristicNodeComparer(distance, heuristic);
        var q = new SortedSet<uint>(new[] {from}, comparer);
        var current = new HashSet<uint>();
        ...

@matiii
Copy link
Owner

matiii commented Mar 29, 2020

Now, it looks much better. Could you provide unit tests as well ? Then we will be certain about reliabilty your solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants