What's wrong with my A* algorithm ?

Hello everybody !

I’m making a A* pathfinder for a 2D grid system, and for that I was using the pseudo code algorithm of Wikipedia.

And I’m almost there !
But look at that :

We can clearly see that it’s not taking the optimal path it should.
Here is a bigger picture of the “almost” correct path :

The path seems to always be attracted in the right and in the down, and I don’t get it. I tried to watch what was going on with breakpoint but I don’t get it, there is too many stuff going on, and it feel like when I press “Step Over”, Unreal miss a lot of actual step.

Here is the blueprint (I tried to make it the cleanest I could) :




I’m pretty much following the normal pseudocode.
Here are my calculation functions :

The way I calculate the cost from start

My estimated cost :

The way I compare costs :

I have read those again and again, tried to twist everything and I can’t find what’s wrong without breaking everything.

I’m doing this project to learn how to code in Unreal the cleanest way possible and to understand the A* Algorithm, so if you see any “bad usage” or huge lack of optimization in what I’m doing, I will take notes.

Thanks to everyone that will take the time to help me understand this CLASSIC :pray:

By helping in sorting you problem, I think you should try some of these methods:
1.Heuristic Function: Your heuristic function (‘‘Get Heuristic Cost’’) looks correct, as it calculates the Manhattan distance between the current node and the target node.

  1. Cost Functions: Your cost functions (‘‘Get Cost From Start’’ and ‘‘Get Total Cost’’) also seem to be implemented correctly, using the distance from the start node and the heuristic function to calculate the total cost.

  2. Open List Sorting: In your ‘‘Sort Open List’’ function, ensure that you’re sorting the open list based on the total cost of each node correctly. It seems like you’re sorting in ascending order, which should prioritize nodes with lower costs first.

  3. Neighbor Expansion: Check your implementation of expanding neighbors in the ''Expand Neighbors" function. Ensure that you’re considering all valid neighboring nodes and updating their costs and parent nodes accordingly.

  4. Debugging: Since you’re having trouble debugging with breakpoints, try adding print statements or debug visualization to track the progress of the algorithm and identify any issues with node expansion or cost calculation.

  5. Optimization: Consider optimizing your code by reducing unnecessary calculations or iterations. For example, you could cache the heuristic values for each node instead of recalculating them every time.

  6. Edge Cases: Make sure to handle edge cases such as unreachable nodes or obstacles properly in your pathfinding algorithm.

  7. Learning and Refactoring: As you mentioned, this project is for learning purposes, so take the opportunity to refactor and improve your code based on best practices and optimizations as you learn more about Unreal Engine and A* algorithm.

By reviewing and refining your implementation based on these suggestions, you should be able to identify and resolve the issues with your A* pathfinding algorithm in Unreal Engine. Don’t hesitate to experiment and iterate on your code to achieve the desired behavior.

1 Like

Thanks a lot, such a precise answer ! I will try that today.

IT’S WORKING !
I’m so happy you have no idea.

You were right, the issue was coming from the sorting function. The way I was doing that was kind of weird, I made it way more clear and that worked almost instantly.
For the other advices, I already implemented some of those and I will try to do the others, especially about the optimization.

Thanks again for your time :pray:

Your welcome