• Create Account

## pathfinding behaviour

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

18 replies to this topic

### #1dworm  Members

683
Like
1Likes
Like

Posted 22 January 2014 - 09:34 AM

Wanted to asked if this is normal or I did some mistake:

http://imgur.com/RJONEkq

http://imgur.com/eoDsyiC

ofc pink is path, black is obstacle, yellow is the open tiles evaluated

It seems there are too many yellow or at least I did expect them to be less, dunno really

### #2boogyman19946  Members

1476
Like
2Likes
Like

Posted 22 January 2014 - 10:28 AM

I think that might depend on which algorithm you're using. Some of the more naive ones can paint the whole darn thing yellow so that there wouldn't be any white left.

From the second picture it looks to me like you're using A* because the algorithm will start with checking paths that produce the shortest traveled distance + heuristic distance. It doesn't take into consideration the fact that there might be an obstacle on it's path. You can picture this in your own house for example. If you have two people standing on opposite sides of a wall, the straight line distance (commonly used as heuristic in A*) would direct A* at the wall first, but the actual shortest path might mean that you have to backtrack to get to the door. If there were no obstacles in your path, and all tiles had equal cost, you could probably just expect a single straight path to your goal.

The first one looks like it might have goofed up, unless some of the tiles incur significantly different costs.

But, all said and done, I've only ever really had to deal with pathfinding once, so my knowledge of it is rather limited.

- Java API Documentation - For all your Java info needs
- C++ Standard Library Reference - For some of your C++ needs ^.^

### #3KnolanCross  Members

1935
Like
2Likes
Like

Posted 23 January 2014 - 07:33 AM

Seems a bit strange...

I tried on mine in a total open area. Here is my output:

Total search time 213.
x11.................................................................................................................................
1x11................................................................................................................................
11x11...............................................................................................................................
.11x11..............................................................................................................................
..11x11.............................................................................................................................
...11x11............................................................................................................................
....11x11...........................................................................................................................
.....11x11..........................................................................................................................
......11x11.........................................................................................................................
.......11x11........................................................................................................................
........11x11.......................................................................................................................
.........11x11......................................................................................................................
..........11x11.....................................................................................................................
...........11x11....................................................................................................................
............11x11...................................................................................................................
.............11x11..................................................................................................................
..............11x11.................................................................................................................
...............11x11................................................................................................................
................11x11...............................................................................................................
.................11x11..............................................................................................................
..................11x11.............................................................................................................
...................11x11............................................................................................................
....................11x11...........................................................................................................
.....................11x11..........................................................................................................
......................11x11.........................................................................................................
.......................11x11........................................................................................................
........................11x11.......................................................................................................
.........................11x11......................................................................................................
..........................11x11.....................................................................................................
...........................11x11....................................................................................................
............................11x11...................................................................................................
.............................11x11..................................................................................................
..............................11x11.................................................................................................
...............................11x11................................................................................................
................................11x11...............................................................................................
.................................11x11..............................................................................................
..................................11x11.............................................................................................
...................................11x11............................................................................................
....................................11x11...........................................................................................
.....................................11x11..........................................................................................
......................................11x11.........................................................................................
.......................................11x11........................................................................................
........................................11x11.......................................................................................
.........................................11x11......................................................................................
..........................................11x11.....................................................................................
...........................................11x11....................................................................................
............................................11x11...................................................................................
.............................................11x11..................................................................................
..............................................11x11.................................................................................
...............................................11x11................................................................................
................................................11x11...............................................................................
.................................................11x11..............................................................................
..................................................11x11.............................................................................
...................................................11x11............................................................................
....................................................11x11...........................................................................
.....................................................11x11..........................................................................
......................................................11x11.........................................................................
.......................................................11x11........................................................................
........................................................11x11.......................................................................
.........................................................11x11......................................................................
..........................................................11x11.....................................................................
...........................................................11x11....................................................................
............................................................11x11...................................................................
.............................................................11x11..................................................................
..............................................................11x11.................................................................
...............................................................11x11................................................................
................................................................11x11...............................................................
.................................................................11x11..............................................................
..................................................................11x11.............................................................
...................................................................11x11............................................................
....................................................................11x11...........................................................
.....................................................................11x11..........................................................
......................................................................11x11.........................................................
.......................................................................11x11........................................................
........................................................................11x11.......................................................
.........................................................................11x11......................................................
..........................................................................11x11.....................................................
...........................................................................11x11....................................................
............................................................................11x11...................................................
.............................................................................11x11..................................................
..............................................................................11x11.................................................
...............................................................................11x11................................................
................................................................................11x11...............................................
.................................................................................11x11..............................................
..................................................................................11x11.............................................
...................................................................................11x11............................................
....................................................................................11x11...........................................
.....................................................................................11x11..........................................
......................................................................................11x11.........................................
.......................................................................................11x11........................................
........................................................................................11x11.......................................
.........................................................................................11x11......................................
..........................................................................................11x11.....................................
...........................................................................................11x1111111111111111111...................
............................................................................................11xxxxxxxxxxxxxxxxxxx...................
.............................................................................................11111111111111111111...................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................
....................................................................................................................................

