3

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

两天来我一直试图找出错误(我假设我犯了一个),我非常感谢我的代码中的一些外部输入。

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;
}

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;
    }
}

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

谢谢你。

4

2 回答 2

5

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

1. 以价值通过Board 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

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

4. 板子 bTemp(false);

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

  // Step 14. Make the move
  pos.do_move(move, st, ci, givesCheck);

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

5. bTemp.cloneBoard(b);

与 4 类似,更糟糕的是,这是针对节点中的每一步移动完成的。

6. std::cout << "最大截止" << std::endl;

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

7.不排序PV移动

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

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

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

于 2015-06-03T06:20:29.993 回答
0

我只打算解决您算法的运行时成本问题,因为我不知道您的板评估功能的实现细节。

为了使事情尽可能简单,我将假设算法的最坏情况。

getMove 函数对 alphaBeta 函数进行 len1 调用,该函数又对自身进行 len2 调用,而后者又对自身进行 len3 调用,依此类推,直到深度达到 0 并且递归停止。由于最坏的情况假设,假设 n = max(len1, len2, ...),所以你有

n * n * n * ... * n 调用 alphaBeta,乘法次数取决于深度 d,这导致 n^d 调用 alphaBeta,这意味着您具有指数运行时行为。这是超慢的,只能被阶乘运行时行为打败。

我认为您应该为此目的查看 Big O 表示法,并尝试相应地优化您的算法以获得更快的结果。

最好的问候, OPM

于 2015-06-01T06:39:50.290 回答