MTD experiment with stockfish 1.7.1

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

QED
Posts: 60
Joined: Thu Nov 05, 2009 9:53 pm

MTD experiment with stockfish 1.7.1

Post by QED »

I decided it is a good time (again) to implement a MTD search in stockfish.

After few tries I realized that it only works well when used to prove that the first move of root move list is not worse than some bound (gamma) and other moves are not better than gamma. It works poorly when it is needed to find a new gamma. But finding true score is what PVS is made for, so I implemented a version of root_search that tries to verify that gamma does not change, but falls back to PVS whenever necessary.

MTD verification does not change the first move, so its only purpose is to speed up search and hopefully find a better move at higher depth PVS. I also made some changes to time management to reflect that it is good idea to save time when there is no change of gamma, so we could have more time to finish that higher depth PVS iteration.

I did not make any other optimizations, and I nearly did not test it at all. Now the search somewhat works, and does not look very ugly when watching it, but I have no idea if it is better or worse then pure PVS.

Anyway, here is what diff says, if you are interested.

Code: Select all

vrato@notas:~/Prg/stockfish-171-mtd/src$ diff -d search.cpp ../../stockfish-171-ja/src/search.cpp 
283d282                                                                                           
<   Value root_search_pv&#40;Position& pos, SearchStack ss&#91;&#93;, RootMoveList& rml, Value* alphaPtr, Value* betaPtr&#41;;
715,743d713                                                                                                   
<             // If we had successfull MTD verification, Iteration is a little ahead,                         
<             // and we would need more time in case of PVS fallback,                                         
<             // so better idea is to save time for harder times.                                             
<             if &#40;Iteration >= 7 && ValueByIteration&#91;Iteration - 1&#93; == ValueByIteration&#91;Iteration&#93;            
<                 && current_search_time&#40;) > (&#40;MaxSearchTime + ExtraSearchTime&#41; * 70&#41; / 128&#41;                  
<                 stopSearch = true;                                                                          
<                                                                                                             
<             // If we had 2 succesive MTD verifications, Iteration is high,                                  
<             // and we would need much more time in case of PVS fallback,                                    
<             // so better idea is to save time for harder times.                                             
<             if &#40;Iteration >= 8 && ValueByIteration&#91;Iteration - 1&#93; == ValueByIteration&#91;Iteration&#93;            
<                 && ValueByIteration&#91;Iteration - 2&#93; == ValueByIteration&#91;Iteration - 1&#93;                       
<                 && current_search_time&#40;) > (&#40;MaxSearchTime + ExtraSearchTime&#41; * 60&#41; / 128&#41;                  
<                 stopSearch = true;                                                                          
<                                                                                                             
<             // And 3 succesive MTD verifications even more.                                                 
<             if &#40;Iteration >= 9 && ValueByIteration&#91;Iteration - 1&#93; == ValueByIteration&#91;Iteration&#93;            
<                 && ValueByIteration&#91;Iteration - 2&#93; == ValueByIteration&#91;Iteration - 1&#93;                       
<                 && ValueByIteration&#91;Iteration - 3&#93; == ValueByIteration&#91;Iteration - 2&#93;                       
<                 && current_search_time&#40;) > (&#40;MaxSearchTime + ExtraSearchTime&#41; * 50&#41; / 128&#41;                  
<                 stopSearch = true;                                                                          
<                                                                                                             
<             // 4 Succesive MTD verifications, that is max for now.                                          
<             if &#40;Iteration >= 10 && ValueByIteration&#91;Iteration - 1&#93; == ValueByIteration&#91;Iteration&#93;           
<                 && ValueByIteration&#91;Iteration - 2&#93; == ValueByIteration&#91;Iteration - 1&#93;                       
<                 && ValueByIteration&#91;Iteration - 3&#93; == ValueByIteration&#91;Iteration - 2&#93;                       
<                 && current_search_time&#40;) > (&#40;MaxSearchTime + ExtraSearchTime&#41; * 40&#41; / 128&#41;                  
<                 stopSearch = true;                                                                          
<                                                                                                             
768d737                                                                                                       
<                                                                                                             
807c776                                                                                                       
<   // root_search_pv&#40;) is the function which searches the root node. It is                                   
---                                                                                                           
>   // root_search&#40;) is the function which searches the root node. It is                                      
811d779                                                                                                       
<   // The original PVS version of root_search&#40;) used by MTD as a fallback.                                   
813c781                                                                                                       
<   Value root_search_pv&#40;Position& pos, SearchStack ss&#91;&#93;, RootMoveList& rml, Value* alphaPtr, Value* betaPtr&#41; &#123;
---                                                                                                            
>   Value root_search&#40;Position& pos, SearchStack ss&#91;&#93;, RootMoveList& rml, Value* alphaPtr, Value* betaPtr&#41; &#123;   
1038c1006                                                                                                      
<         &#125; // End of root move list.                                                                          
---                                                                                                            
>         &#125;                                                                                                    
1055,1276d1022                                                                                                 
<                                                                                                              
<                                                                                                              
<   // root_search&#40;) is the function which searches the root node. It is                                       
<   // similar to root_search_pv except that it only verifies that the first move score                        
<   // stays at least at gamma and other move scores stay at most at gamma.                                    
<   // If not, root_search_pv&#40;) is called.                                                                     
<   // There are no fail high or fail low loops.                                                               
<                                                                                                              
<   // The experimental minimalistic MTD form of root_search&#40;) by Vratko Polak.                                
<                                                                                                              
<   Value root_search&#40;Position& pos, SearchStack ss&#91;&#93;, RootMoveList& rml, Value* alphaPtr, Value* betaPtr&#41; &#123;   
<                                                                                                              
<     // MultiPV is Not Supported &#40;MNS&#41;.                                                                       
<     // Also, at low Iterations it is for some reason not safe to do MTD.                                     
<     if &#40;1 < MultiPV || Iteration <= 6&#41;                                                                       
<         return root_search_pv&#40;pos, ss, rml, alphaPtr, betaPtr&#41;;                                              
<                                                                                                              
<     EvalInfo ei;                                                                                             
<     StateInfo st;                                                                                            
<     CheckInfo ci&#40;pos&#41;;                                                                                       
<     int64_t nodes;                                                                                           
<     Move move;                                                                                               
<     Depth depth, ext, newDepth;                                                                              
<     Value bound, gamma;                                                                                      
<     bool isCheck, moveIsCheck, captureOrPromotion, dangerous;                                                
<                                                                                                              
<     bound = - VALUE_INFINITE;                                                                                
<     // The natural choice for the initial gamma is the score from previous iteration.                        
<     gamma = *alphaPtr / 2 + *betaPtr / 2;                                                                    
<     isCheck = pos.is_check&#40;);                                                                                
<                                                                                                              
<     // Step 1. Initialize node and poll &#40;omitted at root, but I can see no good reason for this, FIXME&#41;      
<     // Step 2. Check for aborted search &#40;omitted at root, because we do not initialize root node&#41;            
<     // Step 3. Mate distance pruning &#40;omitted at root&#41;                                                       
<     // Step 4. Transposition table lookup &#40;omitted at root&#41;                                                  
<                                                                                                              
<     // Step 5. Evaluate the position statically                                                              
<     // At root we do this only to get reference value for child nodes                                        
<     if (!isCheck&#41;                                                                                            
<         ss&#91;0&#93;.eval = evaluate&#40;pos, ei, 0&#41;;                                                                   
<     else                                                                                                     
<         ss&#91;0&#93;.eval = VALUE_NONE; // HACK because we do not initialize root node                              
<                                                                                                              
<     // Step 6. Razoring &#40;omitted at root&#41;                                                                    
<     // Step 7. Static null move pruning &#40;omitted at root&#41;                                                    
<     // Step 8. Null move search with verification search &#40;omitted at root&#41;                                   
<     // Step 9. Internal iterative deepening &#40;omitted at root&#41;                                                
<                                                                                                              
<     // Sort the moves before to &#40;re&#41;search                                                                   
<     rml.sort&#40;);                                                                                              
<                                                                                                              
<     // Step extra. Fail low loop &#40;ommited at MTD root search&#41;.                                               
<     &#123;                                                                                                        
<         // Step 10. Loop through all moves in the root move list                                             
<         for &#40;int i = 0; i <  rml.move_count&#40;) && !AbortSearch; i++)                                          
<         &#123;                                                                                                    
<             // This is used by time management                                                               
<             // Not much gain from finishing MTD iteration, better save time.                                 
<             FirstRootMove = true; // &#40;0 == i&#41;;                                                               
<                                                                                                              
<             // Save the current node count before the move is searched                                       
<             nodes = TM.nodes_searched&#40;);                                                                     
<                                                                                                              
<             // Reset beta cut-off counters                                                                   
<             TM.resetBetaCounters&#40;);                                                                          
<                                                                                                              
<             // Pick the next root move, and print the move and the move number to                            
<             // the standard output.                                                                          
<             move = ss&#91;0&#93;.currentMove = rml.get_move&#40;i&#41;;                                                      
<                                                                                                              
<             if &#40;current_search_time&#40;) >= 1000&#41;                                                               
<                 cout << "info currmove " << move                                                             
<                      << " currmovenumber " << i + 1 << endl;                                                 
<                                                                                                              
<             moveIsCheck = pos.move_is_check&#40;move&#41;;                                                           
<             captureOrPromotion = pos.move_is_capture_or_promotion&#40;move&#41;;                                     
<                                                                                                              
<             // Step 11. Decide the new search depth                                                          
<             depth = &#40;Iteration - 2&#41; * OnePly + InitialDepth;                                                 
<             ext = extension&#40;pos, move, true, captureOrPromotion, moveIsCheck, false, false, &dangerous&#41;;     
<             newDepth = depth + ext;                                                                          
<                                                                                                              
<             // Step 12. Futility pruning &#40;omitted at root&#41;                                                   
<                                                                                                              
<             bound = - VALUE_INFINITE;                                                                        
<                                                                                                              
<             // Step extra. Fail high loop &#40;omitted at MTD root search&#41;.                                      
<             &#123;                                                                                                
<                 // Step 13. Make the move                                                                    
<                 pos.do_move&#40;move, st, ci, moveIsCheck&#41;;                                                      
<                                                                                                              
<                 // Step extra. pv search &#40;omitted at MTD root search&#41;.                                       
<                 // But we treat the first move the special way, anyway.                                      
<                 if &#40;1 > i&#41;                                                                                   
<                 &#123; // First move.                                                                             
<                                                                                                              
<                     // Full depth PV search, done on first move or after a fail high                         
<                     // In MTD, it is full depth, but not pv.                                                 
<                                                                                                              
<                     // We want gamma to be the lower bound here, so upper bound is gamma - 1.                
<                     bound = -search&#40;pos, ss, -&#40;gamma - 1&#41;, newDepth, 1, true, 0&#41;;                            
<                                                                                                              
<                 &#125; else &#123; // Non-first move.                                                                  
<                                                                                                              
<                     // Step 14. Reduced search                                                               
<                     // if the move fails high will be re-searched at full depth                              
<                     bool doFullDepthSearch = true;                                                           
<                                                                                                              
<                     if (    depth >= 3 * OnePly                                                              
<                         && !dangerous                                                                        
<                         && !captureOrPromotion                                                               
<                         && !move_is_castle&#40;move&#41;)                                                            
<                     &#123;                                                                                        
<                         ss&#91;0&#93;.reduction = pv_reduction&#40;depth, i - MultiPV + 2&#41;;                              
<                         if &#40;ss&#91;0&#93;.reduction&#41;                                                                 
<                         &#123;                                                                                    
<                             // Reduced depth non-pv search using gamma as upperbound                         
<                             bound = -search&#40;pos, ss, -gamma, newDepth-ss&#91;0&#93;.reduction, 1, true, 0&#41;;          
<                             doFullDepthSearch = &#40;bound > gamma&#41;;                                             
<                         &#125;                                                                                    
<                     &#125;                                                                                        
<                                                                                                              
<                     // Step 15. Full depth search                                                            
<                     if &#40;doFullDepthSearch&#41;                                                                   
<                     &#123;                                                                                        
<                         // Full depth non-pv search using gamma as upperbound                                
<                         ss&#91;0&#93;.reduction = Depth&#40;0&#41;;                                                          
<                         bound = -search&#40;pos, ss, -gamma, newDepth, 1, true, 0&#41;;                              
<                     &#125;                                                                                        
<                 &#125; // End of non-first move case.                                                             
<                                                                                                              
<                 // Step 16. Undo move                                                                        
<                 pos.undo_move&#40;move&#41;;                                                                         
<             &#125; // End of fail high loop would be here.                                                        
<                                                                                                              
<             // Finished searching the move. If AbortSearch is true, the search                               
<             // was aborted because the user interrupted the search or because we                             
<             // ran out of time. In this case, the return value of the search cannot                          
<             // be trusted, and we break out of the loop without updating the best                            
<             // move and/or PV.                                                                               
<             if &#40;AbortSearch&#41;                                                                                 
<                 break;                                                                                       
<                                                                                                              
<             // Remember beta-cutoff and searched nodes counts for this move. The                             
<             // info is used to sort the root moves for the next iteration.                                   
<             int64_t our, their;                                                                              
<             TM.get_beta_counters&#40;pos.side_to_move&#40;), our, their&#41;;                                            
<             rml.set_beta_counters&#40;i, our, their&#41;;                                                            
<             rml.set_move_nodes&#40;i, TM.nodes_searched&#40;) - nodes&#41;;                                              
<                                                                                                              
<             assert&#40;bound >= -VALUE_INFINITE && bound <= VALUE_INFINITE&#41;;                                     
<                                                                                                              
<             // Step 17. Check for new best move                                                              
<             if &#40;bound <= gamma && i >= 1&#41; // MNS                                                             
<                 rml.set_move_score&#40;i, -VALUE_INFINITE&#41;;                                                      
<             else                                                                                             
<             &#123;                                                                                                
<                 // PV move or new best move!                                                                 
<                 // That means just bound > gamma or 0 == i                                                   
<                 // It is just a candidate for new best move OR the first move. We need if.                   
<                 if &#40;0 == i&#41;                                                                                  
<                 &#123; // The first move.                                                                         
<                                                                                                              
<                     // Now, if the first move have failed low, &#40;bound < gamma&#41; we anticipate gamma will change.
<                     // So we happily leave the work of finding new gamma to PVS.                               
<                     if &#40;bound < gamma&#41;                                                                         
<                     &#123;                                                                                          
<                         // We are failing low, which is analogous to AspirationFailLow of PVS.                 
<                         AspirationFailLow = true;                                                              
<                         if &#40;AspirationFailLow && StopOnPonderhit&#41;                                              
<                             StopOnPonderhit = false;                                                           
<
<                         // Update PV, but not score, to not change the move order.
<                         update_pv&#40;ss, 0&#41;;
<                         TT.extract_pv&#40;pos, ss&#91;0&#93;.pv, PLY_MAX&#41;;
<                         rml.set_move_pv&#40;i, ss&#91;0&#93;.pv&#41;;
<
<                         // Print information to the standard output
<                         print_pv_info&#40;pos, ss, gamma - 1, gamma, bound&#41;;
<
<                         // We to not touch the aspiration window.
<                         return root_search_pv&#40;pos, ss, rml, alphaPtr, betaPtr&#41;;
<                     &#125;
<
<                 &#125; else &#123; // Non-first candidate move.
<
<                     // We are failing high on a non-first move.
<                     // If we shift gamma, the first move might fail low, so it is like AspirationFailLow again.
<                     AspirationFailLow = true;
<                     if &#40;AspirationFailLow && StopOnPonderhit&#41;
<                         StopOnPonderhit = false;
<
<                     // We shift the aspiration window, so AspirationFailLow would stay true, if the first move does not improve.
<                     *betaPtr = Min&#40;bound - 1 + 2 * AspirationDelta, VALUE_INFINITE&#41;;
<                     *alphaPtr = bound - 1;
<
<                     // Update scores
<                     rml.set_move_score&#40;0, bound + 1&#41;; // So that the first move will stay first after sort.
<                     rml.set_move_score&#40;i, bound&#41;; // And the candidate comes second, with a fail high score.
<
<                     // Update PV, but not score, to not change the move order.
<                     update_pv&#40;ss, 0&#41;;
<                     TT.extract_pv&#40;pos, ss&#91;0&#93;.pv, PLY_MAX&#41;;
<                     rml.set_move_pv&#40;i, ss&#91;0&#93;.pv&#41;;
<
<                     // Print information to the standard output
<                     print_pv_info&#40;pos, ss, gamma - 1, gamma, bound&#41;;
<
<                     return root_search_pv&#40;pos, ss, rml, alphaPtr, betaPtr&#41;;
<                 &#125;
<             &#125; // PV move or new best move
<
<         &#125; // End of root move list.
<
<     &#125; // End of fail low loop would be here.
<
<     // So we have finally finished &#40;or aborted&#41; the MTD iteration. Let us sort and return.
<     rml.sort&#40;); // Perhaps redundant.
<     return gamma;
<
<   &#125; // End of root_search&#40;).
<
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: MTD experiment with stockfish 1.7.1

Post by mcostalba »

QED wrote: I did not make any other optimizations, and I nearly did not test it at all. Now the search somewhat works, and does not look very ugly when watching it, but I have no idea if it is better or worse then pure PVS.
Thanks ! I very welcome people sending patches with new ideas.

Unfortunatly I am not going to test it for you, not because of lack of time but because I think that if the patch author is not enough motivated to test his patch (is a 2 days job, made by your computer BTW) I _should_ not be either. ;-)

In case you are willing to test your work, please drop me a pm.