def main(): images = {"eagle": EAGLE, "toucan": TOUCAN, "winnie": WINNIE} args = sys.argv[2:] art = images[args[0] if len(sys.argv) > 1 else "eagle"] lines = art.split("\n")[1:] rows = len(lines) columns = max(len(line) for line in lines) buffer = [] for line in lines: whitespaces = columns - len(line) + 1 buffer_line = line + " " * whitespaces buffer.append(list(buffer_line)) initial, goal = ("4 2 5 3 6 8 1 7 0", "0 1 2 3 4 5 6 7 8") factory = problem.ProblemFactory() problem_instance = factory.from_npuzzle(initial, goal) heuristic = factory.heuristic_for(problem_instance) size = int(len(goal.replace(" ", ""))**0.5) step_row = len(buffer) // size step_col = len(buffer[0]) // size for i in range(step_row): for j in range(step_col): buffer[i][j] = "#" solution = search.astar(problem_instance, heuristic) image = deepcopy(buffer) for state, action in solution: draw_image(state, image, buffer, size) sleep(1)
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)
def run_search(self, search_algorithm, graph_file_name, start_node, end_node): loader = graphs.GraphLoader() graph = loader.from_file(config.TEST_GRAPHS[graph_file_name]) factory = problem.ProblemFactory() problem_instance = factory.from_graph(graph, start_node, end_node) solution = search_algorithm(problem_instance) return (problem_instance, solution)
def test_local_minima(self): factory = problem.ProblemFactory() local_minima = factory.from_nqueens(8, initial=(7, 2, 6, 3, 1, 4, 0, 5)) solution = search.hill_climbing(local_minima, 0) self.assertNotEqual(solution, problem.FAILURE) solution = search.hill_climbing(local_minima, 0, local_minima_acceptable=False) self.assertEqual(solution, problem.FAILURE)
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)
def test_genetic_nqueens(self): factory = problem.ProblemFactory() size = 8 state_gen = lambda: factory.from_nqueens(size).initial.state heuristic = factory.heuristic_for(factory.from_nqueens(size)) most_attacking_queens = sum(range(size)) fitness = lambda state: most_attacking_queens - heuristic(state) population_size = 10 res = search.genetic(state_gen, fitness, most_attacking_queens, self.__reproduce_nqueens, self.__mutate_nqueens, population_size) self.assertEqual(heuristic(res), 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")
def assertActions(self, state): factory = problem.ProblemFactory() size = len(state) queens = factory.from_nqueens(size, initial=state) actions_len = sum(1 for _ in queens.actions_iter(queens.initial.state)) self.assertEqual(actions_len, size * (size - 1))
def assertHeuristic(self, state, expected_value): factory = problem.ProblemFactory() size = len(state) queens = factory.from_nqueens(size, initial=state) attacking = problem._NQueensProblem.attacking self.assertEqual(attacking(queens.initial.state), expected_value)
def test_random_restart_hill_climbing_giant_queens(self): factory = problem.ProblemFactory() size = 14 queens_gen = functools.partial(factory.from_nqueens, size) solution = search.random_restart(queens_gen) self.assertNotEqual(solution, problem.FAILURE)
def test_random_restart_hill_climbing(self): factory = problem.ProblemFactory() queens8_gen = functools.partial(factory.from_nqueens, 8) for _ in range(10): solution = search.random_restart(queens8_gen) self.assertNotEqual(solution, problem.FAILURE)
def assert_npuzzle(self, initial, goal, expected_solution_length): factory = problem.ProblemFactory() problem_instance = factory.from_npuzzle(initial, goal) heuristic = factory.heuristic_for(problem_instance) solution = search.astar(problem_instance, heuristic) self.assertEqual(len(solution), expected_solution_length)