Wdl model #168

Merged
rektdie merged 2 commits from wdl-model into main 2026-03-24 14:24:05 +01:00
5 changed files with 126 additions and 19 deletions

No files matched your search

+46 -18
View File
@@ -7,11 +7,32 @@
#include "types.h"
#include "tunables.h"
#include "movepicker.h"
#include "wdl.h"
#include "termcolor.hpp"
#include <iomanip>
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) {
+1 -1
View File
@@ -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);
}
+7
View File
@@ -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();
+1
View File
@@ -3,6 +3,7 @@
#include "board.h"
inline bool UCIEnabled = false;
inline bool UCIShowWDL = false;
inline int threads = 1;
inline bool searchStopped = false;
+71
View File
@@ -0,0 +1,71 @@
#pragma once
#include <algorithm>
#include <cmath>
#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<double>(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) {
auto [a, b] = winRateParams(board);
return std::round(100 * eval / a);
}