双向最佳路径优先搜索算法

概念

双向最佳优先搜索(Bidirectional Best-First Search)是一种图搜索算法,用于在给定的图或树中找到两个节点之间的最短路径。该算法尝试从起始节点和目标节点同时扩展搜索,直到两个搜索方向相遇。

双向最佳优先搜索的步骤如下:

1、初始化起始节点和目标节点,并分别创建两个优先队列(open1和open2)保存待扩展的节点。

2、设置两个哈希表(closed1和closed2)用于存放已扩展的节点,并初始化为空。

3、将起始节点和目标节点分别放入open1和open2中,并计算起始节点和目标节点的启发式函数值(例如,启发式函数可以使用估计的最短路径长度)。

4、进入循环,直到open1或open2为空:
    4.1、从open1中取出启发式函数值最小的节点,并将其加入closed1中。
    4.2、对该节点进行扩展,生成其所有邻居节点,并计算它们的启发式函数值。
    4.3、如果某个邻居节点已在closed2中出现,意味着两个搜索方向相遇,找到了最短路径。
    4.4、否则,将未出现在closed1中的邻居节点加入open1。
    4.5、从open2中以相同的方式处理节点,并将其加入closed2中。

5、如果搜索结束时没有找到最短路径,表示起始节点和目标节点之间没有连接。

双向最佳优先搜索相较于传统的单向搜索算法,可以大幅减少搜索范围,提高搜索效率。由于每次扩展都是在两个方向上进行,搜索空间的大小可以被双向同时扩展的方式削减一半。

如下图,左侧是直接进行次搜索产生的搜索树,右侧是双向搜索的两棵搜索树,避免了层数过深时分支数量的大规模增长。
在这里插入图片描述

代码示例:

import os
import sys
import math
import heapq
import matplotlib.pyplot as plt


