Esempio n. 1
0
 def test_id_astar_move_all_tiles(self):
     start = BoardState((2, 1), {'A':(0,2), 'B':(1,2), 'C':(2,2)}, (3,3))
     goal = BoardState((0, 1), {'A':(1,2), 'B':(2,2), 'C':(2,1)}, (3,3))
     id_a_star = IterativeDeepeningAStarSearch(start, goal, ManhattonDistCostEvaluator())
     solution = id_a_star.search()
     print(solution.get_actions())
     self.assertTrue(solution is not None)
     self.assertTrue(solution.node.state == goal)
 def test_move_invalid(self):
     board_size = (3, 3)
     tiles_pos = {'A': (0, 0), 'B': (0, 0), 'C': (0, 0)}
     agent_pos = (0, 0)
     agent_pos_2 = (2, 2)
     board = BoardState(agent_pos, tiles_pos, board_size)
     board_2 = BoardState(agent_pos_2, tiles_pos, board_size)
     self.assertIsNone(board.move(Action.Up))
     self.assertIsNone(board.move(Action.Left))
     self.assertIsNone(board_2.move(Action.Down))
     self.assertIsNone(board_2.move(Action.Right))
Esempio n. 3
0
 def test_ids_move_all_tiles(self):
     start = BoardState((2, 1), {
         'A': (0, 2),
         'B': (1, 2),
         'C': (2, 2)
     }, (3, 3))
     goal = BoardState((0, 1), {
         'A': (1, 2),
         'B': (2, 2),
         'C': (2, 1)
     }, (3, 3))
     ids = IterativeDeepeningSearch(start, goal)
     solution = ids.search()
     print(solution.get_actions())
     self.assertTrue(solution is not None)
     self.assertTrue(solution.node.state == goal)
Esempio n. 4
0
 def test_bfs(self):
     start = BoardState((2, 1), {
         'A': (0, 2),
         'B': (1, 2),
         'C': (2, 2)
     }, (3, 3))
     goal = BoardState((0, 0), {
         'A': (0, 2),
         'B': (1, 2),
         'C': (2, 2)
     }, (3, 3))
     bfs = BreadthFirstSearch(start, goal)
     solution = bfs.search()
     print(solution.get_actions())
     self.assertTrue(solution is not None)
     self.assertTrue(solution.node.state == goal)
 def test_move_basic(self):
     board_size = (3, 3)
     agent_pos = (1, 1)
     tiles_pos = {'A': (0, 0), 'B': (0, 0), 'C': (0, 0)}
     board = BoardState(agent_pos, tiles_pos, board_size)
     a_pos = tiles_pos['A']
     b_pos = tiles_pos['B']
     c_pos = tiles_pos['C']
     new_board_up = board.move(Action.Up)
     new_board_down = board.move(Action.Down)
     new_board_left = board.move(Action.Left)
     new_board_right = board.move(Action.Right)
     self.assertEqual(new_board_up.agent_pos,
                      (agent_pos[0], agent_pos[1] - 1))
     self.assertEqual(new_board_up.get_tile_pos('A'), a_pos)
     self.assertEqual(new_board_up.get_tile_pos('B'), b_pos)
     self.assertEqual(new_board_up.get_tile_pos('C'), c_pos)
     self.assertEqual(new_board_down.agent_pos,
                      (agent_pos[0], agent_pos[1] + 1))
     self.assertEqual(new_board_down.get_tile_pos('A'), a_pos)
     self.assertEqual(new_board_down.get_tile_pos('B'), b_pos)
     self.assertEqual(new_board_down.get_tile_pos('C'), c_pos)
     self.assertEqual(new_board_left.agent_pos,
                      (agent_pos[0] - 1, agent_pos[1]))
     self.assertEqual(new_board_left.get_tile_pos('A'), a_pos)
     self.assertEqual(new_board_left.get_tile_pos('B'), b_pos)
     self.assertEqual(new_board_left.get_tile_pos('C'), c_pos)
     self.assertEqual(new_board_right.agent_pos,
                      (agent_pos[0] + 1, agent_pos[1]))
     self.assertEqual(new_board_right.get_tile_pos('A'), a_pos)
     self.assertEqual(new_board_right.get_tile_pos('B'), b_pos)
     self.assertEqual(new_board_right.get_tile_pos('C'), c_pos)