Where:

"." is a free area.

"1" is an explored area.

"x" is the path.

Post your algorithm so we can check.

Edit: I am assuming you are using A*.

Edited by KnolanCross, 23 January 2014 - 07:36 AM.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

### #4dworm  Members

683
Like
0Likes
Like

Posted 23 January 2014 - 09:42 AM

im using a* with binary heaps following this tutorial:

http://www.policyalmanac.org/games/binaryHeaps.htm

btw your search time is 213 ms? or what?

Edited by dworm, 23 January 2014 - 09:43 AM.

### #5KnolanCross  Members

1935
Like
2Likes
Like

Posted 23 January 2014 - 09:53 AM

Same here (A* with binary heap), maybe there is something wrong in your open list? Are you sure you are adding all those points? It could be a bug.

Also, what heuristic are you using? Last week there was a guy who was not updating the openList after editing the value of a node, could be a hint...

Ops, the time output was not intended (I copied the whole output, my bad), anyways the time is in microsseconds (1s = 10^6).

Edited by KnolanCross, 23 January 2014 - 09:54 AM.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

### #6dworm  Members

683
Like
2Likes
Like

Posted 23 January 2014 - 11:11 AM

yes there surely is something wrong, here is the code

 public void Pathfind(int startX, int startY, int targetX, int targetY)
{

Point start = new Point (startX,startY);
Point target = new Point (targetX,targetY);

if (start.X == -1 || start.Y == -1)
{
return;
}

int actualX = start.X;
int actualY = start.Y;
squareMap[actualX, actualY].D = 0;
squareMap[actualX, actualY].F = Math.Abs(actualX - target.X) + Math.Abs(actualY - target.Y);

openListNumber = 1;
Openlist[1] = new Point(actualX, actualY);
bool found = false;
int step = 0;
while (!found)
{

int D;
step++;
D = squareMap[actualX, actualY].D;
int S;

foreach (Point dir in ValidMoves(actualX, actualY)) // find lowest  F score
{
int k = D + 1 + Math.Abs(dir.X - target.X) + Math.Abs(dir.Y - target.Y);
S = squareMap[dir.X, dir.Y].F;
if (S > k)
{
squareMap[dir.X, dir.Y].F = k;
squareMap[dir.X, dir.Y].D = D + 1;
squareMap[dir.X, dir.Y].DirX = actualX - dir.X;
squareMap[dir.X, dir.Y].DirY = actualY - dir.Y;

if (S == 5000)
{
}
else
{
correctOpenList(dir.X, dir.Y);
}
}

}
squareMap[actualX, actualY].Checked = true;

if (target.X == actualX && target.Y == actualY)
{
found = true;
}

//pick lowest open list
actualX = Openlist[1].X;
actualY = Openlist[1].Y;

RemoveFromList();
if (openListNumber < 0) { found = true; }

}

//backtrack path
int pointX = target.X;
int pointY = target.Y;
int dirX = 1;
int dirY = 1;
if (pointX == -1 && pointY == -1)
{
return;
}

while (pointX != start.X || pointY != start.Y)
{
dirX = squareMap[pointX, pointY].DirX;
dirY = squareMap[pointX, pointY].DirY;
pointX += dirX;
pointY += dirY;
squareMap[pointX, pointY].IsPath = true;
}
squareMap[pointX, pointY].IsPath = false;
}

this to add to open list:

void addToOpenList(int x, int y)
{
openListNumber++;
Openlist[openListNumber] = new Point(x, y);
int nTemp = openListNumber;
int nTemp2 = nTemp;
int F = squareMap[x, y].F;
Point temp;
while (nTemp2 > 1)
{
nTemp2 = nTemp / 2;
if (squareMap[Openlist[nTemp].X, Openlist[nTemp].Y].F > F)
{
temp = Openlist[nTemp2];
Openlist[nTemp2] = Openlist[nTemp];
Openlist[nTemp] = temp;
}
nTemp = nTemp2;
}

}

