X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fsearch.cpp;h=1660fcb151e573b5fe5f9ea6f0360e2be4c3e9c4;hp=c393b75b5e4a4ba75998b96c85c5b8316ca0d2fd;hb=b27e237b04ad523d3492669eba6e00380df77a14;hpb=d372f2e39a65e2b14fc0335841e652a9d208d3ec diff --git a/src/search.cpp b/src/search.cpp index c393b75b..1660fcb1 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -243,7 +243,7 @@ namespace { RootMoveList Rml; // MultiPV mode - int MultiPV; + int MultiPV, UCIMultiPV; // Time management variables int SearchStartTime, MaxNodes, MaxDepth, ExactMaxTime; @@ -255,6 +255,10 @@ namespace { bool UseLogFile; std::ofstream LogFile; + // Skill level adjustment + int SkillLevel; + RKISS RK; + // Multi-threads manager object ThreadsManager ThreadsMgr; @@ -289,14 +293,12 @@ namespace { bool check_is_dangerous(Position &pos, Move move, Value futilityBase, Value beta, Value *bValue); bool connected_moves(const Position& pos, Move m1, Move m2); - bool value_is_mate(Value value); Value value_to_tt(Value v, int ply); Value value_from_tt(Value v, int ply); bool ok_to_use_TT(const TTEntry* tte, Depth depth, Value beta, int ply); bool connected_threat(const Position& pos, Move m, Move threat); Value refine_eval(const TTEntry* tte, Value defaultEval, int ply); void update_history(const Position& pos, Move move, Depth depth, Move movesSearched[], int moveCount); - void update_killers(Move m, Move killers[]); void update_gains(const Position& pos, Move move, Value before, Value after); int current_search_time(); @@ -503,11 +505,16 @@ bool think(Position& pos, bool infinite, bool ponder, int time[], int increment[ PawnEndgameExtension[0] = Options["Pawn Endgame Extension (non-PV nodes)"].value(); MateThreatExtension[1] = Options["Mate Threat Extension (PV nodes)"].value(); MateThreatExtension[0] = Options["Mate Threat Extension (non-PV nodes)"].value(); - MultiPV = Options["MultiPV"].value(); + UCIMultiPV = Options["MultiPV"].value(); + SkillLevel = Options["Skill level"].value(); UseLogFile = Options["Use Search Log"].value(); read_evaluation_uci_options(pos.side_to_move()); + // Do we have to play with skill handicap? In this case enable MultiPV that + // we will use behind the scenes to retrieve a set of possible moves. + MultiPV = (SkillLevel < 20 ? Max(UCIMultiPV, 4) : UCIMultiPV); + // Set the number of active threads ThreadsMgr.read_uci_options(); init_eval(ThreadsMgr.active_threads()); @@ -687,11 +694,12 @@ namespace { // Collect info about search result bestMove = Rml[0].pv[0]; + *ponderMove = Rml[0].pv[1]; bestValues[depth] = value; bestMoveChanges[depth] = Rml.bestMoveChanges; // Send PV line to GUI and to log file - for (int i = 0; i < Min(MultiPV, (int)Rml.size()); i++) + for (int i = 0; i < Min(UCIMultiPV, (int)Rml.size()); i++) cout << Rml[i].pv_info_to_uci(pos, depth, alpha, beta, i) << endl; if (UseLogFile) @@ -746,7 +754,47 @@ namespace { } } - *ponderMove = Rml[0].pv[1]; + // When playing with strength handicap choose best move among the MultiPV set + // using a statistical rule dependent on SkillLevel. Idea by Heinz van Saanen. + if (SkillLevel < 20) + { + assert(MultiPV > 1); + + // Rml list is already sorted by pv_score in descending order + int s; + int max_s = -VALUE_INFINITE; + int size = Min(MultiPV, (int)Rml.size()); + int max = Rml[0].pv_score; + int var = Min(max - Rml[size - 1].pv_score, PawnValueMidgame); + int wk = 120 - 2 * SkillLevel; + + // PRNG sequence should be non deterministic + for (int i = abs(get_system_time() % 50); i > 0; i--) + RK.rand(); + + // Choose best move. For each move's score we add two terms both dependent + // on wk, one deterministic and bigger for weaker moves, and one random, + // then we choose the move with the resulting highest score. + for (int i = 0; i < size; i++) + { + s = Rml[i].pv_score; + + // Don't allow crazy blunders even at very low skills + if (i > 0 && Rml[i-1].pv_score > s + EasyMoveMargin) + break; + + // This is our magical formula + s += ((max - s) * wk + var * (RK.rand() % wk)) / 128; + + if (s > max_s) + { + max_s = s; + bestMove = Rml[i].pv[0]; + *ponderMove = Rml[i].pv[1]; + } + } + } + return bestMove; } @@ -872,7 +920,7 @@ namespace { && !isCheck && refinedValue < beta - razor_margin(depth) && ttMove == MOVE_NONE - && !value_is_mate(beta) + && abs(beta) < value_mate_in(PLY_MAX) && !pos.has_pawn_on_7th(pos.side_to_move())) { Value rbeta = beta - razor_margin(depth); @@ -891,7 +939,7 @@ namespace { && depth < RazorDepth && !isCheck && refinedValue >= beta + futility_margin(depth, 0) - && !value_is_mate(beta) + && abs(beta) < value_mate_in(PLY_MAX) && pos.non_pawn_material(pos.side_to_move())) return refinedValue - futility_margin(depth, 0); @@ -901,7 +949,7 @@ namespace { && depth > ONE_PLY && !isCheck && refinedValue >= beta - && !value_is_mate(beta) + && abs(beta) < value_mate_in(PLY_MAX) && pos.non_pawn_material(pos.side_to_move())) { ss->currentMove = MOVE_NULL; @@ -1297,8 +1345,12 @@ split_point_start: // At split points actual search starts from here if ( bestValue >= beta && !pos.move_is_capture_or_promotion(move)) { + if (move != ss->killers[0]) + { + ss->killers[1] = ss->killers[0]; + ss->killers[0] = move; + } update_history(pos, move, depth, movesSearched, playedMoveCount); - update_killers(move, ss->killers); } } @@ -1616,18 +1668,6 @@ split_point_start: // At split points actual search starts from here } - // value_is_mate() checks if the given value is a mate one eventually - // compensated for the ply. - - bool value_is_mate(Value value) { - - assert(abs(value) <= VALUE_INFINITE); - - return value <= value_mated_in(PLY_MAX) - || value >= value_mate_in(PLY_MAX); - } - - // value_to_tt() adjusts a mate score from "plies to mate from the root" to // "plies to mate from the current ply". Non-mate scores are unchanged. // The function is called before storing a value to the transposition table. @@ -1817,19 +1857,6 @@ split_point_start: // At split points actual search starts from here } - // update_killers() add a good move that produced a beta-cutoff - // among the killer moves of that ply. - - void update_killers(Move m, Move killers[]) { - - if (m != killers[0]) - { - killers[1] = killers[0]; - killers[0] = m; - } - } - - // update_gains() updates the gains table of a non-capture move given // the static position evaluation before and after the move. @@ -1843,6 +1870,7 @@ split_point_start: // At split points actual search starts from here H.update_gain(pos.piece_on(move_to(m)), move_to(m), -(before + after)); } + // current_search_time() returns the number of milliseconds which have passed // since the beginning of the current search. @@ -1866,7 +1894,7 @@ split_point_start: // At split points actual search starts from here if (abs(v) < VALUE_MATE - PLY_MAX * ONE_PLY) s << "cp " << int(v) * 100 / int(PawnValueMidgame); // Scale to centipawns else - s << "mate " << (v > 0 ? (VALUE_MATE - v + 1) / 2 : -(VALUE_MATE + v) / 2); + s << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2; return s.str(); } @@ -1903,10 +1931,7 @@ split_point_start: // At split points actual search starts from here // We are line oriented, don't read single chars std::string command; - if (!std::getline(std::cin, command)) - command = "quit"; - - if (command == "quit") + if (!std::getline(std::cin, command) || command == "quit") { // Quit the program as soon as possible Pondering = false; @@ -1984,20 +2009,12 @@ split_point_start: // At split points actual search starts from here std::string command; - while (true) - { - // Wait for a command from stdin - if (!std::getline(std::cin, command)) - command = "quit"; + // Wait for a command from stdin + while ( std::getline(std::cin, command) + && command != "ponderhit" && command != "stop" && command != "quit") {}; - if (command == "quit") - { - QuitRequest = true; - break; - } - else if (command == "ponderhit" || command == "stop") - break; - } + if (command != "ponderhit" && command != "stop") + QuitRequest = true; // Must be "quit" or getline() returned false }