Пример #1
0
    def test_simulated_annealing_landscape(self):
        height = 8
        landscape = [2, 3, 4, 3, 2, 3, 5, 6, 5, 4, 5,
                     4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4]
        state = 0
        def actions(state):
            actions = []
            if state > 0: actions.append(-1)
            if state < len(landscape) - 1: actions.append(1)
            return actions

        result = lambda state, action: state + action
        step_cost = lambda state, action: 1

        heuristic = lambda state: (height - landscape[state] - 1) / (height - 1)
        goal_state = lambda state: heuristic(state) == 0

        factory = problem.ProblemFactory()
        pr = factory.from_functions(state, actions, step_cost,
                                    result, goal_state)

        solution = search.simulated_annealing(pr, heuristic,
                                              local_minima_acceptable=True)
        self.assertNotEqual(solution, problem.FAILURE)
        final_state = solution[-1][0]
        # Assert that SA has found at least the second best local minima
        self.assertLessEqual(heuristic(final_state), 1)
Пример #2
0
    def test_simulated_annealing_landscape(self):
        height = 8
        landscape = [
            2, 3, 4, 3, 2, 3, 5, 6, 5, 4, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4
        ]
        state = 0

        def actions(state):
            actions = []
            if state > 0: actions.append(-1)
            if state < len(landscape) - 1: actions.append(1)
            return actions

        result = lambda state, action: state + action
        step_cost = lambda state, action: 1

        heuristic = lambda state: (height - landscape[state] - 1) / (height - 1
                                                                     )
        goal_state = lambda state: heuristic(state) == 0

        factory = problem.ProblemFactory()
        pr = factory.from_functions(state, actions, step_cost, result,
                                    goal_state)

        solution = search.simulated_annealing(pr,
                                              heuristic,
                                              local_minima_acceptable=True)
        self.assertNotEqual(solution, problem.FAILURE)
        final_state = solution[-1][0]
        # Assert that SA has found at least the second best local minima
        self.assertLessEqual(heuristic(final_state), 1)
Пример #3
0
    def test_simulated_annealing_success_ratio(self):
        factory = problem.ProblemFactory()
        queens8_gen = functools.partial(factory.from_nqueens, 8)
        heuristic = factory.heuristic_for(queens8_gen())
        problem_count = 10
        expected_solutions = 0.7 * problem_count
        found_solutions = 0
        for _ in range(problem_count):
            solution = search.simulated_annealing(queens8_gen(), heuristic)
            found_solutions += 1 if solution != problem.FAILURE else 0

        self.assertGreaterEqual(found_solutions, expected_solutions)
Пример #4
0
    def test_simulated_annealing_success_ratio(self):
        factory = problem.ProblemFactory()
        queens8_gen = functools.partial(factory.from_nqueens, 8)
        heuristic = factory.heuristic_for(queens8_gen())
        problem_count = 10
        expected_solutions = 0.7 * problem_count
        found_solutions = 0
        for _ in range(problem_count):
            solution = search.simulated_annealing(queens8_gen(), heuristic)
            found_solutions += 1 if solution != problem.FAILURE else 0

        self.assertGreaterEqual(found_solutions, expected_solutions)
Пример #5
0
def main():
    height = 8
    landscape = [2, 3, 4, 3, 2, 3, 5, 6, 5, 4, 5,
                 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4]
    width = len(landscape)
    state = 0

    def actions(state):
        actions = []
        if state > 0:
            actions.append(-1)
        if state < len(landscape) - 1:
            actions.append(1)
        return actions

    def print_landscape(state):
        for row_index in range(height):
            row = [' ' if landscape[i] > row_index else
                   ('|' if landscape[i] < row_index
                    else ('Y' if i == state else 'T'))
                   for i in range(width)]
            print("".join(row))

        sleep(0.4)

    result = lambda state, action: state + action
    step_cost = lambda state, action: 1

    heuristic = lambda state: (height - landscape[state] - 1) / (height - 1)
    goal_state = lambda state: heuristic(state) == 0

    factory = problem.ProblemFactory()
    pr = factory.from_functions(state, actions, step_cost, result, goal_state)

    solution = search.simulated_annealing(pr, heuristic,
                                          local_minima_acceptable=True,
                                          min_temperature=0.1,
                                          print_state=print_landscape)
    print("Done")