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
