Question

function negamax(node, depth, α, β, color)
    if node is a terminal node or depth = 0
        return color * the heuristic value of node
    else
        foreach child of node
            val := -negamax(child, depth-1, -β, -α, -color)
            {the following if statement constitutes alpha-beta pruning}
            if val≥β
                return val
            if val≥α
                α:=val
        return α

So if the above is my negamax code (copied from wikipedia), and it is called as follows:

negamax(origin, depth, -inf, +inf, 1)

Then, will this function always return a positive value regardless of what depth we call the function with. This is assuming that the heuristic value by itself is always positive.

Was it helpful?

Solution

Yes, if the evaluation score of the leaf node is positive, a positive value will be returned by negamax. That's what the multiplication by the color value accomplishes, it ensures that there's always a countering negation to reverse the final negation if there are an odd number of recursive negamax calls. This is because with an odd number of recursive calls, color will always -1. If there are an even number of recursive calls, all the negations cancel out and color will be 1, which will leave the returned value unaffected.

Note that if you're calling negamax with color == -1 (it's the other side's turn to move), you have to negate that call in order to get the correct value. That is:

-negamax(origin, depth, -inf, +inf, -1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top