X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fsearch.cpp;h=a13453cf425e7893cad2b0c818cb75816a7cf7c4;hp=09b483cfc4143ec056b1ced25620245f9a925672;hb=dba072c449061654201c606dc198d0dcae50e567;hpb=807844eab194adc7c5378575f81d28ce9ca66287 diff --git a/src/search.cpp b/src/search.cpp index 09b483cf..a13453cf 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -326,6 +326,13 @@ namespace { //// Functions //// +//FIXME: HACK +static double lnArray[512]; + +inline double ln(int i) +{ + return lnArray[i]; +} /// perft() is our utility to verify move generation is bug free. All the legal /// moves up to given depth are generated and counted and the sum returned. @@ -562,8 +569,14 @@ bool think(const Position& pos, bool infinite, bool ponder, int side_to_move, /// and initializes the split point stack and the global locks and condition /// objects. +#include //FIXME: HACK + void init_threads() { + // FIXME: HACK!! + for (int i = 0; i < 512; i++) + lnArray[i] = log(double(i)); + volatile int i; #if !defined(_MSC_VER) @@ -899,7 +912,7 @@ namespace { int64_t nodes; Move move; StateInfo st; - Depth ext, newDepth; + Depth depth, ext, newDepth; RootMoveNumber = i + 1; FailHigh = false; @@ -922,8 +935,9 @@ namespace { bool moveIsCheck = pos.move_is_check(move); bool captureOrPromotion = pos.move_is_capture_or_promotion(move); bool dangerous; + depth = (Iteration - 2) * OnePly + InitialDepth; ext = extension(pos, move, true, captureOrPromotion, moveIsCheck, false, false, &dangerous); - newDepth = (Iteration - 2) * OnePly + ext + InitialDepth; + newDepth = depth + ext; // Make the move, and search it pos.do_move(move, st, ci, moveIsCheck); @@ -950,14 +964,19 @@ namespace { { // Try to reduce non-pv search depth by one ply if move seems not problematic, // if the move fails high will be re-searched at full depth. - if ( newDepth >= 3*OnePly - && i >= MultiPV + LMRPVMoves + if ( depth >= 3*OnePly // FIXME was newDepth && !dangerous && !captureOrPromotion && !move_is_castle(move)) { - ss[0].reduction = OnePly; - value = -search(pos, ss, -alpha, newDepth-OnePly, 1, true, 0); + double red = 0.5 + ln(RootMoveNumber - MultiPV + 1) * ln(depth / 2) / 6.0; + if (red >= 1.0) + { + ss[0].reduction = Depth(int(floor(red * int(OnePly)))); + value = -search(pos, ss, -alpha, newDepth-ss[0].reduction, 1, true, 0); + } + else + value = alpha + 1; // Just to trigger next condition } else value = alpha + 1; // Just to trigger next condition @@ -1202,14 +1221,19 @@ namespace { // Try to reduce non-pv search depth by one ply if move seems not problematic, // if the move fails high will be re-searched at full depth. if ( depth >= 3*OnePly - && moveCount >= LMRPVMoves && !dangerous && !captureOrPromotion && !move_is_castle(move) && !move_is_killer(move, ss[ply])) { - ss[ply].reduction = OnePly; - value = -search(pos, ss, -alpha, newDepth-OnePly, ply+1, true, threadID); + double red = 0.5 + ln(moveCount) * ln(depth / 2) / 6.0; + if (red >= 1.0) + { + ss[ply].reduction = Depth(int(floor(red * int(OnePly)))); + value = -search(pos, ss, -alpha, newDepth-ss[ply].reduction, ply+1, true, threadID); + } + else + value = alpha + 1; // Just to trigger next condition } else value = alpha + 1; // Just to trigger next condition @@ -1317,7 +1341,7 @@ namespace { const TTEntry* tte; Move ttMove, move; Depth ext, newDepth; - Value approximateEval, nullValue, value, futilityValue, futilityValueScaled; + Value staticValue, nullValue, value, futilityValue, futilityValueScaled; bool isCheck, useFutilityPruning, singleEvasion, moveIsCheck, captureOrPromotion, dangerous; bool mateThreat = false; int moveCount = 0; @@ -1361,8 +1385,24 @@ namespace { return value_from_tt(tte->value(), ply); } - approximateEval = refine_eval(tte, quick_evaluate(pos), ply); isCheck = pos.is_check(); + ei.futilityMargin = Value(0); // Manually initialize futilityMargin + + // Evaluate the position statically + if (isCheck) + staticValue = quick_evaluate(pos); + else if (tte && (tte->type() & VALUE_TYPE_EVAL)) + staticValue = value_from_tt(tte->value(), ply); + else + staticValue = evaluate(pos, ei, threadID); + + // Calculate depth dependant futility pruning parameters + const int FutilityMoveCountMargin = 3 + (1 << (3 * int(depth) / 8)); + const int FutilityValueMargin = 112 * bitScanReverse32(int(depth) * int(depth) / 2); + + // Enhance position with TT value if possible + futilityValue = staticValue + FutilityValueMargin; + staticValue = refine_eval(tte, staticValue, ply); // Null move search if ( allowNullmove @@ -1370,7 +1410,7 @@ namespace { && !isCheck && !value_is_mate(beta) && ok_to_do_nullmove(pos) - && approximateEval >= beta - NullMoveMargin) + && staticValue >= beta - NullMoveMargin) { ss[ply].currentMove = MOVE_NULL; @@ -1380,7 +1420,7 @@ namespace { int R = 3 + (depth >= 5 * OnePly ? depth / 8 : 0); // Null move dynamic reduction based on value - if (approximateEval - beta > PawnValueMidgame) + if (staticValue - beta > PawnValueMidgame) R++; nullValue = -search(pos, ss, -(beta-1), depth-R*OnePly, ply+1, false, threadID); @@ -1416,7 +1456,7 @@ namespace { // Null move search not allowed, try razoring else if ( !value_is_mate(beta) && depth < RazorDepth - && approximateEval < beta - RazorApprMargins[int(depth) - 2] + && staticValue < beta - RazorApprMargins[int(depth) - 2] && ss[ply - 1].currentMove != MOVE_NULL && ttMove == MOVE_NONE && !pos.has_pawn_on_7th(pos.side_to_move())) @@ -1440,17 +1480,8 @@ namespace { // to search all moves. MovePicker mp = MovePicker(pos, ttMove, depth, H, &ss[ply]); CheckInfo ci(pos); - futilityValue = VALUE_NONE; useFutilityPruning = depth < SelectiveDepth && !isCheck; - // Calculate depth dependant futility pruning parameters - const int FutilityMoveCountMargin = 3 + (1 << (3 * int(depth) / 8)); - const int FutilityValueMargin = 112 * bitScanReverse32(int(depth) * int(depth) / 2); - - // Avoid calling evaluate() if we already have the score in TT - if (tte && (tte->type() & VALUE_TYPE_EVAL)) - futilityValue = value_from_tt(tte->value(), ply) + FutilityValueMargin; - // Loop through all legal moves until no moves remain or a beta cutoff occurs while ( bestValue < beta && (move = mp.get_next_move()) != MOVE_NONE @@ -1508,9 +1539,6 @@ namespace { continue; // Value based pruning - if (futilityValue == VALUE_NONE) - futilityValue = evaluate(pos, ei, threadID) + FutilityValueMargin; - futilityValueScaled = futilityValue - moveCount * IncrementalFutilityMargin; if (futilityValueScaled < beta) @@ -1527,14 +1555,20 @@ namespace { // Try to reduce non-pv search depth by one ply if move seems not problematic, // if the move fails high will be re-searched at full depth. if ( depth >= 3*OnePly - && moveCount >= LMRNonPVMoves && !dangerous && !captureOrPromotion && !move_is_castle(move) - && !move_is_killer(move, ss[ply])) + && !move_is_killer(move, ss[ply]) + /* && move != ttMove*/) { - ss[ply].reduction = OnePly; - value = -search(pos, ss, -(beta-1), newDepth-OnePly, ply+1, true, threadID); + double red = 0.5 + ln(moveCount) * ln(depth / 2) / 3.0; + if (red >= 1.0) + { + ss[ply].reduction = Depth(int(floor(red * int(OnePly)))); + value = -search(pos, ss, -(beta-1), newDepth-ss[ply].reduction, ply+1, true, threadID); + } + else + value = beta; // Just to trigger next condition } else value = beta; // Just to trigger next condition @@ -1868,13 +1902,18 @@ namespace { // Try to reduce non-pv search depth by one ply if move seems not problematic, // if the move fails high will be re-searched at full depth. if ( !dangerous - && moveCount >= LMRNonPVMoves && !captureOrPromotion && !move_is_castle(move) && !move_is_killer(move, ss[sp->ply])) { - ss[sp->ply].reduction = OnePly; - value = -search(pos, ss, -(sp->beta-1), newDepth - OnePly, sp->ply+1, true, threadID); + double red = 0.5 + ln(moveCount) * ln(sp->depth / 2) / 3.0; + if (red >= 1.0) + { + ss[sp->ply].reduction = Depth(int(floor(red * int(OnePly)))); + value = -search(pos, ss, -(sp->beta-1), newDepth-ss[sp->ply].reduction, sp->ply+1, true, threadID); + } + else + value = sp->beta; // Just to trigger next condition } else value = sp->beta; // Just to trigger next condition @@ -1974,13 +2013,18 @@ namespace { // Try to reduce non-pv search depth by one ply if move seems not problematic, // if the move fails high will be re-searched at full depth. if ( !dangerous - && moveCount >= LMRPVMoves && !captureOrPromotion && !move_is_castle(move) && !move_is_killer(move, ss[sp->ply])) { - ss[sp->ply].reduction = OnePly; - value = -search(pos, ss, -sp->alpha, newDepth - OnePly, sp->ply+1, true, threadID); + double red = 0.5 + ln(moveCount) * ln(sp->depth / 2) / 6.0; + if (red >= 1.0) + { + ss[sp->ply].reduction = Depth(int(floor(red * int(OnePly)))); + value = -search(pos, ss, -sp->alpha, newDepth-ss[sp->ply].reduction, sp->ply+1, true, threadID); + } + else + value = sp->alpha + 1; // Just to trigger next condition } else value = sp->alpha + 1; // Just to trigger next condition