-
- // 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<unsigned>();
-
- // 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<unsigned>() % weakness)) / 128;
-
- if (s > max_s)
- {
- max_s = s;
- best = RootMoves[i].pv[0];
- }
- }
- return best;
- }
-