Any-goalbidirectionalA*pathfindingreference
创始人
2024-08-22 08:30:31
0

双向A算法是一种常用的路径规划算法,如何实现任意起点和终点的双向A算法呢?这里提供一种参考实现:

def any_goal_bidirectional_A_star(start, goals, heuristic_func, neighbor_func, distance_func): # 初始化起点和终点列表 start_to_goals = {goal: [start, float('inf')] for goal in goals} goal_to_starts = {start: [goal, float('inf')] for goal in goals}

# 初始化搜索队列和已经访问过的点集
forward_queue = PriorityQueue()
backward_queue = PriorityQueue()
forward_closed = set()
backward_closed = set()

# 将起点和终点添加到搜索队列中
forward_queue.put(start_to_goals[goals[0]], start_to_goals[goals[0]][1] + heuristic_func(start, goals[0]))
backward_queue.put(goal_to_starts[goals[0]], goal_to_starts[goals[0]][1] + heuristic_func(start, goals[0]))

while not forward_queue.empty() and not backward_queue.empty():
    # 前向搜索一步
    forward_node, forward_f_value = forward_queue.get()
    forward_curr, forward_dist = forward_node
    forward_closed.add(forward_curr)

    # 找到与前向搜索相同的点
    back_node, back_f_value = goal_to_starts.get(forward_curr, [None, float('inf')])
    if back_node is not None:
        # 找到路径
        path = [forward_node[0]]
        while forward_curr != start:
            path.append(start_to_goals[forward_curr][0])
            forward_curr = start_to_goals[forward_curr][0]

        path = list(reversed(path))
        while back_node[0] != start:
            path.append(back_node[0])
            back_node = goal_to_starts[back_node[0]]

        return path

    # 扩展前向搜索节点
    for neighbor in neighbor_func(forward_curr):
        if neighbor in forward_closed:
            continue

        new_dist = forward_dist + distance_func(forward_curr, neighbor)

相关内容

热门资讯

举措透视!wepoker游戏安... 举措透视!wepoker游戏安装教程,wepoker有辅助插件吗,第三方教程(有挂秘笈)-哔哩哔哩>...
发现透视"wepok... 发现透视"wepoker辅助工具好用吗"开挂(透视)辅助器(9分钟了解高科技教程)发现透视"wepo...
模块透视!hhpoker可以控... 【亲,hhpoker可以控制吗 这款游戏可以开挂的,确实是有挂的,很多玩家在这款hhpoker可以控...
实测透视"微扑克发牌... 实测透视"微扑克发牌机制详解"开挂(透视)辅助器(玩家亲测黑科技教程)实测透视"微扑克发牌机制详解"...
秘籍透视!wepoker可以透... >>您好:wepoker辅助器官方确实是有挂的,很多玩家在这款wepoker辅助器官方游戏中打牌都会...
正版透视"wepok... 正版透视"wepoker透视软件下载"开挂(透视)辅助软件(科技分享揭秘教程)正版透视"wepoke...
模块透视!pokemmo辅助官... wepoker模拟器哪个好用 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四...
曝光透视"微扑克辅助... 曝光透视"微扑克辅助期是干嘛的"开挂(透视)辅助工具(记者发布总结教程)曝光透视"微扑克辅助期是干嘛...
大纲透视!约局吧辅助脚本,aa... aapoker安装包怎么使用是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用...
必看透视"微扑克辅助... 必看透视"微扑克辅助器官网入口"开挂(透视)辅助脚本(今日公布必赢教程)必看透视"微扑克辅助器官网入...