- // Search to the current depth, rml is updated and sorted
- value = root_search(pos, ss, alpha, beta, depth, rml);
+ researchCountFL = researchCountFH = 0;
+
+ // We start with small aspiration window and in case of fail high/low, we
+ // research with bigger window until we are not failing high/low anymore.
+ while (true)
+ {
+ // Sort the moves before to (re)search
+ rml.set_non_pv_scores(pos, rml[0].pv[0], ss);
+ rml.sort();
+
+ // Search to the current depth, rml is updated and sorted
+ value = root_search(pos, ss, alpha, beta, depth, rml);
+
+ // Sort the moves before to return
+ rml.sort();
+
+ // Write PV lines to transposition table, in case the relevant entries
+ // have been overwritten during the search.
+ for (int i = 0; i < Min(MultiPV, (int)rml.size()); i++)
+ rml[i].insert_pv_in_tt(pos);
+
+ if (StopRequest)
+ break;
+
+ assert(value >= alpha);
+
+ if (value >= beta)
+ {
+ // Prepare for a research after a fail high, each time with a wider window
+ beta = Min(beta + AspirationDelta * (1 << researchCountFH), VALUE_INFINITE);
+ researchCountFH++;
+ }
+ else if (value <= alpha)
+ {
+ AspirationFailLow = true;
+ StopOnPonderhit = false;
+
+ // Prepare for a research after a fail low, each time with a wider window
+ alpha = Max(alpha - AspirationDelta * (1 << researchCountFL), -VALUE_INFINITE);
+ researchCountFL++;
+ }
+ else
+ break;
+ }