第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用二叉搜索樹進(jìn)行 Alpha Beta 剪枝

使用二叉搜索樹進(jìn)行 Alpha Beta 剪枝

素胚勾勒不出你 2021-11-17 15:21:44
我正在使用此處找到的 Alpha-Beta 修剪示例來研究 Minimax 算法。在示例中,他們使用數(shù)組來實(shí)現(xiàn)搜索樹。我遵循了這個(gè)例子,但也嘗試用二叉搜索樹來實(shí)現(xiàn)它。以下是我在樹中使用的值:3、5、6、9、1、2、0、-1。最后的最佳值應(yīng)該是 5。使用 BST 實(shí)現(xiàn),我一直得到 2。我認(rèn)為這是問題所在,但我不知道如何解決它:如果在嘗試檢查下一個(gè)值時(shí)看到葉節(jié)點(diǎn)停止獲取空指針異常,我編寫了代碼以退出遞歸。但是相反,我認(rèn)為它過早地停止搜索(基于我在使用調(diào)試器單步執(zhí)行代碼時(shí)看到的內(nèi)容)。但是,如果我刪除檢查,則代碼會(huì)在空指針上失敗。有人可以指出我正確的方向嗎?我究竟做錯(cuò)了什么?這是代碼:public class AlphaBetaMiniMax {    private static BinarySearchTree myTree = new BinarySearchTree();    static int MAX = 1000;    static int MIN = -1000;    static int opt;    public static void main(String[] args) {        //Start constructing the game        AlphaBetaMiniMax demo = new AlphaBetaMiniMax();        //3, 5, 6, 9, 1, 2, 0, -1        demo.myTree.insert(3);        demo.myTree.insert(5);        demo.myTree.insert(6);        demo.myTree.insert(9);        demo.myTree.insert(1);        demo.myTree.insert(2);        demo.myTree.insert(0);        demo.myTree.insert(-1);        //print the tree        System.out.println("Game Tree: ");        demo.myTree.printTree(demo.myTree.root);        //Print the results of the game        System.out.println("\nGame Results:");        //run the  minimax algorithm with the following inputs        int optimalVal = demo.minimax(0, myTree.root, true, MAX, MIN);        System.out.println("Optimal Value: " + optimalVal);    }    /**     * @param alpha = 1000     * @param beta = -1000     * @param nodeIndex - the current node     * @param depth - the depth to search     * @param maximizingPlayer - the current player making a move     * @return - the best move for the current player     */    public int minimax(int depth, MiniMaxNode nodeIndex, boolean maximizingPlayer, double alpha, double beta) {        //Base Case #1: Reached the bottom of the tree        if (depth == 2) {            return nodeIndex.getValue();        }        }    }}輸出:Game Tree: -1 ~ 0 ~ 1 ~ 2 ~ 3 ~ 5 ~ 6 ~ 9 ~ Game Results:Optimal Value: 2
查看完整描述

1 回答

?
揚(yáng)帆大魚

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超9個(gè)贊

您的問題是您的迭代取決于 2 的循環(huán)控制,而不是 node== null 查找 nodeIndex.getRight()(for max) getLeft(for min.)


記住一棵樹有 1 個(gè)頭(第一級(jí))


第二級(jí) = 2


第 3 級(jí) = 4


4日8等。所以你的循環(huán)算法甚至不會(huì)下降 3 個(gè)級(jí)別。


for (int i = 0; i < 2; i++) {


     int val = minimax(depth + 1, nodeIndex.getLeft(), false, alpha, beta);

                best = Math.max(best, val);

                alpha = Math.max(alpha, best);


                //Alpha Beta Pruning

                if (beta <= alpha) {

                    break;

                }

更改循環(huán)以正確控制迭代,您應(yīng)該很容易找到最高值。


查看完整回答
反對(duì) 回復(fù) 2021-11-17
  • 1 回答
  • 0 關(guān)注
  • 240 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)