def getPrioritySucc(self, state): items = state.getSuccessors().items() if not self.use_extentions[0] or self.time_manager.time_left < 0.3: return items factor = state.getCurrentPlayer() == self.player and -1 or 1 pq = PriorityQueue(lambda x : factor * self.utility(x[1])) for item in items: pq.append(item) if self.time_manager.bTimeOver: return items return pq
def best_first_graph_search(problem, f): """Search the nodes with the lowest f scores first. You specify the function f(node) that you want to minimize; for example, if f is a heuristic estimate to the goal, then we have greedy best first search; if f is node.depth then we have breadth-first search. There is a subtlety: the line "f = memoize(f, 'f')" means that the f values will be cached on the nodes as they are computed. So after doing a best first search you can examine the f values of the path returned.""" f = memoize(f, 'f') node = Node(problem.initial) if problem.goal_test(node.state): return node frontier = PriorityQueue(min, f) frontier.append(node) explored = set() while frontier: node = frontier.pop() if problem.goal_test(node.state): return node explored.add(node.state) for child in node.expand(problem): if child.state not in explored and child not in frontier: frontier.append(child) elif child in frontier: incumbent = frontier[child] if f(child) < f(incumbent): del frontier[incumbent] frontier.append(child) return None
def best_first_graph_search(problem, f): """Search the nodes with the lowest f scores first. You specify the function f(node) that you want to minimize; for example, if f is a heuristic estimate to the goal, then we have greedy best first search; if f is node.depth then we have breadth-first search. There is a subtlety: the line "f = memoize(f, 'f')" means that the f values will be cached on the nodes as they are computed. So after doing a best first search you can examine the f values of the path returned.""" f = memoize(f, 'f') node = Node(problem.initial) if problem.goal_test(node.state): return node frontier = PriorityQueue(min, f) frontier.append(node) explored = set() while frontier: node = frontier.pop() if problem.goal_test(node.state): return node explored.add(node.state) for child in node.expand(problem): if child.state not in explored and child not in frontier: frontier.append(child) elif child in frontier: incumbent = frontier[child] if f(child) < f(incumbent): del frontier[incumbent] frontier.append(child) return None
def best_first_graph_search(problem, f): """Пребарувај низ следбениците на даден проблем за да најдеш цел. Користи функција за евалуација за да се одлучи кој е сосед најмногу ветува и потоа да се истражи. Ако до дадена состојба стигнат два пата, употреби го најдобриот пат. :param problem: даден проблем :param f: дадена функција за евристика :return: Node or None """ f = memoize(f, 'f') node = Node(problem.initial) if problem.goal_test(node.state): return node frontier = PriorityQueue(min, f) frontier.append(node) explored = set() while frontier: node = frontier.pop() if problem.goal_test(node.state): return node explored.add(node.state) for child in node.expand(problem): if child.state not in explored and child not in frontier: frontier.append(child) elif child in frontier: incumbent = frontier[child] if f(child) < f(incumbent): del frontier[incumbent] frontier.append(child) return None
def best_first_graph_search(problem, f, display=False): """Search the nodes with the lowest f scores first. You specify the function f(node) that you want to minimize; for example, if f is a heuristic estimate to the goal, then we have greedy best first search; if f is node.depth then we have breadth-first search. There is a subtlety: the line "f = memoize(f, 'f')" means that the f values will be cached on the nodes as they are computed. So after doing a best first search you can examine the f values of the path returned.""" f = memoize(f, 'f') node = Node(problem.initial_state) frontier = PriorityQueue('min', f) frontier.append(node) explored = set() while frontier: print(frontier.heap) print("***************") print(explored) print("\n") print("--------------") node = frontier.pop() if problem.is_goal_state(node.state): if display: print(len(explored), "paths have been expanded and", len(frontier), "paths remain in the frontier") return node explored.add(node.state) for child in node.expand(problem): if child.state not in explored and child not in frontier: frontier.append(child) elif child in frontier: if f(child) < frontier[child]: del frontier[child] frontier.append(child) return None
def depth_limited_best_first_graph_search(problem, f, depth_limit): f = memoize(f, 'f') node = Node(problem.initial) total_nodes = 0 if problem.goal_test(node.state): return node, total_nodes frontier = PriorityQueue(min, f) frontier.append(node) explored = set() while frontier: node = frontier.pop() total_nodes += 1 if problem.goal_test(node.state): return node, total_nodes explored.add(node.state) if node.depth < depth_limit: for child in node.expand(problem): if child.state not in explored and child not in frontier: frontier.append(child) elif child in frontier: incumbent = frontier[child] if f(child) < f(incumbent): del frontier[incumbent] frontier.append(child) return None, total_nodes
def bidirectional_best_first_graph_search(problem, h=None, h_reverse=None): h = memoize(h or problem.h, 'h') h_reverse = memoize(h_reverse or problem.h_reverse, 'h_reverse') node_forward = Node(problem.initial) node_backward = Node(problem.goal) frontier_forward = PriorityQueue('min', h) frontier_backward = PriorityQueue('min', h_reverse) frontier_forward.append(node_forward) frontier_backward.append(node_backward) explored = set() while frontier_forward and frontier_backward: node_forward = frontier_forward.pop() # print(node_forward.state) if problem.goal_test_forward(node_forward.state): print('[f]meet point:') print(node_forward.state) while True: node_backward = frontier_backward.pop() if node_backward.state == node_forward.state: break return [node_forward, node_backward] explored.add(node_forward.state) for child in node_forward.expand(problem): if child.state not in explored and child not in frontier_forward: frontier_forward.append(child) problem.backward_goal.append(child.state) problem.backward_goal.remove(node_forward.state) node_backward = frontier_backward.pop() # print(node_backward.state) if problem.goal_test_backward(node_backward.state): print('[b]meet point:') print(node_backward.state) while True: node_forward = frontier_forward.pop() if node_backward.state == node_forward.state: break return [node_forward, node_backward] explored.add(node_backward.state) # problem.backward_goal = [problem.initial] for child in node_backward.expand(problem): if child.state not in explored and child not in frontier_backward: frontier_backward.append(child) problem.forward_goal.append(child.state) problem.forward_goal.remove(node_backward.state) return None
def best_first_graph_search(problem, f): """Search the nodes with the lowest f scores first. You specify the function f(node) that you want to minimize; for example, if f is a heuristic estimate to the goal, then we have greedy best first search; if f is node.depth then we have breadth-first search. There is a subtlety: the line "f = memoize(f, 'f')" means that the f values will be cached on the nodes as they are computed. So after doing a best first search you can examine the f values of the path returned.""" global frontier, node, explored, counter if counter == -1: f = memoize(f, 'f') node = Node(problem.initial) display_current(node) if problem.goal_test(node.state): return node frontier = PriorityQueue('min', f) frontier.append(node) display_frontier(frontier) explored = set() add_node(node) draw_tree() if counter % 3 == 0 and counter >= 0: node = frontier.pop() display_current(node) if problem.goal_test(node.state): return node explored.add(node.state) mark_exploring(node) draw_tree() if counter % 3 == 1 and counter >= 0: for child in node.expand(problem): if child.state not in explored and child not in frontier: frontier.append(child) add_node(child) elif child in frontier: if f(child) < frontier[child]: del frontier[child] frontier.append(child) remove_node(child) display_frontier(frontier) draw_tree() if counter % 3 == 2 and counter >= 0: display_explored(node) mark_explored(node) draw_tree() return None
def best_first_graph_search(problem, f): f = memoize(f, 'f') node = Node(problem.initial) frontier = PriorityQueue('min', f) frontier.append(node) explored = list() itr = 1 print("Initial Node: " + number_to_city_map[node.state]) while frontier: print("Iteration#" + str(itr)) dist, current_city = frontier.heap[0] print("Current Node: " + number_to_city_map[current_city.state]) itr = itr + 1 node = frontier.pop() if problem.goal_test(node.state): print("Found the goal node") print(trace_path(node)) return node print("Evaluation function(" + number_to_city_map[current_city.state] + ")" + "=" + str(dist)) explored.append(node.state) print("Explored:") explrd = list() for e in explored: explrd.append(number_to_city_map[e]) print(explrd) for child in node.expand(problem): if child.state not in explored and child not in frontier: frontier.append(child) elif child in frontier: if f(child) < frontier[child]: del frontier[child] frontier.append(child) frnt = dict() print("Frontier:") for e in frontier.heap: dist, city = e frnt[city] = dist print(sorted(frnt.items(), key=lambda x: x[1])) return None
def best_first_graph_search(problem, f): f = memoize(f, 'f') node = Node(problem.initial) frontier = PriorityQueue('min', f) frontier.append(node) explored = set() while frontier: node = frontier.pop() if problem.goal_test(node.state): return node explored.add(node.state) for child in node.expand(problem): if child.state not in explored and child not in frontier: frontier.append(child) elif child in frontier: if f(child) < frontier[child]: del frontier[child] frontier.append(child) return None