示例#1
0
    def start_search(self, limit):
        assert limit>0

        position_list = [Node(self.start, [], 1)]
        explored = 0
        isFound = False

        while len(position_list) > 0:
            current = position_list.pop()
            x, y = current.point

            if current.point == self.end:
                current.path.append(current.point)
                self.maze[y][x] = 3
                explored += 1
                isFound = True
                break

            path = current.path[:]
            path.append(current.point)

            # up
            if self.isValid((x, y - 1)) and current.depth <= limit:
                position_list.append(Node((x, y - 1), path, current.cost + 1, depth=current.depth + 1))
                self.maze[y - 1][x] = 2
            # down
            if self.isValid((x, y + 1)) and current.depth <= limit:
                position_list.append(Node((x, y + 1), path, current.cost + 1, depth=current.depth + 1))
                self.maze[y + 1][x] = 2
            # left
            if self.isValid((x - 1, y)) and current.depth <= limit:
                position_list.append(Node((x - 1, y), path, current.cost + 1, depth=current.depth + 1))
                self.maze[y][x - 1] = 2
            # right
            if self.isValid((x + 1, y)) and current.depth <= limit:
                position_list.append(Node((x + 1, y), path, cost=current.cost + 1, depth=current.depth + 1))
                self.maze[y][x + 1] = 2

            self.maze[y][x] = 3
            explored += 1

        return current, explored, isFound
示例#2
0
    def start_search(self):
        position_list = [Node(self.start, [], 1)]
        explored = 0

        while len(position_list) > 0:
            current = position_list.pop()
            x, y = current.point

            if current.point == self.end:
                current.path.append(current.point)
                self.maze[y][x] = 3
                explored += 1
                break

            path = current.path[:]
            path.append(current.point)

            # up
            if self.isValid((x, y - 1)):
                position_list.append(Node((x, y - 1), path, current.cost + 1))
                self.maze[y - 1][x] = 2
            # down
            if self.isValid((x, y + 1)):
                position_list.append(Node((x, y + 1), path, current.cost + 1))
                self.maze[y + 1][x] = 2
            # left
            if self.isValid((x - 1, y)):
                position_list.append(Node((x - 1, y), path, current.cost + 1))
                self.maze[y][x - 1] = 2
            # right
            if self.isValid((x + 1, y)):
                position_list.append(Node((x + 1, y), path, cost=current.cost + 1))
                self.maze[y][x + 1] = 2

            self.maze[y][x] = 3
            explored += 1

        return current, explored
示例#3
0
    def start_search(self):
        position_list = [Node(self.start, [], 1)]
        explored = 0

        while len(position_list) > 0:
            current = position_list.pop(0)
            x, y = current.point

            if current.point == self.end:
                current.path.append(current.point)
                self.maze[y][x] = 3
                explored += 1
                break

            path = current.path[:]
            path.append(current.point)

            current_total_loss = current.cost + current.heuristic

            # up
            if self.isValid((x, y - 1)):
                move_cost = self.get_move_cost(current.point,
                                               (x, y - 1)) + current.cost
                heuristic_cost = self.get_heuristic_loss((x, y - 1), self.end)
                move_cost = max(current_total_loss,
                                move_cost + heuristic_cost) - heuristic_cost

                position_list.append(
                    Node((x, y - 1),
                         path,
                         cost=move_cost,
                         heuristic=heuristic_cost,
                         depth=current.depth + 1))
                self.maze[y - 1][x] = 2
            # down
            if self.isValid((x, y + 1)):
                move_cost = self.get_move_cost(current.point,
                                               (x, y + 1)) + current.cost
                heuristic_cost = self.get_heuristic_loss((x, y + 1), self.end)
                move_cost = max(current_total_loss,
                                move_cost + heuristic_cost) - heuristic_cost

                position_list.append(
                    Node((x, y + 1),
                         path,
                         cost=move_cost,
                         heuristic=heuristic_cost,
                         depth=current.depth + 1))
                self.maze[y + 1][x] = 2
            # left
            if self.isValid((x - 1, y)):
                move_cost = self.get_move_cost(current.point,
                                               (x - 1, y)) + current.cost
                heuristic_cost = self.get_heuristic_loss((x - 1, y), self.end)
                move_cost = max(current_total_loss,
                                move_cost + heuristic_cost) - heuristic_cost

                position_list.append(
                    Node((x - 1, y),
                         path,
                         cost=move_cost,
                         heuristic=heuristic_cost,
                         depth=current.depth + 1))
                self.maze[y][x - 1] = 2
            # right
            if self.isValid((x + 1, y)):
                move_cost = self.get_move_cost(current.point,
                                               (x + 1, y)) + current.cost
                heuristic_cost = self.get_heuristic_loss((x + 1, y), self.end)
                move_cost = max(current_total_loss,
                                move_cost + heuristic_cost) - heuristic_cost

                position_list.append(
                    Node((x + 1, y),
                         path,
                         cost=move_cost,
                         heuristic=heuristic_cost,
                         depth=current.depth + 1))
                self.maze[y][x + 1] = 2

            position_list = sorted(position_list,
                                   key=lambda x: x.cost + x.heuristic)

            self.maze[y][x] = 3
            explored += 1

        return current, explored
示例#4
0
    def depth_limited_search(self, limit):
        maze = copy.deepcopy(self.maze)
        position_list = [Node(self.start, [], 1)]
        explored = 0
        isFound = False
        end_search_list = []

        while len(position_list) > 0:
            current = position_list.pop()
            x, y = current.point

            if current.point == self.end:
                current.path.append(current.point)
                maze[y][x] = 3
                explored += 1
                isFound = True
                break

            path = current.path[:]
            path.append(current.point)

            # up
            if self.isValid((x, y - 1)) and current.depth <= limit:
                next_node = Node((x, y - 1), path, current.cost + 1, depth=current.depth + 1)
                position_list.append(next_node)
                maze[y - 1][x] = 2

                if current.depth+1>limit:
                    end_search_list.append(next_node)

            # down
            if self.isValid((x, y + 1)) and current.depth <= limit:
                next_node = Node((x, y + 1), path, current.cost + 1, depth=current.depth + 1)
                position_list.append(next_node)
                maze[y + 1][x] = 2

                if current.depth+1>limit:
                    end_search_list.append(next_node)

            # left
            if self.isValid((x - 1, y)) and current.depth <= limit:
                next_node = Node((x - 1, y), path, current.cost + 1, depth=current.depth + 1)
                position_list.append(next_node)
                maze[y][x - 1] = 2

                if current.depth+1>limit:
                    end_search_list.append(next_node)

            # right
            if self.isValid((x + 1, y)) and current.depth <= limit:
                next_node = Node((x + 1, y), path, cost=current.cost + 1, depth=current.depth + 1)
                position_list.append(next_node)
                maze[y][x + 1] = 2

                if current.depth+1>limit:
                    end_search_list.append(next_node)

            maze[y][x] = 3
            explored += 1

        return current, explored, isFound, end_search_list