当前位置: 首页>>代码示例>>Python>>正文


Python PriorityQueue.popleft方法代码示例

本文整理汇总了Python中queue.PriorityQueue.popleft方法的典型用法代码示例。如果您正苦于以下问题:Python PriorityQueue.popleft方法的具体用法?Python PriorityQueue.popleft怎么用?Python PriorityQueue.popleft使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在queue.PriorityQueue的用法示例。


在下文中一共展示了PriorityQueue.popleft方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: heap

# 需要导入模块: from queue import PriorityQueue [as 别名]
# 或者: from queue.PriorityQueue import popleft [as 别名]
class NPuzzleSolver:
    """
        Uses the BFS/DFS/A* algos to solve a given n-puzzle board
    """
    initial_state = None
    algo = ""

    # The frontier in BFS/DFS is a queue/stack and for A*, its a heap (PriorityQueue)
    # Frontier set made to test membership in O(1)
    frontier = None
    frontier_set = set()

    # All of the fully explored states in this set
    explored = set()

    def __init__(self, algo, initial_state=None):
        self.initial_state = initial_state
        if algo == "bfs" or algo == "dfs":
            self.frontier = dq()
            self.frontier.append(initial_state)
        else:
            self.frontier = PriorityQueue()
            self.frontier.put(initial_state)
        self.frontier_set.add(initial_state)
        self.algo = algo

    def solve(self):
        """
        Attempts to solve an n-puzzle and returns a stats
        dict, or None if no solution exists
        """
        start_time = time.time()
        maxdepth = 0
        break_cond = False

        while(not break_cond):
            if self.algo == "bfs":
                # Pop the leftmost element if doing a bfs (Queue)
                current_state = self.frontier.popleft()
            elif self.algo == "dfs":
                # Pop the rightmost element if doing a dfs (Stack)
                current_state = self.frontier.pop()
            else:
                # Get element with highest priority if doing A* (Heap)
                current_state = self.frontier.get()
            self.frontier_set.remove(current_state)
            if (self.isFinalState(current_state)):
                soln = self.get_solution_moves(current_state)
                end_time = time.time()
                stats = {}
                stats["nodes_expanded"] = len(self.explored)
                stats["search_depth"] = current_state.depth
                stats["max_search_depth"] = maxdepth
                stats["cost_of_path"] = len(soln)
                stats["time"] = end_time - start_time
                stats["path"] = soln
                return stats
            neighbors = current_state.generate_possible_states()
            if self.algo == "dfs":
                neighbors.reverse()

            for neighbor in neighbors:
                if neighbor not in self.explored and neighbor not in self.frontier_set:
                    if self.algo == "bfs" or self.algo == "dfs":
                        self.frontier.append(neighbor)
                    else:
                        self.frontier.put(neighbor)
                    self.frontier_set.add(neighbor)
                    if neighbor.depth > maxdepth:
                        maxdepth = neighbor.depth
            self.explored.add(current_state)
            if self.algo == "bfs" or self.algo == "dfs":
                frontier_sz = len(self.frontier)
            else:
                frontier_sz = self.frontier.qsize()
            logging.debug("Frontier size = " +
                          str(frontier_sz) +
                          "; Explored size = " +
                          str(len(self.explored)))
            if self.algo == "bfs" or self.algo == "dfs":
                break_cond = len(self.frontier) == 0
            else:
                break_cond = self.frontier.empty()
        logging.error("This is an unsolvable board!")
        return None

    def get_solution_moves(self, final_state):
        """
        Gets the sequence of moves from parent to this state
        """
        moves = dq()
        current_state = final_state
        while current_state is not None:
            if current_state.parent_move is not None:
                moves.appendleft(current_state.parent_move)
            current_state = current_state.parent
        res = []
        [res.append(move) for move in moves]
        return res

#.........这里部分代码省略.........
开发者ID:chandravadans,项目名称:artificial-intelligence,代码行数:103,代码来源:npuzzle.py


注:本文中的queue.PriorityQueue.popleft方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。