Esempio n. 6
0
 def test_astar_move_tile(self):
     start = BoardState((2, 1), {
         'A': (0, 2),
         'B': (1, 2),
         'C': (2, 2)
     }, (3, 3))
     goal = BoardState((2, 2), {
         'A': (0, 2),
         'B': (1, 1),
         'C': (1, 2)
     }, (3, 3))
     a_star = AStarSearch(start, goal, ManhattonDistCostEvaluator())
     solution = a_star.search()
     print(solution.get_actions())
     self.assertTrue(solution is not None)
     self.assertTrue(solution.node.state == goal)
     self.assertTrue(len(solution.get_actions()) >= 3)
 def test_constructor(self):
     board_size = (3, 4)
     agent_pos = (3, 3)
     tiles_pos = {'A': (0, 3), 'B': (1, 3), 'C': (2, 3)}
     board = BoardState(agent_pos, tiles_pos, board_size)
     self.assertEqual(board.get_tile_pos('A'), tiles_pos['A'])
     self.assertEqual(board.get_tile_pos('B'), tiles_pos['B'])
     self.assertEqual(board.get_tile_pos('C'), tiles_pos['C'])
 def test_goal_check(self):
     start = BoardState((2, 2), {
         'A': (0, 0),
         'B': (0, 2),
         'C': (2, 0)
     }, (3, 3))
     goal = BoardState((1, 1), {
         'A': (0, 0),
         'B': (0, 2),
         'C': (2, 0)
     }, (3, 3))
     node_1 = Node(goal, 0, 0, Action.Unknown)
     node_2 = Node(goal, 100, 2, Action.Right)
     node_3 = Node(goal, 80, 5, Action.Left)
     tree_search = AbstractTreeSearch(start, goal)
     self.assertTrue(tree_search.goal_test(node_1))
     self.assertTrue(tree_search.goal_test(node_2))
     self.assertTrue(tree_search.goal_test(node_3))
 def test_find_solution(self):
     board = BoardState((1, 1), {
         'A': (0, 0),
         'B': (2, 2),
         'C': (2, 1)
     }, (3, 3))
     node_1 = Node(board, 0, 0, Action.Unknown)
     node_2 = Node(board, 0, 0, Action.Right, node_1)
     node_3 = Node(board, 0, 0, Action.Down, node_2)
     node_4 = Node(board, 0, 0, Action.Down, node_3)
     solution = Solution(node_4)
     self.assertEqual(solution.get_actions(), ['Right', 'Down', 'Down'])
