예제 #1
0
    def RunAndSaveImage(self, ax, plt):
        start_time = time.time()

        start_point = random_map.Point(0, 0)
        self.open_set.append(start_point)

        while True:
            p = self.SelectPointInOpenList()
            if not p:
                print('No path found, algorithm failed!!!')
                return

            if (not (p.x == 0 and p.y == 0)) and (not (
                    p.x == self.map.size - 1 and p.y == self.map.size - 1)):
                rec = Rectangle((p.x, p.y), 1, 1, color='c')
                ax.add_patch(rec)

                # plt.draw()
                plt.pause(0.05)

            if self.IsEndPoint(p):
                return self.BuildPath(p, ax, plt, start_time, baseName="DFS")

            self.close_set.append(p)

            # Process all neighbors
            x = p.x
            y = p.y
            # 启发函数为曼哈顿距离,遍历下左上右4个点
            # 因为终点在右上方,所以优先向上和右遍历
            self.ProcessPoint(x, y - 1, p)
            self.ProcessPoint(x - 1, y, p)
            self.ProcessPoint(x + 1, y, p)
            self.ProcessPoint(x, y + 1, p)
예제 #2
0
    def ProcessPoint(self, x, y, parent):
        # 不合法的点
        if not self.IsValidPoint(x, y):
            return  # Do nothing for invalid point
        p = random_map.Point(x, y)

        # 邻点在close_set中,跳过
        if self.IsInCloseList(p):
            return  # Do nothing for visited point

        # 邻点p在open_set,比较g(n)是否比原来更小,如果更小则更新其g(n)、优先级f(n)和其父节点
        # 这里可以删除open_set中的对应点,修改后重新加入;也可以调用函数返回这个点然后对其修改
        open_p = self.IsInOpenList(p)
        if open_p:
            p = open_p
            # 比较该点前一次遍历的点和该点的父节点的g_cost,前一个遍历点小则更新父节点为前一个遍历点
            if parent.g_cost < p.parent.g_cost:
                p.parent = parent
                p.g_cost = self.BaseCost(p)
                # p.g_cost = parent.g_cost + 1
                p.f_cost = self.TotalCost(p)

        # 邻点p既不在open_set,也不在close_set中,设置节点p的parent为节点n,计算节点p的优先级f(n),将节点m加入open_set中
        else:
            p.parent = parent
            p.g_cost = self.BaseCost(p)
            # p.g_cost = parent.g_cost + 1
            p.f_cost = self.TotalCost(p)
            self.open_set.append(p)

        print('Process Point [', p.x, ',', p.y, ']', ', f_cost: ', p.f_cost)
예제 #3
0
    def ProcessPoint(self, x, y, parent):
        # 不合法的点,不做处理
        if not self.IsValidPoint(x, y):
            return
        p = random_map.Point(x, y)

        # 邻点在close_set和open_set中,跳过
        if self.IsInCloseList(p) or self.IsInOpenList(p):
            return  # Do nothing for visited point

        else:
            p.parent = parent
            self.open_set.append(p)
        print('Process Point [', p.x, ',', p.y, ']')
예제 #4
0
    def ProcessPoint(self, x, y, parent):
        # 不合法的点,不做处理
        if not self.IsValidPoint(x, y):
            return
        p = random_map.Point(x, y)

        # 邻点在close_set,跳过
        if self.IsInCloseList(p):
            return  # Do nothing for visited point

        open_p = self.IsInOpenList(p)
        if open_p:
            p = open_p
            if p.parent and parent.g_cost < p.parent.g_cost:
                p.parent = parent
                # p.g_cost = parent.g_cost + 1
                p.g_cost = self.BaseCost(p)

        else:
            p.parent = parent
            p.g_cost = self.BaseCost(p)
            self.open_set.append(p)
        print('Process Point [', p.x, ',', p.y, ']')
예제 #5
0
    def RunAndSaveImage(self, ax, plt):
        start_time = time.time()
        start_point = random_map.Point(0, 0)
        start_point.g_cost = 0
        start_point.f_cost = 0
        self.open_set.append(start_point)

        while True:
            index = self.SelectPointInOpenList()
            if index < 0:
                print('No path found, algorithm failed!!!')
                return
            p = self.open_set[index]

            if (not (p.x == 0 and p.y == 0)) and (not (
                    p.x == self.map.size - 1 and p.y == self.map.size - 1)):
                rec = Rectangle((p.x, p.y), 1, 1, color='c')
                ax.add_patch(rec)

                # plt.draw()
                plt.pause(0.05)
            # self.SaveImage(plt)

            if self.IsEndPoint(p):
                return self.BuildPath(p, ax, plt, start_time, baseName="AStar")

            del self.open_set[index]
            self.close_set.append(p)

            # Process all neighbors
            x = p.x
            y = p.y
            # 启发函数为曼哈顿距离,可以遍历上右下左4个点
            self.ProcessPoint(x + 1, y, p)
            self.ProcessPoint(x, y + 1, p)
            self.ProcessPoint(x - 1, y, p)
            self.ProcessPoint(x, y - 1, p)