def A_STAR(problem, stat): fringe = lab2.Fringe() start_node = problem.start_node() fringe.add_by_priority(start_node, 0) closed_list = {} closed_list[start_node] = 0 while not fringe.is_empty(): stat.increment_node_que(len(fringe)) node = fringe.remove_front() stat.increment_node_depth(node.depth) stat.increment_node_count() if problem.is_goal(node): return node children = problem.expand(node) stat.increment_node_children_count(len(children)) for child in children: new_cost = closed_list[node] + lab2.h1(problem, node) new_cost2 = node.path_cost() + lab2.h1(problem, node) if new_cost < closed_list.get(child, float("inf")): closed_list[child] = new_cost priority = new_cost + lab2.h1(problem, child) fringe.add_by_priority(child, priority)
def GREEDY(problem, stat): fringe = lab2.Fringe() start_node = problem.start_node() fringe.add_by_priority(start_node, 0) visited = set() visited.add(start_node) while not fringe.is_empty(): stat.increment_node_que(len(fringe)) node = fringe.remove_front() stat.increment_node_depth(node.depth) stat.increment_node_count() if problem.is_goal(node): return node children = problem.expand(node) stat.increment_node_children_count(len(children)) for child in children: new_cost = node.path_cost() + lab2.h1(problem, node) if child.path_cost() < new_cost and child not in visited: fringe.add_by_priority(child, new_cost) visited.add(child)