Negamax通常看起来像下面这样:
function negamax(node, depth, α, β, color) is if depth = 0 or node is a terminal node then return color × the heuristic value of node childNodes := generateMoves(node) childNodes := orderMoves(childNodes) value := −∞ foreach child in childNodes do value := max(value, −negamax(child, depth − 1, −β, −α, −color)) α := max(α, value) if α ≥ β then break (* cut-off *) return value
如果是最大化玩家调用,初始调用是 negamax(rootNode, depth, −∞, +∞, 1)
。
我以最大化玩家调用的方式实现了Negamax,但每个rootNode
都是最大化玩家的一个移动:
function negamaxHandler() is bestValue := −∞ bestNode := null childNodes := generateMoves(currentGameState) foreach child in childNodes do value := negamax(child, depth-1, ???, ???, ???) if value > bestValue then bestValue := value bestNode := child return bestNode
由于Negamax返回一个值,我反而想要一个棋盘状态(移动)。所以我手动进行Negamax的第一级,以便解析出最佳移动。但我应该用什么值来调用negamax
呢?更明确地说,如果最大化玩家调用了negamaxHandler
,negamaxHandler
应该调用:
negamax(child, depth-1, −∞, +∞, 1)-negamax(child, depth-1, −∞, +∞, 1)negamax(child, depth-1, +∞, −∞, -1)-negamax(child, depth-1, +∞, −∞, -1)
还是别的什么?为了澄清:
- 最大化玩家调用
negamaxHandler
negamaxHandler
中对negamax
的每个顶级调用应该最小化
回答:
正确的函数调用最终是-negamax(child, depth-1, −∞, +∞, -1)
,尽管negamaxHandler
函数需要进行修改:
function negamaxHandler(α, β, color) is bestValue := −∞ bestNode := null childNodes := generateMoves(currentGameState) foreach child in childNodes do value := -negamax(child, depth-1, -β, -α, -color) if value > bestValue then bestValue := value bestNode := child α := max(bestValue, α) if α ≥ β then break return bestNode
negamaxHandler
的调用方式为negamaxHandler(−∞, +∞, 1)
。