- // 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);
- }
- }
-
-
- // current_search_time() returns the number of milliseconds which have passed
- // since the beginning of the current search.
-
- int elapsed_time(bool reset) {
-
- static int searchStartTime;
-
- if (reset)
- searchStartTime = get_system_time();
-
- 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 = elapsed_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;
- int selDepth = 0;
-
- // Retrieve max searched depth among threads
- 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();
- }
-
-
- // 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"). It uses the two below helper 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_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();
- }
-
- 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[PLY_MAX_PLUS_2], *st = state;
- Move* m = pv;
- string san, padding;
- size_t length;
- std::stringstream s;
-
- 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 ";
-
- 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++);
- }
-
- // Restore original position before to leave
- while (m != pv)
- pos.undo_move(*--m);
-
- return s.str();
- }
-
-