Esempio n. 10
0
 def test_total_cost(self):
     board = BoardState((1, 1), {
         'A': (0, 0),
         'B': (2, 2),
         'C': (2, 1)
     }, (3, 3))
     node_1 = Node(board, 10, 0, Action.Unknown)
     node_2 = Node(board, 15, 0, Action.Right, node_1)
     node_3 = Node(board, 30, 0, Action.Down, node_2)
     node_4 = Node(board, 50, 0, Action.Down, node_3)
     solution = Solution(node_4)
     self.assertEqual(solution.get_total_cost(), node_4.cost)
 def test_expand_set_parent_node(self):
     board = BoardState((1, 1), {
         'A': (0, 0),
         'B': (0, 2),
         'C': (2, 0)
     }, (3, 3))
     node = Node(board, 0, 0, Action.Unknown)
     tree_search = AbstractTreeSearch(board, board)
     successors = tree_search.expand(node)
     self.assertTrue(len(successors) == 4)
     for s in successors:
         self.assertTrue(s.parent == node)
 def test_expand_update_cost_and_depth(self):
     board = BoardState((1, 1), {
         'A': (0, 0),
         'B': (0, 2),
         'C': (2, 0)
     }, (3, 3))
     node = Node(board, 0, 0, Action.Unknown)
     tree_search = AbstractTreeSearch(board, board)
     c = tree_search.expand(node)[0]
     successors = tree_search.expand(c)
     for s in successors:
         self.assertTrue(s.cost == 2)
         self.assertTrue(s.depth == 2)
 def test_expand_generate_only_available_moves(self):
     board = BoardState((2, 1), {
         'A': (0, 0),
         'B': (0, 2),
         'C': (2, 2)
     }, (3, 3))
     node = Node(board, 0, 0, Action.Unknown)
     tree_search = AbstractTreeSearch(board, board)
     successors = tree_search.expand(node)
     self.assertTrue(len(successors) == 3)
     child_up = Node(board.move(Action.Up), 1, 1, Action.Up)
     child_down = Node(board.move(Action.Down), 1, 1, Action.Down)
     child_left = Node(board.move(Action.Left), 1, 1, Action.Left)
     self.assertTrue(child_up in successors)
     self.assertTrue(child_down in successors)
     self.assertTrue(child_left in successors)
 def test_move_to_tile(self):
     """
     - - A
     - B ☺
     - - C
     """
     board_size = (3, 3)
     tiles_pos = {'A': (2, 0), 'B': (1, 1), 'C': (2, 2)}
     agent_pos = (2, 1)
     a_pos = tiles_pos['A']
     b_pos = tiles_pos['B']
     c_pos = tiles_pos['C']
     board = BoardState(agent_pos, tiles_pos, board_size)
     new_board_1 = board.move(Action.Up)  # Move onto A
     new_board_2 = board.move(Action.Left)  # Move onto B
     new_board_3 = board.move(Action.Down)  # Move onto C
     self.assertEqual(new_board_1.agent_pos, a_pos)
     self.assertEqual(new_board_1.get_tile_pos('A'), agent_pos)
     self.assertEqual(new_board_2.agent_pos, b_pos)
     self.assertEqual(new_board_2.get_tile_pos('B'), agent_pos)
     self.assertEqual(new_board_3.agent_pos, c_pos)
     self.assertEqual(new_board_3.get_tile_pos('C'), agent_pos)
 def test_comparison(self):
     tiles_pos_1 = {'A': (0, 0), 'B': (2, 2), 'C': (2, 0)}
     tiles_pos_2 = {'A': (0, 1), 'B': (2, 2), 'C': (2, 0)}
     tiles_pos_3 = {'A': (0, 0), 'B': (2, 1), 'C': (2, 0)}
     tiles_pos_4 = {'A': (0, 0), 'B': (2, 2), 'C': (1, 0)}
     board_1 = BoardState((1, 1), tiles_pos_1, (3, 3))
     board_2 = BoardState((1, 1), tiles_pos_1, (3, 3))  # Identical
     board_3 = BoardState((1, 2), tiles_pos_1,
                          (3, 3))  # Agent position mismatch
     board_4 = BoardState((1, 1), tiles_pos_2,
                          (3, 3))  # A position mismatch
     board_5 = BoardState((1, 1), tiles_pos_3,
                          (3, 3))  # B position mismatch
     board_6 = BoardState((1, 1), tiles_pos_4,
                          (3, 3))  # C position mismatch
     board_7 = BoardState((1, 1), tiles_pos_1,
                          (4, 4))  # Different board size
     self.assertTrue(board_1 == board_2)
     self.assertFalse(board_1 == board_3)
     self.assertFalse(board_1 == board_4)
     self.assertFalse(board_1 == board_5)
     self.assertFalse(board_1 == board_6)
     self.assertFalse(board_1 == board_7)
 def test_manhattan_dist_agent(self):
     a = BoardState((2, 1), {'A': (0, 2), 'B': (1, 2), 'C': (2, 2)}, (3, 3))
     b = BoardState((0, 0), {'A': (0, 2), 'B': (1, 2), 'C': (2, 2)}, (3, 3))
     evaluator = ManhattonDistCostEvaluator()
     self.assertEqual(evaluator.estimate_cost_to_goal(a, b), 3)