- // update_history() registers a good move that produced a beta-cutoff
- // in history and marks as failures all the other moves of that ply.
-
- void update_history(const Position& pos, Move move, Depth depth,
- Move movesSearched[], int moveCount) {
- Move m;
- Value bonus = Value(int(depth) * int(depth));
-
- H.update(pos.piece_on(move_from(move)), move_to(move), bonus);
-
- for (int i = 0; i < moveCount - 1; i++)
- {
- m = movesSearched[i];
-
- assert(m != move);
-
- H.update(pos.piece_on(move_from(m)), move_to(m), -bonus);
- }
- }
-
-
- // update_gains() updates the gains table of a non-capture move given
- // the static position evaluation before and after the move.
-
- void update_gains(const Position& pos, Move m, Value before, Value after) {
-
- if ( m != MOVE_NULL
- && before != VALUE_NONE
- && after != VALUE_NONE
- && pos.captured_piece_type() == PIECE_TYPE_NONE
- && !move_is_special(m))
- 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.
-
- int current_search_time(int set) {
-
- static int searchStartTime;
-
- if (set)
- searchStartTime = set;
-
- return get_system_time() - searchStartTime;
- }
-
-
- // score_to_uci() converts a value to a string suitable for use with the UCI
- // protocol specifications:
- //
- // cp <x> The score from the engine's point of view in centipawns.
- // mate <y> Mate in y moves, not plies. If the engine is getting mated
- // use negative values for y.
-
- string score_to_uci(Value v, Value alpha, Value beta) {
-
- std::stringstream s;
-
- if (abs(v) < VALUE_MATE - PLY_MAX * ONE_PLY)
- s << " score cp " << int(v) * 100 / int(PawnValueMidgame); // Scale to centipawns
- else
- s << " score mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
-
- s << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : "");
-
- return s.str();
- }
-
-
- // speed_to_uci() returns a string with time stats of current search suitable
- // to be sent to UCI gui.
-
- string speed_to_uci(int64_t nodes) {
-
- std::stringstream s;
- int t = current_search_time();
-
- s << " nodes " << nodes
- << " nps " << (t > 0 ? int(nodes * 1000 / t) : 0)
- << " time " << t;
-
- return s.str();
- }
-
- // pv_to_uci() returns a string with information on the current PV line
- // formatted according to UCI specification.
-
- string pv_to_uci(const Move pv[], int pvNum, bool chess960) {
-
- std::stringstream s;
-
- s << " multipv " << pvNum << " pv " << set960(chess960);
-
- for ( ; *pv != MOVE_NONE; pv++)
- s << *pv << " ";
-
- return s.str();
- }
-
- // depth_to_uci() returns a string with information on the current depth and
- // seldepth formatted according to UCI specification.
-
- string depth_to_uci(Depth depth) {
-
- std::stringstream s;
-
- // Retrieve max searched depth among threads
- int selDepth = 0;
- for (int i = 0; i < Threads.size(); i++)
- if (Threads[i].maxPly > selDepth)
- selDepth = Threads[i].maxPly;
-
- s << " depth " << depth / ONE_PLY << " seldepth " << selDepth;
-
- return s.str();
- }
-
- 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_PLY_MAX)
- s << "#" << (VALUE_MATE - v + 1) / 2;
- else if (v <= VALUE_MATED_IN_PLY_MAX)
- s << "-#" << (VALUE_MATE + v) / 2;
- else
- s << std::setprecision(2) << std::fixed << std::showpos << float(v) / PawnValueMidgame;
-
- return s.str();
- }
-
- // pretty_pv() creates a human-readable string from a position and a PV.
- // It is used to write search information to the log file (which is created
- // when the UCI parameter "Use Search Log" is "true").
-
- string pretty_pv(Position& pos, int depth, Value value, int time, Move pv[]) {
-
- const int64_t K = 1000;
- const int64_t M = 1000000;
- const int startColumn = 28;
- const size_t maxLength = 80 - startColumn;
-
- StateInfo state[PLY_MAX_PLUS_2], *st = state;
- Move* m = pv;
- string san;
- std::stringstream s;
- size_t length = 0;
-
- // First print depth, score, time and searched nodes...
- s << set960(pos.is_chess960())
- << 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 ";
-
- // ...then print the full PV line in short algebraic notation
- while (*m != MOVE_NONE)
- {
- san = move_to_san(pos, *m);
- length += san.length() + 1;
-
- if (length > maxLength)
- {
- length = san.length() + 1;
- s << "\n" + string(startColumn, ' ');
- }
- s << san << ' ';
-
- pos.do_move(*m++, *st++);
- }
-
- // Restore original position before to leave
- while (m != pv) pos.undo_move(*--m);
-
- return s.str();
- }
-
- // poll() performs two different functions: It polls for user input, and it
- // looks at the time consumed so far and decides if it's time to abort the
- // search.
-
- void poll(const Position& pos) {
-
- static int lastInfoTime;
- int t = current_search_time();
-
- // Poll for input
- if (input_available())
- {
- // We are line oriented, don't read single chars
- string command;
-
- if (!std::getline(std::cin, command) || command == "quit")
- {
- // Quit the program as soon as possible
- Limits.ponder = false;
- QuitRequest = StopRequest = true;
- return;
- }
- else if (command == "stop")
- {
- // Stop calculating as soon as possible, but still send the "bestmove"
- // and possibly the "ponder" token when finishing the search.
- Limits.ponder = false;
- StopRequest = true;
- }
- else if (command == "ponderhit")
- {
- // The opponent has played the expected move. GUI sends "ponderhit" if
- // we were told to ponder on the same move the opponent has played. We
- // should continue searching but switching from pondering to normal search.
- Limits.ponder = false;
-
- if (StopOnPonderhit)
- StopRequest = true;
- }
- }
-
- // 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;
-
- dbg_print_mean();
- dbg_print_hit_rate();
-
- // Send info on searched nodes as soon as we return to root
- SendSearchedNodes = true;
- }
-
- // Should we stop the search?
- if (Limits.ponder)
- return;
-
- bool stillAtFirstMove = FirstRootMove
- && !AspirationFailLow
- && t > TimeMgr.available_time();
-
- bool noMoreTime = t > TimeMgr.maximum_time()
- || stillAtFirstMove;
-
- if ( (Limits.useTimeManagement() && noMoreTime)
- || (Limits.maxTime && t >= Limits.maxTime)
- || (Limits.maxNodes && pos.nodes_searched() >= Limits.maxNodes)) // FIXME
- StopRequest = true;
- }
-
-
- // wait_for_stop_or_ponderhit() is called when the maximum depth is reached
- // while the program is pondering. The point is to work around a wrinkle in
- // the UCI protocol: When pondering, the engine is not allowed to give a
- // "bestmove" before the GUI sends it a "stop" or "ponderhit" command.
- // We simply wait here until one of these commands is sent, and return,
- // after which the bestmove and pondermove will be printed.
-
- void wait_for_stop_or_ponderhit() {
-
- string command;
-
- // Wait for a command from stdin
- while ( std::getline(std::cin, command)
- && command != "ponderhit" && command != "stop" && command != "quit") {};
-
- if (command != "ponderhit" && command != "stop")
- QuitRequest = true; // Must be "quit" or getline() returned false
- }
-
-