Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pathfinding - A* with least turns

Is it possible to modify A* to return the shortest path with the least number of turns?

One complication: Nodes can no longer be distinguished solely by their location, because their parent node is relevant in determining future turns, so they have to have a direction associated with them as well.

But the main problem I'm having, is how to work number of turns into the partial path cost (g). If I multiply g by the number of turns taken (t), weird things are happening like: A longer path with N turns near the end is favored over a shorter path with N turns near the beginning.

Another less optimal solution I'm considering is: After calculating the shortest path, I could run a second A* iteration (with a different path cost formula), this time bounded within the x/y range of the shortest path, and return the path with the least turns. Any other ideas?

like image 276
user202987 Avatar asked Oct 17 '13 20:10

user202987


1 Answers

The current "state" of the search is actually represented by two things: The node you're in, and the direction you're facing. What you want is to separate each of those states into different nodes.

So, for each node in the initial graph, split it into E separate nodes, where E is the number of incoming edges. Each of these new nodes represents the old node, but facing in different directions. The outgoing edges of these new nodes will all be the same as the old outgoing edges, but with a different weight. If the old weight was w, then...

  • If the edge doesn't represent a turn, make the new weight w as well
  • If the edge does represent a turn, make the new weight w + ε, where ε is some number significantly smaller than the smallest weight.

Then just do a normal A* search. Since none of the weights have decreased, your heuristic will still be admissible, so you can still use the same heuristic.

like image 109
BlueRaja - Danny Pflughoeft Avatar answered Jan 04 '23 14:01

BlueRaja - Danny Pflughoeft