def heuristic_search(root_node: Node, heuristic: Callable[[Node], int]) -> (Node, Monitor): X = set() F = [] heapq.heapify(F) heapq.heappush(F, root_node) monitor = Monitor(X, F) monitor.start() while True: if not F: monitor.finish() return False, monitor # with MonitorPerformance(): v = heapq.heappop(F) if v.state == parameters.objective_state: monitor.finish() return v, monitor elif v.state not in X: #rever isso, o estado pode estar aqui já, mas ter chego por outro caminho X.add(v.state) monitor.count() for node in expand_node(v): node.set_heuristic_cost(heuristic(node)) heapq.heappush(F, node)
def breadth_first_search(root_node: Node) -> (Node, Monitor): X = set() # <-- Explorados F: Deque[Node] = deque() # <-- Fronteira - deque eh uma implementacao para fila F.append(root_node) monitor = Monitor(X, F) monitor.start() while True: if not F: # <-- Se a fronteira fica vazia, nao existe caminho monitor.finish() return False, monitor v = F.popleft() # <-- Retira um nodo v da fronteira if v.state == parameters.objective_state: # <-- Chegou no estado final? monitor.finish() return v, monitor # <-- Entao retorna o ultimo nó elif v.state not in X: # <-- Adiciona v aos explorados caso ainda nao esteja X.add(v.state) monitor.count() for node in expand_node(v): # <-- Adiciona os vizinhos de V a fronteira F.append(node)