Skip to content

Fixes problems with algorithm #4

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

Merged
merged 1 commit into from
May 23, 2023
Merged

Fixes problems with algorithm #4

merged 1 commit into from
May 23, 2023

Conversation

jobh
Copy link
Contributor

@jobh jobh commented May 23, 2023

Hi,

I really liked your approach to implementing and explaining the A* algorithm.

Now, the problem is that the algorithm as it is does not work correctly. There are two issues:

  1. The heuristic is added at each step, so it ends up with really big values. This makes it hard to understand the meaning of the numbers, and can also make A* choose sub-optimal paths.
  2. The loop didn't stop when end was reached, so in effect the A* cutoff was not used.

See printout from example:

70   54   60   69   81   92   98   103  113  124  
54   39   46   56   69   84   88   94   103  113  
39   25   33   44   58   71   79   86   94   103  
25   12   21   ##########82   86   96   101  109  
12   S    10   ##########89   92   99   105  112  
21   10   19   ##########95   103  108  110  116  
29   19   27   ##########107  116  111  114  119  
36   27   34   ##########112  117  113  116  120  
42   34   40   ##########123  116  E    116  119  
51   42   47   ##########123  119  116  119  123  

The cost is way too high, and all points are visited.

The root cause of the problem seems to be conflating the actual cost to reach a point with the estimated total cost of the path. This PR tries to make the difference clearer in README, and fixes the implementation by subtracting the previous heuristic before adding the new.

I understand that this repo is intended for demonstrating and teaching the principles, and should be kept as simple as possible (but correct!). If this understanding is incorrect, I can create a PR for another branch with larger changes that makes it usable for generic graphs and heuristics (not only grids).

1) The heuristic distance was applied multiple times
2) The loop didn't exit when solution found
Copy link
Owner

@pablogsal pablogsal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot, this is a fantastic improvement!

@pablogsal pablogsal merged commit 65e14f7 into pablogsal:master May 23, 2023
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

Successfully merging this pull request may close these issues.

2 participants