def test_iterative_deepening_graph_search(): """ Test iterative deepening graph search. """ for goal in range(1, 10): p = AnnotatedProblem(EasyProblem(0, goal)) sol = next(iterative_deepening_search(p, graph=True)) assert sol.state_node.state == goal assert p.nodes_expanded == sum([i * 2 for i in range(1, goal + 1)]) assert p.goal_tests == sum([i + 1 for i in range(1, goal + 1)]) + 1 p = EasyProblem(0, 10) try: next(iterative_deepening_search(p, graph=True, max_depth_limit=5)) assert False except StopIteration: pass
def test_iterative_deepening_graph_search(): """ Test iterative deepening graph search. """ for goal in range(1, 10): p = AnnotatedProblem(EasyProblem(0, goal)) sol = next(iterative_deepening_search(p, graph=True)) assert sol.state_node.state == goal assert p.nodes_expanded == sum([i * 2 for i in range(1, goal + 1)]) assert p.goal_tests == sum([i + 1 for i in range(1, goal + 1)]) + 1
def test_iterative_deepening_tree_search(): """ Test iterative deepening tree search. """ for goal in range(1, 10): p = AnnotatedProblem(EasyProblem(0, goal)) sol = next(iterative_deepening_search(p, graph=False)) assert sol.state_node.state == goal assert (p.nodes_expanded == sum( [pow(2, i + 1) - 2 for i in range(1, goal)]) + (goal * 2)) assert ( p.goal_tests == sum([pow(2, i + 1) - 2 for i in range(1, goal)]) + (goal * 2) + 1) p = EasyProblem(0, 10) try: next(iterative_deepening_search(p, graph=False, max_depth_limit=5)) assert False except StopIteration: pass
def fc_plan(self, goals, max_depth=5, epsilon=0.0): """ A strategy of expanding forward from the initial state. """ index = build_index(self.facts) fc_problem = FC_Problem(initial=(frozenset(self.facts), frozenset(goals)), extra=(self.operators, index, epsilon)) for solution in iterative_deepening_search(fc_problem, max_depth_limit=max_depth + 1): yield solution
def iterative_deepening(self, x): return iterative_deepening_search(x)