AlphaBeta算法是根据Minimax算法得来的,首先我们必须明白MiniMax算法的思想。Minimax算法常用于棋类等由两方较量的游戏和程序。该算法是一个零总和算法,即一方要在可选的选项中选择将其优势最大化的选择,另一方则选择令对手优势最小化的方法。而开始的时候总和为0。
1. 对于一个MIN节点,若能估计出其倒推值的上确界Beta,并且这个Beta值不大于MIN的父节点(MAX节点)的估计倒推值的下确界Alpha,即Alpha≥Beta,则就不必再扩展该MIN节点的其余子节点了,因为这些节点的估值对MIN父节点的倒推值已无任何影响了,这一过程称为Alpha剪枝。
2. 对于一个MAX节点,若能估计出其倒推值的下确界Alpha,并且这个Alpha值不小于MAX的父节点(MIN节点)的估计倒推值的上确界Beta,即Alpha≥Beta,则就不必再扩展该MAX节点的其余子节点了,因为这些节点的估值对MAX父节点的倒推值已无任何影响了。这一过程称为Beta剪枝。
3. 一个MAX节点的Alpha值等于其后继节点当前最大的最终倒推值,一个MIN节点的Beta值等于其后继节点当前最小的最终倒推值

alpha-beta(player,board,alpha,beta)
if(game over in current board position) return winner
children = all legal moves for player from this board
if(max's turn)
for each child
score = alpha-beta(other player,child,alpha,beta)
(we have found a better best move....)
if score > alpha then alpha = score
(cut off...)
if alpha >= beta then return alpha
return alpha (this is our best move)
else (min's turn)
for each child
score = alpha-beta(other player,child,alpha,beta)
(opponent has found a better worse move.....)
if score < beta then beta = score
(cut off....)
if alpha >= beta then return beta
return beta (this is the opponent's best move)
AlphaBeta的递归形式
02
03
04
06
08
09
11
13
14
15
16
18
19
20
21
24
25
26
27
28
29