Started by Sep 27 2012 12:50 AM

,
15 replies to this topic

Posted 27 September 2012 - 12:50 AM

I'm trying to implement a minimal/shortest path type problem. Normally, you'd think Dijkstra or some other algorithm would work, but this is a special type of graph where the weights aren't fixed. For example, if the problem was like this, it would be easy (just use Dijkstra):

However, the problem I have is like the following.

Think of it like this... because A -> D is downhill and D -> E is uphill, the weight for D -> E is 1.

Because B -> D is flat and D -> E is uphill, the weight for D -> E is a little larger, 5.

Because C -> D is uphill and D -> E is uphill, consecutive uphill are a doozy for the enemy and so he has a weight of 9.

In other words, any weight along the path doesn't just depend on where you were previously, but where you were before that too. It can even extend farther back several levels like going uphill 3 times in a row is a real doozy and you must pay an extra penalty for that too.

I've thought of a bunch of stuff, but backtracking requires a lot of memory especially when the graph gets large and so far the only solution I've come up with is to just test all possible paths (since enemies are usually close to the player the graph doesn't get too large but for even small graphs testing all possible paths takes too much time).

Has anyone seen a problem like this before? Is there an algorithm for it? Algorithms like Dijkstra, Viterbi, etc. don't work because they assume only one weight per edge.

Thanks!

However, the problem I have is like the following.

Think of it like this... because A -> D is downhill and D -> E is uphill, the weight for D -> E is 1.

Because B -> D is flat and D -> E is uphill, the weight for D -> E is a little larger, 5.

Because C -> D is uphill and D -> E is uphill, consecutive uphill are a doozy for the enemy and so he has a weight of 9.

In other words, any weight along the path doesn't just depend on where you were previously, but where you were before that too. It can even extend farther back several levels like going uphill 3 times in a row is a real doozy and you must pay an extra penalty for that too.

I've thought of a bunch of stuff, but backtracking requires a lot of memory especially when the graph gets large and so far the only solution I've come up with is to just test all possible paths (since enemies are usually close to the player the graph doesn't get too large but for even small graphs testing all possible paths takes too much time).

Has anyone seen a problem like this before? Is there an algorithm for it? Algorithms like Dijkstra, Viterbi, etc. don't work because they assume only one weight per edge.

Thanks!

Posted 27 September 2012 - 01:16 AM

An issue I see is, that you add new criteria to an optmization problem. Doing this is always dangerous, because you could create a NP-hard problem faster than you think. So, maybe your problem is already NP-hard, in this case you will have a really hard time to find a fast solution

**Edited by Ashaman73, 27 September 2012 - 01:17 AM.**

Posted 27 September 2012 - 01:32 AM

You can split node "D" into "D from A", "D from B" and "D from C", because those seem to actually be different states. Then Dijkstra will work again.

Posted 27 September 2012 - 02:03 AM

If all you're doing is modeling terrain elevation costs in a path, I don't see why you need such complicated conditional edge weights. You could easily account for elevation cost in a graph by simply defining a baseline cost between two nodes at the same elevation. If the elevation differs, simply add or subtract from that base cost to simulate increased or decreased difficulty of that transition (be sure to never go negative, or else Dijkstra or A* is no longer correct!).

Modeling the problem in this manner allows you to use local information at each step to build up a more global solution without having to inspect the actual decisions made to determine future values. Your path cost here means the sum total of the "ease of travel" and if you minimize this cost, your path will be the one that is easiest to follow.

Modeling the problem in this manner allows you to use local information at each step to build up a more global solution without having to inspect the actual decisions made to determine future values. Your path cost here means the sum total of the "ease of travel" and if you minimize this cost, your path will be the one that is easiest to follow.

Posted 27 September 2012 - 02:26 AM

(double post, see below)

**Edited by Ashaman73, 27 September 2012 - 02:29 AM.**

Posted 27 September 2012 - 02:27 AM

Was my first thought too, but then you need to split E and after that, the following nodes ...You can split node "D" into "D from A", "D from B" and "D from C", because those seem to actually be different states. Then Dijkstra will work again.

This is the killer argument:

In other words, any weight along the path doesn't just depend on where you were previously, but where you were before that too. It can even extend farther back several levels like going uphill 3 times in a row is a real doozy and you must pay an extra penalty for that too.

**Edited by Ashaman73, 27 September 2012 - 02:28 AM.**

Posted 27 September 2012 - 03:20 AM

Exactly guys! You guys are all right on it... it would either require a massive expansion of nodes or probably a metaheuristic to solve! I'm just silly lol. Ever had that thought where you say ok I'm going to add a little twist to an existing problem to see if I can get better results only to find out that what you're trying to do is silly or impossible :-)? I'm surprised I couldn't find anything on google about this type of problem... "Graph" "shortest path" "multiple weights" "conditional weights" etc... turned up nothing!

**Edited by yadango, 27 September 2012 - 04:03 AM.**

Posted 27 September 2012 - 07:02 AM

If the path history can affect the weight in arbitrary ways, I think you'll have to use something like backtracing, and you are in NP territory. If you can summarize the feature of the path history into a few possibly values (say, number of preceding uphills), then unfold the nodes as being different if you arrive there with different values of that feature.

I don't think there is anything else to this problem.

I don't think there is anything else to this problem.

**Edited by alvaro, 27 September 2012 - 11:33 AM.**

Posted 27 September 2012 - 11:39 AM

Thanks alvaro,

Thanks to you guys I think I found a journal article on the topic: "Finding the Shortest Path in Dynamic Network using Labeling Algorithm." The thing I was looking for was a shortest path algorithm with "nondeterministic weights." The problem is actually quite common (happens with traffic as weights change with time) and has a ton of research published on it. I thought the problem I mentioned might be intractable too but it turns out it is not . It says that as long as the weights are known ahead of time (not random as with traffic and can be listed or represented using a function), the problem is not intractable. Though, yeah, having to backtrack to find out what the possible weights are is going to slow down an already slow polynomial time algorithm he he he.

Thanks!

Thanks to you guys I think I found a journal article on the topic: "Finding the Shortest Path in Dynamic Network using Labeling Algorithm." The thing I was looking for was a shortest path algorithm with "nondeterministic weights." The problem is actually quite common (happens with traffic as weights change with time) and has a ton of research published on it. I thought the problem I mentioned might be intractable too but it turns out it is not . It says that as long as the weights are known ahead of time (not random as with traffic and can be listed or represented using a function), the problem is not intractable. Though, yeah, having to backtrack to find out what the possible weights are is going to slow down an already slow polynomial time algorithm he he he.

Thanks!

**Edited by yadango, 27 September 2012 - 11:53 AM.**

Posted 27 September 2012 - 08:38 PM

I'm struggling to follow your reasoning as to why you have to look backwards to determine the cost of the path going forwards. If your weights are done correctly, getting to node D includes the cost of getting to any of the previous nodes that made up the path. Unless your movement along the terrain can cause a landslide that affects the path ahead I don't follow your line of thinking.

Are your weights taking into account the full cost of traversing the node, such as slope, softness of terrain etc or are they just taking into account the distance?

Are your weights taking into account the full cost of traversing the node, such as slope, softness of terrain etc or are they just taking into account the distance?

Currently working on an open world survival RPG - For info check out my Development blog: ByteWrangler

Posted 27 September 2012 - 09:26 PM

Another possibility is to add an additional dimension, exhaustion, for each node. Say exhaustion can have values 0, 1 or 2. Any uphill path goes up a level of exhaustion (capped at 2). Any downhill path goes down a level of exhaustion (capped at 0). Any flat path... not sure. Stay the same? Or maybe have values 0,1,2,3,4,5 and up = +2, down = -2, flat = -1?

In any case, depending upon the exhaustion level you could modify the outgoing costs, e.g. triple if very exhausted. This would not be an NP-hard problem, just a problem with an x times bigger search space.

In any case, depending upon the exhaustion level you could modify the outgoing costs, e.g. triple if very exhausted. This would not be an NP-hard problem, just a problem with an x times bigger search space.

Posted 27 September 2012 - 09:55 PM

Theoretically, you can prune the graph so that D does not exist anymore and your paths are now A-E, B-E, C-E with their associated costs. The trick is identifying these situations and restructuring the graph on the fly. This example is also potentially overly-simplistic and this solution would get brittle quickly.

Professional consultant on game AI, mathematical modeling, simulation modeling

Co-advisor of the GDC AI Summit

Co-founder of the AI Game Programmers Guild

Author of the book, **Behavioral Mathematics for Game AI**

Posted 28 September 2012 - 04:21 AM

Another possibility is to add an additional dimension, exhaustion, for each node. Say exhaustion can have values 0, 1 or 2. Any uphill path goes up a level of exhaustion (capped at 2). Any downhill path goes down a level of exhaustion (capped at 0). Any flat path... not sure. Stay the same? Or maybe have values 0,1,2,3,4,5 and up = +2, down = -2, flat = -1?

In any case, depending upon the exhaustion level you could modify the outgoing costs, e.g. triple if very exhausted. This would not be an NP-hard problem, just a problem with an x times bigger search space.

How is that another possibility? That's exactly what I suggested...

Posted 28 September 2012 - 05:20 AM

How is that another possibility? That's exactly what I suggested...

The way I read your suggestion was exponentially branching based on the full search history. My suggestion is that the exact path is likely not to matter, only how tired you are at a given node. This means you can have different costs from node D to E for each of a fixed number of tiredness values without the need to stretch it into NP territory. Rather like an earlier thread about pathfinding for vehicles with momentum.

I looked back at the posts one more time, I see where you mention a similar scheme. I'm not sure whether I read your post before you edited it, or perhaps I dismissed the post the moment it mentioned NP.

Posted 28 September 2012 - 05:26 AM

I looked back at the posts one more time, I see where you mention a similar scheme. I'm not sure whether I read your post before you edited it, or perhaps I dismissed the post the moment it mentioned NP.

Yeah, I guess I shouldn't have said two separate things in one paragraph. Plus, you probably expressed the idea better.

Anyway, that's probably the way to go.

Posted 28 September 2012 - 06:45 PM

I'm struggling to follow your reasoning as to why you have to look backwards to determine the cost of the path going forwards. If your weights are done correctly, getting to node D includes the cost of getting to any of the previous nodes that made up the path. Unless your movement along the terrain can cause a landslide that affects the path ahead I don't follow your line of thinking.

Correct. Yeah, if say using squared RMS (square of euclidean distance), you really pay for doing large uphills in the first place, so even something simple like that should take care of things like the one large hill + one small hill is worse than two medium size hills of the same distance problem. I was just trying to extend the problem in a different way to see where I could go with it he he he.

So in general, after reading the research on these types of problems, I've accepted that you should never break Bellman's Principle unless you absolutely have to. It's fun stuff though... there are some really interesting problems that break Bellman's Principle (mainly time-based things like traffic and music performance).

**Edited by yadango, 28 September 2012 - 06:54 PM.**