Esempio n. 1
0
    def test_next_iteration(self):
        self.solver.iterate()

        self.assertEqual(3, self.solver.evaluated)

        best = self.solver.pop_best()
        expected_best = PantsPath(
            [PantsState(0, [1, 2, 3, 4, 5]),
             PantsState(2, [1, 2, 3, 4, 5])])

        self.assertEqual(expected_best, best)
Esempio n. 2
0
    def test_only_left_children_generated(self):
        initial_state = PantsState(4, [1, 2, 3, 4, 5])
        initial_path = PantsPath([initial_state])

        actual_child_paths = list(initial_path.children)

        expected_paths = [
            PantsPath([initial_state,
                       PantsState(2, [1, 2, 3, 4, 5])]),
            PantsPath([initial_state,
                       PantsState(3, [1, 2, 3, 5, 4])])
        ]

        self.assertEqual(expected_paths, actual_child_paths)
Esempio n. 3
0
    def test_skip_children_if_already_in_path(self):
        initial_states = [
            PantsState(0, [1, 2, 3, 4, 5]),
            PantsState(1, [2, 1, 3, 4, 5]),
        ]
        initial_path = PantsPath(initial_states)

        actual_child_paths = list(initial_path.children)

        expected_paths = [
            PantsPath(initial_states + [PantsState(3, [2, 1, 3, 4, 5])]),
            PantsPath(initial_states + [PantsState(2, [2, 3, 1, 4, 5])])
        ]

        self.assertEqual(expected_paths, actual_child_paths)
Esempio n. 4
0
    def __init__(self, initial, goal, heuristic, max_iterations=100000):
        """
        Initialization method for PantsSolver

        Args:
            initial (list[int]):  The initial state of the problem
            goal (list[int]):     The desired goal state of the problem
            heuristic (object):   An object that implements a 'score' method for evaluating PantsPaths
            max_iterations (int): The maximum number of iterations to evaluate when solving the problem
        """
        self.working_set = []
        self.goal = goal
        self.heuristic = heuristic
        self.evaluated = 0
        self.solution = None
        self.max_iterations = max_iterations

        # Seed the working set with a single PantsPath, starting at position 0
        self.add_path(PantsPath([PantsState(0, initial)]))
Esempio n. 5
0
    def test_intermediate_string_representation(self):
        expected_string = '[3 5 *2 1 4]'
        actual_string = str(PantsState(2, [3, 5, 2, 1, 4]))

        self.assertEqual(expected_string, actual_string)
Esempio n. 6
0
    def test_initial_string_representation(self):
        expected_string = '[*1 2 3 4 5]'
        actual_string = str(PantsState(0, [1, 2, 3, 4, 5]))

        self.assertEqual(expected_string, actual_string)
Esempio n. 7
0
    def test_invalid_swap_right(self):
        actual_state = PantsState(pointer=3, state=[1, 2, 3, 4]).swap_right()

        self.assertIsNone(actual_state)
Esempio n. 8
0
    def test_valid_move_left(self):
        actual_state = PantsState(pointer=2, state=[1, 2, 3]).move_pointer_left()
        expected_state = PantsState(pointer=0, state=[1, 2, 3])

        self.assertEqual(expected_state, actual_state)
Esempio n. 9
0
    def test_valid_swap_right(self):
        actual_state = PantsState(pointer=1, state=[1, 2, 3, 4]).swap_right()
        expected_state = PantsState(pointer=2, state=[1, 3, 2, 4])

        self.assertEqual(expected_state, actual_state)
Esempio n. 10
0
    def test_invalid_move_right_when_on_edge(self):
        actual_state = PantsState(pointer=3, state=[1, 2, 3, 4]).move_pointer_right()

        self.assertIsNone(actual_state)
Esempio n. 11
0
    def test_valid_move_right(self):
        actual_state = PantsState(pointer=1, state=[1, 2, 3, 4]).move_pointer_right()
        expected_state = PantsState(pointer=3, state=[1, 2, 3, 4])

        self.assertEqual(expected_state, actual_state)
Esempio n. 12
0
    def test_invalid_move_left_when_on_edge(self):
        actual_state = PantsState(pointer=0, state=[1, 2, 3, 4, 5]).move_pointer_left()

        self.assertIsNone(actual_state)
Esempio n. 13
0
    def test_invalid_move_left_when_one_away(self):
        actual_state = PantsState(pointer=1, state=[1, 2, 3]).move_pointer_left()

        self.assertIsNone(actual_state)
Esempio n. 14
0
    def test_pop_best(self):
        best = self.solver.pop_best()

        self.assertEqual(PantsPath([PantsState(0, [1, 2, 3, 4, 5])]), best)
        self.assertEqual([], self.solver.working_set)