예제 #1
0
def hmin_alpha_beta(state, player, pits, initial_max, alpha, beta, d):
    global count
    count = count + 1
    if d == 0:  # Reached depth threshold, evaluate heuristic
        return evaluate(state, pits, initial_max)

    if terminal_test(state, pits):
        return utility(state, pits, initial_max)
    u = math.inf  # max possible utility
    actions = available_actions(state, player, pits)
    for a in actions:  # Check each possible action for minimmum utility
        player_copy = player
        state_copy = [row[:] for row in state]
        result_player, result_state = action(player_copy, a, state_copy, pits)
        if result_player == player:  # Run min_value if next player is MIN
            u = min([
                u,
                hmin_alpha_beta(result_state, result_player, pits, initial_max,
                                alpha, beta, d - 1)
            ])
            if u <= alpha:
                return u
            beta = min(beta, u)
        else:  # Run max_value if next player is MAX
            u = min([
                u,
                hmax_alpha_beta(result_state, result_player, pits, initial_max,
                                alpha, beta, d - 1)
            ])
    return u
예제 #2
0
def hmaxvalue(state, player, pits, p_o, d):
    global count
    count = count + 1
    v = -(math.inf)
    player_initial = player

    if d == 0:  # Reached depth threshold, evaluate heuristic
        return evaluate(state, pits, p_o)

    if not terminal_test(state, pits):

        actions = available_actions(state, player, pits)

        for move in actions:
            new_state = [row[:] for row in state]
            player, new_state_1 = action(
                player_initial, move, new_state,
                pits)  # Check resulting state of each action
            if player == player_initial:  # If same player, perform hmaxvalue again
                v = max(v, hmaxvalue(new_state_1, player, pits, p_o, d - 1))

            else:  # If player changes, perform hminvalue
                v = max(v, hminvalue(new_state_1, player, pits, p_o, d - 1))

    else:  # Terminal case reached, return utility
        return utility(state, pits, p_o)

    return v
예제 #3
0
def max_alpha_beta(state, player, pits, initial_max, alpha, beta):
    global count
    count = count + 1
    if terminal_test(state, pits):
        return utility(state, pits, initial_max)
    u = -math.inf  # min possible utility
    actions = available_actions(state, player, pits)
    for a in actions:  # Check each possible action for maximum utility
        player_copy = player
        state_copy = [row[:] for row in state]
        result_player, result_state = action(player_copy, a, state_copy, pits)
        if result_player == player:  # Run max_value if next player is MAX
            u = max([
                u,
                max_alpha_beta(result_state, result_player, pits, initial_max,
                               alpha, beta)
            ])
            if u >= beta:
                return u
            alpha = max(alpha, u)
        else:  # Run min_value if next player is MIN
            u = max([
                u,
                min_alpha_beta(result_state, result_player, pits, initial_max,
                               alpha, beta)
            ])
    return u
예제 #4
0
def min_value(state, player, pits, initial_max):
    global count
    count = count + 1
    if terminal_test(state, pits):
        return utility(state, pits, initial_max)
    u = math.inf  # Set maximum possible utility
    actions = available_actions(state, player, pits)
    for a in actions:  # Check each possible action for minimmum utility
        player_copy = player
        state_copy = [row[:] for row in state]
        result_player, result_state = action(player_copy, a, state_copy, pits)
        if result_player == player:  # Run min_value if next player is MIN
            u = min(
                [u,
                 min_value(result_state, result_player, pits, initial_max)])
        else:  # Run max_value if next player is MAX
            u = min(
                [u,
                 max_value(result_state, result_player, pits, initial_max)])
    return u