this to remove

 void RemoveFromList()
{
Openlist[1] = Openlist[openListNumber];
openListNumber--;
int nTemp = 1;
int nTemp2 = 1;
bool jump = true;
Point temp;
while (jump)
{
nTemp2 = nTemp;
if (nTemp2 * 2 < openListNumber)
{
if (squareMap[Openlist[nTemp2].X, Openlist[nTemp2].Y].F > squareMap[Openlist[nTemp2 * 2].X, Openlist[nTemp2 * 2].Y].F)
{
nTemp = nTemp2 * 2;
}
if (squareMap[Openlist[nTemp].X, Openlist[nTemp].Y].F > squareMap[Openlist[nTemp2 * 2 + 1].X, Openlist[nTemp2 * 2 + 1].Y].F)
{
nTemp = nTemp2 * 2 + 1;
}
}
else if (nTemp2 * 2 <= openListNumber)
{
if (squareMap[Openlist[nTemp2].X, Openlist[nTemp2].Y].F > squareMap[Openlist[nTemp2 * 2].X, Openlist[nTemp2 * 2].Y].F)
{
nTemp = nTemp2 * 2;
}
}

if (nTemp != nTemp2)
{
temp = Openlist[nTemp2];
Openlist[nTemp2] = Openlist[nTemp];
Openlist[nTemp] = temp;
}
else
{
jump = false;
}
}


this to correct when a tile is found better than the previous value

      void correctOpenList(int x, int y)
{
int nTemp = 1;
while (Openlist[nTemp].X != x || Openlist[nTemp].Y != y)
{
nTemp += 1;
}
int nTemp2 = nTemp;
int F = squareMap[x, y].F;
Point temp;
while (nTemp2 > 1)
{
nTemp2 = nTemp / 2;
if (squareMap[Openlist[nTemp].X, Openlist[nTemp].Y].F > F)
{
temp = Openlist[nTemp2];
Openlist[nTemp2] = Openlist[nTemp];
Openlist[nTemp] = temp;
}
nTemp = nTemp2;
}

}


### #7KnolanCross  Members

1935
Like
2Likes
Like

Posted 23 January 2014 - 11:51 AM

In a first look to your code (and assuming the add/remove is working correctly):

This

int k = D + 1 + Math.Abs(dir.X - target.X) + Math.Abs(dir.Y - target.Y);


Seems wrong, in the code posted you are not checking if the neighbor is in the closed list (it is not a valid neighbor if it is).

Also, k should be the actual distance from the current node to the neighbor, not the heuristic distance to the target!

You should use the heristic distance to the target only when you are calculating the f value.

Here is the wikipedia algorithm marked where I think it is your mistake:

function A*(start,goal)
closedset := the empty set    // The set of nodes already evaluated.
openset := {start}    // The set of tentative nodes to be evaluated, initially containing the start node
came_from := the empty map    // The map of navigated nodes.

g_score[start] := 0    // Cost from start along best known path.
// Estimated total cost from start to goal through y.
f_score[start] := g_score[start] + heuristic_cost_estimate(start, goal)

while openset is not empty
current := the node in openset having the lowest f_score[] value
if current = goal
return reconstruct_path(came_from, goal)

remove current from openset
for each neighbor in neighbor_nodes(current)
if neighbor in closedset
continue
>>>>>>>>>>> Take a look here! <<<<<<<<<<<<<<<<<<
tentative_g_score := g_score[current] + dist_between(current,neighbor)

if neighbor not in openset or tentative_g_score < g_score[neighbor]
came_from[neighbor] := current
g_score[neighbor] := tentative_g_score
f_score[neighbor] := g_score[neighbor] + heuristic_cost_estimate(neighbor, goal)
if neighbor not in openset

>>>>>>>>>>> Up to here! <<<<<<<<<<<<<<<<<<

return failure


I didn't really understand your list removal algorithm to say if it is correct =x

Also, didn't understand how your algorithm detects that a path is impossible.

Edit:

Now I understood the code, it is a heap update... seems ok, but just for the notice, once this condition:

if (squareMap[Openlist[nTemp].X, Openlist[nTemp].Y].F > F)

Is false, you can stop the loop, the heap property is restored by then.

Edited by KnolanCross, 23 January 2014 - 12:00 PM.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

### #8dworm  Members

683
Like
0Likes
Like

Posted 23 January 2014 - 01:08 PM

Seems wrong, in the code posted you are not checking if the neighbor is in the closed list (it is not a valid neighbor if it is).

this is checked into:

 private IEnumerable<Point> ValidMoves(int x, int y)
{
foreach (Point movePoint in movements)
{
int newX = x + movePoint.X;
int newY = y + movePoint.Y;

if (ValidateCoor(newX, newY))
{
if ((squareMap[newX, newY].ContentCode == 0 || squareMap[newX, newY].ContentCode == 3) && !squareMap[newX, newY].Checked)
{
yield return new Point(newX, newY);
}
}
}
}


but im not totally sure what you mean

You should use the heristic distance to the target only when you are calculating the f value.

i probably renamed F with k dunno why, but isnt it correct the check?

i thought its the same since the funcion distance from target (x,y) is a constant for (x,y)  while D (x,y) depends on the path to reach (x,y)

so if i check the D (distance from start) or the total should be the same, no?

Is false, you can stop the loop, the heap property is restored by then.

true, ill update that, but it should only save a few time, not really change the path

### #9KnolanCross  Members

1935
Like
2Likes
Like

Posted 23 January 2014 - 01:29 PM

Nevermind about the closed list, it wasn't in the code you posted the first time, but it is in the ValidMoves.

On the "k" value, what you are doing is:

int k = D + 1 + Math.Abs(dir.X - target.X) + Math.Abs(dir.Y - target.Y);


What you should be doing is:

int k = D + 1

Which is the cost of moving current node to the neighbor.

Then you should test if:

dir is not on open list (in other words, dir has not been checked yet) OR dir current score is bigger than k (in other words, you found a better path to dir).

If the problem is still not clear, take a look at this link:

http://www.policyalmanac.org/games/aStarTutorial.htm

And pay attention to what happens bellow the Figure 4.

Edited by KnolanCross, 23 January 2014 - 01:45 PM.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

### #10dworm  Members

683
Like
0Likes
Like

Posted 23 January 2014 - 01:46 PM

(i dont get how to multiquote using this forum :/ )

yes i do check , basically my closed list is

!squareMap[newX, newY].Checked

after i check all close square i add actual to

squareMap[actualX, actualY].Checked = true;

making it skipped next time

dir is already on open list, dir is picked already from the intersection of not checked nodes via that code above

the check is basically below when i check if dir value was 5000 which is only possible (until i test on 3000x3000 map ) if it was a unchecked node

so if total value of dir node is higher either its 5000 and so its a unchecked node and it goes into

addToOpenList(dir.X, dir.Y);

if it wasnt, it means it was checked already and in the open list so i just run the update mechanic to move him up the list depending on its new score

correctOpenList(dir.X, dir.Y);

### #11KnolanCross  Members

1935
Like
2Likes
Like

Posted 23 January 2014 - 01:59 PM

That seems to be what is wrong in your code, you are mixing the g cost (the ACTUAL cost to get to a node from the start) with the f cost (the estimated cost to get to the end).

Edit:

I got to go for today, here is my code that evaluates the neighbors:

static int processCell(pathFindingStruct* path, int xAdjust, int yAdjust, edgeRef* edge, edgeRef* end, const unsigned extraCost){

return 1;
}

unsigned distance = path->mainGrid[edge->i][edge->j].costSoFar + extraCost;

if (newNode == NULL || (modifiedHeapInsert(path->openList, newNode) == 0)){
free(newNode);
return 0;
}
}
else{
}

}

