I am currently trying to use A* to create cyclical routes (for plotting driving routes of set distances). I want to find a driving route from my start location that is as close to my specified length as possible. If it is bigger or smaller that is fine, just as close as possible

I have therefore adapted the heuristic function to:

|target distance-distance Travelled so far| + distance left to travel.

for example, I want to travel 20 miles, so my target distance is 20. At some point in the route i may have travelled 18. Therefore my h would be 20-18+2 = 2. I always pick the smallest h value, the idea being a route of 20 miles will be created finished where i starts.

I had 2 immediate issues with this.

  1. The Start node can't be added to the closed list immediately or it will never create a cyclical route - I believe I have solved this one.
  2. If the search goes into a cul-de-sac it gets stuck and the openlist becomes empty and the search terminates.

The second problem I cannot think of how to solve. It occurs because the successor node that would get the search out of the cul-de-sac is already in the closed list.

Can anyone help me come up with a general solution to my problem? Furthermore do you for see any other issues with my implementation?

would really appreciate any help you can give.

  • 1
    $\begingroup$ It's still not clear to me what you're looking for. Is it a closed walk in your graph where the total weight of the edges in the graph is as close as possible to the target? (A walk is like a path in a graph but you're allowed to repeat vertices and and edges; "closed" means that the last vertex is the same as the first.) So, for example, a possible answer might be "drive from $a$ to $b$ and then back to $a$, ten times", assuming nothing was closer to your target? $\endgroup$ – David Richerby Dec 18 '14 at 14:37
  • $\begingroup$ Yes precisely! I was hoping the heuristic function I have chosen would discourage such behaviour but this would certainly be acceptable. $\endgroup$ – Programatt Dec 18 '14 at 14:45
  • $\begingroup$ OK. In that case, my answer doesn't apply so I've deleted it. It may be possible to do what you want with A* but the search space will be exponential so the algorithm might not be efficient. I'm busy for the rest of the afternoon but I might have time to write another answer later. $\endgroup$ – David Richerby Dec 18 '14 at 15:09
  • $\begingroup$ I'd really appreciate that David, thank you. efficiency isn't too important to me at this moment in time as I would just like to get it working. Any help you can give would be really appreciated. $\endgroup$ – Programatt Dec 18 '14 at 19:35
  • $\begingroup$ 1. Why have you honed in on using A* for this task? There might be other ways to do it that would be better than A*. Seems like you're getting ahead of yourself. 2. What do you know about the graph? Are all distances integers, or could they be real numbers? What are typical values for the desired length, the number of vertices, and the number of edges? 3. Your problem is NP-hard (by reduction from subset sum), so you shouldn't expect a general-purpose algorithm. So, please read cs.stackexchange.com/q/19412/755. $\endgroup$ – D.W. Dec 18 '14 at 23:43

This problem is known as the Target Value Search Problem (TVS) and it can be succintly described as follows:

Given a graph $G(V,E)$, two nodes $s$ and $t$ ($s, t\in V)$ and a target value $T$ find a path $P$ from $s$ to $t$ such that the cost of the path is as close as possible to $T$

Of course, $s$ and $t$ might be the same node (as in your case). Note that the problem is not asking for paths (or, alternatively routes) whose cost equals exactly $T$ but that no path with a cost closer to $T$ exists. This is known as solving the Target Value Search Problem optimally. As usual, the cost of a path is defined as the sum of the costs of all edges traversed in the path.

There are different variations of this problem. All of them are described in the paper:

Carlos Linares López, Roni Stern, Ariel Felner: Target-Value Search Revisited. IJCAI 2013

The specific case that interests you most has been also investigated. You can find a short summary of results in the paper:

Carlos Linares López, Roni Stern, Ariel Felner: Solving the Target-Value Search Problem. SOCS 2014

The best solution currently known (to the best of my knowledge) consists of applying a bidirectional search. From your considerations and the notes provided by other people contributing to this thread:

  1. You are right! The problem is that you'd like to come back to the same node but the route to traverse when going back is in the closed list. This problem is specifically addressed in the backwards search suggested in the algorithm $T^*$
  2. David Richerby is right also! A* should traverse a state space which is exponential.

In fact, regarding the second comment. A very simple baseline for solving this problem consists of either using A$^*$ or IDA$^*$ in the path space. IDA$^*$ naturally generates paths (and thus it traverses a state space which can be exponentially large); A$^*$ can do that also if you just ignore nodes in the closed list!

In both cases, a node is never re-expanded if and only if it is an ancestor of the current node. However, it seems to me that you might find this acceptable. In that case, you are forced to expand all nodes!

Hope this helps,

  • $\begingroup$ You have given me a lot of great reading here. I find your last comment particularly of interest: IDA* sounds like exactly what I need. I will certainly read your papers though and see if I can implement a better solution though. Thanks for taking the time to write this. I'm going to mark it as accepted. $\endgroup$ – Programatt Dec 19 '14 at 18:58
  • $\begingroup$ Thanks Programatt for the kind words!! Please, in case you are interested in the implementation of A*, IDA* or T* that we describe in the papers, do not hesitate to contact me (carlos.linares@uc3m.es) and I will send it back to you along with some instructions to make it work. Happy coding! $\endgroup$ – Carlos Linares López Dec 20 '14 at 12:38
  • $\begingroup$ Carlos, Thank you! I will almost certainly be in touch! :) $\endgroup$ – Programatt Dec 20 '14 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.