def test_beam2_graph_search(): """ like test_beam2_tree_search, but eliminates duplicate nodes """ for goal in range(1, 10): p = AnnotatedProblem(EasyProblem(0, goal)) sol = next(beam_search(p, beam_width=2, graph=True)) assert sol.state_node.state == goal assert p.nodes_expanded == goal * 2
def test_beam1_tree_search(): """ Beam search with a width of 1 is like a depth first search, but with no backtracking. """ for goal in range(1,10): p = AnnotatedProblem(EasyProblem(0, extra=goal)) sol = next(beam_search(p, beam_width=1, graph_search=False)) assert sol.state == goal assert p.nodes_expanded == goal*2 assert p.goal_tests == goal+1
def test_beam2_graph_search(): """ like test_beam2_tree_search, but eliminates duplicate nodes """ for goal in range(1,10): p = AnnotatedProblem(EasyProblem(0, extra=goal)) sol = next(beam_search(p, beam_width=2, graph_search=True)) assert sol.state == goal assert p.nodes_expanded == goal*2 assert p.goal_tests == goal+1
def test_beam2_tree_search(): """ Beam search with a width of 2 is slightly different. It is like a fusion of breadth and depth first searches. """ for goal in range(1,10): p = AnnotatedProblem(EasyProblem(0, extra=goal)) sol = next(beam_search(p, beam_width=2, graph_search=False)) assert sol.state == goal assert p.nodes_expanded == 2 + (goal-1)*4 assert p.goal_tests == 2 + (goal-1)*2
def test_beam1_tree_search(): """ Beam search with a width of 1 is like a depth first search, but with no backtracking. """ for goal in range(1, 10): p = AnnotatedProblem(EasyProblem(0, goal)) sol = next(beam_search(p, beam_width=1, graph=False)) assert sol.state_node.state == goal assert p.nodes_expanded == goal * 2 assert p.goal_tests == goal + 1
def test_beam2_tree_search(): """ Beam search with a width of 2 is slightly different. It is like a fusion of breadth and depth first searches. """ for goal in range(1, 10): p = AnnotatedProblem(EasyProblem(0, goal)) sol = next(beam_search(p, beam_width=2, graph=False)) assert sol.state_node.state == goal assert p.nodes_expanded == 2 + (goal - 1) * 4 assert p.goal_tests == 2 + (goal - 1) * 2
def beam_width2(problem): return beam_search(problem, beam_width=2)