# A* question

This topic is 3805 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

ok, the news is good and bad:

Bad news first, look at this:

So the blue circles are start/stop points, the blue lines are the (relevant portion of the) triangulation, the green line is the optimal path, and the light blue line is the path chosen by A* over the triangulation, because the red path which, if optimized would yield the green path, is actually longer.

So, no proof :(

Good news next:

A delaunay triangulation cannot have this bad case.

more good news?

I conjecture that with some spiffy coding you can make an A* variant that will allow for discovery of the shorter secondary edges at search time; going on the Wikipedia description of A*:
function A*(start,goal)    var closed := the empty set    var q := make_queue(path(start))    while q is not empty        var p := remove_first(q)        var x := the last node of p        if x in closed            continue        if x = goal            return p        add x to closed        foreach y in successors(x)            enqueue(q, p, y)       // magic goes here    return failure

At the line which I have marked magic goes here you would add discovery code to check for each successor, if it can create a secondary path with a node earlier in the path, this then provides a newer cheaper path to the successor.

Will this work?

##### Share on other sites
I see no reason why that wouldn't work.

If you are correct that the optimal path in the constrained delaunay triangulation always contains the nodes of the true optimal path, it may still be more efficient to use an unmodified A* and then smooth the resulting path. Adding edges during the A* search can waste time adding edges for nodes that won't be on the optimal path.

##### Share on other sites
or merge your triangles into convex polygons and just use a full navmesh.

and/or pathfind using the middle-point of the edges or the center of the triangles/poygons THEN constraint your navigation to the corners if that makes the path shorter.

or a dozen variations in-between...

but yeah, a delaunay triangulation, which tries to get close to equilateral triangles, would help. There are some pretty good implementations floating on the web.

##### Share on other sites
Quote:
 Original post by VorpyAdding edges during the A* search can waste time adding edges for nodes that won't be on the optimal path.

Excellent point. Well now I've worked through some delaunay examples, and I can't find a counterexample, but a proof that A* with the pruning step will yield the optimal path eludes me...

Reading about navmeshes, more to come ;)

##### Share on other sites
Bad news: I found a counterexample.

I drew it the same way as the image above. In this case it is a Delaunay triangulation, and the path found does not go through the nodes of the shortest path. The red arc makes the upper path longer than the lower path when following the triangulation, but taking the upper path and going straight across is the shortest route. Time to try proving bounds on how far from optimal the path can be?

Edit: Actually it looks like I made a mistake and surprisingly in this picture the green and red path is actually shorter than the light blue path. The counterexample still works though, some stuff needs to be nudged around a little.

##### Share on other sites
Quote:
 Original post by VorpyBad news: I found a counterexample.I drew it the same way as the image above. In this case it is a Delaunay triangulation, and the path found does not go through the nodes of the shortest path. The red arc makes the upper path longer than the lower path when following the triangulation, but taking the upper path and going straight across is the shortest route. Time to try proving bounds on how far from optimal the path can be?Edit: Actually it looks like I made a mistake and surprisingly in this picture the green and red path is actually shorter than the light blue path. The counterexample still works though, some stuff needs to be nudged around a little.

This bad case happens because your discretisation of the continuous search space is too sparse. In other words, your triangles are too big for the precision you want in finding the minimum path.

##### Share on other sites
It's a counterexample to Symphonic's conjecture that the optimal path in a delaunay triangulation will contain a superset of the nodes in the true optimal path. The path it finds is still pretty good though, within sqrt(2) of the length of the optimal path, at least for this counterexample.

Even if the discretization is denser, if the obstacles do have the shape of those triangles the bad case will still exist, it will just be much more limited in how much error it can produce. There's an idea: adding extra points to the empty space could limit the error in the pathfinding around polygonal obstacles when using a triangulation as the navigation graph.

##### Share on other sites
fresh thoughts:

I read up on navmaps and other fun things like that. Our (deathcarrot's) problem can be described like this:

We have a space with some 1st order explicit geometric obstacles (points and straight lines forming polygons), we want the Geometrical shortest path, and which means that nodes in the search tree MUST be the points of the geometry, and not some secondary things (like the centroids of convex portions of the non-obstacle space).

We can construct a complete graph and cull edges that don't cross obstacles, but in the worst case this means storing O(n^2) edges, and an exponentially larger path space for searches than with triangulations.

There is no constrained triangulation that will add the necessary steiner points to provide an equivalent planar triangulation without exploding the search space. This is actually trivial to show; suppose there are O(n) secondary lines each of which crosses O(n) triangulation lines then for a constrained triangulation to detect these provide these secondary lines, there must be O(n^2) steiner points added to the plane.

Sooooo what do we do?

I think, the best approach is my original idea :P create a simple triangulation, and add some mechanism to the search to detect simpler paths at search time. Strictly speaking you can't escape the complexity of the search you're trying to do, but at least this way, you're not storing alot of data that's redundant anyway.

Steiner Point: a non-data point added to the space by an algorithm, (in this case, added by the constrained triangulation)

##### Share on other sites
This might seem a bit out of context, I didn't read the *whole* discussion. But judging from the example map you showed, I'd use simple steering behaviors for outside areas, since they seem pretty sparse (cast ray to target, for the first object it hits, find an intermediate point to go around it), and A* only inside buildings. Since the buildings seem pretty simple I'd just figure out every possible edge and eliminate the ones that intersect with the walls... A bit brute-force, really, but from my experience it would look good and get the job done!

##### Share on other sites
Quote:
 Original post by JotafThis might seem a bit out of context, I didn't read the *whole* discussion. But judging from the example map you showed, I'd use simple steering behaviors for outside areas, since they seem pretty sparse (cast ray to target, for the first object it hits, find an intermediate point to go around it), and A* only inside buildings. Since the buildings seem pretty simple I'd just figure out every possible edge and eliminate the ones that intersect with the walls... A bit brute-force, really, but from my experience it would look good and get the job done!

Yup, that's the approach I'm currently most considering, although there will be much larger indoor areas as well that encompass several cells, so I'll need to think up a proper indoor navigation system as well (navmeshes sound promising for the moment).

As for the triangulation discussion (taking simple collision avoidance out of the equation for the sake of argument) - I'm still not convinced that with the numbers we're talking about here that it's worth sacrificing run-time performance for a decrease in complexity at the node-generation stage.
Memory consumption certainly shouldn't be an issue, each edge would just be 2 pointers (one at each vertex pointing to the other) so 200-300 vertices worth of edges per 3x3 segment still wouldn't be much of a problem, a few hundred K at most.
Also, say we have a worst case of ~100000 edges to check per segment, this is still a one-time calculation, and I can't see that taking a long time given a point-line intersection is fairly quick to check for (no (maybe one?) sqrt, no trig), and there's quite a few optimisations like bounding circle checks that could be done to reduce the number of possibilities.

Thanks for the input everyone =D

##### Share on other sites
Quote:
 Original post by DeathCarrotThanks for the input everyone =D
You're welcome :) I guess this is why I'm not an AI guy [rolleyes]