Exemplo n.º 1
0
    def step(self, verbose=False, tick=0):
        """
        Perform one step of the search.
        :param verbose: boolean, verbose mode
        :param tick: integer, number of steps elapsed
        :return: 2-tuple.
        * (None, None) for no solution,
        * (True, [Steps]) for solved,
        * (False, <state>) for unsolved, searching
        """
        if len(self.frontier) == 0:
            return None, None
        current_node = heappop(self.frontier)

        if verbose:
            print("Step {0}".format(tick))
            print(current_node.state)

        if current_node.state.is_solved():
            if verbose:
                print("Took {0} steps using A Star.".format(tick))
            return True, self.solution(current_node)

        self.explored.add(current_node.state.value())
        for move in current_node.state.valid_moves():
            new_state = current_node.state.copy()
            new_state.apply_move(move)
            child = Node(new_state, move, current_node, self.heuristic)
            in_frontier = self._update_frontier(child)
            if not (new_state.value() in self.explored or in_frontier):
                heappush(self.frontier, child)

        return False, current_node.state
Exemplo n.º 2
0
class TestNode(unittest.TestCase):
    
    def setUp(self):
        self.state = SlidingTilePuzzle(3)
        self.heuristic = "misplaced tiles"
        self.move = SlidingTilePuzzle.DIRECTIONS['up']
        self.node = Node(self.state, self.move, None, self.heuristic)        

    def test_init(self):
        self.assertTrue(self.state.equals(self.node.state))
        self.assertEqual(self.node.heuristic_name, self.heuristic)
        self.assertEqual(self.node.move, self.move)
        self.assertEqual(self.node.parent, None)
        self.assertEqual(self.node.level, 0)
        
    def test_set_heuristic(self):
        new_heuristic = 'manhattan distance'
        self.node.set_heuristic(new_heuristic)
        self.assertEqual(self.node.heuristic_name, new_heuristic)
        
    def test_eq_true(self):
        other_state = SlidingTilePuzzle(3)
        other_node = Node(other_state, self.move, None, self.heuristic)
        self.state.heuristic = dummy_return1
        other_state.heuristic = dummy_return1
        self.assertEqual(self.node, other_node)
        
    def test_eq_false(self):
        other_state = SlidingTilePuzzle(3)
        other_node = Node(other_state, self.move, self.node, self.heuristic)
        self.state.heuristic = dummy_return1
        other_state.heuristic = dummy_return1
        self.assertNotEqual(self.node, other_node)
        
    def test_lt_true(self):
        other_state = SlidingTilePuzzle(3)
        other_node = Node(other_state, self.move, None, self.heuristic)
        self.state.heuristic = dummy_return1
        other_state.heuristic = dummy_return2
        self.assertTrue(self.node < other_node)
        
    def test_lt_false(self):
        other_state = SlidingTilePuzzle(3)
        other_node = Node(other_state, self.move, None, self.heuristic)
        self.state.heuristic = dummy_return2
        other_state.heuristic = dummy_return1
        self.assertFalse(self.node < other_node)        
 def test_update_frontier_false(self):
     state = SlidingTilePuzzle(3, 1)
     time = 10
     heuristic = 'misplaced tiles'
     search = AStar(state, time, heuristic)
     state2 = SlidingTilePuzzle(3, 2)
     move = SlidingTilePuzzle.DIRECTIONS['up']
     node = Node(state2, move, search.rootnode, heuristic)
     self.assertFalse(search._update_frontier(node))
Exemplo n.º 4
0
 def test_solution(self):
     time = 10
     root_state = SlidingTilePuzzle(3, 1)
     heuristic = "misplaced tiles"
     move1 = SlidingTilePuzzle.DIRECTIONS['up']
     move2 = SlidingTilePuzzle.DIRECTIONS['right']
     move3 = SlidingTilePuzzle.DIRECTIONS['down']
     state1 = root_state.copy()
     state1.apply_move(move1)
     state2 = state1.copy()
     state2.apply_move(move2)
     state3 = state2.copy()
     state3.apply_move(move3)
     search = Search(root_state, time)
     node1 = Node(state1, move1, search.rootnode, heuristic)
     node2 = Node(state2, move2, node1, heuristic)
     node3 = Node(state3, move3, node2, heuristic)
     self.assertEqual(search.solution(node3), [move1, move2, move3])
 def test_update_frontier_true(self):
     state = SlidingTilePuzzle(3, 1)
     time = 10
     heuristic = 'misplaced tiles'
     search = AStar(state, time, heuristic)
     search.rootnode.level = 2
     state2 = SlidingTilePuzzle(3, 1)
     move = SlidingTilePuzzle.DIRECTIONS['up']
     node = Node(state2, move, None, heuristic)
     self.assertTrue(search._update_frontier(node))
     self.assertIn(node, search.frontier)
     self.assertNotIn(search.rootnode, search.frontier)
Exemplo n.º 6
0
    def step(self, verbose=False, tick=0):
        """
        Perform one step of the search.
        :param verbose: boolean, verbose mode
        :param tick: integer, number of steps elapsed
        :return: 2-tuple.
        * (None, None) for no solution,
        * (True, [Steps]) for solved,
        * (False, <state>) for unsolved, searching
        """
        if len(self.frontier) == 0:
            return None, None

        current_node = self.frontier.popleft()

        if current_node.state.is_solved():
            return True, []

        if verbose:
            print("Step {0}".format(tick))
            print(current_node.state)

        self.explored.add(current_node.state.value())
        for move in current_node.state.valid_moves():
            new_state = current_node.state.copy()
            new_state.apply_move(move)
            child = Node(new_state, move, current_node)
            if not new_state.value(
            ) in self.explored and not self._in_frontier(new_state):
                if child.state.is_solved():
                    if verbose:
                        print("Took {0} steps using Breadth First Search.".
                              format(tick))
                    return True, self.solution(child)
                self.frontier.append(child)

        return False, current_node.state
Exemplo n.º 7
0
 def test_lt_false(self):
     other_state = SlidingTilePuzzle(3)
     other_node = Node(other_state, self.move, None, self.heuristic)
     self.state.heuristic = dummy_return2
     other_state.heuristic = dummy_return1
     self.assertFalse(self.node < other_node)        
Exemplo n.º 8
0
 def test_eq_false(self):
     other_state = SlidingTilePuzzle(3)
     other_node = Node(other_state, self.move, self.node, self.heuristic)
     self.state.heuristic = dummy_return1
     other_state.heuristic = dummy_return1
     self.assertNotEqual(self.node, other_node)
Exemplo n.º 9
0
 def setUp(self):
     self.state = SlidingTilePuzzle(3)
     self.heuristic = "misplaced tiles"
     self.move = SlidingTilePuzzle.DIRECTIONS['up']
     self.node = Node(self.state, self.move, None, self.heuristic)