Jump to content

  • Log In with Google      Sign In   
  • Create Account


A* problem with heuristic


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.

  • You cannot reply to this topic
15 replies to this topic

#1 gnomgrol   Members   -  Reputation: 493

Like
0Likes
Like

Posted 10 January 2014 - 02:07 PM

Well met!

 

I managed to fix the performaceproblem regarding my A*-implementation, thanks to you guys!

However, that made a new problem appear. When I use a heuristic value for my pathing, it becomes incorrect while skipping it returns the correct path but of corse takes a longer time (WAY longer on larger maps).

 

Illustration (left one is using heuristic):

 

gu2cdtrg.jpg

 

The heuristic is computed like this:

hCost = 10*(abs(checkingNode->pos.x - goal->pos.x) + abs(checkingNode->pos.y - goal->pos.y))
finalCost = hCost + gCost;
// with 10 beeing the G-cost for moving one square (in only left/right/top/bot direction), and 14 for diagonal

 

The next sqaure to be put on my openlist is the one with the lowest cost, of corse. I can see why this is happening, but I can't think of a way to fix this. The tutorials I checked all where using this exact heuristic.

If you can help me out here again, I'd greatly appreciate it!

 

Greetings

-gnomgrol

 

 



Sponsor:

#2 phil_t   Crossbones+   -  Reputation: 2480

Like
1Likes
Like

Posted 10 January 2014 - 04:19 PM

Just a guess, but your heuristic function doesn't take into account the reduced cost of diagonal movement. From this page:

 

http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html

 

  • If h(n) is sometimes greater than the cost of moving from n to the goal, then A* is not guaranteed to find a shortest path, but it can run faster.

 

Consider the case where you move from (0, 0) to (1, 2). Your heuristic will return (10 * (1 + 2)), or 30. But your cost of moving there will be 10 + 14, or 24.

 

How about trying a heuristic that uses the euclidean distance between the two points? e.g. something like:

 

10*(sqrt((checkingNode->pos.x - goal->pos.x) ^2 + (checkingNode->pos.y - goal->pos.y) ^2))


Edited by phil_t, 10 January 2014 - 04:19 PM.


#3 ferrous   Members   -  Reputation: 931

Like
0Likes
Like

Posted 10 January 2014 - 06:19 PM

You're only looking at the distance to the goal, and not the distance that it would take to get to the current node.

 

you could have something like:

heuristic = lengthGoalToCurrentNode + lengthOfCurrentPath

 

EDIT: Silly me, you have costs already, so it should be:

heuristic = cost to get to current node + (estimated) cost to get to goal

ie your gCost should be the total cost to get to that square, not just the cost to move a single square.


Edited by ferrous, 10 January 2014 - 08:20 PM.


#4 gnomgrol   Members   -  Reputation: 493

Like
0Likes
Like

Posted 11 January 2014 - 03:53 PM

You were absolutely right! Using euclidean distance fixed it.

The downsite of it is, that the computation now takes over 20 seconds on a 512 * 512 map.

Do you have something on the top of your mind to make it faster?



#5 ericrrichards22   Members   -  Reputation: 701

Like
0Likes
Like

Posted 11 January 2014 - 04:01 PM

Perhaps you want to use the diagonal movement heuristic (Chebyshev distance) from that same page.  That way you won't have to compute the square root as in Euclidean heuristic.

 

You may also want to look at the way you are representing your open and closed sets.  Using better data structures can help a lot.  Your open set wants to be some kind of a priority queue, which would classically be a binary heap, while the closed set is a good fit for something like a hashset.


Eric Richards

SlimDX tutorials - http://www.richardssoftware.net/

Twitter - @EricRichards22


#6 gnomgrol   Members   -  Reputation: 493

Like
0Likes
Like

Posted 11 January 2014 - 05:15 PM

100% right. Avoiding the sqrt() was what had to be done.

 

EDIT: OK, I was wrong. The speed is fine now, but the path doesn't appear to be the shortest anymore. I used

 
int h;
int dx = abs(currentNode->pos.x - goal->pos.x);
int dy = abs(currentNode->pos.y - goal->pos.y);
h = 10 * (dx + dy) + (14 - 2 * 10) * min(dx, dy);

 

and it looks like this:

 

3x4akdmo.png