class BidirectionalBestFirst:
    def __init__(self, s_start, s_goal, heuristic_type,xI, xG):
        self.s_start = s_start
        self.s_goal = s_goal
        self.heuristic_type = heuristic_type
        self.motions = [(-1, 0), (-1, 1), (0, 1), (1, 1),
                        (1, 0), (1, -1), (0, -1), (-1, -1)]
        self.u_set = self.motions  # feasible input set
        self.obs = self.obs_map()  # position of obstacles

        self.OPEN_fore = []  # OPEN set for forward searching
        self.OPEN_back = []  # OPEN set for backward searching
        self.CLOSED_fore = []  # CLOSED set for forward
        self.CLOSED_back = []  # CLOSED set for backward
        self.PARENT_fore = dict()  # recorded parent for forward
        self.PARENT_back = dict()  # recorded parent for backward
        self.g_fore = dict()  # cost to come for forward
        self.g_back = dict()  # cost to come for backward
        self.x_range = 51  # size of background
        self.y_range = 31
        
        
        self.xI, self.xG = xI, xG
        self.obs = self.obs_map()
    def init(self):
        """
        initialize parameters
        """

        self.g_fore[self.s_start] = 0.0
        self.g_fore[self.s_goal] = math.inf
        self.g_back[self.s_goal] = 0.0
        self.g_back[self.s_start] = math.inf
        self.PARENT_fore[self.s_start] = self.s_start
        self.PARENT_back[self.s_goal] = self.s_goal
        heapq.heappush(self.OPEN_fore,
                       (self.f_value_fore(self.s_start), self.s_start))
        heapq.heappush(self.OPEN_back,
                       (self.f_value_back(self.s_goal), self.s_goal))

    def obs_map(self):
        """
        Initialize obstacles' positions
        :return: map of obstacles
        """

        x = 51
        y = 31
        obs = set()

        for i in range(x):
            obs.add((i, 0))
        for i in range(x):
            obs.add((i, y - 1))

        for i in range(y):
            obs.add((0, i))
        for i in range(y):
            obs.add((x - 1, i))

        for i in range(10, 21):
            obs.add((i, 15))
        for i in range(15):
            obs.add((20, i))

        for i in range(15, 30):
            obs.add((30, i))
        for i in range(16):
            obs.add((40, i))

        return obs

    def animation(self, path, visited, name):
        self.plot_grid(name)
        self.plot_visited(visited)
        self.plot_path(path)
        plt.show()

    def animation_bi_astar(self, path, v_fore, v_back, name):
        self.plot_grid(name)
        self.plot_visited_bi(v_fore, v_back)
        self.plot_path(path)
        plt.show()

    def plot_grid(self, name):
        obs_x = [x[0] for x in self.obs]
        obs_y = [x[1] for x in self.obs]

        plt.plot(self.xI[0], self.xI[1], "bs")
        plt.plot(self.xG[0], self.xG[1], "gs")
        plt.plot(obs_x, obs_y, "sk")
        plt.title(name)
        plt.axis("equal")

    def plot_visited(self, visited, cl='gray'):
        if self.xI in visited:
            visited.remove(self.xI)

        if self.xG in visited:
            visited.remove(self.xG)

        count = 0

        for x in visited:
            count += 1
            plt.plot(x[0], x[1], color=cl, marker='o')
            plt.gcf().canvas.mpl_connect('key_release_event',
                                         lambda event: [exit(0) if event.key == 'escape' else None])

            if count < len(visited) / 3:
                length = 20
            elif count < len(visited) * 2 / 3:
                length = 30
            else:
                length = 40
            #
            # length = 15

            if count % length == 0:
                plt.pause(0.001)
        plt.pause(0.01)

    def plot_path(self, path, cl='r', flag=False):
        path_x = [path[i][0] for i in range(len(path))]
        path_y = [path[i][1] for i in range(len(path))]

        if not flag:
            plt.plot(path_x, path_y, linewidth='3', color='r')
        else:
            plt.plot(path_x, path_y, linewidth='3', color=cl)

        plt.plot(self.xI[0], self.xI[1], "bs")
        plt.plot(self.xG[0], self.xG[1], "gs")

        plt.pause(0.01)

    def plot_visited_bi(self, v_fore, v_back):
        if self.xI in v_fore:
            v_fore.remove(self.xI)

        if self.xG in v_back:
            v_back.remove(self.xG)

        len_fore, len_back = len(v_fore), len(v_back)

        for k in range(max(len_fore, len_back)):
            if k < len_fore:
                plt.plot(v_fore[k][0], v_fore[k][1], linewidth='3', color='gray', marker='o')
            if k < len_back:
                plt.plot(v_back[k][0], v_back[k][1], linewidth='3', color='cornflowerblue', marker='o')

            plt.gcf().canvas.mpl_connect('key_release_event',
                                         lambda event: [exit(0) if event.key == 'escape' else None])

            if k % 10 == 0:
                plt.pause(0.001)
        plt.pause(0.01)


    def searching(self):
        """
        Bidirectional Best First
        :return: connected path, visited order of forward, visited order of backward
        """

        self.init()
        s_meet = self.s_start
        count = 0
        while self.OPEN_fore and self.OPEN_back:
            # solve foreward-search
            print(count)
            count+=1
            _, s_fore = heapq.heappop(self.OPEN_fore)

            if s_fore in self.PARENT_back:
                s_meet = s_fore
                break

            self.CLOSED_fore.append(s_fore)

            for s_n in self.get_neighbor(s_fore):
                new_cost = self.g_fore[s_fore] + self.cost(s_fore, s_n)

                if s_n not in self.g_fore:
                    self.g_fore[s_n] = math.inf

                if new_cost < self.g_fore[s_n]:
                    self.g_fore[s_n] = new_cost
                    self.PARENT_fore[s_n] = s_fore
                    heapq.heappush(self.OPEN_fore,
                                   (self.f_value_fore(s_n), s_n))

            # solve backward-search
            _, s_back = heapq.heappop(self.OPEN_back)

            if s_back in self.PARENT_fore:
                s_meet = s_back
                break

            self.CLOSED_back.append(s_back)

            for s_n in self.get_neighbor(s_back):
                new_cost = self.g_back[s_back] + self.cost(s_back, s_n)

                if s_n not in self.g_back:
                    self.g_back[s_n] = math.inf

                if new_cost < self.g_back[s_n]:
                    self.g_back[s_n] = new_cost
                    self.PARENT_back[s_n] = s_back
                    heapq.heappush(self.OPEN_back,
                                   (self.f_value_back(s_n), s_n))

        return self.extract_path(s_meet), self.CLOSED_fore, self.CLOSED_back

    def get_neighbor(self, s):
        """
        find neighbors of state s that not in obstacles.
        :param s: state
        :return: neighbors
        """

        return [(s[0] + u[0], s[1] + u[1]) for u in self.u_set]

    def extract_path(self, s_meet):
        """
        extract path from start and goal
        :param s_meet: meet point of bi-direction best first
        :return: path
        """

        # extract path for foreward part
        path_fore = [s_meet]
        s = s_meet

        while True:
            s = self.PARENT_fore[s]
            path_fore.append(s)
            if s == self.s_start:
                break

        # extract path for backward part
        path_back = []
        s = s_meet

        while True:
            s = self.PARENT_back[s]
            path_back.append(s)
            if s == self.s_goal:
                break

        return list(reversed(path_fore)) + list(path_back)

    def f_value_fore(self, s):
        """
        forward searching: f = h. (g: Cost to come, h: heuristic value)
        :param s: current state
        :return: f
        """

        return self.h(s, self.s_goal)

    def f_value_back(self, s):
        """
        backward searching: f = h. (g: Cost to come, h: heuristic value)
        :param s: current state
        :return: f
        """

        return self.h(s, self.s_start)

    def h(self, s, goal):
        """
        Calculate heuristic value.
        :param s: current node (state)
        :param goal: goal node (state)
        :return: heuristic value
        """

        heuristic_type = self.heuristic_type

        if heuristic_type == "manhattan":
            return abs(goal[0] - s[0]) + abs(goal[1] - s[1])
        else:
            return math.hypot(goal[0] - s[0], goal[1] - s[1])

    def cost(self, s_start, s_goal):
        """
        Calculate Cost for this motion
        :param s_start: starting node
        :param s_goal: end node
        :return:  Cost for this motion
        :note: Cost function could be more complicate!
        """

        if self.is_collision(s_start, s_goal):
            return math.inf

        return math.hypot(s_goal[0] - s_start[0], s_goal[1] - s_start[1])

    def is_collision(self, s_start, s_end):
        """
        check if the line segment (s_start, s_end) is collision.
        :param s_start: start node
        :param s_end: end node
        :return: True: is collision / False: not collision
        """

        if s_start in self.obs or s_end in self.obs:
            return True

        if s_start[0] != s_end[0] and s_start[1] != s_end[1]:
            if s_end[0] - s_start[0] == s_start[1] - s_end[1]:
                s1 = (min(s_start[0], s_end[0]), min(s_start[1], s_end[1]))
                s2 = (max(s_start[0], s_end[0]), max(s_start[1], s_end[1]))
            else:
                s1 = (min(s_start[0], s_end[0]), max(s_start[1], s_end[1]))
                s2 = (max(s_start[0], s_end[0]), min(s_start[1], s_end[1]))

            if s1 in self.obs or s2 in self.obs:
                return True

        return False


