From 5f8e143cf8f32c7e6e941c6e2cd7aa33c603a81b Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 24 Mar 2026 14:12:53 +0100 Subject: [PATCH 1/2] WDL model bench: 1308674 --- source/search.cpp | 64 +++++++++++++++++++++++++++++------------ source/search.h | 2 +- source/uci.cpp | 7 +++++ source/uci.h | 1 + source/wdl.h | 72 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 127 insertions(+), 19 deletions(-) create mode 100644 source/wdl.h diff --git a/source/search.cpp b/source/search.cpp index 0cef515..34d4820 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -7,11 +7,32 @@ #include "types.h" #include "tunables.h" #include "movepicker.h" +#include "wdl.h" #include "termcolor.hpp" #include namespace SEARCH { +static WDLTriplet GetRoundedWDL(int score, Board& board) { + WDLTriplet wdl = getWDL(score, board); + + wdl.wins = (wdl.wins + 5) / 10; + wdl.losses = (wdl.losses + 5) / 10; + wdl.draws = 100 - wdl.wins - wdl.losses; + + if (wdl.draws < 0) { + if (wdl.wins >= wdl.losses) { + wdl.wins += wdl.draws; + } else { + wdl.losses += wdl.draws; + } + + wdl.draws = 0; + } + + return wdl; +} + void InitLMRTable() { for (int i = 0; i < 2; i++) { const double base = i ? lmrBaseQuiet : lmrBaseNoisy; @@ -862,18 +883,6 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) { double nodeScaling = 1; - if (!UCIEnabled && ctx->doPrint && (mode == normal || mode == nodesMode)) { - std::cout << termcolor::bold; - std::cout << std::setw(6) << std::left << "Depth" - << std::setw(10) << std::right << "Time" - << std::setw(9) << std::right << "Score" - << std::setw(11) << std::right << "Nodes" - << std::setw(12) << std::right << "NPS" - << " PV" << std::endl; - std::cout << termcolor::reset; - std::cout << std::string(70, '-') << std::endl; - } - ctx->sw.Restart(); for (int depth = 1; depth <= toDepth; depth++) { @@ -911,7 +920,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) { if constexpr (mode == normal || mode == nodesMode) { if (ctx->doPrint) - PrintSearchInfo(ctx, safeResults, depth, elapsed); + PrintSearchInfo(board, ctx, safeResults, depth, elapsed); if constexpr (mode == normal) { if (ctx->sw.GetElapsedMS() >= softTime) { @@ -981,7 +990,11 @@ int MoveEstimatedValue(Board& board, Move& move) { return value; } -void PrintSearchInfo(SearchContext* ctx, SearchResults& results, int depth, int elapsed) { +void PrintSearchInfo(Board& board, SearchContext* ctx, SearchResults& results, int depth, int elapsed) { + WDLTriplet wdl = getWDL(results.score, board); + const bool isMateScore = std::abs(results.score) + MAX_DEPTH >= MATE_SCORE; + const int normalizedScore = isMateScore ? results.score : scaleEval(results.score, board); + if (UCIEnabled) { std::cout << "info "; std::cout << "depth " << depth; @@ -989,13 +1002,17 @@ void PrintSearchInfo(SearchContext* ctx, SearchResults& results, int depth, int std::cout << " time " << elapsed; std::cout << " score "; - if (std::abs(results.score) + MAX_DEPTH >= MATE_SCORE) { + if (isMateScore) { int mateIn = (MATE_SCORE - (std::abs(results.score) - 1)) / 2; mateIn = results.score < 0 ? mateIn * -1 : mateIn; std::cout << "mate " << mateIn; } else { - std::cout << "cp " << results.score; + std::cout << "cp " << normalizedScore; + } + + if (UCIShowWDL) { + std::cout << " wdl " << wdl.wins << ' ' << wdl.draws << ' ' << wdl.losses; } std::cout << " nodes " << ctx->nodes << " nps " << int(ctx->nodes/ctx->sw.GetElapsedSec()); @@ -1026,7 +1043,7 @@ void PrintSearchInfo(SearchContext* ctx, SearchResults& results, int depth, int bool isMate = false; int mateIn = 0; - if (std::abs(results.score) + MAX_DEPTH >= MATE_SCORE) { + if (isMateScore) { isMate = true; mateIn = (MATE_SCORE - (std::abs(results.score) - 1)) / 2; mateIn = results.score < 0 ? mateIn * -1 : mateIn; @@ -1037,7 +1054,7 @@ void PrintSearchInfo(SearchContext* ctx, SearchResults& results, int depth, int } } else { scoreStr << std::showpos << std::fixed << std::setprecision(2) - << (results.score / 100.0) << std::noshowpos; + << (normalizedScore / 100.0) << std::noshowpos; } if (isMate) { @@ -1061,6 +1078,17 @@ void PrintSearchInfo(SearchContext* ctx, SearchResults& results, int depth, int } else { std::cout << termcolor::color<251>; } + + WDLTriplet displayWDL = GetRoundedWDL(results.score, board); + std::stringstream wdlStr; + wdlStr << displayWDL.wins << "W " + << displayWDL.draws << "D " + << displayWDL.losses << "L"; + std::cout << std::setw(14) << std::right << wdlStr.str(); + + std::stringstream hashfullStr; + hashfullStr << "TT: " << (ctx->TT->GetUsedPercentage() + 5) / 10 << '%'; + std::cout << std::setw(10) << std::right << hashfullStr.str(); std::stringstream nodesStr; if (ctx->nodes >= 1000000) { diff --git a/source/search.h b/source/search.h index f4dcfff..94be06e 100644 --- a/source/search.h +++ b/source/search.h @@ -265,7 +265,7 @@ SearchResults SearchPosition(Board &board, SearchParams params, SearchContext* c bool IsDraw(Board &board, SearchContext* ctx); -void PrintSearchInfo(SearchContext* ctx, SearchResults& results, int depth, int elapsed); +void PrintSearchInfo(Board& board, SearchContext* ctx, SearchResults& results, int depth, int elapsed); int MoveEstimatedValue(Board& board, Move& move); } diff --git a/source/uci.cpp b/source/uci.cpp index 5b222b1..26354b4 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -177,6 +177,12 @@ static void SetOption(std::string& command, SEARCH::SearchContext* ctx) { return; } + if (command.find("UCI_ShowWDL") != std::string::npos) { + UCIShowWDL = command.find("value true") != std::string::npos + || command.find("value 1") != std::string::npos; + return; + } + #ifdef TUNING size_t namePos = command.find("name"); @@ -224,6 +230,7 @@ static void PrintEngineInfo() { std::cout << "id author rektdie" << std::endl; std::cout << "option name Hash type spin default 8 min 1 max 1024" << std::endl; std::cout << "option name Threads type spin default 1 min 1 max 512" << std::endl; + std::cout << "option name UCI_ShowWDL type check default false" << std::endl; #ifdef TUNING PrintTunablesUCI(); diff --git a/source/uci.h b/source/uci.h index 21fb131..8d20392 100644 --- a/source/uci.h +++ b/source/uci.h @@ -3,6 +3,7 @@ #include "board.h" inline bool UCIEnabled = false; +inline bool UCIShowWDL = false; inline int threads = 1; inline bool searchStopped = false; diff --git a/source/wdl.h b/source/wdl.h new file mode 100644 index 0000000..ae9f37f --- /dev/null +++ b/source/wdl.h @@ -0,0 +1,72 @@ +#pragma once + +#include +#include + +#include "board.h" + +struct WinRateParams { + double a; + double b; +}; + +// From SF +inline WinRateParams winRateParams(const Board& board) { + int material = board.pieces[Pawn].PopCount() + + 3 * board.pieces[Knight].PopCount() + + 3 * board.pieces[Bishop].PopCount() + + 5 * board.pieces[Rook].PopCount() + + 9 * board.pieces[Queen].PopCount(); + + // The fitted model only uses data for material counts in [17, 78], and is anchored at count 58. + double m = std::clamp(material, 17, 78) / 58.0; + + // Return a = p_a(material) and b = p_b(material), see github.com/official-stockfish/WDL_model + constexpr double as[] = {-977.81543545, 2519.99289616, -2279.71042020, 1197.60065700}; + constexpr double bs[] = {-224.61509659, 557.03359920, -440.33347428, 226.68436468}; + + 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]; + + return {a, b}; +} + +// The win rate model is 1 / (1 + exp((a - eval) / b)), where a = p_a(material) and b = p_b(material). +// It fits the LTC fishtest statistics rather accurately. +inline int winRateModel(int v, const Board& board) { + auto [a, b] = winRateParams(board); + + // Return the win rate in per mille units, rounded to the nearest integer. + return int(0.5 + 1000 / (1 + std::exp((a - static_cast(v)) / b))); +} + +struct WDLTriplet { + int wins; + int draws; + int losses; +}; + +inline WDLTriplet getWDL(int v, const Board& board) { + int wins = std::clamp(winRateModel(v, board), 0, 1000); + int losses = std::clamp(winRateModel(-v, board), 0, 1000); + int draws = 1000 - wins - losses; + + if (draws < 0) { + if (wins >= losses) { + wins += draws; + } else { + losses += draws; + } + + draws = 0; + } + + return {wins, draws, losses}; +} + +inline int scaleEval(int eval, const Board& board) { + // This line disables eval scaling, it will be turned back on when the engine has better performance/converts better + auto [a, b] = winRateParams(board); + + return std::round(100 * eval / a); +} -- 2.47.3 From b3ffc1f32f5bf1416079992bf220ec3dde2a84a2 Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 24 Mar 2026 14:23:19 +0100 Subject: [PATCH 2/2] removed a comment bench: 1308674 --- source/wdl.h | 1 - 1 file changed, 1 deletion(-) diff --git a/source/wdl.h b/source/wdl.h index ae9f37f..807b93e 100644 --- a/source/wdl.h +++ b/source/wdl.h @@ -65,7 +65,6 @@ inline WDLTriplet getWDL(int v, const Board& board) { } inline int scaleEval(int eval, const Board& board) { - // This line disables eval scaling, it will be turned back on when the engine has better performance/converts better auto [a, b] = winRateParams(board); return std::round(100 * eval / a); -- 2.47.3