1

How do I know when I can stop increasing the depth for an iterative deepening algorithm with negamax alpha beta pruning and transposition tables? The following pseudo code taken from a wiki page:

function negamax(node, depth, α, β, color)
 alphaOrig := α

 // Transposition Table Lookup; node is the lookup key for ttEntry
 ttEntry := TranspositionTableLookup( node )
 if ttEntry is valid and ttEntry.depth ≥ depth
     if ttEntry.Flag = EXACT
         return ttEntry.Value
     else if ttEntry.Flag = LOWERBOUND
         α := max( α, ttEntry.Value)
     else if ttEntry.Flag = UPPERBOUND
         β := min( β, ttEntry.Value)
     endif
     if α ≥ β
         return ttEntry.Value
 endif

 if depth = 0 or node is a terminal node
     return color * the heuristic value of node

 bestValue := -∞
 childNodes := GenerateMoves(node)
 childNodes := OrderMoves(childNodes)
 foreach child in childNodes
     val := -negamax(child, depth - 1, -β, -α, -color)
     bestValue := max( bestValue, val )
     α := max( α, val )
     if α ≥ β
         break

 // Transposition Table Store; node is the lookup key for ttEntry
 ttEntry.Value := bestValue
 if bestValue ≤ alphaOrig
     ttEntry.Flag := UPPERBOUND
 else if bestValue ≥ β
     ttEntry.Flag := LOWERBOUND
 else
     ttEntry.Flag := EXACT
 endif
 ttEntry.depth := depth 
 TranspositionTableStore( node, ttEntry )

 return bestValue

And this is the iterative deepening call:

while(depth < ?)
{
    depth++;
    rootNegamaxValue := negamax( rootNode, depth, -∞, +∞, 1)
}

Of course, when I know the total number of moves in a game I could use depth < numberOfMovesLeft as an upper bound. But if this information is not given, when do I know that another call of negamax doesn't give any better result then the previous run? What do I need to change in the algorithm?

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
ZzetT
  • 568
  • 4
  • 16
  • `return color * the heuristic value of node` is probably what is normally described as "the evaluation function" ? – wildplasser Oct 12 '15 at 21:44
  • Yes, exactly! (depending on the player) – ZzetT Oct 12 '15 at 21:47
  • My gutfeeling is that the limits from previous runs should become invalid when you increase the depth and re-evaluate the game tree. this implies that the transposition table is basically worthless (because it was based on different (wrong) terminal evaluations. (this is called the "horizon effect" of evaluation functions, IIRC) – wildplasser Oct 12 '15 at 21:58

1 Answers1

5

The short answer is: when you run out of time (and the transpositional tables are irrelevant to the answer/question)


Here I assume that your evaluation function is reasonable (gives good approximation of the position).

The main idea to combine the iterative deepening with alpha beta is the following: let's assume that you have 15 seconds to come up with the best move. How far can you search? I do not know and no one else know. You can try to search till depth = 8 only to find out that the search finished in 1 second (so you waisted available 14 seconds of time). With trial and error you found that depth = 10 gives you result in 13 seconds. So you decided to use it all the time. But now something went terribly wrong (your alpha beta was not pruning good enough, some of the positions took too much time to evaluate) and your result was not ready in 15 seconds. So you either made a random move or have lost the game.

So that this would never happened it is nice to have a good result ready. So you do the following. Get the best result for depth=1 and store it. Find the best result for depth=2, and overwrite it. And so on. From time to time check how much time left, and if it is really close to timelimit - return your best move.

Now you do not need to worry about the time, your method will give the best result you have found so far. With all these recalculations of different subtrees you only waste half of your resources (if you check the whole tree, but in alpha-beta you most probably are not). The additional advantage is that now you reorder the moves from the best to worse on each depth iteration and thus will make pruning more aggressive.

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • Thank you for the very detailed answer. However, for me it explains just the concept of the "iterative deepening algorithm". What I really like to know is if there a condition where I can be absolutly sure to stop the search and return something like "checkmate in 4 moves"? – ZzetT Oct 20 '15 at 21:28
  • 1
    @ZzetT if you are looking for a 'checkmate in 4 moves' you do not need iterative deepening. Minimax/negamax with 8 plies would find it for you. When you are just searching for a best move, you need ID to prune more aggressively and therefore being able to reach deeper depth faster. Can you be absolutely sure in the results? Theoretically having the absolutely correct evaluation function yes (but then you do not need to search - just check all the states after 1 plie and select the best). In reality you almost never have 100% correct evaluation function and you can never be sure. – Salvador Dali Oct 20 '15 at 23:01