alpha-beta剪枝的代码实现

来源:互联网 发布:淘宝联盟官方网站首页 编辑:程序博客网 时间:2024/05/16 09:47

之前在极大化极小算法minimax说得不够清楚而且也没有附带伪代码,所以这里再写一篇专门关于剪枝的blog进行补充

http://blog.csdn.net/joshualiunsw/article/details/52131507

————————————————————————————————————————————————

现在已经有了普通的minimax的伪代码, 由于下文中需要用到α, β所以对应地将其转换成a(alpha)、b(beta)的形式

function minimax(node, depth)    if node is a terminal node or depth = 0        return the heuristic value of node    if the adversary is to play at node        let b := +foreach child of node            b := min(a, minimax(child, depth-1))        return b    else {we are to play at node}        let a := -foreach child of node            a := max(b, minimax(child, depth-1))        return a
我们在此伪代码的基础上添加alpha-beta剪枝

function minimax(node, depth, a, b)
    if node is a terminal node or depth = 0        return the heuristic value of node    if the adversary is to play at node        //let b := +        foreach child of node            b := min(a, minimax(child, depth-1, a, b))            if b <= a                   return b        return b    else {we are to play at node}        //let a := -        foreach child of node            a := max(b, minimax(child, depth-1, a, b))
            if a >= b                   return a
 return a
其实上述添加的两个if语句的条件判断是完全一样的,因为根据原理在min节点只能修改beta,而在max节点只能修改alpha,这样写只是为了让结构更为清晰。


下面再引用Alan Blair博士的图例来说明这个优化的过程,他的绘图规则我认为是比较清易懂的,特别是三角和倒三角的使用,能清晰地展示当前节点是min还是max节点,而且该图例中展示了多个子节点的情况和不能成功剪枝的情况,值得参考



0 0