X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fsearch.cpp;h=bce794ca1a30722e0942532f5bdb6e25408a51a3;hp=43b850bd557efbe219f1972744a8f0ea5772af02;hb=0a687b2cf06fda0cc8e103c8cf9c3bbd564b02d6;hpb=a128faf0b0b6bd93dafbf2b6f6890dbc8d1e8d74 diff --git a/src/search.cpp b/src/search.cpp index 43b850bd..bce794ca 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -89,7 +89,7 @@ namespace { void idle_loop(int threadID, SplitPoint* sp); template - void split(const Position& pos, SearchStack* ss, Value* alpha, const Value beta, Value* bestValue, + void split(const Position& pos, SearchStack* ss, int ply, Value* alpha, const Value beta, Value* bestValue, Depth depth, bool mateThreat, int* moveCount, MovePicker* mp, bool pvNode); private: @@ -235,8 +235,8 @@ namespace { const Value EasyMoveMargin = Value(0x200); // Last seconds noise filtering (LSN) - const bool UseLSNFiltering = true; - const int LSNTime = 4000; // In milliseconds + const bool UseLSNFiltering = false; + const int LSNTime = 100; // In milliseconds const Value LSNValue = value_from_centipawns(200); bool loseOnTime = false; @@ -285,10 +285,10 @@ namespace { Value root_search(Position& pos, SearchStack* ss, RootMoveList& rml, Value* alphaPtr, Value* betaPtr); template - Value search(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth); + Value search(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, int ply); template - Value qsearch(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth); + Value qsearch(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, int ply); template void sp_search(SplitPoint* sp, int threadID); @@ -356,7 +356,7 @@ void init_search() { // Init futility margins array for (d = 0; d < 16; d++) for (mc = 0; mc < 64; mc++) - FutilityMarginsMatrix[d][mc] = 112 * int(log(double(d * d) / 2) / log(2.0) + 1) - 8 * mc + 45; + FutilityMarginsMatrix[d][mc] = 112 * int(log(double(d * d) / 2) / log(2.0) + 1.001) - 8 * mc + 45; // Init futility move count array for (d = 0; d < 32; d++) @@ -364,16 +364,17 @@ void init_search() { } -// SearchStack::init() initializes a search stack. Used at the beginning of a -// new search from the root. +// SearchStack::init() initializes a search stack entry. +// Called at the beginning of search() when starting to examine a new node. void SearchStack::init() { - pv[0] = pv[1] = MOVE_NONE; + pv[0] = pv[1] = bestMove = MOVE_NONE; currentMove = threatMove = MOVE_NONE; reduction = Depth(0); eval = VALUE_NONE; } +// SearchStack::initKillers() initializes killers for a search stack entry void SearchStack::initKillers() { mateKiller = MOVE_NONE; @@ -635,7 +636,6 @@ namespace { H.clear(); init_ss_array(ss, PLY_MAX_PLUS_2); ValueByIteration[1] = rml.get_move_score(0); - p.reset_ply(); Iteration = 1; // Is one move significantly better than others after initial scoring ? @@ -876,7 +876,7 @@ namespace { alpha = -VALUE_INFINITE; // Full depth PV search, done on first move or after a fail high - value = -search(pos, ss+1, -beta, -alpha, newDepth); + value = -search(pos, ss+1, -beta, -alpha, newDepth, 1); } else { @@ -895,7 +895,7 @@ namespace { assert(newDepth-ss->reduction >= OnePly); // Reduced depth non-pv search using alpha as upperbound - value = -search(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction); + value = -search(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction, 1); doFullDepthSearch = (value > alpha); } @@ -907,7 +907,7 @@ namespace { assert(newDepth - OnePly >= OnePly); ss->reduction = OnePly; - value = -search(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction); + value = -search(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction, 1); doFullDepthSearch = (value > alpha); } ss->reduction = Depth(0); // Restore original reduction @@ -917,12 +917,12 @@ namespace { if (doFullDepthSearch) { // Full depth non-pv search using alpha as upperbound - value = -search(pos, ss+1, -(alpha+1), -alpha, newDepth); + value = -search(pos, ss+1, -(alpha+1), -alpha, newDepth, 1); // If we are above alpha then research at same depth but as PV // to get a correct score or eventually a fail high above beta. if (value > alpha) - value = -search(pos, ss+1, -beta, -alpha, newDepth); + value = -search(pos, ss+1, -beta, -alpha, newDepth, 1); } } @@ -1045,12 +1045,12 @@ namespace { // search<>() is the main search function for both PV and non-PV nodes template - Value search(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth) { + Value search(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, int ply) { assert(alpha >= -VALUE_INFINITE && alpha <= VALUE_INFINITE); assert(beta > alpha && beta <= VALUE_INFINITE); assert(PvNode || alpha == beta - 1); - assert(pos.ply() > 0 && pos.ply() < PLY_MAX); + assert(ply > 0 && ply < PLY_MAX); assert(pos.thread() >= 0 && pos.thread() < TM.active_threads()); Move movesSearched[256]; @@ -1066,7 +1066,6 @@ namespace { bool mateThreat = false; int moveCount = 0; int threadID = pos.thread(); - int ply = pos.ply(); refinedValue = bestValue = value = -VALUE_INFINITE; oldAlpha = alpha; @@ -1153,7 +1152,7 @@ namespace { TT.store(posKey, ss->eval, VALUE_TYPE_EXACT, Depth(-127*OnePly), MOVE_NONE, ss->eval, ei.kingDanger[pos.side_to_move()]); Value rbeta = beta - razor_margin(depth); - Value v = qsearch(pos, ss, rbeta-1, rbeta, Depth(0)); + Value v = qsearch(pos, ss, rbeta-1, rbeta, Depth(0), ply); if (v < rbeta) // Logically we should return (v + razor_margin(depth)), but // surprisingly this did slightly weaker in tests. @@ -1196,8 +1195,8 @@ namespace { pos.do_null_move(st); (ss+1)->skipNullMove = true; - nullValue = depth-R*OnePly < OnePly ? -qsearch(pos, ss+1, -beta, -alpha, Depth(0)) - : - search(pos, ss+1, -beta, -alpha, depth-R*OnePly); + nullValue = depth-R*OnePly < OnePly ? -qsearch(pos, ss+1, -beta, -alpha, Depth(0), ply+1) + : - search(pos, ss+1, -beta, -alpha, depth-R*OnePly, ply+1); (ss+1)->skipNullMove = false; pos.undo_null_move(); @@ -1212,7 +1211,7 @@ namespace { return nullValue; ss->skipNullMove = true; - Value v = search(pos, ss, alpha, beta, depth-5*OnePly); + Value v = search(pos, ss, alpha, beta, depth-5*OnePly, ply); ss->skipNullMove = false; if (v >= beta) @@ -1239,16 +1238,16 @@ namespace { // Step 9. Internal iterative deepening if ( depth >= IIDDepth[PvNode] - && (ttMove == MOVE_NONE || (PvNode && tte->depth() <= depth - 4 * OnePly)) + && ttMove == MOVE_NONE && (PvNode || (!isCheck && ss->eval >= beta - IIDMargin))) { Depth d = (PvNode ? depth - 2 * OnePly : depth / 2); ss->skipNullMove = true; - search(pos, ss, alpha, beta, d); + search(pos, ss, alpha, beta, d, ply); ss->skipNullMove = false; - ttMove = ss->pv[0]; + ttMove = ss->bestMove; tte = TT.retrieve(posKey); } @@ -1297,10 +1296,9 @@ namespace { Value b = ttValue - SingularExtensionMargin; ss->excludedMove = move; ss->skipNullMove = true; - Value v = search(pos, ss, b - 1, b, depth / 2); + Value v = search(pos, ss, b - 1, b, depth / 2, ply); ss->skipNullMove = false; ss->excludedMove = MOVE_NONE; - if (v < ttValue - SingularExtensionMargin) ext = OnePly; } @@ -1346,8 +1344,8 @@ namespace { // Step extra. pv search (only in PV nodes) // The first move in list is the expected PV if (PvNode && moveCount == 1) - value = newDepth < OnePly ? -qsearch(pos, ss+1, -beta, -alpha, Depth(0)) - : - search(pos, ss+1, -beta, -alpha, newDepth); + value = newDepth < OnePly ? -qsearch(pos, ss+1, -beta, -alpha, Depth(0), ply+1) + : - search(pos, ss+1, -beta, -alpha, newDepth, ply+1); else { // Step 14. Reduced depth search @@ -1364,8 +1362,8 @@ namespace { if (ss->reduction) { Depth d = newDepth - ss->reduction; - value = d < OnePly ? -qsearch(pos, ss+1, -(alpha+1), -alpha, Depth(0)) - : - search(pos, ss+1, -(alpha+1), -alpha, d); + value = d < OnePly ? -qsearch(pos, ss+1, -(alpha+1), -alpha, Depth(0), ply+1) + : - search(pos, ss+1, -(alpha+1), -alpha, d, ply+1); doFullDepthSearch = (value > alpha); } @@ -1378,7 +1376,7 @@ namespace { assert(newDepth - OnePly >= OnePly); ss->reduction = OnePly; - value = -search(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction); + value = -search(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction, ply+1); doFullDepthSearch = (value > alpha); } ss->reduction = Depth(0); // Restore original reduction @@ -1387,15 +1385,15 @@ namespace { // Step 15. Full depth search if (doFullDepthSearch) { - value = newDepth < OnePly ? -qsearch(pos, ss+1, -(alpha+1), -alpha, Depth(0)) - : - search(pos, ss+1, -(alpha+1), -alpha, newDepth); + value = newDepth < OnePly ? -qsearch(pos, ss+1, -(alpha+1), -alpha, Depth(0), ply+1) + : - search(pos, ss+1, -(alpha+1), -alpha, newDepth, ply+1); // Step extra. pv search (only in PV nodes) // Search only for possible new PV nodes, if instead value >= beta then // parent node fails low with value <= alpha and tries another move. if (PvNode && value > alpha && value < beta) - value = newDepth < OnePly ? -qsearch(pos, ss+1, -beta, -alpha, Depth(0)) - : - search(pos, ss+1, -beta, -alpha, newDepth); + value = newDepth < OnePly ? -qsearch(pos, ss+1, -beta, -alpha, Depth(0), ply+1) + : - search(pos, ss+1, -beta, -alpha, newDepth, ply+1); } } @@ -1428,7 +1426,7 @@ namespace { && !AbortSearch && !TM.thread_should_stop(threadID) && Iteration <= 99) - TM.split(pos, ss, &alpha, beta, &bestValue, depth, + TM.split(pos, ss, ply, &alpha, beta, &bestValue, depth, mateThreat, &moveCount, &mp, PvNode); } @@ -1445,22 +1443,20 @@ namespace { if (AbortSearch || TM.thread_should_stop(threadID)) return bestValue; - if (bestValue <= oldAlpha) - TT.store(posKey, value_to_tt(bestValue, ply), VALUE_TYPE_UPPER, depth, MOVE_NONE, ss->eval, ei.kingDanger[pos.side_to_move()]); + ValueType f = (bestValue <= oldAlpha ? VALUE_TYPE_UPPER : bestValue >= beta ? VALUE_TYPE_LOWER : VALUE_TYPE_EXACT); + move = (bestValue <= oldAlpha ? MOVE_NONE : ss->bestMove); + TT.store(posKey, value_to_tt(bestValue, ply), f, depth, move, ss->eval, ei.kingDanger[pos.side_to_move()]); - else if (bestValue >= beta) + // Update killers and history only for non capture moves that fails high + if (bestValue >= beta) { TM.incrementBetaCounter(pos.side_to_move(), depth, threadID); - move = ss->pv[0]; - TT.store(posKey, value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, depth, move, ss->eval, ei.kingDanger[pos.side_to_move()]); if (!pos.move_is_capture_or_promotion(move)) { update_history(pos, move, depth, movesSearched, moveCount); update_killers(move, ss); } } - else - TT.store(posKey, value_to_tt(bestValue, ply), VALUE_TYPE_EXACT, depth, ss->pv[0], ss->eval, ei.kingDanger[pos.side_to_move()]); assert(bestValue > -VALUE_INFINITE && bestValue < VALUE_INFINITE); @@ -1473,13 +1469,13 @@ namespace { // less than OnePly). template - Value qsearch(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth) { + Value qsearch(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, int ply) { assert(alpha >= -VALUE_INFINITE && alpha <= VALUE_INFINITE); assert(beta >= -VALUE_INFINITE && beta <= VALUE_INFINITE); assert(PvNode || alpha == beta - 1); assert(depth <= 0); - assert(pos.ply() > 0 && pos.ply() < PLY_MAX); + assert(ply > 0 && ply < PLY_MAX); assert(pos.thread() >= 0 && pos.thread() < TM.active_threads()); EvalInfo ei; @@ -1489,10 +1485,9 @@ namespace { bool isCheck, deepChecks, enoughMaterial, moveIsCheck, evasionPrunable; const TTEntry* tte; Value oldAlpha = alpha; - int ply = pos.ply(); TM.incrementNodeCounter(pos.thread()); - ss->pv[0] = ss->pv[1] = ss->currentMove = MOVE_NONE; + ss->pv[0] = ss->pv[1] = ss->bestMove = ss->currentMove = MOVE_NONE; ss->eval = VALUE_NONE; // Check for an instant draw or maximum ply reached @@ -1607,7 +1602,7 @@ namespace { // Make and search the move pos.do_move(move, st, ci, moveIsCheck); - value = -qsearch(pos, ss+1, -beta, -alpha, depth-OnePly); + value = -qsearch(pos, ss+1, -beta, -alpha, depth-OnePly, ply+1); pos.undo_move(move); assert(value > -VALUE_INFINITE && value < VALUE_INFINITE); @@ -1631,19 +1626,13 @@ namespace { // Update transposition table Depth d = (depth == Depth(0) ? Depth(0) : Depth(-1)); - if (bestValue <= oldAlpha) - TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_UPPER, d, MOVE_NONE, ss->eval, ei.kingDanger[pos.side_to_move()]); - else if (bestValue >= beta) - { - move = ss->pv[0]; - TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, d, move, ss->eval, ei.kingDanger[pos.side_to_move()]); + ValueType f = (bestValue <= oldAlpha ? VALUE_TYPE_UPPER : bestValue >= beta ? VALUE_TYPE_LOWER : VALUE_TYPE_EXACT); + TT.store(pos.get_key(), value_to_tt(bestValue, ply), f, d, ss->bestMove, ss->eval, ei.kingDanger[pos.side_to_move()]); - // Update killers only for good checking moves - if (!pos.move_is_capture_or_promotion(move)) - update_killers(move, ss); - } - else - TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_EXACT, d, ss->pv[0], ss->eval, ei.kingDanger[pos.side_to_move()]); + // Update killers only for checking moves that fails high + if ( bestValue >= beta + && !pos.move_is_capture_or_promotion(ss->bestMove)) + update_killers(ss->bestMove, ss); assert(bestValue > -VALUE_INFINITE && bestValue < VALUE_INFINITE); @@ -1750,8 +1739,9 @@ namespace { { Value localAlpha = sp->alpha; Depth d = newDepth - ss->reduction; - value = d < OnePly ? -qsearch(pos, ss+1, -(localAlpha+1), -localAlpha, Depth(0)) - : - search(pos, ss+1, -(localAlpha+1), -localAlpha, d); + value = d < OnePly ? -qsearch(pos, ss+1, -(localAlpha+1), -localAlpha, Depth(0), sp->ply+1) + : - search(pos, ss+1, -(localAlpha+1), -localAlpha, d, sp->ply+1); + doFullDepthSearch = (value > localAlpha); } @@ -1764,7 +1754,7 @@ namespace { ss->reduction = OnePly; Value localAlpha = sp->alpha; - value = -search(pos, ss+1, -(localAlpha+1), -localAlpha, newDepth-ss->reduction); + value = -search(pos, ss+1, -(localAlpha+1), -localAlpha, newDepth-ss->reduction, sp->ply+1); doFullDepthSearch = (value > localAlpha); } ss->reduction = Depth(0); // Restore original reduction @@ -1774,15 +1764,15 @@ namespace { if (doFullDepthSearch) { Value localAlpha = sp->alpha; - value = newDepth < OnePly ? -qsearch(pos, ss+1, -(localAlpha+1), -localAlpha, Depth(0)) - : - search(pos, ss+1, -(localAlpha+1), -localAlpha, newDepth); + value = newDepth < OnePly ? -qsearch(pos, ss+1, -(localAlpha+1), -localAlpha, Depth(0), sp->ply+1) + : - search(pos, ss+1, -(localAlpha+1), -localAlpha, newDepth, sp->ply+1); // Step extra. pv search (only in PV nodes) // Search only for possible new PV nodes, if instead value >= beta then // parent node fails low with value <= alpha and tries another move. if (PvNode && value > localAlpha && value < sp->beta) - value = newDepth < OnePly ? -qsearch(pos, ss+1, -sp->beta, -sp->alpha, Depth(0)) - : - search(pos, ss+1, -sp->beta, -sp->alpha, newDepth); + value = newDepth < OnePly ? -qsearch(pos, ss+1, -sp->beta, -sp->alpha, Depth(0), sp->ply+1) + : - search(pos, ss+1, -sp->beta, -sp->alpha, newDepth, sp->ply+1); } // Step 16. Undo move @@ -1826,7 +1816,7 @@ namespace { Move* src = (ss+1)->pv; Move* dst = ss->pv; - *dst = ss->currentMove; + *dst = ss->bestMove = ss->currentMove; do *++dst = *src; @@ -1844,7 +1834,7 @@ namespace { Move* dst = ss->pv; Move* pdst = pss->pv; - *dst = *pdst = ss->currentMove; + *dst = *pdst = pss->bestMove = ss->bestMove = ss->currentMove; do *++dst = *++pdst = *src; @@ -1951,7 +1941,7 @@ namespace { if (*dangerous) { - if (moveIsCheck) + if (moveIsCheck && pos.see_sign(m) >= 0) result += CheckExtension[PvNode]; if (singleEvasion) @@ -2627,10 +2617,11 @@ namespace { // split() returns. template - void ThreadsManager::split(const Position& p, SearchStack* ss, Value* alpha, const Value beta, - Value* bestValue, Depth depth, bool mateThreat, int* moveCount, - MovePicker* mp, bool pvNode) { + void ThreadsManager::split(const Position& p, SearchStack* ss, int ply, Value* alpha, + const Value beta, Value* bestValue, Depth depth, bool mateThreat, + int* moveCount, MovePicker* mp, bool pvNode) { assert(p.is_ok()); + assert(ply > 0 && ply < PLY_MAX); assert(*bestValue >= -VALUE_INFINITE); assert(*bestValue <= *alpha); assert(*alpha < beta); @@ -2658,6 +2649,7 @@ namespace { // Initialize the split point object splitPoint->parent = threads[master].splitPoint; splitPoint->stopRequest = false; + splitPoint->ply = ply; splitPoint->depth = depth; splitPoint->mateThreat = mateThreat; splitPoint->alpha = *alpha; @@ -2792,7 +2784,7 @@ namespace { init_ss_array(ss, PLY_MAX_PLUS_2); pos.do_move(cur->move, st); moves[count].move = cur->move; - moves[count].score = -qsearch(pos, ss+1, -VALUE_INFINITE, VALUE_INFINITE, Depth(0)); + moves[count].score = -qsearch(pos, ss+1, -VALUE_INFINITE, VALUE_INFINITE, Depth(0), 1); moves[count].pv[0] = cur->move; moves[count].pv[1] = MOVE_NONE; pos.undo_move(cur->move);