Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does A* not find the optimal path?
    primarykey
    data
    text
    <p>I implemented the A*-pathfinding in a simple Winforms application. Where one can also define obstacles on the grid so the agent can navigate around them. </p> <p>The Problem is my agent can't foresee paths. This is why he does not always take the optimal path.</p> <p><a href="http://www.youtube.com/watch?v=em-D-2tvUC4" rel="nofollow">Here you can see my exact problem.</a></p> <p>How can I make the pathfinder anticipate the shorter paths? There is also an issue with dead ends once the agent took the wrong path.</p> <p>This Method gets the adjascent cells and calculates the values:</p> <pre><code>public void addAdjascent(Vector pos) { foreach(Cell cell in allCells) { bool containedInClosedList = closedList.Any(c =&gt; c.id == cell.id); if (!containedInClosedList) { if (cell.positionCR.X == pos.X - 1 &amp;&amp; cell.positionCR.Y == pos.Y) { bool containedInBlockedList = blockedList.Any(c =&gt; c.id == cell.id); if(!containedInBlockedList) { cell.H = calcH(goal,cell); cell.G = calcG(start, cell); cell.F = cell.G + cell.H; openList.Add(cell); } } if (cell.positionCR.X == pos.X + 1 &amp;&amp; cell.positionCR.Y == pos.Y) { bool containedInBlockedList = blockedList.Any(c =&gt; c.id == cell.id); if (!containedInBlockedList) { cell.H = calcH(goal, cell); cell.G = calcG(start, cell); cell.F = cell.G + cell.H; openList.Add(cell); } } if (cell.positionCR.X == pos.X &amp;&amp; cell.positionCR.Y == pos.Y - 1) { bool containedInBlockedList = blockedList.Any(c =&gt; c.id == cell.id); if (!containedInBlockedList) { cell.H = calcH(goal, cell); cell.G = calcG(start, cell); cell.F = cell.G + cell.H; openList.Add(cell); } } if (cell.positionCR.X == pos.X &amp;&amp; cell.positionCR.Y == pos.Y + 1) { bool containedInBlockedList = blockedList.Any(c =&gt; c.id == cell.id); if (!containedInBlockedList) { cell.H = calcH(goal, cell); cell.G = calcG(start, cell); cell.F = cell.G + cell.H; openList.Add(cell); } } if (cell.positionCR.X == pos.X - 1 &amp;&amp; cell.positionCR.Y == pos.Y - 1) { bool containedInBlockedList = blockedList.Any(c =&gt; c.id == cell.id); if (!containedInBlockedList) { cell.H = calcH(goal, cell); cell.G = calcG(start, cell); cell.F = cell.G + cell.H; openList.Add(cell); } } if (cell.positionCR.X == pos.X - 1 &amp;&amp; cell.positionCR.Y == pos.Y + 1) { bool containedInBlockedList = blockedList.Any(c =&gt; c.id == cell.id); if (!containedInBlockedList) { cell.H = calcH(goal, cell); cell.G = calcG(start, cell); cell.F = cell.G + cell.H; openList.Add(cell); } } if (cell.positionCR.X == pos.X + 1 &amp;&amp; cell.positionCR.Y == pos.Y - 1) { bool containedInBlockedList = blockedList.Any(c =&gt; c.id == cell.id); if (!containedInBlockedList) { cell.H = calcH(goal, cell); cell.G = calcG(start, cell); cell.F = cell.G + cell.H; openList.Add(cell); } } if (cell.positionCR.X == pos.X + 1 &amp;&amp; cell.positionCR.Y == pos.Y + 1) { bool containedInBlockedList = blockedList.Any(c =&gt; c.id == cell.id); if (!containedInBlockedList) { cell.H = calcH(goal, cell); cell.G = calcG(start, cell); cell.F = cell.G + cell.H; openList.Add(cell); } } } } } </code></pre> <p>Here is the code for the calculation of G and H values:</p> <pre><code> public int calcG(Vector start,Cell cell) { int distance = closedList.Count; return distance; } public int calcH(Vector goal, Cell cell) { int distance; int distanceX = (goal.X) - cell.positionCR.X; int distanceY = (goal.Y) - cell.positionCR.Y; if (distanceX &lt; 0) { distanceX = distanceX * -1; } if (distanceY &lt; 0) { distanceY = distanceY * -1; } distance = distanceX + distanceY; return distance; } </code></pre> <p>And finally I calculate the next cell I want to step on:</p> <pre><code>public void step() { addAdjascent(stepPos); bool foundDestination = false; int min = 8000; foreach(Cell openCell in openList) { if (openCell.F &lt; min) { min = openCell.F; } } if(openList.Count &gt; 0) { foreach (Cell openCell in openList) { if (openCell.positionCR.X == goal.X &amp;&amp; openCell.positionCR.Y == goal.Y) { closedList.Add(openCell); stepPos = openCell.positionCR; foundDestination = true; } } } if(!foundDestination) { closedList.Add(openList.First(item =&gt; item.F == min)); stepPos = openList.First(item =&gt; item.F == min).positionCR; } openList.Clear(); } </code></pre>
    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