- // Start with a small aspiration window and, in case of fail high/low,
- // research with bigger window until not failing high/low anymore.
- while (true)
- {
- bestValue = search<Root>(pos, ss, alpha, beta, depth * ONE_PLY);
-
- // Bring to front the best move. It is critical that sorting is
- // done with a stable algorithm because all the values but the first
- // and eventually the new best one are set to -VALUE_INFINITE and
- // we want to keep the same order for all the moves but the new
- // PV that goes to the front. Note that in case of MultiPV search
- // the already searched PV lines are preserved.
- std::stable_sort(RootMoves.begin() + PVIdx, RootMoves.end());
-
- // Write PV back to transposition table in case the relevant
- // entries have been overwritten during the search.
- for (size_t i = 0; i <= PVIdx; i++)
- RootMoves[i].insert_pv_in_tt(pos);
-
- // If search has been stopped return immediately. Sorting and
- // writing PV back to TT is safe becuase RootMoves is still
- // valid, although refers to previous iteration.
- if (Signals.stop)
- return;
-
- // In case of failing high/low increase aspiration window and
- // research, otherwise exit the loop.
- if (bestValue > alpha && bestValue < beta)
- break;
-
- // Give some update (without cluttering the UI) before to research
- if (Time::now() - SearchTime > 3000)
- sync_cout << uci_pv(pos, depth, alpha, beta) << sync_endl;
-
- if (abs(bestValue) >= VALUE_KNOWN_WIN)
- {
- alpha = -VALUE_INFINITE;
- beta = VALUE_INFINITE;
- }
- else if (bestValue >= beta)
- {
- beta += delta;
- delta += delta / 2;
- }
- else
- {
- Signals.failedLowAtRoot = true;
- Signals.stopOnPonderhit = false;
-
- alpha -= delta;
- delta += delta / 2;
- }
-
- assert(alpha >= -VALUE_INFINITE && beta <= VALUE_INFINITE);
- }