Edited by gnomgrol, 11 January 2014 - 05:37 PM.


#7 ngoaho91   Members   -  Reputation: 227

Like
2Likes
Like

Posted 11 January 2014 - 10:36 PM

you just doing wrong when implement the algorithm.
 
according to A*, f = g+h, where f = decision function, g = cost, h = heuristic function.
your h function is right
but your g function, i think something wrong here.
 
take a look at that example
 
Start = 0-0 goal = 4,0

 

1.png

 

Go to 1-0

1-0 has neighbor 1-1, 0-1

Update 1-1, G(1,1) = min(G(1,1), G(1,0) + 1) = 1.4

Update 0-1, G(0,1) = min(G(0,1),G(1,0) + 1.4) = 1

 

2.png

 

Go to 0-1

0-1  has neighbor 1-1, 0-2, 1-2

Update 1-1, G(1,1) = min(G(1,1),G(0,1)+ 1) = 1.4

Update 0-2, G(0,2) = min(G(0,2),G(0,1)+1) = 2

Update 1-2, G(1,2) = min(G(1,2), G(0,1)+1.4) = 2.4

 

3.png

 

Go to 1,1

1-1 has 3 neighbor 0-2, 1-2, 2-2

Update 0-2, G(0,2) = min(G(0,2),G(1,1)+1.4) = 2

Update 1-2, G(1,2) = 2.4

G(2,2) = 2.8

 

4.png

 

Go to 2,2

G(3,1) = 4.2; G(3,3) = 4.2; G(2,3) = 3.8;G(1,3) = 4.2;

 

5.png

 

Go to 1,3

G(3,0) = 5.2; G(4,0) = 5.6; G(4,1) = 5.2; G(4,2) = 5.6

 

6.png

 

Go to 4,0 => finish

Trace path 4-0, 1-3, 2-2,1-1,0-0

 

p/s: sorry, no step "Go to 0-1"


Edited by ngoaho91, 11 January 2014 - 11:25 PM.


#8 gnomgrol   Members   -  Reputation: 493

Like
0Likes
Like

Posted 12 January 2014 - 04:29 AM

Thanks, that was finally it! I had used 10 and 14 flat for Gcost, instead of using the parents + 10 or 14.

Works perfectly now, much love to you guys for helping me out!



#9 greenpig83   Members   -  Reputation: 287

Like
1Likes
Like

Posted 12 January 2014 - 12:13 PM

512x512 is quite large for A*. If u cant reach it, u will search all map! (nearly 500x500 node to be processed)

And it will be too slow. What is your test result (milisecond)  in unReachable case ?


Edited by greenpig83, 12 January 2014 - 12:14 PM.


#10 gnomgrol   Members   -  Reputation: 493

Like
0Likes
Like

Posted 12 January 2014 - 03:17 PM

I didn't think of that, you are right. It takes about 10 secs for worst-case-no-path on a 512*512 map. Much too slow, obviously.

So, I somehow need to make the maps that needs to be computed smaller. First thing I would think of is subdividing the map in like 16*16 chunks.

But I have no clue on how to implement that with A*.

I would need to have information if one chunk can be accessed from his neighbours or something like that ... but I can't think of a way to make that work properly :(

 

Do you know any articles on that question? I couldn't find anything with googleing over an hour now.



#11 ngoaho91   Members   -  Reputation: 227

Like
0Likes
Like

Posted 12 January 2014 - 07:52 PM

with this data structure, i can't find any algorithm better than A*. but there're many way to find path in a map. 

like this one

http://www.david-gouveia.com/portfolio/pathfinding-on-a-2d-polygonal-map/



#12 greenpig83   Members   -  Reputation: 287

Like
0Likes
Like

Posted 12 January 2014 - 08:34 PM

I didn't think of that, you are right. It takes about 10 secs for worst-case-no-path on a 512*512 map. Much too slow, obviously.
So, I somehow need to make the maps that needs to be computed smaller. First thing I would think of is subdividing the map in like 16*16 chunks.
But I have no clue on how to implement that with A*.
I would need to have information if one chunk can be accessed from his neighbours or something like that ... but I can't think of a way to make that work properly sad.png

Do you know any articles on that question? I couldn't find anything with googleing over an hour now.

