- // 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_IN_MAX_PLY)
- s << "cp " << v * 100 / int(PawnValueMidgame);
- else
- s << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
-
- s << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : "");
-
- return s.str();
- }
-
-
- // pv_info_to_uci() sends search info to GUI. UCI protocol requires to send all
- // the PV lines also if are still to be searched and so refer to the previous
- // search score.
-
- void pv_info_to_uci(const Position& pos, int depth, Value alpha, Value beta) {
-
- int t = SearchTime.elapsed();
- int selDepth = 0;
-
- for (int i = 0; i < Threads.size(); i++)
- if (Threads[i].maxPly > selDepth)
- selDepth = Threads[i].maxPly;
-
- for (size_t i = 0; i < std::min(UCIMultiPV, RootMoves.size()); i++)
- {
- bool updated = (i <= PVIdx);
-
- if (depth == 1 && !updated)
- continue;
-
- int d = (updated ? depth : depth - 1);
- Value v = (updated ? RootMoves[i].score : RootMoves[i].prevScore);
- std::stringstream s;
-
- for (int j = 0; RootMoves[i].pv[j] != MOVE_NONE; j++)
- s << " " << move_to_uci(RootMoves[i].pv[j], Chess960);
-
- cout << "info depth " << d
- << " seldepth " << selDepth
- << " score " << (i == PVIdx ? score_to_uci(v, alpha, beta) : score_to_uci(v))
- << " nodes " << pos.nodes_searched()
- << " nps " << (t > 0 ? pos.nodes_searched() * 1000 / t : 0)
- << " time " << t
- << " multipv " << i + 1
- << " pv" << s.str() << endl;
- }
- }
-
-
- // pv_info_to_log() writes human-readable search information to the log file
- // (which is created when the UCI parameter "Use Search Log" is "true"). It
- // uses the two below helpers 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();
- }
-
- void pv_info_to_log(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);
-
- Log l(Options["Search Log Filename"]);
- l << s.str() << endl;
- }
-
-