python - 如何让 minimax 算法返回实际移动?

标签 python algorithm artificial-intelligence minimax

<分区>

我目前正在尝试为 tic tac toe 实现 minimax 算法,但我不确定在获得所有 game_states 的最小/最大值后如何找出如何着手。我知道您应该查看哪条路径的获胜次数最多,但我不知道从哪里开始。

def minimax(game_state):
    if game_state.available_moves():
        return evaluate(game_state)
    else:
        return max_play(game_state)

def evaluate(game_state):
    if game_state.has_won(game_state.next_player):
        return 1
    elif game_state.has_won(game_state.opponent()):
        return -1
    else:
        return 0

def min_play(game_state):
    if game_state.available_moves() == []:
        return evaluate(game_state) 
    else:
        moves = game_state.available_moves()
        best_score = -1
        for move in moves:
            clone = game_state.make_move(move)
            score = max_play(clone)
            if score < best_score:
                best_move = move
                best_score = score
        return best_score

def max_play(game_state):
    if game_state.available_moves() == []:
        return evaluate(game_state) 
    else:
        moves = game_state.available_moves()
        best_score = 1
        for move in moves:
            clone = game_state.make_move(move)
            score = min_play(clone)
            if score > best_score:
                best_move = move
                best_score = score
        return best_score

最佳答案

在顶层非常简单 - 您只需要记住当前搜索深度的最佳着法,如果您充分评估深度,则将所有最佳着法设置为该深度中的最佳着法;并尝试用更深的树再次评估。顺便说一句,最多的胜利并不重要,胜利就是胜利。

案例的伪代码:

bestest_move = None
try:
    for depth in range(1, max_depth):
        best_score = float('-inf')
        for move in possible_moves:
            score = evaluate(move)
            if score > best_score:
                best_move = move
                best_score = score

    bestest_move = best_move

except Timeout:
    pass

move(bestest_move)

关于python - 如何让 minimax 算法返回实际移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28798249/

相关文章:

python - 瓶内IP过滤

python - 比较两列并用数字替换 NaN

java - 快速搜索字典

python - 获取多变量模型中单个样本的分数?

c++ - 如何确保给定的一组点位于可能正方形的边界上?

c# - 需要有关树状结构数据检索的帮助

artificial-intelligence - 使用多个语音转文本 API 来提高准确性

c# - .NET 中的自动问答 (FAQ)

algorithm - 随机决策算法

Python 变量和函数注释引发 AttributeError