X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fsearch.cpp;h=8f1964dd6e9f8b426fc03a2189f30a068de6b7dc;hp=df09007f1538f8f15134e6df34edbc8dafacb93e;hb=bc4de9edaec0a618279092abbf465f47720736b8;hpb=abc6a0be2f1465545d0ff1d2dbcc1b8c4d94daaa diff --git a/src/search.cpp b/src/search.cpp index df09007f..8f1964dd 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -21,7 +21,6 @@ #include #include #include -#include #include #include @@ -30,6 +29,7 @@ #include "history.h" #include "movegen.h" #include "movepick.h" +#include "notation.h" #include "search.h" #include "timeman.h" #include "thread.h" @@ -51,11 +51,6 @@ using std::endl; using Eval::evaluate; using namespace Search; -// For some reason argument-dependent lookup (ADL) doesn't work for Android's -// STLPort, so explicitly qualify following functions. -using std::count; -using std::find; - namespace { // Set to true to force running with one thread. Used for debugging @@ -144,29 +139,8 @@ namespace { bool connected_threat(const Position& pos, Move m, Move threat); Value refine_eval(const TTEntry* tte, Value ttValue, Value defaultEval); Move do_skill_level(); - string score_to_uci(Value v, Value alpha = -VALUE_INFINITE, Value beta = VALUE_INFINITE); - string pretty_pv(Position& pos, int depth, Value score, int time, Move pv[]); string uci_pv(const Position& pos, int depth, Value alpha, Value beta); - // MovePickerExt class template extends MovePicker and allows to choose at - // compile time the proper moves source according to the type of node. In the - // default case we simply create and use a standard MovePicker object. - template struct MovePickerExt : public MovePicker { - - MovePickerExt(const Position& p, Move ttm, Depth d, const History& h, Stack* ss, Value b) - : MovePicker(p, ttm, d, h, ss, b) {} - }; - - // In case of a SpNode we use split point's shared MovePicker object as moves source - template<> struct MovePickerExt : public MovePicker { - - MovePickerExt(const Position& p, Move ttm, Depth d, const History& h, Stack* ss, Value b) - : MovePicker(p, ttm, d, h, ss, b), mp(ss->sp->mp) {} - - Move next_move() { return mp->next_move(); } - MovePicker* mp; - }; - // is_dangerous() checks whether a move belongs to some classes of known // 'dangerous' moves so that we avoid to prune it. FORCE_INLINE bool is_dangerous(const Position& pos, Move m, bool captureOrPromotion) { @@ -274,9 +248,9 @@ void Search::think() { { Move bookMove = book.probe(pos, Options["Book File"], Options["Best Book Move"]); - if (bookMove && count(RootMoves.begin(), RootMoves.end(), bookMove)) + if (bookMove && std::count(RootMoves.begin(), RootMoves.end(), bookMove)) { - std::swap(RootMoves[0], *find(RootMoves.begin(), RootMoves.end(), bookMove)); + std::swap(RootMoves[0], *std::find(RootMoves.begin(), RootMoves.end(), bookMove)); goto finalize; } } @@ -517,7 +491,7 @@ namespace { if (skillBest == MOVE_NONE) // Still unassigned ? skillBest = do_skill_level(); - std::swap(RootMoves[0], *find(RootMoves.begin(), RootMoves.end(), skillBest)); + std::swap(RootMoves[0], *std::find(RootMoves.begin(), RootMoves.end(), skillBest)); } } @@ -781,7 +755,7 @@ namespace { MovePicker mp(pos, ttMove, H, pos.captured_piece_type()); CheckInfo ci(pos); - while ((move = mp.next_move()) != MOVE_NONE) + while ((move = mp.next_move()) != MOVE_NONE) if (pos.pl_move_is_legal(move, ci.pinned)) { ss->currentMove = move; @@ -810,7 +784,7 @@ namespace { split_point_start: // At split points actual search starts from here - MovePickerExt mp(pos, ttMove, depth, H, ss, PvNode ? -VALUE_INFINITE : beta); + MovePicker mp(pos, ttMove, depth, H, ss, PvNode ? -VALUE_INFINITE : beta); CheckInfo ci(pos); futilityBase = ss->eval + ss->evalMargin; singularExtensionNode = !RootNode @@ -824,7 +798,7 @@ split_point_start: // At split points actual search starts from here // Step 11. Loop through moves // Loop through all pseudo-legal moves until no moves remain or a beta cutoff occurs while ( bestValue < beta - && (move = mp.next_move()) != MOVE_NONE + && (move = mp.next_move()) != MOVE_NONE && !thisThread->cutoff_occurred() && !Signals.stop) { @@ -836,7 +810,7 @@ split_point_start: // At split points actual search starts from here // At root obey the "searchmoves" option and skip moves not listed in Root // Move List, as a consequence any illegal move is also skipped. In MultiPV // mode we also skip PV moves which have been already searched. - if (RootNode && !count(RootMoves.begin() + PVIdx, RootMoves.end(), move)) + if (RootNode && !std::count(RootMoves.begin() + PVIdx, RootMoves.end(), move)) continue; // At PV and SpNode nodes we want all moves to be legal since the beginning @@ -1012,7 +986,7 @@ split_point_start: // At split points actual search starts from here // be trusted, and we don't update the best move and/or PV. if (RootNode && !Signals.stop) { - RootMove& rm = *find(RootMoves.begin(), RootMoves.end(), move); + RootMove& rm = *std::find(RootMoves.begin(), RootMoves.end(), move); // PV move or new best move ? if (isPvMove || value > alpha) @@ -1215,7 +1189,7 @@ split_point_start: // At split points actual search starts from here // Loop through the moves until no moves remain or a beta cutoff occurs while ( bestValue < beta - && (move = mp.next_move()) != MOVE_NONE) + && (move = mp.next_move()) != MOVE_NONE) { assert(is_ok(move)); @@ -1512,25 +1486,48 @@ split_point_start: // At split points actual search starts from here } - // score_to_uci() converts a value to a string suitable for use with the UCI - // protocol specifications: - // - // cp The score from the engine's point of view in centipawns. - // mate Mate in y moves, not plies. If the engine is getting mated - // use negative values for y. + // When playing with strength handicap choose best move among the MultiPV set + // using a statistical rule dependent on SkillLevel. Idea by Heinz van Saanen. - string score_to_uci(Value v, Value alpha, Value beta) { + Move do_skill_level() { - std::stringstream s; + assert(MultiPV > 1); - if (abs(v) < VALUE_MATE_IN_MAX_PLY) - s << "cp " << v * 100 / int(PawnValueMidgame); - else - s << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2; + static RKISS rk; - s << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : ""); + // PRNG sequence should be not deterministic + for (int i = Time::current_time().msec() % 50; i > 0; i--) + rk.rand(); - return s.str(); + // RootMoves are already sorted by score in descending order + size_t size = std::min(MultiPV, RootMoves.size()); + int variance = std::min(RootMoves[0].score - RootMoves[size - 1].score, PawnValueMidgame); + int weakness = 120 - 2 * SkillLevel; + int max_s = -VALUE_INFINITE; + Move best = MOVE_NONE; + + // Choose best move. For each move score we add two terms both dependent on + // weakness, one deterministic and bigger for weaker moves, and one random, + // then we choose the move with the resulting highest score. + for (size_t i = 0; i < size; i++) + { + int s = RootMoves[i].score; + + // Don't allow crazy blunders even at very low skills + if (i > 0 && RootMoves[i-1].score > s + EasyMoveMargin) + break; + + // This is our magic formula + s += ( weakness * int(RootMoves[0].score - s) + + variance * (rk.rand() % weakness)) / 128; + + if (s > max_s) + { + max_s = s; + best = RootMoves[i].pv[0]; + } + } + return best; } @@ -1577,141 +1574,6 @@ split_point_start: // At split points actual search starts from here return s.str(); } - - // pretty_pv() formats human-readable search information, typically to be - // appended to the search log file. It uses the two helpers below to pretty - // format time and score respectively. - - string time_to_string(int millisecs) { - - const int MSecMinute = 1000 * 60; - const int MSecHour = 1000 * 60 * 60; - - int hours = millisecs / MSecHour; - int minutes = (millisecs % MSecHour) / MSecMinute; - int seconds = ((millisecs % MSecHour) % MSecMinute) / 1000; - - std::stringstream s; - - if (hours) - s << hours << ':'; - - s << std::setfill('0') << std::setw(2) << minutes << ':' - << std::setw(2) << seconds; - return s.str(); - } - - string score_to_string(Value v) { - - std::stringstream s; - - if (v >= VALUE_MATE_IN_MAX_PLY) - s << "#" << (VALUE_MATE - v + 1) / 2; - - else if (v <= VALUE_MATED_IN_MAX_PLY) - s << "-#" << (VALUE_MATE + v) / 2; - - else - s << std::setprecision(2) << std::fixed << std::showpos - << float(v) / PawnValueMidgame; - - return s.str(); - } - - string pretty_pv(Position& pos, int depth, Value value, int time, Move pv[]) { - - const int64_t K = 1000; - const int64_t M = 1000000; - - StateInfo state[MAX_PLY_PLUS_2], *st = state; - Move* m = pv; - string san, padding; - size_t length; - std::stringstream s; - - s << std::setw(2) << depth - << std::setw(8) << score_to_string(value) - << std::setw(8) << time_to_string(time); - - if (pos.nodes_searched() < M) - s << std::setw(8) << pos.nodes_searched() / 1 << " "; - - else if (pos.nodes_searched() < K * M) - s << std::setw(7) << pos.nodes_searched() / K << "K "; - - else - s << std::setw(7) << pos.nodes_searched() / M << "M "; - - padding = string(s.str().length(), ' '); - length = padding.length(); - - while (*m != MOVE_NONE) - { - san = move_to_san(pos, *m); - - if (length + san.length() > 80) - { - s << "\n" + padding; - length = padding.length(); - } - - s << san << ' '; - length += san.length() + 1; - - pos.do_move(*m++, *st++); - } - - while (m != pv) - pos.undo_move(*--m); - - return s.str(); - } - - - // When playing with strength handicap choose best move among the MultiPV set - // using a statistical rule dependent on SkillLevel. Idea by Heinz van Saanen. - - Move do_skill_level() { - - assert(MultiPV > 1); - - static RKISS rk; - - // PRNG sequence should be not deterministic - for (int i = Time::current_time().msec() % 50; i > 0; i--) - rk.rand(); - - // RootMoves are already sorted by score in descending order - size_t size = std::min(MultiPV, RootMoves.size()); - int variance = std::min(RootMoves[0].score - RootMoves[size - 1].score, PawnValueMidgame); - int weakness = 120 - 2 * SkillLevel; - int max_s = -VALUE_INFINITE; - Move best = MOVE_NONE; - - // Choose best move. For each move score we add two terms both dependent on - // weakness, one deterministic and bigger for weaker moves, and one random, - // then we choose the move with the resulting highest score. - for (size_t i = 0; i < size; i++) - { - int s = RootMoves[i].score; - - // Don't allow crazy blunders even at very low skills - if (i > 0 && RootMoves[i-1].score > s + EasyMoveMargin) - break; - - // This is our magic formula - s += ( weakness * int(RootMoves[0].score - s) - + variance * (rk.rand() % weakness)) / 128; - - if (s > max_s) - { - max_s = s; - best = RootMoves[i].pv[0]; - } - } - return best; - } - } // namespace