• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Anand Baumunk

A* problem with heuristic

15 posts in this topic

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

 

 

0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

 

[attachment=19410: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

 

[attachment=19411: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

 

[attachment=19412: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

 

[attachment=19413: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;

 

[attachment=19414: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

 

[attachment=19415: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
2

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0