具有 alpha beta 算法的国际象棋 AI

use*_*853 3 c++ algorithm chess artificial-intelligence alpha-beta-pruning

我已经为我的国际象棋游戏实现了 alpha beta 算法,但是最终做出一个相当愚蠢的举动需要很多时间(4 层需要几分钟)。

我一直在努力寻找错误(我假设我犯了一个)2 天了,我非常感谢我的代码中的一些外部输入。

getMove 函数:为根节点调用,它为其所有子节点(可能的移动)调用 alphaBeta 函数,然后选择得分最高的移动。

Move AIPlayer::getMove(Board b, MoveGenerator& gen)
{
    // defined constants: ALPHA=-20000 and BETA= 20000
    int alpha = ALPHA; 
    Board bTemp(false); // test Board
    Move BestMov;
    int i = -1; int temp;
    int len = gen.moves.getLength();  // moves is a linked list holding all legal moves
    BoardCounter++; // private attribute of AIPlayer object, counts analyzed boards
    Move mTemp;     // mTemp is used to apply the nextmove in the list to the temporary test Board
    gen.mouvements.Begin();   // sets the list counter to the first element in the list
    while (++i < len && alpha < BETA){
        mTemp = gen.moves.nextElement();
        bTemp.cloneBoard(b);
        bTemp.applyMove(mTemp);
        temp = MAX(alpha, alphaBeta(bTemp, alpha, BETA, depth, MIN_NODE));
        if (temp > alpha){
            alpha = temp;
            BestMov = mTemp;
        }
    }
    return BestMov;
}
Run Code Online (Sandbox Code Playgroud)

alphaBeta 函数:

int AIPlayer::alphaBeta(Board b, int alpha, int beta, char depth, bool nodeType)
{
    Move m;
    b.changeSide();
    compteurBoards++;
    MoveGenerator genMoves(b); // when the constructor is given a board, it automatically generates possible moves
    // the Board object has a player attribute that holds the current player
    if (genMoves.checkMate(b, b.getSide(), moves)){ // if the current player is in checkmate
        return 100000;
    }
    else if (genMoves.checkMate(b, ((b.getSide() == BLACK) ? BLACK : WHITE), moves)){ // if the other player is in checkmate
        return -100000;
    }
    else if (!depth){
        return b.evaluateBoard(nodeType);

    }
    else{
        int scoreMove = alpha;
        int best;
        genMoves.moves.Begin();
        short i = -1, len = genMoves.moves.getLength();
        Board bTemp(false);

        if (nodeType == MAX_NODE){
            best = ALPHA;
            while (++i < len){
                bTemp.cloneBoard(b);
                if (bTemp.applyMove(genMoves.moves.nextElement())){ 
                    scoreMove = alphaBeta(bTemp, alpha, beta, depth - 1, !nodeType);
                    best = MAX(best, scoreMove);
                    alpha = MAX(alpha, best);

                    if (beta <= alpha){ 
                        std::cout << "max cutoff" << std::endl;
                        break;
                    }
                }
            }
            return scoreMove;
        //return alpha;
        }
        else{
            best = BETA;
            while (++i < len){
                bTemp.cloneBoard(b);
                if (bTemp.applyMove(genMoves.moves.nextElement())){ 
                    scoreMove = alphaBeta(bTemp, alpha, beta, depth - 1, !nodeType);
                    best = MIN(best, scoreMove);
                    beta = MIN(beta, best);
                    if (beta <= alpha){ 
                        std::cout << "min cutoff" << std::endl;
                        break;
                    }
                }
            }
            return scoreMove;
            //return beta;
        }
        return meilleur;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:我应该注意,evaluateBoard 只评估棋子的移动性(可能的移动次数,捕获移动会根据捕获的棋子获得更高的分数)

谢谢你。

Sma*_*ess 5

我可以看到您正在尝试实现最小最大算法。但是,代码中有一些东西让我怀疑。我们将代码与开源 Stockfish 国际象棋引擎进行比较。请参考https://github.com/mcostalba/Stockfish/blob/master/src/search.cpp上的搜索算法

1. 按值传递板 b

你的代码中有这个:

alphaBeta(Board b, int alpha, int beta, char depth, bool nodeType)

我不知道“董事会”到底是什么。但在我看来并不合适。让我们来看看 Stockfish:

值搜索(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, bool cutNode)

位置对象在 Stockfish 中通过引用传递。如果“Board”是一个类,则每次调用 alpha-beta 函数时,程序都需要制作一个新副本。在国际象棋中,当我们必须评估许多节点时,这显然是不可接受的。

2. 没有散列

散列是在 Stockfish 中完成的:

ttValue = ttHit ? value_from_tt(tte->value(), ss->ply) : VALUE_NONE;

如果没有散列,您将需要一次又一次地评估相同的位置。如果没有实施散列,你将不会去任何地方。

3.检查将死

可能不是最显着的减速,但我们永远不应该检查每个节点中的将死。在 Stockfish 中:

// All legal moves have been searched. A special case: If we're in check
// and no legal moves were found, it is checkmate.
if (InCheck && bestValue == -VALUE_INFINITE)
    return mated_in(ss->ply); // Plies to mate from the root
Run Code Online (Sandbox Code Playgroud)

这是在搜索所有可能的移动之后完成的。我们这样做是因为我们通常有比将死节点更多的非将死节点。

4. 板 bTemp(false);

这看起来像是一个重大的放缓。让我们来看看 Stockfish:

  // Step 14. Make the move
  pos.do_move(move, st, ci, givesCheck);
Run Code Online (Sandbox Code Playgroud)

您不应该在每个节点中创建一个临时对象(创建 bTemp 的对象)。机器需要分配一些堆栈空间来保存 bTemp。这可能是严重的性能损失,特别是如果 bTemp 不是主要变量(即,不太可能被处理器缓存)。Stockfish 只是修改了内部数据结构,而无需创建新的数据结构。

5. bTemp.cloneBoard(b);

与 4 类似,更糟糕的是,这是对节点中的每一次移动都进行的。

6. std::cout << "max cutoff" << std::endl;

也许很难相信,打印到终端比处理慢得多。在这里,您正在创建一个潜在的减速,字符串需要保存到 IO 缓冲区。该函数可能(我不是 100% 确定)甚至会阻止您的程序,直到文本显示在终端上。Stockfish 仅用于统计摘要,绝对不是每次出现故障高或故障低时。

7.不排序PV招式

在解决其他问题之前,您可能不想做的事情。在 Stockfish 中,他们有:

std::stable_sort(RootMoves.begin() + PVIdx, RootMoves.end());

这是在迭代深化框架中的每次迭代中完成的。