def main():
    x_start = (5, 5)
    x_goal = (45, 25)

    bastar = BidirectionalBestFirst(x_start, x_goal, "euclidean",x_start,x_goal)
    
    path, visited_fore, visited_back = bastar.searching()
    bastar.animation_bi_astar(path, visited_fore, visited_back, "Bidirectional-Best-First")  # animation


if __name__ == '__main__':
    main()

简单思路解析:

首先将起点跟终点都放入到一个堆栈内,然后分别对两个栈进行出栈操作,计算周围点位的代价值。对于起点,其周围点的代价值计算公式为:

        return self.h(s, self.s_goal)
        
    def h(self, s, goal):
        heuristic_type = self.heuristic_type
        if heuristic_type == "manhattan":
            return abs(goal[0] - s[0]) + abs(goal[1] - s[1])
        else:
            return math.hypot(goal[0] - s[0], goal[1] - s[1])

即当前点到终点的代价值。

对于终点,其周围点的代价值计算公式为:

        return self.h(s, self.s_start)
        
    def h(self, s, goal):
        heuristic_type = self.heuristic_type
        if heuristic_type == "manhattan":
            return abs(goal[0] - s[0]) + abs(goal[1] - s[1])
        else:
            return math.hypot(goal[0] - s[0], goal[1] - s[1])

然后循环进行出栈入栈操作,直到满足以下条件之一:

            if s_fore in self.PARENT_back:
                s_meet = s_fore
                break
            if s_back in self.PARENT_fore:
                s_meet = s_back
                break

即前向搜索遇到的点在后向搜索中出现过或者后向搜索中的点在前向搜索中出现过。说明前后搜索到同一个位置了,则代表找到了路径。这里self.PARENT_back与self.PARENT_fore分别维护了一个字典,方便查找。

效果:

在这里插入图片描述

与最佳路径优先搜索算法的对比

双向最佳路径优先搜索算法最大的优点就在于它极大程度的降低了搜索的深度,可以很快的完成迭代。前一章我们讲述了最佳路径优先搜索算法,为了查看它的迭代次数,在while里面设置一个打印输出count,每迭代一次就自加一。到结束时可以看到数据已经自加到了947:
在这里插入图片描述
也就是说最佳路径优先搜索算法在寻找起点到终点的过程中一共迭代了947次,而同样的我们使用双向最佳路径优先搜索算法后,同样查看它的迭代次数:
在这里插入图片描述
可以看到双向最佳路径优先搜索算法只迭代了192次。即便它每一次会操作两个字典,将操作次数翻倍,也只有384次操作。比最佳路径优先搜索算法还是要快很多。这也说明了双向最佳路径优先搜索算法在搜索深度上确实要比单向算法更浅,速度也比单向算法更快捷。

参考:

1、搜索优化技巧:双向搜索

2、双向BFS

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一叶执念

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值