Search code examples
algorithmpath-findinga-star

I don't understand A* Pathfinding


From what I understand:

Add the current node to the closed list.

Find adjacent nodes to the current node, and if they are not unwalkable nodes and not on the closed list, add that node to the open list with the parent being the current node and calculate the F, G and H values. If the node already exists on the open list, check if going to that node through the current node will result in a lower G value - if yes, make the parent node of that node the current node.

Find the node on the open list with the highest F value, and make the current node that node.

Repeat until you end up in your destination, then go through the parents of the destination node, and you will come back to your start node. That will be the best path.

So, this makes sense to my brain, but when I actually try it out on a diagram, I think I'm not understanding it correctly.

(From the picture below) Go down from the starting green tile, the one with a F value of 60. That is on the open list, and has a lower F value than bottom-right 74 one. Why is the 74 one picked instead of the 60?

A*


Solution

  • In my opinion, you should take a look at Amit's A* Pages. They really are great to explain how the algorithm works and how to make it work.

    As for your case, the diagram shows the G score from the first node on the open list. When you look at the website, the whole diagram is built first for the first node evaluation, and the author shows that the first best node is the one to the right. Then, moving forward uses the G score based on the score of the current node plus the moving cost of the next one, which is not shown on the diagram.

    It is said on the website though:

    And the last square, to the immediate left of the current square, is checked to see if the G score is any lower if you go through the current square to get there. No dice.

    It's G score would actually be 24 (14 (current cost) + 10 (horizontal movement cost)), just like the square below it, if I remember correctly.