X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fsearch.cpp;h=ee7d906e61698010afd4b7e64336c44505493b2e;hp=71da3ec18a985f04b340b544959c396846184f6d;hb=d8268024a9663727d1f183bf88639475eabca9d4;hpb=295352d04ad932743941b043ddd90d556bedd89d diff --git a/src/search.cpp b/src/search.cpp index 71da3ec1..ee7d906e 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -22,7 +22,6 @@ //// #include -#include #include #include #include @@ -70,16 +69,16 @@ namespace { public: RootMoveList(Position &pos, Move searchMoves[]); - Move get_move(int moveNum) const; - Value get_move_score(int moveNum) const; - void set_move_score(int moveNum, Value score); - void set_move_nodes(int moveNum, int64_t nodes); + inline Move get_move(int moveNum) const; + inline Value get_move_score(int moveNum) const; + inline void set_move_score(int moveNum, Value score); + inline void set_move_nodes(int moveNum, int64_t nodes); void set_move_pv(int moveNum, const Move pv[]); - Move get_move_pv(int moveNum, int i) const; - int64_t get_move_cumulative_nodes(int moveNum) const; - int move_count() const; + inline Move get_move_pv(int moveNum, int i) const; + inline int64_t get_move_cumulative_nodes(int moveNum) const; + inline int move_count() const; Move scan_for_easy_move() const; - void sort(); + inline void sort(); void sort_multipv(int n); private: @@ -153,6 +152,12 @@ namespace { Depth RazorDepth = 4*OnePly; Value RazorMargin = Value(0x300); + // Last seconds noise filtering (LSN) + bool UseLSNFiltering = false; + bool looseOnTime = false; + int LSNTime = 4 * 1000; // In milliseconds + Value LSNValue = Value(0x200); + // Extensions. Array index 0 is used at non-PV nodes, index 1 at PV nodes. Depth CheckExtension[2] = {OnePly, OnePly}; Depth SingleReplyExtension[2] = {OnePly / 2, OnePly / 2}; @@ -181,7 +186,7 @@ namespace { // Time managment variables int SearchStartTime; int MaxNodes, MaxDepth; - int MaxSearchTime, AbsoluteMaxSearchTime, ExtraSearchTime; + int MaxSearchTime, AbsoluteMaxSearchTime, ExtraSearchTime, TimeAdvantage; Move BestRootMove, PonderMove, EasyMove; int RootMoveNumber; bool InfiniteSearch; @@ -221,7 +226,7 @@ namespace { /// Functions - void id_loop(const Position &pos, Move searchMoves[]); + Value id_loop(const Position &pos, Move searchMoves[]); Value root_search(Position &pos, SearchStack ss[], RootMoveList &rml); Value search_pv(Position &pos, SearchStack ss[], Value alpha, Value beta, Depth depth, int ply, int threadID); @@ -300,9 +305,9 @@ History H; // Should be made local? /// the program receives the UCI 'go' command. It initializes various /// search-related global variables, and calls root_search() -void think(const Position &pos, bool infinite, bool ponder, int time, - int increment, int movesToGo, int maxDepth, int maxNodes, - int maxTime, Move searchMoves[]) { +void think(const Position &pos, bool infinite, bool ponder, int side_to_move, + int time[], int increment[], int movesToGo, int maxDepth, + int maxNodes, int maxTime, Move searchMoves[]) { // Look for a book move: if(!infinite && !ponder && get_option_value_bool("OwnBook")) { @@ -395,6 +400,10 @@ void think(const Position &pos, bool infinite, bool ponder, int time, RazorDepth = (get_option_value_int("Maximum Razoring Depth") + 1) * OnePly; RazorMargin = value_from_centipawns(get_option_value_int("Razoring Margin")); + UseLSNFiltering = get_option_value_bool("LSN filtering"); + LSNTime = get_option_value_int("LSN Time Margin (sec)") * 1000; + LSNValue = value_from_centipawns(get_option_value_int("LSN Value Margin")); + MinimumSplitDepth = get_option_value_int("Minimum Split Depth") * OnePly; MaxThreadsPerSplitPoint = get_option_value_int("Maximum Number of Threads per Split Point"); @@ -407,14 +416,6 @@ void think(const Position &pos, bool infinite, bool ponder, int time, init_eval(ActiveThreads); } - // Write information to search log file: - if(UseLogFile) { - LogFile << "Searching: " << pos.to_fen() << '\n'; - LogFile << "infinite: " << infinite << " ponder: " << ponder - << " time: " << time << " increment: " << increment - << " moves to go: " << movesToGo << '\n'; - } - // Wake up sleeping threads: wake_sleeping_threads(); @@ -422,24 +423,30 @@ void think(const Position &pos, bool infinite, bool ponder, int time, assert(thread_is_available(i, 0)); // Set thinking time: + int myTime = time[side_to_move]; + int myIncrement = increment[side_to_move]; + int oppTime = time[1 - side_to_move]; + + TimeAdvantage = myTime - oppTime; + if(!movesToGo) { // Sudden death time control if(increment) { - MaxSearchTime = time / 30 + increment; - AbsoluteMaxSearchTime = Max(time / 4, increment - 100); + MaxSearchTime = myTime / 30 + myIncrement; + AbsoluteMaxSearchTime = Max(myTime / 4, myIncrement - 100); } else { // Blitz game without increment - MaxSearchTime = time / 40; - AbsoluteMaxSearchTime = time / 8; + MaxSearchTime = myTime / 40; + AbsoluteMaxSearchTime = myTime / 8; } } else { // (x moves) / (y minutes) if(movesToGo == 1) { - MaxSearchTime = time / 2; - AbsoluteMaxSearchTime = Min(time / 2, time - 500); + MaxSearchTime = myTime / 2; + AbsoluteMaxSearchTime = Min(myTime / 2, myTime - 500); } else { - MaxSearchTime = time / Min(movesToGo, 20); - AbsoluteMaxSearchTime = Min((4 * time) / movesToGo, time / 3); + MaxSearchTime = myTime / Min(movesToGo, 20); + AbsoluteMaxSearchTime = Min((4 * myTime) / movesToGo, myTime / 3); } } if(PonderingEnabled) { @@ -460,9 +467,32 @@ void think(const Position &pos, bool infinite, bool ponder, int time, else NodesBetweenPolls = 30000; + + // Write information to search log file: + if(UseLogFile) { + LogFile << "Searching: " << pos.to_fen() << '\n'; + LogFile << "infinite: " << infinite << " ponder: " << ponder + << " time: " << myTime << " increment: " << myIncrement + << " moves to go: " << movesToGo << '\n'; + } + // We're ready to start thinking. Call the iterative deepening loop // function: - id_loop(pos, searchMoves); + if (!looseOnTime) + { + Value v = id_loop(pos, searchMoves); + looseOnTime = ( UseLSNFiltering + && myTime < LSNTime + && myIncrement == 0 + && v < -LSNValue); + } + else + { + looseOnTime = false; // reset for next match + while (SearchStartTime + myTime + 1000 > get_system_time()) + ; // wait here + id_loop(pos, searchMoves); // to fail gracefully + } if(UseLogFile) LogFile.close(); @@ -564,7 +594,7 @@ namespace { // been consumed, the user stops the search, or the maximum search depth is // reached. - void id_loop(const Position &pos, Move searchMoves[]) { + Value id_loop(const Position &pos, Move searchMoves[]) { Position p(pos); SearchStack ss[PLY_MAX_PLUS_2]; @@ -635,6 +665,10 @@ namespace { BestMoveChangesByIteration[Iteration] * (MaxSearchTime / 2) + BestMoveChangesByIteration[Iteration-1] * (MaxSearchTime / 3); + // If we need some more and we are in time advantage take it. + if (ExtraSearchTime > 0 && TimeAdvantage > 2 * MaxSearchTime) + ExtraSearchTime += MaxSearchTime / 2; + // Stop search if most of MaxSearchTime is consumed at the end of the // iteration. We probably don't have enough time to search the first // move at the next iteration anyway. @@ -684,6 +718,7 @@ namespace { LogFile << "Ponder move: " << move_to_san(p, ss[0].pv[1]) << '\n'; LogFile << std::endl; } + return rml.get_move_score(0); } @@ -1112,9 +1147,8 @@ namespace { } } // Null move search not allowed, try razoring - else if ( depth < RazorDepth - && approximateEval < beta - RazorMargin - && evaluate(pos, ei, threadID) < beta - RazorMargin) + else if ( (approximateEval < beta - RazorMargin && depth < RazorDepth) + ||(approximateEval < beta - PawnValueMidgame && depth <= OnePly)) { Value v = qsearch(pos, ss, beta-1, beta, Depth(0), ply, threadID); if (v < beta) @@ -2125,62 +2159,70 @@ namespace { // search. void poll() { - int t, data; - static int lastInfoTime; - t = current_search_time(); + static int lastInfoTime; + int t = current_search_time(); // Poll for input - data = Bioskey(); - if(data) { - char input[256]; - if(fgets(input, 255, stdin) == NULL) - strcpy(input, "quit\n"); - if(strncmp(input, "quit", 4) == 0) { - AbortSearch = true; - PonderSearch = false; - Quit = true; - } - else if(strncmp(input, "stop", 4) == 0) { - AbortSearch = true; - PonderSearch = false; - } - else if(strncmp(input, "ponderhit", 9) == 0) - ponderhit(); - } + if (Bioskey()) + { + // We are line oriented, don't read single chars + std::string command; + if (!std::getline(std::cin, command)) + command = "quit"; - // Print search information - if(t < 1000) - lastInfoTime = 0; - else if(lastInfoTime > t) - // HACK: Must be a new search where we searched less than - // NodesBetweenPolls nodes during the first second of search. - lastInfoTime = 0; - else if(t - lastInfoTime >= 1000) { - lastInfoTime = t; - lock_grab(&IOLock); - std::cout << "info nodes " << nodes_searched() << " nps " << nps() - << " time " << t << " hashfull " << TT.full() << std::endl; - lock_release(&IOLock); - if(ShowCurrentLine) - Threads[0].printCurrentLine = true; + if (command == "quit") + { + AbortSearch = true; + PonderSearch = false; + Quit = true; + } + else if(command == "stop") + { + AbortSearch = true; + PonderSearch = false; + } + else if(command == "ponderhit") + ponderhit(); } + // Print search information + if (t < 1000) + lastInfoTime = 0; + + else if (lastInfoTime > t) + // HACK: Must be a new search where we searched less than + // NodesBetweenPolls nodes during the first second of search. + lastInfoTime = 0; + else if (t - lastInfoTime >= 1000) + { + lastInfoTime = t; + lock_grab(&IOLock); + if (dbg_show_mean) + dbg_print_mean(); + + if (dbg_show_hit_rate) + dbg_print_hit_rate(); + + std::cout << "info nodes " << nodes_searched() << " nps " << nps() + << " time " << t << " hashfull " << TT.full() << std::endl; + lock_release(&IOLock); + if (ShowCurrentLine) + Threads[0].printCurrentLine = true; + } // Should we stop the search? - if(!PonderSearch && Iteration >= 2 && - (!InfiniteSearch && (t > AbsoluteMaxSearchTime || - (RootMoveNumber == 1 && - t > MaxSearchTime + ExtraSearchTime) || - (!FailHigh && !fail_high_ply_1() && !Problem && - t > 6*(MaxSearchTime + ExtraSearchTime))))) - AbortSearch = true; + if (PonderSearch) + return; - if(!PonderSearch && ExactMaxTime && t >= ExactMaxTime) - AbortSearch = true; + bool overTime = t > AbsoluteMaxSearchTime + || (RootMoveNumber == 1 && t > MaxSearchTime + ExtraSearchTime) + || ( !FailHigh && !fail_high_ply_1() && !Problem + && t > 6*(MaxSearchTime + ExtraSearchTime)); - if(!PonderSearch && Iteration >= 3 && MaxNodes - && nodes_searched() >= MaxNodes) - AbortSearch = true; + if ( (Iteration >= 2 && (!InfiniteSearch && overTime)) + || (ExactMaxTime && t >= ExactMaxTime) + || (Iteration >= 3 && MaxNodes && nodes_searched() >= MaxNodes)) + AbortSearch = true; }