【问题标题】:3D Tic Tac Toe with Minimax & Alpha-beta pruning picks sub-optimal moves具有 Minimax 和 Alpha-beta 修剪的 3D Tic Tac Toe 选择次优动作
【发布时间】:2016-03-01 02:36:48
【问题描述】:

我正在尝试为 3D Tic Tac Toe 游戏实现带有 Alpha-beta 修剪的 Minimax。但是,该算法似乎选择了次优路径。

例如,您只需直接穿过立方体的中间或穿过一个棋盘即可获胜。 AI 似乎会选择在下一个回合而不是当前回合最佳的单元格。

我尝试重新创建和使用我为算法返回的启发式,但我没有取得太大进展。不管是哪一层,它似乎都有同样的问题。

代码是here

相关部分是 computers_movethink_ahead(以及 '2' 变体,这些只是我在尝试一种稍微替代的方法)。

我希望这可能是我忽略的一些简单问题,但据我所知,我不确定问题是什么。如果有人能阐明这个问题,我将不胜感激。

def computers_move2(self):
    best_score = -1000
    best_move = None
    h = None
    win = False

    for move in self.allowed_moves:
        self.move(move, self.ai)
        if self.complete:
            win = True
            break
        else:
            h = self.think_ahead2(self.human, -1000, 1000)
        self.depth_count = 0
        if h >= best_score:
            best_score = h
            best_move = move
            self.undo_move(move)
        else:
            self.undo_move(move)

    if not win:
        self.move(best_move, self.ai)
    self.human_turn = True

def think_ahead2(self, player, a, b):
    if self.depth_count <= self.difficulty:
        self.depth_count += 1
        if player == self.ai:
            h = None
            for move in self.allowed_moves:
                self.move(move, player)
                if self.complete:
                    self.undo_move(move)
                    return 1000
                else:
                    h = self.think_ahead2(self.human, a, b)
                    if h > a:
                        a = h
                        self.undo_move(move)
                    else:
                        self.undo_move(move)
                if a >= b:
                    break
            return a
        else:
            h = None
            for move in self.allowed_moves:
                self.move(move, player)
                if self.complete:
                    self.undo_move(move)
                    return -1000
                else:
                    h = self.think_ahead2(self.ai, a, b)
                    if h < b:
                        b = h
                        self.undo_move(move)
                    else:
                        self.undo_move(move)
                if a >= b:
                    break
            return b
    else:
        diff = self.check_available(self.ai) - self.check_available(self.human)
        return diff

【问题讨论】:

    标签: python algorithm artificial-intelligence minimax alpha-beta-pruning


    【解决方案1】:

    结果我的算法似乎工作正常。问题是由我的辅助函数moveundo_move 引起的。此外,根本问题是我的一组允许的移动。

    我注意到它在探索树时,在computer_plays 的最外层循环中移动的数量大大减少了。即使在第一次扫描期间,计算机和人类玩家每对回合所允许的移动次数也会从总共 27 次减少到 20 次,然后是 10 次,最后是 5 次。

    原来临时测试的招式没有被替换。因此,我将集合换成标准列表,并在每次移动/撤消后对列表进行排序,彻底解决了我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      相关资源
      最近更新 更多