From b6c9bbbb8da712d180deeb704cfd3e1efa457977 Mon Sep 17 00:00:00 2001 From: rektdie Date: Thu, 4 Sep 2025 15:20:29 +0200 Subject: [PATCH] implemented tuning | bench 8216434 --- source/search.cpp | 32 +++++++++++-------------- source/search.h | 11 +++++++-- source/tunables.h | 61 +++++++++++++++++++++++++++++++++++++++++++++++ source/uci.cpp | 52 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 134 insertions(+), 22 deletions(-) create mode 100644 source/tunables.h diff --git a/source/search.cpp b/source/search.cpp index 358388a..e4f1206 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -5,6 +5,7 @@ #include "datagen.h" #include "benchmark.h" #include "types.h" +#include "tunables.h" namespace SEARCH { @@ -14,10 +15,7 @@ void InitLMRTable() { if (depth == 0 || move == 0) { lmrTable[depth][move] = 0; } else { - double base = 0.77; - double divisor = 2.36; - - lmrTable[depth][move] = std::floor(base + std::log(depth) * std::log(move) / divisor); + lmrTable[depth][move] = std::floor(lmrBase + std::log(depth) * std::log(move) / lmrDivisor); } } } @@ -159,7 +157,7 @@ static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int int reduction = 0; // Late Move Reduction - if (depth >= 3 && moveSeen >= 2 + (2 * isPV) && !move.IsCapture()) { + if (depth >= lmrDepth && moveSeen >= lmrMoveSeen + (lmrMoveSeenPVModifier * isPV) && !move.IsCapture()) { reduction = lmrTable[depth][moveSeen]; if (cutnode) @@ -409,18 +407,18 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC if (!board.InCheck() && !ctx->excluded) { if (ply) { // Reverse Futility Pruning - int margin = 100 * (depth - improving); - if (!ttHit && staticEval - margin >= beta && depth < 7) { + int margin = rfpMargin * (depth - improving); + if (!ttHit && staticEval - margin >= beta && depth < rfpDepth) { return (beta + (staticEval - beta) / 3); } // Null Move Pruning if (!ctx->doingNullMove && staticEval >= beta) { - if (depth > 1 && !board.InPossibleZug()) { + if (depth > nmpDepth && !board.InPossibleZug()) { Board copy = board; copy.MakeMove(Move()); - const int reduction = 4 + improving + depth / 3; + const int reduction = nmpReduction + improving + depth / 3; ctx->doingNullMove = true; int score = -PVS(copy, depth - reduction, -beta, -beta + 1, ply + 1, ctx, !cutnode).score; @@ -458,10 +456,8 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC // If we are near a leaf node we prune moves // that are late in the list if (!isPV && !board.InCheck() && currMove.IsQuiet() && notMated) { - int lmpBase = 7; - - int lmpThreshold = lmpBase + 4 * depth; + int lmpThreshold = lmpBase + lmpMultiplier * depth; if (moveSeen >= lmpThreshold) { continue; @@ -480,10 +476,10 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC historyScore += ctx->conthist.GetTwoPly(board, currMove, ctx, ply); } - int fpMargin = 100 * depth + historyScore / 32; + int margin = fpMargin * depth + historyScore / 32; if (!isPV && ply && currMove.IsQuiet() - && depth <= 5 && staticEval + fpMargin < alpha && notMated) { + && depth <= fpDepth && staticEval + margin < alpha && notMated) { continue; } @@ -525,7 +521,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC if constexpr (!isPV) { // Double extension - if (singularScore <= sBeta - 1 - 30) { + if (singularScore <= sBeta - 1 - doubleExtMargin) { extension++; } } @@ -537,9 +533,9 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC } // PVS SEE - int SEEThreshold = currMove.IsQuiet() ? -80 * depth : -30 * depth * depth; + int SEEThreshold = currMove.IsQuiet() ? seeQuietThreshold * depth : seeNoisyThreshold * depth * depth; - if (ply && depth <= 10 && !SEE(board, currMove, SEEThreshold)) + if (ply && depth <= seeDepth && !SEE(board, currMove, SEEThreshold)) continue; int reductions = GetReductions(board, currMove, depth, moveSeen, ply, cutnode, ctx); @@ -674,7 +670,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC class AspirationWindow { public: - int delta = 50; + int delta = aspInitialDelta; int alpha = -inf; int beta = inf; diff --git a/source/search.h b/source/search.h index 44b671a..46fce6c 100644 --- a/source/search.h +++ b/source/search.h @@ -5,6 +5,7 @@ #include "move.h" #include #include "stopwatch.h" +#include "tunables.h" #include "tt.h" #include "../external/multi_array.h" @@ -34,9 +35,15 @@ constexpr int inf = 100000; inline int lmrTable[MAX_DEPTH+1][MAX_MOVES]; -constexpr std::array SEEPieceValues = { - 100, 300, 350, 500, 900, 0 +#ifdef TUNING +inline std::array SEEPieceValues = { + seePawnValue, seeKnightValue, seeBishopValue, seeRookValue, seeQueenValue, 0 }; +#else +constexpr std::array SEEPieceValues = { + seePawnValue, seeKnightValue, seeBishopValue, seeRookValue, seeQueenValue, 0 +}; +#endif void InitLMRTable(); class SearchContext; diff --git a/source/tunables.h b/source/tunables.h new file mode 100644 index 0000000..7b4affa --- /dev/null +++ b/source/tunables.h @@ -0,0 +1,61 @@ +#pragma once +#include + +#define TUNING + +#define TUNABLE_LIST \ + X_DOUBLE(lmrBase, 0.77, 0.5, 1.5, 0.01) \ + X_DOUBLE(lmrDivisor, 2.36, 1.5, 4.0, 0.01) \ + X_INT(lmrDepth, 3, 1, 6, 1) \ + X_INT(lmrMoveSeen, 2, 1, 5, 1) \ + X_INT(lmrMoveSeenPVModifier, 2, 1, 5, 1) \ + X_INT(rfpMargin, 100, 50, 200, 5) \ + X_INT(rfpDepth, 7, 4, 12, 1) \ + X_INT(nmpDepth, 1, 1, 4, 1) \ + X_INT(nmpReduction, 4, 2, 6, 1) \ + X_INT(lmpBase, 7, 3, 15, 1) \ + X_INT(lmpMultiplier, 4, 2, 8, 1) \ + X_INT(fpMargin, 100, 50, 200, 5) \ + X_INT(fpDepth, 5, 3, 8, 1) \ + X_INT(doubleExtMargin, 30, 10, 60, 5) \ + X_INT(seeQuietThreshold, -80, -150, -20, 5) \ + X_INT(seeNoisyThreshold, -30, -80, 10, 5) \ + X_INT(seeDepth, 10, 6, 15, 1) \ + X_INT(aspInitialDelta, 50, 20, 100, 5) \ + X_INT(seePawnValue, 100, 80, 120, 5) \ + X_INT(seeKnightValue, 300, 250, 350, 10) \ + X_INT(seeBishopValue, 350, 300, 400, 10) \ + X_INT(seeRookValue, 500, 450, 550, 10) \ + X_INT(seeQueenValue, 900, 800, 1000, 25) + +#ifdef TUNING + #define X_DOUBLE(name, default_val, min_val, max_val, step_val) \ + inline double name = default_val; + #define X_INT(name, default_val, min_val, max_val, step_val) \ + inline int name = default_val; +#else + #define X_DOUBLE(name, default_val, min_val, max_val, step_val) \ + constexpr double name = default_val; + #define X_INT(name, default_val, min_val, max_val, step_val) \ + constexpr int name = default_val; +#endif + +TUNABLE_LIST + +#undef X_DOUBLE +#undef X_INT + +inline void PrintTunables() { + #define X_DOUBLE(name, default_val, min_val, max_val, step_val) \ + std::cout << "option name " #name " type string default " << default_val \ + << " min " << min_val << " max " << max_val << " step " << step_val << std::endl; + + #define X_INT(name, default_val, min_val, max_val, step_val) \ + std::cout << "option name " #name " type spin default " << default_val \ + << " min " << min_val << " max " << max_val << " step " << step_val << std::endl; + + TUNABLE_LIST + + #undef X_DOUBLE + #undef X_INT +} diff --git a/source/uci.cpp b/source/uci.cpp index 9b28d53..e8efd67 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -11,6 +11,7 @@ #include "perft.h" #include "utils.h" #include "datagen.h" +#include "tunables.h" // OS-dependent threading includes #ifdef _WIN32 @@ -146,11 +147,53 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* c StartSearchThread(board, params, ctx); } -static void SetOption(std::string &command, SEARCH::SearchContext* ctx) { +static void SetOption(std::string& command, SEARCH::SearchContext* ctx) { if (command.find("Hash") != std::string::npos) { - U64 hashSize = (ReadParam("value", command) * 1000000) / sizeof(TTEntry); + U64 hashSize = (ReadParam("value", command) * 1000000ULL) / sizeof(TTEntry); ctx->TT.Resize(hashSize); + return; } + + #ifdef TUNING + + size_t namePos = command.find("name"); + if (namePos == std::string::npos) + return; + + namePos += 5; // skip "name " + size_t valuePos = command.find(" value"); + if (valuePos == std::string::npos) + return; + + std::string name = command.substr(namePos, valuePos - namePos); + name.erase(0, name.find_first_not_of(' ')); + name.erase(name.find_last_not_of(' ') + 1); + + int rawValue = ReadParam("value", command); + + #define X_DOUBLE(paramName, default_val, min_val, max_val, step_val) \ + if (name == #paramName) { \ + double v = static_cast(rawValue); \ + if (v < min_val) v = min_val; \ + if (v > max_val) v = max_val; \ + paramName = v; \ + return; \ + } + + #define X_INT(paramName, default_val, min_val, max_val, step_val) \ + if (name == #paramName) { \ + int v = rawValue; \ + if (v < min_val) v = min_val; \ + if (v > max_val) v = max_val; \ + paramName = v; \ + return; \ + } + + TUNABLE_LIST + + #undef X_DOUBLE + #undef X_INT + #endif } static void PrintEngineInfo() { @@ -257,6 +300,11 @@ void UCILoop(Board &board) { continue; } + if (input.find("tunables") != std::string::npos) { + PrintTunables(); + continue; + } + if (input.find("datagen") != std::string::npos) { DATAGEN::Run(500000*2, 22); continue;