【问题标题】:How to get minimax algorithm to return an actual move? [closed]如何让极小极大算法返回实际移动? [关闭]
【发布时间】:2015-03-01 19:48:04
【问题描述】:

我目前正在尝试为井字游戏实现极小极大算法,但我不确定如何在获得所有游戏状态的最小值/最大值后找出如何移动。我知道你应该看看哪条路径的获胜次数最多,但我不知道从哪里开始。

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

【问题讨论】:

  • 在评估状态时,请保持目前评估的最佳状态。当您用完时间/尽可能地沿着树向下走/评估整个搜索树时,请遵循您保持的状态,访问其父级,其父级的父级等,直到您到达一个节点没有父母。将您带到那里的举动就是您的下一步。
  • 实际上现在我想起来了,minimax 听起来不像是tictactoe 的现有算法,因为最好的游戏总是会导致平局。

标签: python algorithm artificial-intelligence minimax


【解决方案1】:

顶层真的很简单——你只需要记住当前搜索深度的最佳移动,如果你全面评估深度,然后将最好的设置为该深度的最佳;并尝试用更深的树再次评估。顺便说一句,最大的胜利次数并不重要,胜利就是胜利。

案例的伪代码:

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)

【讨论】:

    猜你喜欢
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多