More than 1 second is almost unacceptable for game!
Your thinking is very intuitive! And it's what come to my mind when i have to solve this problem!
In this case there should be 2 search. Short search for close range, long search for long range use regions as node! Short search should be fast, so the range should be 100-150. Divide the map in to clusters of NxN size. In each cluster do breath first search to detect regions and its connection. The cluster size is make sure the region is not too large so we can search path from 1 region to other with short search! But not too small, so the number of regions are not too many. In my current setup, Cluster size is 40, number of regions is about 270!

Long search : use simple search from region start to region goal. It's very fast. If it's reachable, we do short search from region start to its neighbour region. So each time there will be only 1 long search and 1 short search!

The main problem is doing region divide for all map is slow! it's just like running A* on whole map in worst case. So it should be update only clusters that changed. Like obstacle disappear(we update only the cluster that the obstacle is inside, or maybe not if it doesn't affect the connectivity at all) , if your map is quite static, it's very easy!

 

I did a lot of google search like u. There are some variations of A* that can be used like Hierarchical Abstraction and its family (HPA*...there are many upgrade,optimize...) : This link is good : http://aigamedev.com/open/tutorials/clearance-based-pathfinding/ and this one : http://www.ru.is/~yngvi/pdf/BjornssonH06.pdf

 

I also think a bout another idea. Using steering option. It's just like how people move in real life, a unit will just heading to the direction of it's location, if it's stuck, it will choose to steer left or right to avoid the obstacle very human-like ! It will be lighting fast this way. But decide to move left or right is trouble some, some time move left is openroad, and move right is deadEnd. If we cant choose the right way, when we reach deadEnd, we must running back all the way !


Edited by greenpig83, 12 January 2014 - 10:00 PM.


#13 KnolanCross   Members   -  Reputation: 1064

Like
1Likes
Like

Posted 13 January 2014 - 06:16 AM

To limit the algorithm time I used a max iterations counter, if I explore more than a certain number of nodes I assume no path exist. It is not a perfect method, but it is a simple one that mostly works.

If you want a perfect one you need to use a flood algorithm to determine map regions when two points are in different regions they are not reacheable from one another and hence the path is impossible.

 

How are you keeping your open list and your closed list?

 

Changing the open list to a heap can improve your performance greatly.

I have a C implementation that I changed the open list from a linked list to a heap, you can check the results here:

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

 

Also, are you sure you are not doing any useless copies? My C version of it is using memory pooling and a load of pointers, my 640 x 640 case time is 0,002006.

 

PS: I believe this post should be on AI section.


My blog on programming and games.
http://16bitsflag.blogspot.com.br/

#14 ferrous   Members   -  Reputation: 931

Like
0Likes
Like

Posted 13 January 2014 - 01:00 PM

I didn't think of that, you are right. It takes about 10 secs for worst-case-no-path on a 512*512 map. Much too slow, obviously.

So, I somehow need to make the maps that needs to be computed smaller. First thing I would think of is subdividing the map in like 16*16 chunks.

But I have no clue on how to implement that with A*.

I would need to have information if one chunk can be accessed from his neighbours or something like that ... but I can't think of a way to make that work properly sad.png

 

Do you know any articles on that question? I couldn't find anything with googleing over an hour now.

 

You could split your map up into regions.  Keep track of region entrances/exits.  For example, in your last picture of pathfinding, you have these very large rectangular areas, and they only have one or two entrances/exits to each region.  If you know you are in region 1, and want to get to region 4, you simply iterate over the regions first to see if you can get to region 4 from region 1, and in addition, you can pare down the pathfinding as well, as you can have your agent find the path to the exit of region 1 and start moving before finding the entire path to the goal.


Edited by ferrous, 13 January 2014 - 01:00 PM.


#15 ApochPiQ   Moderators   -  Reputation: 12346

Like
4Likes
Like

Posted 13 January 2014 - 01:11 PM

Complicated solutions aren't necessary. A* is perfectly capable of handling many hundreds of thousands of nodes in a search, and a good implementation can search that entire space in milliseconds.

 

I recommend grabbing a profiler and learning some optimization tricks :-)



#16 recursively   Members   -  Reputation: 286

Like
0Likes
Like

Posted 16 January 2014 - 02:02 PM

Also, if the costs are not dynamic, you could pre-calculate them or cache path results between nodes and use the nearest point on the path as the starting node, etc.






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.



PARTNERS