def test_phenotype_get_prediction(): input_nodes = 4 hidden_layers_nodes = [6, 8] output_nodes = 3 number_of_nn_weights = get_number_of_nn_weights(input_nodes, hidden_layers_nodes, output_nodes) weight_lower_threshold = -1 weight_upper_threshold = 1 random_simple_genotype = SimpleGenotype.get_random_genotype( number_of_nn_weights, weight_lower_threshold, weight_upper_threshold) phenotype = Phenotype.get_phenotype_from_genotype(random_simple_genotype, input_nodes, hidden_layers_nodes, output_nodes) input_values = array([[1, 0, 0, -1]]) prediction = Phenotype.get_prediction(phenotype, input_values) assert type(prediction) is ndarray assert prediction.shape == (1, 3)
def test_phenotype_get_phenotype_for_genotype(): input_nodes = 4 hidden_layers_nodes = [6, 8] output_nodes = 3 number_of_nn_weights = get_number_of_nn_weights(input_nodes, hidden_layers_nodes, output_nodes) weight_lower_threshold = -1 weight_upper_threshold = 1 random_simple_genotype = SimpleGenotype.get_random_genotype( number_of_nn_weights, weight_lower_threshold, weight_upper_threshold) phenotype = Phenotype.get_phenotype_from_genotype(random_simple_genotype, input_nodes, hidden_layers_nodes, output_nodes) assert type(phenotype) is Phenotype assert len(phenotype.layers) == 3 assert phenotype.input_nodes == input_nodes assert phenotype.hidden_layers_nodes == hidden_layers_nodes assert phenotype.output_nodes == output_nodes for element in phenotype.layers: assert type(element) is ndarray
def test_get_full_game_representation_strategy(): def game_representation_strategy(game): return Game.get_full_game_representation_strategy(game) snake_length = 3 width = 8 height = 8 input_nodes = width * height + 4 hidden_layer_nodes = [64] output_nodes = 3 number_of_nn_weights = get_number_of_nn_weights(input_nodes, hidden_layer_nodes, output_nodes) weight_lower_threshold = -1 weight_upper_threshold = 1 sample_genotype = SimpleGenotype.get_random_genotype( number_of_nn_weights, weight_lower_threshold, weight_upper_threshold) sample_phenotype = Phenotype(sample_genotype.weights, input_nodes, hidden_layer_nodes, output_nodes) sample_game = Game(width, height, sample_phenotype, 777, game_representation_strategy, snake_length) game_representation = Game.get_full_game_representation_strategy( sample_game) assert len(game_representation) == 8 * 8 + 4 assert game_representation[36] == 1 assert game_representation[44] == 1 assert game_representation[52] == 1
def evaluation_strategy(phenotypes): population_size = len(phenotypes) phenotype_values = [] for index in range(population_size): selected_phenotype = phenotypes[index] raw_prediction = Phenotype.get_prediction(selected_phenotype, x_train) parsed_prediction = argmax(raw_prediction, axis=1) score = 0 total_rows = len(y_train) for row_index in range(total_rows): if y_train[row_index] == parsed_prediction[row_index]: score += 1 phenotype_values.append(score / total_rows) return phenotype_values
def get_next_game(game): snake_head_position = game.get_snake_head_position() if game.score >= game.max_points_threshold or game.score <= game.min_points_threshold: game.status = GameStatus.ENDED return game if game.is_snake_head_in_wall(snake_head_position): game.status = GameStatus.ENDED return game # Handle snake eating snack points new_snack_distance = Game.get_snake_snacks_distances( snake_head_position, game.snack) if game.is_snake_eating_snack(snake_head_position): game.score += game.snack_eaten_points game.initialize_snack() game.snake.append(game.snack_perspective) else: # Handle snake approach nearest snack if new_snack_distance < game.last_snack_distance: game.score += game.moved_toward_snack_points else: game.score += game.moved_away_from_snack_points game.last_snack_distance = new_snack_distance # Make decision game_state_representation = game.game_representation_strategy(game) prediction = Phenotype.get_prediction(game.phenotype, game_state_representation) new_direction = game.get_new_direction_from_prediction( prediction, game.direction) # Move forward game.move_forward(new_direction) # Update current direction game.direction = new_direction return deepcopy(game)
def test_game_max_points_threshold(): def game_representation_strategy(game): return Game.get_full_game_representation_strategy(game) snake_length = 5 width = 32 height = 18 snack_eaten_points = 2 input_nodes = width * height + 4 hidden_layer_nodes = [64] output_nodes = 3 number_of_nn_weights = get_number_of_nn_weights(input_nodes, hidden_layer_nodes, output_nodes) weight_lower_threshold = -1 weight_upper_threshold = 1 max_points_threshold = 200 min_points_threshold = -10 sample_genotype = SimpleGenotype.get_random_genotype( number_of_nn_weights, weight_lower_threshold, weight_upper_threshold) sample_phenotype = Phenotype(sample_genotype.weights, input_nodes, hidden_layer_nodes, output_nodes) sample_game = Game(width, height, sample_phenotype, 777, game_representation_strategy, snake_length, snack_eaten_points, max_points_threshold=max_points_threshold, min_points_threshold=min_points_threshold) sample_game.score = 200 next_game_state = Game.get_next_game(sample_game) assert next_game_state.status == GameStatus.ENDED next_game_state.score = -10 next_game_state = Game.get_next_game(next_game_state) assert next_game_state.status == GameStatus.ENDED
def test_game_initialization(): def game_representation_strategy(game): return Game.get_full_game_representation_strategy(game) snake_length = 5 width = 32 height = 18 seed = 777 input_nodes = width * height + 4 hidden_layer_nodes = [64] output_nodes = 3 number_of_nn_weights = get_number_of_nn_weights(input_nodes, hidden_layer_nodes, output_nodes) weight_lower_threshold = -1 weight_upper_threshold = 1 sample_genotype = SimpleGenotype.get_random_genotype( number_of_nn_weights, weight_lower_threshold, weight_upper_threshold) sample_phenotype = Phenotype(sample_genotype.weights, input_nodes, hidden_layer_nodes, output_nodes) sample_game = Game(width, height, sample_phenotype, seed, game_representation_strategy, snake_length) assert len(sample_game.snake) == snake_length correct_snake_blocks = [(16, 9), (17, 9), (18, 9), (19, 9), (20, 9)] assert sample_game.snake == correct_snake_blocks assert sample_game.snack_perspective == (21, 9) assert sample_game.snack not in correct_snake_blocks assert 0 <= sample_game.snack[0] <= width assert 0 <= sample_game.snack[1] <= height another_game = Game(width, height, sample_phenotype, seed, game_representation_strategy, snake_length) assert sample_game.snack == another_game.snack
def test_snake_points_assignment(): def game_representation_strategy(game): return Game.get_full_game_representation_strategy(game) snake_length = 5 width = 32 height = 18 snack_eaten_points = 2 input_nodes = width * height + 4 hidden_layer_nodes = [64] output_nodes = 3 number_of_nn_weights = get_number_of_nn_weights(input_nodes, hidden_layer_nodes, output_nodes) weight_lower_threshold = -1 weight_upper_threshold = 1 sample_genotype = SimpleGenotype.get_random_genotype( number_of_nn_weights, weight_lower_threshold, weight_upper_threshold) sample_phenotype = Phenotype(sample_genotype.weights, input_nodes, hidden_layer_nodes, output_nodes) sample_game = Game(width, height, sample_phenotype, 777, game_representation_strategy, snake_length, snack_eaten_points) # Hack snack position # Initial snake blocks are [(16, 9), (17, 9), (18, 9), (19, 9), (20, 9)] # So we put snack on snake head sample_game.snack = (16, 9) next_game_object = Game.get_next_game(sample_game) assert next_game_object.score == snack_eaten_points assert next_game_object.snack != (16, 9) assert next_game_object.snack is not None assert len(next_game_object.snake) == snake_length + 1 assert next_game_object.snack_perspective is not None
def phenotype_strategy(genotype): return Phenotype.get_phenotype_from_genotype( genotype, input_nodes, arguments.hidden_layer_nodes, output_nodes, arguments.bias)
def test_snake_moves(): def game_representation_strategy(game): return Game.get_full_game_representation_strategy(game) snake_length = 5 width = 32 height = 18 input_nodes = width * height + 4 hidden_layer_nodes = [64] output_nodes = 3 number_of_nn_weights = get_number_of_nn_weights(input_nodes, hidden_layer_nodes, output_nodes) weight_lower_threshold = -1 weight_upper_threshold = 1 sample_genotype = SimpleGenotype.get_random_genotype( number_of_nn_weights, weight_lower_threshold, weight_upper_threshold) sample_phenotype = Phenotype(sample_genotype.weights, input_nodes, hidden_layer_nodes, output_nodes) sample_game = Game(width, height, sample_phenotype, 777, game_representation_strategy, snake_length) # Test snake going LEFT and changed to LEFT game_copy = deepcopy(sample_game) game_copy.move_forward(Direction.LEFT) correct_snake_blocks = [(15, 9), (16, 9), (17, 9), (18, 9), (19, 9)] assert game_copy.snake == correct_snake_blocks assert len(game_copy.snake) == snake_length # Test snake going LEFT and changed to RIGHT game_copy = deepcopy(sample_game) game_copy.move_forward(Direction.RIGHT) correct_snake_blocks = [(15, 9), (16, 9), (17, 9), (18, 9), (19, 9)] assert game_copy.snake == correct_snake_blocks assert len(game_copy.snake) == snake_length # Test snake going LEFT and changed to UP game_copy = deepcopy(sample_game) game_copy.move_forward(Direction.UP) correct_snake_blocks = [(16, 8), (16, 9), (17, 9), (18, 9), (19, 9)] assert game_copy.snake == correct_snake_blocks assert len(game_copy.snake) == snake_length # Test snake going LEFT and changed to DOWN game_copy = deepcopy(sample_game) game_copy.move_forward(Direction.DOWN) correct_snake_blocks = [(16, 10), (16, 9), (17, 9), (18, 9), (19, 9)] assert game_copy.snake == correct_snake_blocks assert len(game_copy.snake) == snake_length # Test snake going RIGHT and changed to LEFT game_copy = deepcopy(sample_game) # Set up snake rotated to RIGHT game_copy.snake = [(20, 9), (19, 9), (18, 9), (17, 9), (16, 9)] game_copy.direction = Direction.RIGHT correct_snake_blocks = [(21, 9), (20, 9), (19, 9), (18, 9), (17, 9)] game_copy.move_forward(Direction.LEFT) assert game_copy.snake == correct_snake_blocks assert len(game_copy.snake) == snake_length # Test snake going RIGHT and changed to RIGHT game_copy = deepcopy(sample_game) # Set up snake rotated to RIGHT game_copy.snake = [(20, 9), (19, 9), (18, 9), (17, 9), (16, 9)] game_copy.direction = Direction.RIGHT correct_snake_blocks = [(21, 9), (20, 9), (19, 9), (18, 9), (17, 9)] game_copy.move_forward(Direction.RIGHT) assert game_copy.snake == correct_snake_blocks assert len(game_copy.snake) == snake_length # Test snake going RIGHT and changed to UP game_copy = deepcopy(sample_game) # Set up snake rotated to RIGHT game_copy.snake = [(20, 9), (19, 9), (18, 9), (17, 9), (16, 9)] game_copy.direction = Direction.RIGHT correct_snake_blocks = [(20, 8), (20, 9), (19, 9), (18, 9), (17, 9)] game_copy.move_forward(Direction.UP) assert game_copy.snake == correct_snake_blocks assert len(game_copy.snake) == snake_length # Test snake going RIGHT and changed to DOWN game_copy = deepcopy(sample_game) # Set up snake rotated to RIGHT game_copy.snake = [(20, 9), (19, 9), (18, 9), (17, 9), (16, 9)] game_copy.direction = Direction.RIGHT correct_snake_blocks = [(20, 10), (20, 9), (19, 9), (18, 9), (17, 9)] game_copy.move_forward(Direction.DOWN) assert game_copy.snake == correct_snake_blocks assert len(game_copy.snake) == snake_length # Test snake going UP and changed to LEFT game_copy = deepcopy(sample_game) # Set up snake rotated to UP game_copy.snake = [(16, 9), (16, 8), (16, 7), (16, 6), (16, 5)] game_copy.direction = Direction.UP correct_snake_blocks = [(15, 9), (16, 9), (16, 8), (16, 7), (16, 6)] game_copy.move_forward(Direction.LEFT) assert game_copy.snake == correct_snake_blocks assert len(game_copy.snake) == snake_length # Test snake going UP and changed to RIGHT game_copy = deepcopy(sample_game) # Set up snake rotated to UP game_copy.snake = [(16, 5), (16, 6), (16, 7), (16, 8), (16, 9)] game_copy.direction = Direction.UP correct_snake_blocks = [(17, 5), (16, 5), (16, 6), (16, 7), (16, 8)] game_copy.move_forward(Direction.RIGHT) assert game_copy.snake == correct_snake_blocks assert len(game_copy.snake) == snake_length # Test snake going UP and changed to UP game_copy = deepcopy(sample_game) # Set up snake rotated to UP game_copy.snake = [(16, 5), (16, 6), (16, 7), (16, 8), (16, 9)] game_copy.direction = Direction.UP correct_snake_blocks = [(16, 4), (16, 5), (16, 6), (16, 7), (16, 8)] game_copy.move_forward(Direction.UP) assert game_copy.snake == correct_snake_blocks assert len(game_copy.snake) == snake_length # Test snake going UP and changed to DOWN game_copy = deepcopy(sample_game) # Set up snake rotated to UP game_copy.snake = [(16, 5), (16, 6), (16, 7), (16, 8), (16, 9)] game_copy.direction = Direction.UP correct_snake_blocks = [(16, 4), (16, 5), (16, 6), (16, 7), (16, 8)] game_copy.move_forward(Direction.DOWN) assert game_copy.snake == correct_snake_blocks assert len(game_copy.snake) == snake_length # Test snake going DOWN and changed to LEFT game_copy = deepcopy(sample_game) # Set up snake rotated DOWN game_copy.snake = [(16, 9), (16, 8), (16, 7), (16, 6), (16, 5)] game_copy.direction = Direction.DOWN correct_snake_blocks = [(15, 9), (16, 9), (16, 8), (16, 7), (16, 6)] game_copy.move_forward(Direction.LEFT) assert game_copy.snake == correct_snake_blocks assert len(game_copy.snake) == snake_length # Test snake going DOWN and changed to RIGHT game_copy = deepcopy(sample_game) # Set up snake rotated DOWN game_copy.snake = [(16, 9), (16, 8), (16, 7), (16, 6), (16, 5)] game_copy.direction = Direction.DOWN correct_snake_blocks = [(17, 9), (16, 9), (16, 8), (16, 7), (16, 6)] game_copy.move_forward(Direction.RIGHT) assert game_copy.snake == correct_snake_blocks assert len(game_copy.snake) == snake_length # Test snake going DOWN and changed to UP game_copy = deepcopy(sample_game) # Set up snake rotated DOWN game_copy.snake = [(16, 9), (16, 8), (16, 7), (16, 6), (16, 5)] game_copy.direction = Direction.DOWN correct_snake_blocks = [(16, 10), (16, 9), (16, 8), (16, 7), (16, 6)] game_copy.move_forward(Direction.UP) assert game_copy.snake == correct_snake_blocks assert len(game_copy.snake) == snake_length # Test snake going DOWN and changed to DOWN game_copy = deepcopy(sample_game) # Set up snake rotated DOWN game_copy.snake = [(16, 9), (16, 8), (16, 7), (16, 6), (16, 5)] game_copy.direction = Direction.DOWN correct_snake_blocks = [(16, 10), (16, 9), (16, 8), (16, 7), (16, 6)] game_copy.move_forward(Direction.DOWN) assert game_copy.snake == correct_snake_blocks assert len(game_copy.snake) == snake_length
input_nodes = 8 hidden_layer_nodes = [64] output_nodes = 3 number_of_nn_weights = get_number_of_nn_weights(input_nodes, hidden_layer_nodes, output_nodes) weight_lower_threshold = -1 weight_upper_threshold = 1 mutation_step_size_lower_threshold = -0.3 mutation_step_size_upper_threshold = 0.3 genotype = UncorrelatedNStepSizeGenotype.get_random_genotype( number_of_nn_weights, weight_lower_threshold, weight_upper_threshold, mutation_step_size_lower_threshold, mutation_step_size_upper_threshold) phenotype = Phenotype.get_phenotype_from_genotype(genotype, input_nodes, hidden_layer_nodes, output_nodes) game = Game(game_board_width, game_board_height, phenotype, seed, Game.get_full_game_representation_strategy, initial_snake_length) pygame.init() scale = 24 game_screen_width = game_board_width * scale game_screen_height = game_board_height * scale background_color = (255, 255, 255) snake_color = (0, 255, 0) snack_color = (255, 0, 0) delay = 150