Note that there are some explanatory texts on larger screens.

plurals
  1. POWikipedia A* pathfinding algorithm takes a lot of time
    primarykey
    data
    text
    <p>I've successfully implemented A* pathfinding in C# but it is very slow, and I don't understand why. I even tried not sorting the openNodes list but it's still the same.</p> <p>The map is 80x80, and there are 10-11 nodes.</p> <p>I took the pseudocode from here <a href="http://en.wikipedia.org/wiki/A%2a_search_algorithm#Pseudocode" rel="nofollow">Wikipedia</a></p> <p>And this is my implementation:</p> <pre><code> public static List&lt;PGNode&gt; Pathfind(PGMap mMap, PGNode mStart, PGNode mEnd) { mMap.ClearNodes(); mMap.GetTile(mStart.X, mStart.Y).Value = 0; mMap.GetTile(mEnd.X, mEnd.Y).Value = 0; List&lt;PGNode&gt; openNodes = new List&lt;PGNode&gt;(); List&lt;PGNode&gt; closedNodes = new List&lt;PGNode&gt;(); List&lt;PGNode&gt; solutionNodes = new List&lt;PGNode&gt;(); mStart.G = 0; mStart.H = GetManhattanHeuristic(mStart, mEnd); solutionNodes.Add(mStart); solutionNodes.Add(mEnd); openNodes.Add(mStart); // 1) Add the starting square (or node) to the open list. while (openNodes.Count &gt; 0) // 2) Repeat the following: { openNodes.Sort((p1, p2) =&gt; p1.F.CompareTo(p2.F)); PGNode current = openNodes[0]; // a) We refer to this as the current square.) if (current == mEnd) { while (current != null) { solutionNodes.Add(current); current = current.Parent; } return solutionNodes; } openNodes.Remove(current); closedNodes.Add(current); // b) Switch it to the closed list. List&lt;PGNode&gt; neighborNodes = current.GetNeighborNodes(); double cost = 0; bool isCostBetter = false; for (int i = 0; i &lt; neighborNodes.Count; i++) { PGNode neighbor = neighborNodes[i]; cost = current.G + 10; isCostBetter = false; if (neighbor.Passable == false || closedNodes.Contains(neighbor)) continue; // If it is not walkable or if it is on the closed list, ignore it. if (openNodes.Contains(neighbor) == false) { openNodes.Add(neighbor); // If it isn’t on the open list, add it to the open list. isCostBetter = true; } else if (cost &lt; neighbor.G) { isCostBetter = true; } if (isCostBetter) { neighbor.Parent = current; // Make the current square the parent of this square. neighbor.G = cost; neighbor.H = GetManhattanHeuristic(current, neighbor); } } } return null; } </code></pre> <p>Here's the heuristic I'm using:</p> <pre><code> private static double GetManhattanHeuristic(PGNode mStart, PGNode mEnd) { return Math.Abs(mStart.X - mEnd.X) + Math.Abs(mStart.Y - mEnd.Y); } </code></pre> <p>What am I doing wrong? It's an entire day I keep looking at the same code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload