My goal is to find the shortest path between A and B but B might be outside of the known graph. Currently my cost function works perfectly and heuristics function is 3D euclidean.
To solve the problem of target might be outside of the graph, I've tried to exit the algorithm when priority value stopped changing a certain time but that didn't worked. The algorithm spitted out a random path.
The reason why I don't select a node that is closest to the target is that if I select that algorithm will try to reach that point for certain. In my application I will rerun the algorithm when new information comes (i.e. update to the graph that might include the target).
So what I need is to find a path that is closest to target outside graph with least cost. I don't expect a code or whatsoever as an answer but any comment on how this kind of problem might be solved is greatly appreciated.
Below, I've added my A* implementation, PriorityQueue is just an wrapper for heapq.
def a_star_search(mesh, start, goal):
frontier = PriorityQueue()
frontier.put(start, 0)
came_from = {}
cost_so_far = {}
came_from[start] = None
cost_so_far[start] = 0
while not frontier.empty():
current = frontier.get()
if current == goal:
break
for i, next in enumerate(list(mesh.neighbors(current))):
new_cost = cost_so_far[current] mesh.cost(current, next)
if next not in cost_so_far or new_cost < cost_so_far[next]:
cost_so_far[next] = new_cost
priority = new_cost mesh.heuristic(next, goal) / mesh.unit
frontier.put(next, priority)
came_from[next] = current
return came_from, cost_so_far
Example result when target is inside graph
CodePudding user response:
What does it even mean to be "close" to something that's not in the graph? If the target node could appear anywhere, then there's no point in precomputing anything because you'll have to start over after it appears. If you know where it will eventually appear, then add it to the graph now and compute your path, then truncate the result at the edge of the (currently) known graph.
CodePudding user response:
Can you just add goal
to your graph (connected to every other node, presumably using whatever distance-metric your mesh used)? Or if you don't want to modify the mesh data-structure, you could just loop over list(mesh.neighbors(current)) goal
(after updating new_cost = …
to handle that situation).
Apologies if I've misunderstood something, and this doesn't apply at all…