•      Sign In
• Create Account

## A* Pathfinding

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.

6 replies to this topic

### #1markypooch  Members

1035
Like
0Likes
Like

Posted 09 March 2014 - 11:27 AM

Hello all,

I recently undertook an ambitious goal of implementing A* pathfinding in my aliens AI

While the result in my opinion for a first attempt has been relatively successful, I still have questions that need to be answered.

Thats why I'm here to talk to the best ;)

My aliens traverse a level consisting of tiles that carry a hurestic, gCost, and a final FCost given by

fCost = hurestic + gCost;

My aliens will choose the 8 nodes around their position and choose the one with the lowest fCost.

Notes

----------------------------

I am using the manhattan method for the gCost and hurestic.

the hurestic is given by

diffTileX = StartTileX - TargetTileX

diffTileY = StartTileY - TargetTileY

huresticMulitplier = abs(diffTileX) + abs(diffTileY);

hurestic  = 10 * huresticMultiplier;

the gCost is given by

a orthongonal move is 10 + the accumulative gCost of parent Node

a diagonal       moveis 14 + the accumulative gCost of parent Node

--------------------------

My alien will pick the node in the list (after a bubble sort) that has the lowest f-Cost, no more, no less.

However!

My alien DOES find my position, but does not take a direct route AT ALL!

Should there be futher testing other then just an f-Cost?

If so, then what? Should I check if the path that leads to the target has a incrementally dropping hurestic?

I will include a picture to show the aliens path during run-time

### #2markypooch  Members

1035
Like
0Likes
Like

Posted 09 March 2014 - 11:29 AM

This may not have been the correct place to post this topic, ill move it if asked.

Also as you can see the Alien will always find the target, but will take a very unconcise path

### #3markypooch  Members

1035
Like
0Likes
Like

Posted 09 March 2014 - 04:19 PM

I figured it out....sorta.

Once all of the nodes have been recorded with the lowest f-Cost, then I trace the path from the target and check the adajecent nodes while tracing and only trace the one with the lowest g-Cost to eliminate redundant moves. However, I feel this is the most brute-force way to approach it. Does anybody know a better one?

I mean, I am check against all the nodes in the path to see if there adajecent to that current square (this involves several for-loops that iterate several hundred times). once I get all the squares adajcent to the current square, I then check to see which has the lowest g-Cost. If only one node is adajecent to the current node, then I dont bother doing any testing and add it to the final path.

Is there a better way to approach this? Like when I am recording the f-Cost maybe instead of offsetting until after the fact?

http://i912.photobucket.com/albums/ac327/markypooch/final_zps31e712dd.png

P.S.

For what it's worth the alien does take a now much better path

-Marcus

### #4tobiasjs  Members

148
Like
0Likes
Like

Posted 09 March 2014 - 05:02 PM

In order to find the optimal route, you need to implement A* somewhat differently.

First off, your heuristic needs to underestimate the cost after a candidate step - at the moment, I suspect that it doesn't.

Secondly, to find the optimal path, the A* algorithm needs to explore completely to the target. You need to keep a heap of <fCost,path> tuples, and iterate popping the least cost path off the heap, enumerating all the valid moves from that point, and pushing updated tuples onto the heap until you reach the goal.

You can't make a single iteration of the above loop and then make a movement choice based upon that.

A* is essentially a breadth-first search of the tree of moves, with the heuristic serving to delay searching likely unprofitable paths.

What it sounds like you're implementing currently (by fixing a move choice at each step) is more like depth-first search.

### #5markypooch  Members

1035
Like
0Likes
Like

Posted 09 March 2014 - 05:14 PM

The algorithm that I am currently using is as you guessed does indeed overestimate it.

I like your suggestion of using the tuples. I feel this is once again a instance where I jumped head first into a concept way over my head without thinking about it enough.

My implementation of A* works now and DOES find a concise path, but at the cost of I fear performance, and the creeping suspicion of knowing that Im going about it as you mentioned the least optimal way.

Ill switch my hurestic to underestimate . And eventually revise the entire algorithm to embody a breadth-first search as opposed a depth-first search (which I am going to google after this reply

But your completely right, my hurestic overestimates it, and I choose a step every iteration.

Thanks a bunch for the reply,

-Marcus

### #6tobiasjs  Members

148
Like
0Likes
Like

Posted 10 March 2014 - 04:10 AM

The good thing is that you don't need to recompute paths all that often. You should be able to implement it efficiently enough for realtime use.

Definitely you should use a heap rather than bubble sort. Bubble sort is only ever a good idea for almost sorted data. Its average time complexity is O(n^2), which means that it will quickly dominate the pathfinding. A heap, in contrast, can be used to completely sort in O(nlog(n)) time, and incremental operations like adding and removing values, it's O(log(n)).

Another useful optimization is to break your map up into convex regions, because the shortest path in a convex region is straightforward. Thus you can take bigger steps (crossing an entire convex space in one move) and potentially reach your goal with less exploration.

### #7markypooch  Members

1035
Like
0Likes
Like

Posted 16 March 2014 - 12:36 PM

Alright, I think I got it.

The alien will always find the shortest path (IF THERE IS ONE (Denoted by no more nodes in the OpenList))

For the sake of visual representation, I drew on the tiles to show nodes contained in the OpenList, ClosedList, and the FinalPath traced back from the target to

each parent back to the start node

In the last photo you'll see the alien went off the map to find the shortest path, that of course will be reprimanded.

I'm glad I got it working correctly, now I can work on aliens decision making and other areas of the AI

**Edit

Red is the openList,

Blue is the ClosedList

yellow is the finalPath

#### Attached Thumbnails

Edited by markypooch, 16 March 2014 - 12:37 PM.

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.