简体   繁体   中英

Using A* with target ouside graph

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 (ie 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目标在图中时的示例结果

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.

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…

You can minimize the cost of reaching a node plus the estimated cost of reaching the target from that node. It won't be guaranteed to be optimal once you know the full grid, but depending on your heuristic it should be a reasonable guess.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM