def complex_evaluation_function(state: TwoPlayerGameState) -> float: """Return zero, except for terminal game states.""" state_value = 0 if state.end_of_game: scores = state.scores # Evaluation of the state from the point of view of MAX assert isinstance(scores, (Sequence, np.ndarray)) score_difference = scores[0] - scores[1] if state.is_player_max(state.player1): state_value = score_difference elif state.is_player_max(state.player2): state_value = -score_difference else: raise ValueError('Player MAX not defined') else: successors = state.game.generate_successors(state) # Minimize the number of your opponent moves (for MAX). score_difference = -len(successors) if state.is_player_max(state.player1): state_value = score_difference elif state.is_player_max(state.player2): state_value = -score_difference return state_value
def evaluate(self, state: TwoPlayerGameState) -> float: if not isinstance(state.game, Reversi): return 0 if isinstance(state.board, dict): board = state.board else: board = reversi.from_array_to_dictionary_board(state.board) game = state.game corners = [board.get((1, 1)), board.get((1, game.height)), board.get((game.width, 1)), board.get((game.width, game.height))] x_squares = [board.get((2, 2)), board.get((2, game.height - 1)), board.get((game.width - 1, 2)), board.get((game.width - 1, game.height - 1))] corner_diff = 0 for i in range(4): if corners[i] == game.player1.label: corner_diff += 4 elif corners[i] == game.player2.label: corner_diff -= 4 else: if x_squares[i] == game.player1.label: corner_diff -= 1 elif x_squares[i] == game.player2.label: corner_diff += 1 mobility = len(game._get_valid_moves(board, state.player2.label)) # Opponent's mobility result = 4 * corner_diff - mobility if state.is_player_max(state.player1): return result elif state.is_player_max(state.player2): return - result else: raise ValueError('Player MAX not defined')
def evaluate(self, state: TwoPlayerGameState) -> float: """ Minimizes captures to limit opponent's mobility """ coins_taken = state.scores[1] - state.parent.scores[1] if state.is_player_max(state.player1): return -coins_taken if state.is_player_max(state.player2): return coins_taken else: raise ValueError('Player MAX not defined')
def evaluate(self, state: TwoPlayerGameState) -> float: game, board = state.game, state.board utility = 0.4 * game._coin_diff(board) + 0.3 * game._choice_diff( board) + 0.3 * game._corner_diff(board) if state.is_player_max(state.player1): return -utility / 100 if state.is_player_max(state.player2): return utility / 100 else: raise ValueError('Player MAX not defined')
def general_evaluation_function(state: TwoPlayerGameState, func: Callable[[TwoPlayerGameState], float]) -> float: if state.end_of_game: scores = state.scores score_difference = scores[0] - scores[1] if state.is_player_max(state.player1): return score_difference if state.is_player_max(state.player2): return - score_difference else: raise ValueError('Player MAX not defined') return func(state)
def evaluate(self, state: TwoPlayerGameState) -> float: score = 0 for pos in state.board: if pos not in state.parent.board: # i. e. the move played if pos in X_POSITIONS: score = -1 if pos in C_POSITIONS: score = -.5 break if state.is_player_max(state.player1): return score / 2 if state.is_player_max(state.player2): return -score / 2 else: raise ValueError('Player MAX not defined')
def simple_evaluation_function(state: TwoPlayerGameState) -> float: """Return a random value, except for terminal game states.""" state_value = 2 * np.random.rand() - 1 if state.end_of_game: scores = state.scores # Evaluation of the state from the point of view of MAX assert isinstance(scores, (Sequence, np.ndarray)) score_difference = scores[0] - scores[1] if state.is_player_max(state.player1): state_value = score_difference elif state.is_player_max(state.player2): state_value = -score_difference else: raise ValueError('Player MAX not defined') return state_value
def evaluation_function(state: TwoPlayerGameState) -> float: scores = state.scores score_difference = scores[0] - scores[1] score_sum = scores[0] + scores[1] if state.end_of_game: if state.is_player_max(state.player1): state_value = score_difference elif state.is_player_max(state.player2): state_value = -score_difference else: raise ValueError('Player MAX not defined') else: # Calculamos el momento de la partida stage = ((state.game.height * state.game.width) - 4) / 60 stab = stability(state) mob = mobility(state) cor = corner(state) score = 100 * score_difference / score_sum # Early game if score_sum <= (stage * 20): num = (45 * stab + 30 * mob + 5 * cor + 20 * score) if state.is_player_max(state.player1): state_value = num elif state.is_player_max(state.player2): state_value = -num # Late game elif score_sum >= (stage * 54): num = (20 * stab + 5 * mob + 30 * cor + 45 * score) if state.is_player_max(state.player1): state_value = num elif state.is_player_max(state.player2): state_value = -num # Mid game else: num = (30 * stab + 20 * mob + 45 * cor + 5 * score) if state.is_player_max(state.player1): state_value = num elif state.is_player_max(state.player2): state_value = -num return state_value
def evaluate(self, state: TwoPlayerGameState) -> float: if isinstance(state.board, dict): board = state.board else: board = reversi.from_array_to_dictionary_board(state.board) count = 0 # Inferior edge c = board.get((1, 1)) ini = c if c is None: count -= get_sign(board.get((2, 2)), state) * 2 else: count += get_sign(c, state) * 4 inc, nxt_streak, n_streak = edge(board, state, c, 1, False, False) count += inc # Right edge c = board.get((8, 1)) if c is not None and c == nxt_streak: count += get_sign(c, state) * n_streak * 3 if c is None: count -= get_sign(board.get((7, 2)), state) * 2 else: count += get_sign(c, state) * 4 inc, nxt_streak, n_streak = edge(board, state, c, 8, False, True) count += inc # Superior edge c = board.get((8, 8)) if c is not None and c == nxt_streak: count += get_sign(c, state) * n_streak * 3 if c is None: count -= get_sign(board.get((7, 7)), state) * 2 else: count += get_sign(c, state) * 4 inc, nxt_streak, n_streak = edge(board, state, c, 8, True, False) count += inc # Left edge c = board.get((1, 8)) if c is not None and c == nxt_streak: count += get_sign(c, state) * n_streak * 3 if c is None: count -= get_sign(board.get((2, 7)), state) * 2 else: count += get_sign(c, state) * 4 inc, nxt_streak, n_streak = edge(board, state, c, 1, True, True) count += inc if nxt_streak is not None and nxt_streak == ini: count += get_sign(c, state) * n_streak * 3 if state.is_player_max(state.player1): return count elif state.is_player_max(state.player2): return -count else: raise ValueError('Player MAX not defined')
def evaluate(self, state: TwoPlayerGameState) -> float: if isinstance(state.board, dict): board = state.board else: board = reversi.from_array_to_dictionary_board(state.board) count = 0 # Inferior edge n_streak = 0 nxt_streak = None c = board.get((1, 1)) ini = c if c is None: count -= get_sign(board.get((2, 2)), state) * 2 cur_streak = None else: count += get_sign(c, state) * 4 cur_streak = c for i in range(2, 8): c = board.get((i, 1)) count += get_sign(c, state) if c == cur_streak and c is not None: n_streak += 1 else: if cur_streak is not None: count += get_sign(cur_streak, state) * n_streak * 3 cur_streak = None if cur_streak is None: if c == nxt_streak and c is not None: n_streak += 1 else: nxt_streak = c n_streak = 1 if cur_streak is not None: count += get_sign(c, state) * n_streak * 3 # Right edge c = board.get((8, 1)) if c is not None and c == nxt_streak: count += get_sign(c, state) * n_streak * 3 cur_streak = c nxt_streak = None n_streak = 0 if c is None: count -= get_sign(board.get((7, 2)), state) * 2 else: count += get_sign(c, state) * 4 for i in range(2, 8): c = board.get((8, i)) count += get_sign(c, state) if c == cur_streak and c is not None: n_streak += 1 else: if cur_streak is not None: count += get_sign(cur_streak, state) * n_streak * 3 cur_streak = None if cur_streak is None: if c == nxt_streak and c is not None: n_streak += 1 else: nxt_streak = c n_streak = 1 if cur_streak is not None: count += get_sign(c, state) * n_streak * 3 # Superior edge c = board.get((8, 8)) if c is not None and c == nxt_streak: count += get_sign(c, state) * n_streak * 3 cur_streak = c nxt_streak = None n_streak = 0 if c is None: count -= get_sign(board.get((7, 7)), state) * 2 else: count += get_sign(c, state) * 4 for i in range(1, 7): c = board.get((8 - i, 8)) count += get_sign(c, state) if c == cur_streak and c is not None: n_streak += 1 else: if cur_streak is not None: count += get_sign(cur_streak, state) * n_streak * 3 cur_streak = None if cur_streak is None: if c == nxt_streak and c is not None: n_streak += 1 else: nxt_streak = c n_streak = 1 if cur_streak is not None: count += get_sign(c, state) * n_streak * 3 # Left edge c = board.get((1, 8)) if c is not None and c == nxt_streak: count += get_sign(c, state) * n_streak * 3 cur_streak = c nxt_streak = None n_streak = 0 if c is None: count -= get_sign(board.get((2, 7)), state) * 2 else: count += get_sign(c, state) * 4 for i in range(1, 7): c = board.get((1, 8 - i)) count += get_sign(c, state) if c == cur_streak and c is not None: n_streak += 1 else: if cur_streak is not None: count += get_sign(cur_streak, state) * n_streak * 3 cur_streak = None if cur_streak is None: if c == nxt_streak and c is not None: n_streak += 1 else: nxt_streak = c n_streak = 1 if cur_streak is not None: count += get_sign(c, state) * n_streak * 3 if nxt_streak is not None and nxt_streak == ini: count += get_sign(c, state) * n_streak * 3 if state.is_player_max(state.player1): return count elif state.is_player_max(state.player2): return - count else: raise ValueError('Player MAX not defined')