极小极大实现中的逻辑错误

时间:2016-01-11 11:43:52

标签: python artificial-intelligence minimax

我正在制作人工智能程序来玩棋盘游戏。麻烦的是我的minimax算法的实现有一个严重的错误,我已经尝试了几天,但仍然没有设法解决它。

它正在做我的坚果,所以如果有人可以查看我的代码,看看为什么它没有正确终止,我会很感激。

当我运行它时,它似乎正确搜索树并在它到达最终深度时终止但由于某种原因它无法搜索最深层并永远找到更多要搜索的节点(这在理论上是不可能的)。

我用unittests覆盖了所有剩下的代码,并且一切正常。然而,我在这部分的单元测试并不起作用,因为它只是无限地执行而不会终止。

如果有任何信息可以帮助我提供它。

由于

def recursive_minimax(self, board, depth, player_id):
    #there are no more moves possible so the game is over
    if board.get_available_moves(player_id)==[]:
        return 1 if player_id == self.max_player else -1

    #check to ensure correct depth has been passed into method
    if depth>self.depth:
        raise ValueError("Impossible value for depth")

    #if max depth reached calculate estimated node value
    if depth == 0:
        return self.eval_func(board, player_id)

    if player_id == self.max_player:
        best_val = -999
        moves = board.get_available_moves(player_id)

        for move in moves:
            new_board = board.make_move(move, player_id, True)
            opposing_player = board.get_opposing_player(player_id)

            val = self.recursive_minimax(new_board, depth - 1, opposing_player)
            best_val = max(val, best_val)


    else:
        best_val = 999
        moves = board.get_available_moves(player_id)

        for move in moves:
            #make copy of the board and play move
            new_board = board.make_move(move, player_id, True)
            opposing_player = board.get_opposing_player(player_id)

            val = self.recursive_minimax(new_board, depth - 1, opposing_player)
            best_val = min(val, best_val)

    return best_val

这是unittest(原谅我在测试代码中的邋iness):

def test_recursive_minimax(self):
    #self.skipTest("recursive error")
    init_board = _make_board(5)

    for tile_id in init_board.keys():
        counters = choice(range(len(init_board[tile_id]["neighbours"])-1))
        init_board[tile_id]["counters"] = counters if counters!= 0 else None
        init_board[tile_id]["player"] = choice([0,1]) if counters!=0 else None

    board=Board(init_board, 0)
    strategy = Minimax(board)

    for tile_id, tile in strategy.board.tiles():
        player = 0 if tile["player"]==None else tile["player"]

        new_board = strategy.board.make_move(tile_id, player)
        value = strategy.recursive_minimax(new_board, 1,player)

        self.assertIsNotNone(value)
        self.assertTrue(0<=value<=1 or value in(999, -999))

    for tile_id, tile in strategy.board.tiles():
        player = 0 if tile["player"]==None else tile["player"]

        new_board = strategy.board.make_move(tile_id, player)
        value = strategy.recursive_minimax(new_board, 2,player)

        self.assertIsNotNone(value)
        self.assertTrue(0<=value<=1 or value in(999, -999))

    for tile_id, tile in strategy.board.tiles():
        player = 0 if tile["player"]==None else tile["player"]

        new_board = strategy.board.make_move(tile_id, player)
        value = strategy.recursive_minimax(new_board, 3,player)

        self.assertIsNotNone(value)
        self.assertTrue(0<=value<=1 or value in(999, -999))

单元测试从未进入断言,因为极小极大不会终止

0 个答案:

没有答案