- elapsed = now() - elapsed + 1; // Ensure positivity to avoid a 'divide by zero'
-
- dbg_print(); // Just before exiting
-
- cerr << "\n==========================="
- << "\nTotal time (ms) : " << elapsed
- << "\nNodes searched : " << nodes
- << "\nNodes/second : " << 1000 * nodes / elapsed << endl;
- }
-
- // The win rate model returns the probability (per mille) of winning given an eval
- // and a game-ply. The model fits rather accurately the LTC fishtest statistics.
- int win_rate_model(Value v, int ply) {
-
- // The model captures only up to 240 plies, so limit input (and rescale)
- double m = std::min(240, ply) / 64.0;
-
- // Coefficients of a 3rd order polynomial fit based on fishtest data
- // for two parameters needed to transform eval to the argument of a
- // logistic function.
- double as[] = {-8.24404295, 64.23892342, -95.73056462, 153.86478679};
- double bs[] = {-3.37154371, 28.44489198, -56.67657741, 72.05858751};
- double a = (((as[0] * m + as[1]) * m + as[2]) * m) + as[3];
- double b = (((bs[0] * m + bs[1]) * m + bs[2]) * m) + bs[3];
-
- // Transform eval to centipawns with limited range
- double x = std::clamp(double(100 * v) / PawnValueEg, -1000.0, 1000.0);
-
- // Return win rate in per mille (rounded to nearest)
- return int(0.5 + 1000 / (1 + std::exp((a - x) / b)));
- }
-
-} // namespace
-
-
-/// UCI::loop() waits for a command from stdin, parses it and calls the appropriate
-/// function. Also intercepts EOF from stdin to ensure gracefully exiting if the
-/// GUI dies unexpectedly. When called with some command line arguments, e.g. to
-/// run 'bench', once the command is executed the function returns immediately.
-/// In addition to the UCI ones, also some additional debug commands are supported.
-
-void UCI::loop(int argc, char* argv[]) {
-
- Position pos;
- string token, cmd;
- StateListPtr states(new std::deque<StateInfo>(1));
-
- pos.set(StartFEN, false, &states->back(), Threads.main());
-
- for (int i = 1; i < argc; ++i)
- cmd += std::string(argv[i]) + " ";
-
- do {
- if (argc == 1 && !getline(cin, cmd)) // Block here waiting for input or EOF
- cmd = "quit";
-
- istringstream is(cmd);
-
- token.clear(); // Avoid a stale if getline() returns empty or blank line
- is >> skipws >> token;
-
- if ( token == "quit"
- || token == "stop")
- Threads.stop = true;
-
- // The GUI sends 'ponderhit' to tell us the user has played the expected move.
- // So 'ponderhit' will be sent if we were told to ponder on the same move the
- // user has played. We should continue searching but switch from pondering to
- // normal search.
- else if (token == "ponderhit")
- Threads.main()->ponder = false; // Switch to normal search
-
- else if (token == "uci")
- sync_cout << "id name " << engine_info(true)
- << "\n" << Options
- << "\nuciok" << sync_endl;
-
- else if (token == "setoption") setoption(is);
- else if (token == "go") go(pos, is, states);
- else if (token == "position") position(pos, is, states);
- else if (token == "ucinewgame") Search::clear();
- else if (token == "isready") sync_cout << "readyok" << sync_endl;
-
- // Additional custom non-UCI commands, mainly for debugging.
- // Do not use these commands during a search!
- else if (token == "flip") pos.flip();
- else if (token == "bench") bench(pos, is, states);
- else if (token == "d") sync_cout << pos << sync_endl;
- else if (token == "eval") trace_eval(pos);
- else if (token == "compiler") sync_cout << compiler_info() << sync_endl;
- else if (token == "export_net") {
- std::optional<std::string> filename;
- std::string f;
- if (is >> skipws >> f) {
- filename = f;
- }
- Eval::NNUE::export_net(filename);
- }
- else if (!token.empty() && token[0] != '#')
- sync_cout << "Unknown command: " << cmd << sync_endl;
-
- } while (token != "quit" && argc == 1); // Command line args are one-shot