- init_ss_array(ss, PLY_MAX_PLUS_2);
- pv[0] = pv[1] = MOVE_NONE;
- ValueByIteration[1] = rml.move_score(0);
- Iteration = 1;
-
- // Is one move significantly better than others after initial scoring ?
- if ( rml.move_count() == 1
- || rml.move_score(0) > rml.move_score(1) + EasyMoveMargin)
- EasyMove = rml.move(0);
-
- // Iterative deepening loop
- while (Iteration < PLY_MAX)
- {
- // Initialize iteration
- Iteration++;
- BestMoveChangesByIteration[Iteration] = 0;
-
- cout << "info depth " << Iteration << endl;
-
- // Calculate dynamic aspiration window based on previous iterations
- if (MultiPV == 1 && Iteration >= 6 && abs(ValueByIteration[Iteration - 1]) < VALUE_KNOWN_WIN)
- {
- int prevDelta1 = ValueByIteration[Iteration - 1] - ValueByIteration[Iteration - 2];
- int prevDelta2 = ValueByIteration[Iteration - 2] - ValueByIteration[Iteration - 3];
-
- AspirationDelta = Max(abs(prevDelta1) + abs(prevDelta2) / 2, 16);
- AspirationDelta = (AspirationDelta + 7) / 8 * 8; // Round to match grainSize
-
- alpha = Max(ValueByIteration[Iteration - 1] - AspirationDelta, -VALUE_INFINITE);
- beta = Min(ValueByIteration[Iteration - 1] + AspirationDelta, VALUE_INFINITE);
- }
-
- // Search to the current depth, rml is updated and sorted, alpha and beta could change
- value = root_search(pos, ss, pv, rml, &alpha, &beta);
-
- // Write PV to transposition table, in case the relevant entries have
- // been overwritten during the search.
- insert_pv_in_tt(pos, pv);
-
- if (AbortSearch)
- break; // Value cannot be trusted. Break out immediately!
-
- //Save info about search result
- ValueByIteration[Iteration] = value;
-
- // Drop the easy move if differs from the new best move
- if (pv[0] != EasyMove)
- EasyMove = MOVE_NONE;
-
- if (UseTimeManagement)
- {
- // Time to stop?
- bool stopSearch = false;
-
- // Stop search early if there is only a single legal move,
- // we search up to Iteration 6 anyway to get a proper score.
- if (Iteration >= 6 && rml.move_count() == 1)
- stopSearch = true;
-
- // Stop search early when the last two iterations returned a mate score
- if ( Iteration >= 6
- && abs(ValueByIteration[Iteration]) >= abs(VALUE_MATE) - 100
- && abs(ValueByIteration[Iteration-1]) >= abs(VALUE_MATE) - 100)
- stopSearch = true;
-
- // Stop search early if one move seems to be much better than the others
- if ( Iteration >= 8
- && EasyMove == pv[0]
- && ( ( rml.move_nodes(0) > (pos.nodes_searched() * 85) / 100
- && current_search_time() > TimeMgr.available_time() / 16)
- ||( rml.move_nodes(0) > (pos.nodes_searched() * 98) / 100
- && current_search_time() > TimeMgr.available_time() / 32)))
- stopSearch = true;