java - 为什么我的 alpha-beta 剪枝实现不起作用?

标签 java artificial-intelligence reversi

我正在使用维基百科伪代码来基于 -

function alphabeta(node, depth, α, β, Player)         
    if  depth = 0 or node is a terminal node
        return the heuristic value of node
    if  Player = MaxPlayer
        for each child of node
            α := max(α, alphabeta(child, depth-1, α, β, not(Player) ))     
            if β ≤ α
                break                             (* Beta cut-off *)
        return α
    else
        for each child of node
            β := min(β, alphabeta(child, depth-1, α, β, not(Player) ))     
            if β ≤ α
                break                             (* Alpha cut-off *)
        return β

这是我的 java 实现 -

private int alphabeta(Node newNode, int depth, int alpha, int beta, boolean Player) {
    Integer[] children;
    if(depth == 0 || newNode.allNodesFull()){
        return (newNode.blacknodes() - newNode.whitenodes());
    }
    if(Player == false){
        children = newNode.findMovesBlack();
        Arrays.sort(children);
        for(Integer child: children){
            nodesGenerated ++;
            alpha = Math.max(alpha, alphabeta(new Node(newNode.move(child), true),
                            depth - 1, alpha, beta, !Player));
            if(beta <= alpha)
                break;
        }return alpha;
    }else{
        children = newNode.findMovesWhite();
        Arrays.sort(children);
        for(Integer child: children){
            nodesGenerated ++;
            beta  = Math.min(beta, alphabeta(new Node(newNode.move(child), false), 
                            depth - 1, alpha, beta, !Player));
            if(beta <= alpha)
                break;
        }return beta;
    }
} 

对我的代码进行一些修改后,提早返回不再有问题,但我确实遇到了 alpha 和 beta 永远不会改变的问题

假设它们有效,我将解释发生的情况

findMovesBlack() 和 findMovesWhite() 都返回 Integer[] 数组,该数组具有任一玩家可以移动的可能位置,无论轮到谁。 对于黑白棋的初始位置,findMovesBlack() 将返回 [19, 26, 37, 44]

如果 findMovesBlack() 和 findMovesWhite() 的长度均为 0,则 allNodesFull() 返回 boolean 值。

blacknodes()和whitenodes()分别返回黑色或白色节点的数量。

Node.move(int坐标)返回一个String[]数组,其中包含已翻转和放置的新位置。相信我,它工作正常。

Node(String[] gameboard, boolean player-to-move) 只是使用我们找到的参数设置一个新位置。

我相信这就是您需要看到的。我已经解决了后端的所有问题。

最佳答案

答案在于 beta 和 alpha 值的实现。我不得不经常打乱相对于 = 符号的位置。

关于java - 为什么我的 alpha-beta 剪枝实现不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10229662/

相关文章:

java - 如何获取树的所有叶节点?

artificial-intelligence - 如何将人工神经网络的输出转换为概率?

java - 黑白棋算法检查

java - beans.xml 上的文件过早结束

java - charAt 用于确定唯一字符时

java - 使用 jooq 进行 hstore 合并的重复键

python - train_on_batch() 在 keras 模型中做了什么?

algorithm - 实现 HexQ 算法

c++ - 改变数组的值

java - 如果距离大于 2,寻路搜索崩溃