feat: coloured search output for a terminal
Started from a terminal, Rigby now shows its search for a human: coloured progress lines (depth/seldepth, playouts, speed, time, a tree fill bar, score in pawns, the PV fading with depth) at 125, 250, 500 ms and then every second, and at the end a table of the most visited root moves with a bar for their share of the visits, their score and prior. Tree reuse and collections are shown as notes, and bestmove is highlighted. The first "uci" command switches to plain UCI output for good. GUIs and match runners send it first and read stdout through a pipe, so they never see the coloured output. NO_COLOR turns it off. On Windows the console gets ANSI processing and UTF-8 output. Checked: the UCI output of a scripted session (two searches with tree reuse, a stalemate root) is identical to main apart from nps and time; go wtime, go infinite with stop, and quit work; the pretty mode was driven in a real console, including the switch to UCI. Search is untouched, so the bench is unchanged. Bench: 546018 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1754753a6e
commit
6490286852
6 files changed
+319
-17
No files matched your search
+52
-16
@@ -7,6 +7,7 @@
|
||||
#include <sstream>
|
||||
|
||||
#include "eval.h"
|
||||
#include "pretty.h"
|
||||
#include "uci_io.h"
|
||||
|
||||
namespace rigby {
|
||||
@@ -412,26 +413,45 @@ void Searcher::print_info() const {
|
||||
const i64 ms = elapsed_ms();
|
||||
const u32 best = best_child(Tree::root());
|
||||
|
||||
std::ostringstream out;
|
||||
out << "info depth " << (iterations_ ? std::max<u64>(1, depth_sum_ / iterations_) : 0) << " seldepth "
|
||||
<< seldepth_;
|
||||
pretty::Progress p;
|
||||
p.depth = int(iterations_ ? std::max<u64>(1, depth_sum_ / iterations_) : 0);
|
||||
p.seldepth = seldepth_;
|
||||
p.cp = eval::q_to_cp(best && tree_[best].visits ? tree_[best].q : -root.q);
|
||||
p.playouts = iterations_;
|
||||
p.nps = ms ? iterations_ * 1000 / u64(ms) : iterations_;
|
||||
p.ms = ms;
|
||||
p.hashfull = int(tree_.used() * 1000 / tree_.capacity());
|
||||
|
||||
const float q = best && tree_[best].visits ? tree_[best].q : -root.q;
|
||||
out << " score cp " << eval::q_to_cp(q);
|
||||
out << " nodes " << iterations_ << " nps " << (ms ? iterations_ * 1000 / u64(ms) : iterations_) << " time " << ms;
|
||||
out << " hashfull " << (tree_.used() * 1000 / tree_.capacity());
|
||||
|
||||
out << " pv";
|
||||
u32 idx = Tree::root();
|
||||
for (int ply = 0; ply < 64; ply++) {
|
||||
const u32 c = best_child(idx);
|
||||
if (!c || !tree_[c].visits) break;
|
||||
out << ' ' << Move::from_raw(tree_[c].move).to_uci();
|
||||
p.pv.push_back(Move::from_raw(tree_[c].move).to_uci());
|
||||
idx = c;
|
||||
}
|
||||
|
||||
if (pretty) {
|
||||
uci_io::send(pretty::progress(p));
|
||||
return;
|
||||
}
|
||||
|
||||
std::ostringstream out;
|
||||
out << "info depth " << p.depth << " seldepth " << p.seldepth << " score cp " << p.cp << " nodes " << p.playouts
|
||||
<< " nps " << p.nps << " time " << p.ms << " hashfull " << p.hashfull << " pv";
|
||||
for (const std::string& m : p.pv) out << ' ' << m;
|
||||
uci_io::send(out.str());
|
||||
}
|
||||
|
||||
void Searcher::print_root_moves() const {
|
||||
const Node& root = tree_[Tree::root()];
|
||||
std::vector<pretty::RootMove> moves;
|
||||
for (u32 i = 0; i < root.num_children; i++) {
|
||||
const Node& child = tree_[root.first_child + i];
|
||||
moves.push_back({Move::from_raw(child.move).to_uci(), child.visits, eval::q_to_cp(child.q), child.prior()});
|
||||
}
|
||||
uci_io::send(pretty::root_table(std::move(moves), 8));
|
||||
}
|
||||
|
||||
SearchResult Searcher::search(const Position& root, const std::vector<u64>& history, const SearchLimits& limits,
|
||||
bool print) {
|
||||
start_time_ = Clock::now();
|
||||
@@ -453,7 +473,7 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
|
||||
#ifdef RIGBY_VERIFY_TREE
|
||||
if (reused) verify_tree();
|
||||
#endif
|
||||
if (print && reused) uci_io::send("info string reused " + std::to_string(reused) + " nodes");
|
||||
if (print && !pretty && reused) uci_io::send("info string reused " + std::to_string(reused) + " nodes");
|
||||
iterations_ = 0;
|
||||
depth_sum_ = 0;
|
||||
seldepth_ = 0;
|
||||
@@ -467,10 +487,18 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
|
||||
generate_legal(root, root_moves);
|
||||
if (root_moves.empty()) {
|
||||
tree_valid_ = false;
|
||||
if (print) uci_io::send("info depth 0 score " + std::string(root.in_check() ? "mate 0" : "cp 0"));
|
||||
if (print && pretty)
|
||||
uci_io::send(pretty::note(root.in_check() ? "\n checkmate, no legal moves" : "\n stalemate, no legal moves"));
|
||||
else if (print)
|
||||
uci_io::send("info depth 0 score " + std::string(root.in_check() ? "mate 0" : "cp 0"));
|
||||
return result;
|
||||
}
|
||||
|
||||
if (print && pretty) {
|
||||
if (reused) uci_io::send(pretty::reused(reused));
|
||||
uci_io::send(pretty::header());
|
||||
}
|
||||
|
||||
// The root needs its children before the first playout, so that even a
|
||||
// single playout leaves a move to play.
|
||||
if (tree_[Tree::root()].state == UNEXPANDED && !expand(Tree::root(), root)) {
|
||||
@@ -479,7 +507,9 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
|
||||
expand(Tree::root(), root);
|
||||
}
|
||||
|
||||
i64 last_info = 0;
|
||||
// UCI reports once a second. A human gets lines at 125, 250, 500 ms and so
|
||||
// on, so short searches still show how the search developed.
|
||||
i64 next_info = pretty ? 125 : 1000;
|
||||
do {
|
||||
playout();
|
||||
if (gc_pending_) {
|
||||
@@ -492,8 +522,8 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
|
||||
}
|
||||
if (print && (iterations_ & 1023) == 0) {
|
||||
const i64 ms = elapsed_ms();
|
||||
if (ms - last_info >= 1000) {
|
||||
last_info = ms;
|
||||
if (ms >= next_info) {
|
||||
next_info = pretty && next_info < 1000 ? next_info * 2 : ms + 1000;
|
||||
print_info();
|
||||
}
|
||||
}
|
||||
@@ -501,7 +531,13 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
|
||||
|
||||
if (print) {
|
||||
print_info();
|
||||
if (gc_runs_) uci_io::send("info string tree collected " + std::to_string(gc_runs_) + " times");
|
||||
if (pretty) {
|
||||
print_root_moves();
|
||||
if (gc_runs_) uci_io::send(pretty::note("\n tree memory was full " + std::to_string(gc_runs_) +
|
||||
"x: the least visited branches were collected"));
|
||||
} else if (gc_runs_) {
|
||||
uci_io::send("info string tree collected " + std::to_string(gc_runs_) + " times");
|
||||
}
|
||||
}
|
||||
|
||||
const u32 best = best_child(Tree::root());
|
||||
|
||||
@@ -87,6 +87,8 @@ class Searcher {
|
||||
public:
|
||||
SearchParams params;
|
||||
std::atomic<bool> stop_flag{false};
|
||||
// Coloured output for a human at a terminal instead of UCI info lines.
|
||||
bool pretty = false;
|
||||
|
||||
Searcher();
|
||||
|
||||
@@ -148,6 +150,7 @@ private:
|
||||
|
||||
u32 best_child(u32 idx) const;
|
||||
void print_info() const;
|
||||
void print_root_moves() const;
|
||||
};
|
||||
|
||||
} // namespace rigby
|
||||
+200
@@ -0,0 +1,200 @@
|
||||
#include "pretty.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#include <io.h>
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace rigby::pretty {
|
||||
|
||||
namespace {
|
||||
|
||||
// ANSI SGR sequences.
|
||||
constexpr const char* RESET = "\x1b[0m";
|
||||
constexpr const char* BOLD = "\x1b[1m";
|
||||
constexpr const char* DIM = "\x1b[90m";
|
||||
constexpr const char* WHITE_ = "\x1b[97m";
|
||||
constexpr const char* CYAN = "\x1b[36m";
|
||||
constexpr const char* BLUE = "\x1b[94m";
|
||||
constexpr const char* MAGENTA = "\x1b[95m";
|
||||
constexpr const char* GREEN = "\x1b[32m";
|
||||
constexpr const char* BRIGHT_GREEN = "\x1b[92m";
|
||||
constexpr const char* YELLOW = "\x1b[93m";
|
||||
constexpr const char* RED = "\x1b[31m";
|
||||
constexpr const char* BRIGHT_RED = "\x1b[91m";
|
||||
|
||||
std::string paint(const char* colour, const std::string& text) { return colour + text + RESET; }
|
||||
|
||||
// Right- or left-aligns `text` (plain ASCII) to `width` columns.
|
||||
std::string pad_left(const std::string& text, size_t width) {
|
||||
return text.size() >= width ? text : std::string(width - text.size(), ' ') + text;
|
||||
}
|
||||
std::string pad_right(const std::string& text, size_t width) {
|
||||
return text.size() >= width ? text : text + std::string(width - text.size(), ' ');
|
||||
}
|
||||
|
||||
std::string format(const char* fmt, double value) {
|
||||
char buf[32];
|
||||
std::snprintf(buf, sizeof(buf), fmt, value);
|
||||
return buf;
|
||||
}
|
||||
|
||||
// 999, 12.3k, 123k, 1.23M, 12.3M, 1.23G
|
||||
std::string count(u64 n) {
|
||||
if (n < 1000) return std::to_string(n);
|
||||
const char* units = "kMGT";
|
||||
double v = double(n);
|
||||
int unit = -1;
|
||||
while (v >= 1000.0 && unit < 3) {
|
||||
v /= 1000.0;
|
||||
unit++;
|
||||
}
|
||||
const std::string digits = v < 10.0 ? format("%.2f", v) : v < 100.0 ? format("%.1f", v) : format("%.0f", v);
|
||||
return digits + units[unit];
|
||||
}
|
||||
|
||||
// 0.38s, 12.4s, 2m05s
|
||||
std::string duration(i64 ms) {
|
||||
if (ms < 10000) return format("%.2fs", double(ms) / 1000.0);
|
||||
if (ms < 60000) return format("%.1fs", double(ms) / 1000.0);
|
||||
const i64 s = ms / 1000;
|
||||
char buf[32];
|
||||
std::snprintf(buf, sizeof(buf), "%lldm%02llds", static_cast<long long>(s / 60), static_cast<long long>(s % 60));
|
||||
return buf;
|
||||
}
|
||||
|
||||
// Pawns with a sign: +0.42, -1.30
|
||||
std::string score_text(int cp) { return format("%+.2f", double(cp) / 100.0); }
|
||||
|
||||
const char* score_colour(int cp) {
|
||||
if (cp >= 150) return BRIGHT_GREEN;
|
||||
if (cp >= 25) return GREEN;
|
||||
if (cp > -25) return WHITE_;
|
||||
if (cp > -150) return RED;
|
||||
return BRIGHT_RED;
|
||||
}
|
||||
|
||||
// A bar `width` cells wide; eighth blocks make it smooth.
|
||||
std::string bar(double fraction, int width) {
|
||||
static constexpr const char* EIGHTHS[] = {"", "▏", "▎", "▍", "▌", "▋", "▊", "▉"};
|
||||
const int eighths = int(std::lround(std::clamp(fraction, 0.0, 1.0) * width * 8));
|
||||
std::string out;
|
||||
for (int i = 0; i < eighths / 8; i++) out += "█";
|
||||
int cells = eighths / 8;
|
||||
if (eighths % 8) {
|
||||
out += EIGHTHS[eighths % 8];
|
||||
cells++;
|
||||
}
|
||||
return out + std::string(size_t(width - cells), ' ');
|
||||
}
|
||||
|
||||
// The tree's fill level as a bar of `width` cells, filled ones coloured by how full it is.
|
||||
std::string tree_bar(int permille, int width) {
|
||||
const int filled = std::clamp((permille * width + 500) / 1000, 0, width);
|
||||
const char* colour = permille < 500 ? GREEN : permille < 900 ? YELLOW : RED;
|
||||
std::string full, empty;
|
||||
for (int i = 0; i < filled; i++) full += "■";
|
||||
for (int i = filled; i < width; i++) empty += "■";
|
||||
return paint(colour, full) + paint(DIM, empty);
|
||||
}
|
||||
|
||||
constexpr int TREE_WIDTH = 8;
|
||||
|
||||
} // namespace
|
||||
|
||||
bool init_terminal() {
|
||||
if (std::getenv("NO_COLOR")) return false;
|
||||
#ifdef _WIN32
|
||||
if (!_isatty(_fileno(stdout))) return false;
|
||||
const HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
DWORD mode = 0;
|
||||
if (!GetConsoleMode(out, &mode) || !SetConsoleMode(out, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) return false;
|
||||
SetConsoleOutputCP(CP_UTF8);
|
||||
return true;
|
||||
#else
|
||||
return isatty(STDOUT_FILENO);
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string banner(const std::string& name, const std::string& version, const std::string& author) {
|
||||
return std::string("\n ") + BOLD + MAGENTA + name + RESET + " " + WHITE_ + version + RESET + DIM + " by " +
|
||||
author + RESET + "\n " + DIM + "MCTS chess engine · type " + RESET + CYAN + "uci" + RESET + DIM +
|
||||
" for UCI mode, or e.g. " + RESET + CYAN + "go movetime 5000" + RESET + "\n";
|
||||
}
|
||||
|
||||
std::string header() {
|
||||
const std::string cols = " " + pad_left("depth", 7) + " " + pad_left("playouts", 8) + " " + pad_left("speed", 9) +
|
||||
" " + pad_left("time", 6) + " " + pad_right("tree", TREE_WIDTH + 5) + " " +
|
||||
pad_left("score", 6) + " pv";
|
||||
return std::string("\n") + DIM + cols + RESET;
|
||||
}
|
||||
|
||||
std::string progress(const Progress& p) {
|
||||
std::string line = " ";
|
||||
line += paint(WHITE_, pad_left(std::to_string(p.depth), 3)) + paint(DIM, pad_right("/" + std::to_string(p.seldepth), 4));
|
||||
line += " " + paint(CYAN, pad_left(count(p.playouts), 8));
|
||||
line += " " + paint(BLUE, pad_left(count(p.nps), 7)) + paint(DIM, "/s");
|
||||
line += " " + paint(WHITE_, pad_left(duration(p.ms), 6));
|
||||
line += " " + tree_bar(p.hashfull, TREE_WIDTH) + paint(DIM, pad_left(std::to_string((p.hashfull + 5) / 10) + "%", 5));
|
||||
line += " " + paint(score_colour(p.cp), std::string(BOLD) + pad_left(score_text(p.cp), 6));
|
||||
line += " ";
|
||||
for (size_t i = 0; i < p.pv.size(); i++) {
|
||||
// The move to play stands out; the rest of the line fades with depth.
|
||||
line += " " + (i == 0 ? paint(WHITE_, std::string(BOLD) + p.pv[i]) : paint(i < 4 ? WHITE_ : DIM, p.pv[i]));
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
std::string root_table(std::vector<RootMove> moves, size_t max_rows) {
|
||||
u64 total = 0;
|
||||
for (const RootMove& m : moves) total += m.visits;
|
||||
std::stable_sort(moves.begin(), moves.end(),
|
||||
[](const RootMove& a, const RootMove& b) { return a.visits > b.visits; });
|
||||
|
||||
constexpr int SHARE_WIDTH = 24;
|
||||
std::string out = std::string("\n") + DIM + " " + pad_right("move", 7) + pad_left("visits", 8) + " " +
|
||||
pad_right("share", SHARE_WIDTH + 8) + pad_left("score", 6) + " " + pad_left("prior", 6) + RESET;
|
||||
|
||||
const size_t rows = std::min(moves.size(), max_rows);
|
||||
for (size_t i = 0; i < rows; i++) {
|
||||
const RootMove& m = moves[i];
|
||||
const double share = total ? double(m.visits) / double(total) : 0.0;
|
||||
const bool best = i == 0;
|
||||
out += "\n ";
|
||||
out += best ? paint(WHITE_, std::string(BOLD) + pad_right(m.move, 7)) : paint(WHITE_, pad_right(m.move, 7));
|
||||
out += paint(CYAN, pad_left(count(m.visits), 8)) + " ";
|
||||
out += paint(best ? MAGENTA : BLUE, bar(share, SHARE_WIDTH));
|
||||
out += paint(DIM, pad_left(format("%.1f%%", share * 100.0), 7)) + " ";
|
||||
out += m.visits ? paint(score_colour(m.cp), pad_left(score_text(m.cp), 6)) : paint(DIM, pad_left("-", 6));
|
||||
out += " " + paint(DIM, pad_left(format("%.1f%%", double(m.prior) * 100.0), 6));
|
||||
}
|
||||
if (moves.size() > rows)
|
||||
out += "\n " + paint(DIM, "... " + std::to_string(moves.size() - rows) + " more moves");
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string note(const std::string& text) { return std::string(" ") + DIM + text + RESET; }
|
||||
|
||||
std::string reused(u64 nodes) {
|
||||
return std::string("\n ") + DIM + "reused " + RESET + CYAN + count(nodes) + RESET + DIM +
|
||||
" tree nodes from the last search" + RESET;
|
||||
}
|
||||
|
||||
std::string bestmove(const std::string& move) {
|
||||
return std::string("\n ") + DIM + "bestmove " + RESET + BOLD + BRIGHT_GREEN + move + RESET + "\n";
|
||||
}
|
||||
|
||||
} // namespace rigby::pretty
|
||||
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
namespace rigby::pretty {
|
||||
|
||||
// Human-readable, coloured search output for a terminal. The engine uses it
|
||||
// until the first "uci" command; GUIs and match runners always send "uci"
|
||||
// (and read stdout through a pipe), so they only ever see plain UCI.
|
||||
|
||||
// True when stdout is a terminal (and NO_COLOR is not set). Turns on ANSI
|
||||
// colours and UTF-8 output on the Windows console.
|
||||
bool init_terminal();
|
||||
|
||||
std::string banner(const std::string& name, const std::string& version, const std::string& author);
|
||||
|
||||
// One snapshot of a running search.
|
||||
struct Progress {
|
||||
int depth = 0;
|
||||
int seldepth = 0;
|
||||
u64 playouts = 0;
|
||||
u64 nps = 0;
|
||||
i64 ms = 0;
|
||||
int hashfull = 0; // per mille
|
||||
int cp = 0; // side to move's view
|
||||
std::vector<std::string> pv;
|
||||
};
|
||||
|
||||
struct RootMove {
|
||||
std::string move;
|
||||
u32 visits = 0;
|
||||
int cp = 0; // from Q, side to move's view
|
||||
float prior = 0.0f;
|
||||
};
|
||||
|
||||
std::string header();
|
||||
std::string progress(const Progress& p);
|
||||
// The most visited root moves, with their share of the visits.
|
||||
std::string root_table(std::vector<RootMove> moves, size_t max_rows);
|
||||
std::string note(const std::string& text);
|
||||
std::string reused(u64 nodes);
|
||||
std::string bestmove(const std::string& move);
|
||||
|
||||
} // namespace rigby::pretty
|
||||
+10
-1
@@ -14,6 +14,7 @@
|
||||
#include "mcts.h"
|
||||
#include "perft.h"
|
||||
#include "position.h"
|
||||
#include "pretty.h"
|
||||
#include "uci_io.h"
|
||||
|
||||
namespace rigby {
|
||||
@@ -144,7 +145,7 @@ void go(Engine& e, std::istringstream& is) {
|
||||
e.searcher.stop_flag = false;
|
||||
e.worker = std::thread([&e, pos = e.pos, history = e.history, limits] {
|
||||
const SearchResult result = e.searcher.search(pos, history, limits, true);
|
||||
send("bestmove " + result.best.to_uci());
|
||||
send(e.searcher.pretty ? pretty::bestmove(result.best.to_uci()) : "bestmove " + result.best.to_uci());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -198,6 +199,10 @@ void uci_loop() {
|
||||
auto engine = std::make_unique<Engine>();
|
||||
Engine& e = *engine;
|
||||
|
||||
// A human at a terminal gets coloured search output until "uci" arrives.
|
||||
e.searcher.pretty = pretty::init_terminal();
|
||||
if (e.searcher.pretty) send(pretty::banner(ENGINE_NAME, ENGINE_VERSION, ENGINE_AUTHOR));
|
||||
|
||||
std::string line;
|
||||
while (std::getline(std::cin, line)) {
|
||||
std::istringstream is(line);
|
||||
@@ -205,6 +210,10 @@ void uci_loop() {
|
||||
is >> cmd;
|
||||
|
||||
if (cmd == "uci") {
|
||||
if (e.searcher.pretty) {
|
||||
e.stop();
|
||||
e.searcher.pretty = false;
|
||||
}
|
||||
print_id();
|
||||
} else if (cmd == "isready") {
|
||||
send("readyok");
|
||||
|
||||
Reference in new issue
Block a user