def combined_full(game, player): if game.move_count < 10: return distance_to_center(game, player) if game.move_count < 20: return improved_score(game, player) # if above 20: return improved_score(game, player) + diff_density(game, player)
def combined_improved_density_at_end(game, player): """ Simple evaluation function that combine improved-score heuristic and density function heuristic """ if game.move_count < 20: # At the beginning we need to go fast score = improved_score(game, player) else: # At the end of a game/middle of a game we need a more precise heuristic that could take a bit more of time score = improved_score(game, player) + diff_density(game, player) return float(score)
def custom_score(game, player): """Calculate the heuristic value of a game state from the point of view of the given player. Note: this function should be called from within a Player instance as `self.score()` -- you should not need to call this function directly. Parameters ---------- game : `isolation.Board` An instance of `isolation.Board` encoding the current state of the game (e.g., player locations and blocked cells). player : object A player instance in the current game (i.e., an object corresponding to one of the player objects `game.__player_1__` or `game.__player_2__`.) Returns ------- float The heuristic value of the current game state to the specified player. """ # TODO: finish this function! return improved_score(game, player)
def heuristic_main_alt_less_more(game, player): """ weight number of moves more highly at the beginning of the game, then amount of open space near end game (when improved score gives a winner or loser, prop won't have an effect due to the infinities) """ prop = len(game.get_blank_spaces()) / total_board_spaces return prop * improved_score(game, player) + heuristic2(game, player)
def weighted_improved_score(game, player): """This metric is comparable to the improved score, but it will divide this outcome by the number of blank spaces. For instance, if there are only 3 blank spaces, 2 possible moves is pretty good. 2 possible moves if there are 20 blank spaces is not as good. Parameters ---------- game : `isolation.Board` An instance of `isolation.Board` encoding the current state of the game (e.g., player locations and blocked cells). player : object A player instance in the current game (i.e., an object corresponding to one of the player objects `game.__player_1__` or `game.__player_2__`.) Returns ------- float The heuristic value of the current game state to the specified player. """ if game.is_loser(player): return float("-inf") if game.is_winner(player): return float("inf") improved = improved_score(game, player) available_spaces = len(game.get_blank_spaces()) return improved / available_spaces
def improved_with_sleep(game, player): """ return opposite of improved_score, so we help the opponnent :param game: :param player: :return: """ res = -improved_score(game, player) time.sleep(0.01) # just to make sure we timeout a lot return float(res)
def test6(): player1 = AlphaBetaPlayer(search_depth=1, score_fn=custom_score) player2 = AlphaBetaPlayer(search_depth=1, score_fn=custom_score_3) game = isolation.Board(player1, player2, height=5, width=5) game.apply_move((0, 3)) print(game.to_string()) game.apply_move((4, 4)) print(game.to_string()) game.apply_move((3, 2)) print(game.to_string()) game.apply_move((1, 1)) game.apply_move((2, 0)) game.apply_move((3, 0)) game.apply_move((1, 2)) print(game.to_string()) print(custom_score(game, game.active_player)) print(custom_score(game, game.inactive_player)) print(custom_score_2(game, game.active_player)) print(custom_score_2(game, game.inactive_player)) print(custom_score_3(game, game.active_player)) print(custom_score_3(game, game.inactive_player)) print(improved_score(game, game.active_player)) print(improved_score(game, game.inactive_player))
def agrressive_first_then_preserving(game, player): """ fonctionne pas trop mal :param game: :param player: :return: """ if game.is_loser(player): return float("-inf") if game.is_winner(player): return float("inf") total_space = game.width * game.height game_duration = len(game.get_blank_spaces()) / float(total_space) if game_duration <= 0.25: # aggressif en debut de partie.. return improved_agressive(game, player) else: return improved_score(game, player)
def combined_improved_and_density(game, player): return improved_score(game, player) + diff_density(game, player)