return 1;
}


It is in C, using pointers to avoid copies. Important notes:

Edge is the current node.

xAdjust and yAdjust is the move, for instance, if their values are (1, 1) I am moving right and down.

The grid is [y][x] aligned.

The return value is just to check memory errors.

"costSoFar" is g and "totalCost" is f.

extraCost is used because diagonal movements are more expensive.

Edited by KnolanCross, 23 January 2014 - 02:05 PM.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

### #12dworm  Members

683
Like
0Likes
Like

Posted 23 January 2014 - 02:03 PM

this:: ?

foreach (Point dir in ValidMoves(actualX, actualY)) // find lowest  F score
{
int k = D + 1;
S = squareMap[dir.X, dir.Y].D;
if (S > k)
{
squareMap[dir.X, dir.Y].F = D + 1 + Math.Abs(dir.X - target.X) + Math.Abs(dir.Y - target.Y);
squareMap[dir.X, dir.Y].D = D + 1;
squareMap[dir.X, dir.Y].DirX = actualX - dir.X;
squareMap[dir.X, dir.Y].DirY = actualY - dir.Y;


doesnt change anything ofc..

maybe im not understanding what you mean...

can you post yoru compare code so i see the difference from mine?

edit: i think my problem is in the heap tbh

for some reason i suspect the new "best" nodes are not going to the top which makes them evaluated after other bad nodes in the open list

i mean for an empty map binary heap should just do

-check all near nodes

- new best nodes (tipically 1 if diagonal is allowed or 2 otherwise) come to the top of the list

other open list node should never be evaluated after distance 1 which is more than any other node on diagonal has...

Edited by dworm, 23 January 2014 - 02:08 PM.

### #13KnolanCross  Members

1935
Like
0Likes
Like

Posted 23 January 2014 - 02:27 PM

Hmm, yes, those are the values you should use.

About the heap, try with a smaller example and print the whole heap to check if it is always the lowest F on top.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

### #14dworm  Members

683
Like
0Likes
Like

Posted 23 January 2014 - 02:37 PM

http://imgur.com/qZJCg0D

it seems the open list get not ordered well so sometimes a node that should be on top its not so random open list nodes get evaluated increasing the "noise"

### #15boogyman19946  Members

1476
Like
4Likes
Like

Posted 23 January 2014 - 03:28 PM

The function that adds to heap does not seem correct to me. I tried to figure out what the proper name of the variables should have been, and it seems rather odd.

void addToOpenList(int x, int y)
{
openListNumber++;
Openlist[openListNumber] = new Point(x, y);
int child = openListNumber;
int parent = child;
int F = squareMap[x, y].F;
Point temp;
while (child > 1)
{
parent = child / 2;

if (squareMap[Openlist[child].X, Openlist[child].Y].F > F)
{
temp = Openlist[parent];
Openlist[parent] = Openlist[child];
Openlist[child] = temp;
}
child = parent;
}
}


Now this algorithm is much easier to understand. The if statement seems to be a likely culprit. You are comparing the cost of a child node to the cost of the inserted node. Notice however, that in the body of the if statement, you swap the parent (which hasn't been compared to anything yet) with the child node. I think you meant to say the following:

void addToOpenList(int x, int y)
{
openListNumber++;
Openlist[openListNumber] = new Point(x, y);
int child = openListNumber;
int parent = child;
Point temp;
while (child > 1)
{
parent = child / 2;

int childCost = squareMap[Openlist[child].X, Openlist[child].Y].F;
int parentCost = squareMap[OpenList[parent].X, OpenList[parent].Y].F;

if (childCost < parentCost)
{
temp = Openlist[parent];
Openlist[parent] = Openlist[child];
Openlist[child] = temp;
}
child = parent;
}
}


Now at least the code clearly shows that it's moving the smallest node to the head of the tree. I think there's also a way to terminate the while loop early once the if statement evaluates false (since you're adding only one node and are moving up one branch of a binary tree).

- Java API Documentation - For all your Java info needs
- C++ Standard Library Reference - For some of your C++ needs ^.^

### #16dworm  Members

683
Like
0Likes
Like

Posted 23 January 2014 - 03:56 PM

THNX that was a huge bug, and improved everything a lot

still its not perfect, there is a small derive anyway :/

### #17dworm  Members

683
Like
0Likes
Like

Posted 23 January 2014 - 06:02 PM

update it seems to work decently now after all the small fixes, also i add diagonal tiles cost and it seems to work much better

probably another problem was too many tiles with same cost, ill try to improve that too

dunno how fast it should be thouhg on a 200 x 200 map neraly empty it takes few ms but with many obstacles it take till 30 40 ms (on my core duo)

24735
Like
1Likes
Like

Posted 24 January 2014 - 04:56 AM

(i dont get how to multiquote using this forum :/ )

How quoting works

### #19KnolanCross  Members

1935
Like
0Likes
Like

Posted 24 January 2014 - 07:29 AM

update it seems to work decently now after all the small fixes, also i add diagonal tiles cost and it seems to work much better

probably another problem was too many tiles with same cost, ill try to improve that too

dunno how fast it should be thouhg on a 200 x 200 map neraly empty it takes few ms but with many obstacles it take till 30 40 ms (on my core duo)

Performance will depend a lot on the language used. I posted my results the other day, you can use it as basis:

http://www.gamedev.net/topic/651968-binary-heap-vs-list-a-my-humble-performance-comparison-updated-with-jps/

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.