diff --git a/Makefile b/Makefile index d64c121..5475eda 100644 --- a/Makefile +++ b/Makefile @@ -33,13 +33,19 @@ LDFLAGS := -O3 -flto ifeq ($(PLATFORM),mac) ifeq ($(ARCH),arm64) - ARCH_FLAGS := + # Apple Silicon + ARCH_FLAGS := -mcpu=apple-m1 else ARCH_FLAGS := -march=native endif else - ARCH_FLAGS := -march=native - LDFLAGS += -fuse-ld=lld -static + ifeq ($(ARCH),aarch64) + # Generic Linux ARM64 + ARCH_FLAGS := -mcpu=native + else + ARCH_FLAGS := -march=native + LDFLAGS += -fuse-ld=lld -static + endif endif CXXFLAGS += $(ARCH_FLAGS) diff --git a/Scripts/game.pgn b/Scripts/game.pgn index d73d958..a3409e7 100644 --- a/Scripts/game.pgn +++ b/Scripts/game.pgn @@ -1,45 +1,59 @@ [Event "?"] [Site "?"] -[Date "2025.04.01"] +[Date "2025.09.04"] [Round "1"] [White "Eleanor-base"] [Black "Eleanor-dev"] [Result "1-0"] -[FEN "2r5/1k6/2p4p/4p3/3b1p1B/5B1P/2R5/7K b - - 0 1"] -[GameDuration "00:00:05"] -[GameEndTime "2025-04-01T18:10:20.331 Central Europe Daylight Time"] -[GameStartTime "2025-04-01T18:10:14.365 Central Europe Daylight Time"] -[PlyCount "58"] +[FEN "rn1qk1nr/ppp2pRp/3bp3/3p4/3P4/2P2P1P/PP2PP2/RNBQKB2 b Qkq - 0 1"] +[GameDuration "00:01:31"] +[GameEndTime "2025-09-04T22:39:24.808 CEST"] +[GameStartTime "2025-09-04T22:37:53.089 CEST"] +[PlyCount "84"] [SetUp "1"] [Termination "abandoned"] -[TimeControl "2.53+0.03"] +[TimeControl "37.56+0.38"] -1... Kb6 {-0.43 11/0 127 176492} 2. Kg2 {+0.45 10/0 117 182773} -Kb5 {-0.43 10/0 139 43190} 3. Bf2 {+0.48 10/0 94 154681} -Rg8+ {-0.41 10/0 104 181006} 4. Kf1 {+0.43 11/0 137 107880} -c5 {-0.42 11/0 114 186919} 5. Ke2 {+0.42 10/0 131 26558} -Rd8 {-0.31 11/0 126 178073} 6. Rd2 {+0.36 8/0 129 64726} -c4 {+0.11 11/0 124 101858} 7. Bxd4 {-0.11 11/0 86 135900} -Rxd4 {+0.10 11/0 124 121262} 8. Rxd4 {-0.01 13/0 117 207646} -exd4 {0.00 14/0 95 197856} 9. h4 {0.00 13/0 105 197264} -Kb4 {0.00 14/0 91 195163} 10. Kd2 {0.00 13/0 111 105382} -Kc5 {0.00 13/0 109 62420} 11. h5 {0.00 14/0 78 150612} Kb4 {0.00 16/0 79 181784} -12. Bd1 {0.00 15/0 86 193192} Kc5 {0.00 16/0 97 111839} -13. Kc2 {0.00 17/0 90 192373} Kb4 {0.00 17/0 95 120438} -14. Bf3 {0.00 17/0 76 176571} Kc5 {0.00 17/0 101 66844} -15. Ba8 {0.00 18/0 75 165249} Kb4 {0.00 18/0 86 198233} -16. Bf3 {0.00 18/0 77 183908} d3+ {+0.01 16/0 69 164278} -17. Kd2 {-0.03 16/0 81 145118} Kb3 {0.00 17/0 84 107987} -18. Be4 {0.00 16/0 61 125373} f3 {+0.09 17/0 75 163654} -19. Bxd3 {-0.01 16/0 86 109653} cxd3 {+0.57 18/0 55 140500} -20. Kxd3 {-0.97 22/0 73 227257} f2 {+0.98 22/0 71 204108} -21. Ke2 {-0.99 20/0 81 29772} Kc4 {+1.48 22/0 62 180691} -22. Kxf2 {-2.11 23/0 59 175088} Kd4 {+2.51 27/0 52 189697} -23. Kf3 {-3.34 26/0 62 187791} Ke5 {+2.50 32/0 48 167062} -24. Ke3 {-2.24 28/0 54 178563} Kf5 {+2.15 31/0 72 166014} -25. Kf3 {-2.22 32/0 52 188757} Kg5 {0.00 35/0 49 143292} -26. Ke4 {-2.09 32/0 62 209505} Kxh5 {0.00 39/0 48 159744} -27. Kf5 {-2.09 33/0 70 226933} Kh4 {0.00 52/0 46 161548} -28. Kf4 {0.00 37/0 51 178144} h5 {0.00 64/0 29 95868} -29. Kf3 {0.00 51/0 46 165319} Kh3 {0.00 64/0 15 14259} -30. Kf4 {0.00 52/0 64 186059, Black disconnects} 1-0 \ No newline at end of file +1... Ne7 {-1.65 14/26 1861 1544983} 2. Rg1 {+1.64 12/23 2087 934687} +c5 {-1.74 15/32 1565 1167061} 3. f4 {+1.61 13/27 2002 772197} +Nd7 {-1.59 15/31 1455 1090717} 4. e3 {+1.84 14/31 1753 1387350} +Qb6 {-1.65 14/31 1385 1273463} 5. a4 {+1.98 14/28 1505 1375155} +Qc7 {-2.11 14/29 1849 830961} 6. dxc5 {+1.98 15/30 1571 1101192} +Nxc5 {-2.16 15/34 1778 545375} 7. a5 {+1.90 14/29 1555 1021903} +a6 {-2.22 15/33 1706 667700} 8. Nd2 {+2.38 14/29 1267 922229} +O-O-O {-2.34 14/31 1641 580655} 9. b4 {+2.62 14/31 1463 975771} +Ne4 {-2.37 15/39 1336 932258} 10. Nxe4 {+2.55 13/31 1579 228776} +dxe4 {-2.85 15/31 1004 694847} 11. Bd2 {+3.02 13/30 1247 753167} +Ng6 {-2.40 14/35 1118 862794} 12. Qb1 {+2.40 12/26 1475 339681} +f5 {-1.97 13/31 1465 304169} 13. Qb3 {+2.25 12/34 957 597544} +Nh4 {-2.21 14/38 1409 432849} 14. Be2 {+2.30 13/35 1343 843064} +Nf3+ {-2.18 14/40 1355 318067} 15. Bxf3 {+2.49 14/39 1346 645681} +exf3 {-2.59 15/30 1115 864898} 16. O-O-O {+2.85 14/35 1297 425564} +Rhe8 {-2.11 13/34 1269 275858} 17. c4 {+2.35 13/30 1249 86929} +e5 {-1.53 14/40 1188 1007892} 18. Bc3 {+2.71 12/37 1207 154226} +exf4 {-0.71 13/45 1186 280646} 19. c5 {+0.71 13/34 883 618110} +Be5 {-0.52 13/65 1145 440206} 20. Rxd8+ {+0.56 13/36 861 757974} +Qxd8 {-0.55 15/56 1106 373775} 21. Bxe5 {+0.98 14/49 845 689690} +Rxe5 {-0.87 14/83 799 511973} 22. exf4 {+0.98 13/36 1092 370730} +Re2 {-0.87 13/89 751 367978} 23. Rg8 {+1.05 13/43 1056 334030} +Re8 {-0.87 13/79 1030 242199} 24. Rxe8 {+1.13 12/34 1023 141620} +Qxe8 {-0.87 13/41 998 22345} 25. Qd1 {+1.46 11/36 991 227470} +Qe4 {-0.89 12/59 968 192246} 26. Qd2 {+1.17 13/42 856 592181} +Qc4+ {-0.90 11/95 891 510827} 27. Kb2 {+1.46 12/38 937 154015} +h5 {-1.01 11/66 913 404234} 28. Qc3 {+1.38 13/37 909 437481} +Qe2+ {-0.73 10/76 886 55851} 29. Qc2 {+1.42 14/34 882 272787} +Qe4 {-1.19 10/49 860 32225} 30. Kb3 {+1.27 12/37 824 598253} +Qxf4 {-1.13 10/98 838 184366} 31. c6 {+1.24 12/51 734 627843} +Qe4 {-0.96 10/103 814 271816} 32. cxb7+ {+1.24 13/50 817 324130} +Kxb7 {-0.96 9/16 792 1604} 33. Qc3 {+1.24 14/30 796 171391} +Qd5+ {-1.20 8/35 772 15385} 34. Ka3 {+1.23 11/40 525 374116} +Qe4 {-1.25 8/85 752 9999} 35. Qd2 {+1.19 12/37 767 281732} +Kc7 {-1.09 10/65 733 175302} 36. Qb2 {+1.21 12/40 748 316739} +h4 {-1.09 7/48 715 39908} 37. Qc1+ {+1.19 11/38 529 523703} +Kd8 {-0.65 10/83 700 298167} 38. Qc3 {+1.26 12/49 723 322911} +Kd7 {-0.64 9/44 684 18906} 39. Qg7+ {+1.23 11/37 537 395773} +Kc8 {-1.04 8/14 667 847} 40. Qf7 {+1.23 11/45 572 501521} +Qd3+ {-1.04 7/81 653 28141} 41. Qb3 {+1.23 11/36 689 130089} +Qe2 {-1.28 8/54 639 10579} 42. Qc3+ {+1.08 11/38 581 477548} +Kd7 {-0.67 8/40 627 10552} 43. Qb2 {+1.08 11/33 663 104343, Black disconnects} +1-0 diff --git a/source/search.cpp b/source/search.cpp index 358388a..cf777ee 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); } } } @@ -65,7 +63,7 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { targetType = Pawn; } - return 50000 * ((SEE(board, move, -100))) + (100 * targetType - attackerType + 105); + return 50000 * ((SEE(board, move, seeOrderingThreshold))) + (100 * targetType - attackerType + 105); } else { if (ctx->killerMoves[0][ply] == move) { return 41000; @@ -160,27 +158,30 @@ static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int // Late Move Reduction if (depth >= 3 && moveSeen >= 2 + (2 * isPV) && !move.IsCapture()) { - reduction = lmrTable[depth][moveSeen]; + reduction = lmrTable[depth][moveSeen] * 1024; if (cutnode) - reduction += 2; + reduction += lmrCutnode; if constexpr (isPV) - reduction--; + reduction -= lmrIsPV; // History LMR int historyReduction = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()]; - if (ply > 0) historyReduction += ctx->conthist.GetOnePly(board, move, ctx, ply); - historyReduction /= 8192; + reduction /= 1024; + + historyReduction = historyReduction / lmrHistoryDivisor; + reduction -= historyReduction; } return std::clamp(reduction, -1, depth - 1); } + bool SEE(Board& board, Move& move, int threshold) { int from = move.MoveFrom(); int to = move.MoveTo(); @@ -313,7 +314,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear ctx->ss[ply].moveTo = board.moveList[i].MoveTo(); ctx->ss[ply].side = board.sideToMove; - if (!SEE(board, board.moveList[i], 0)) + if (!SEE(board, board.moveList[i], seeQsThreshold)) continue; if (copy.positionIndex >= ctx->positionHistory.size()) { @@ -409,7 +410,7 @@ 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); + int margin = rfpMargin * (depth - improving); if (!ttHit && staticEval - margin >= beta && depth < 7) { return (beta + (staticEval - beta) / 3); } @@ -458,10 +459,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 = 7 + 4 * depth; if (moveSeen >= lmpThreshold) { continue; @@ -480,10 +479,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 <= 5 && staticEval + margin < alpha && notMated) { continue; } @@ -525,7 +524,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,7 +536,7 @@ 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)) continue; @@ -587,7 +586,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC ctx->killerMoves[1][ply] = ctx->killerMoves[0][ply]; ctx->killerMoves[0][ply] = currMove; - int bonus = 300 * depth - 250; + int bonus = historyBonusMultiplier * depth - historyBonusSub; ctx->history.Update(board.sideToMove, currMove, bonus); @@ -674,7 +673,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; @@ -690,7 +689,7 @@ public: } void Set(int score) { - delta = 50; + delta = aspInitialDelta; alpha = score - delta; beta = score + delta; } 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..9084a92 --- /dev/null +++ b/source/tunables.h @@ -0,0 +1,91 @@ +#pragma once +#include + +#define TUNING + +#define AUTO_STEP(min_val, max_val) ((max_val - min_val) / 20.0) + +#define TUNABLE_LIST \ + X_DOUBLE(lmrBase, 0.77, 0.5, 1.5) \ + X_DOUBLE(lmrDivisor, 2.36, 1.5, 3.5) \ + X_INT(lmrCutnode, 2048, 1024, 3072) \ + X_INT(lmrIsPV, 1024, 512, 2048) \ + X_INT(lmrHistoryDivisor, 8192, 6000, 10000) \ + X_INT(rfpMargin, 100, 50, 200) \ + X_INT(fpMargin, 100, 50, 200) \ + X_INT(doubleExtMargin, 30, 10, 60) \ + X_INT(seeQuietThreshold, -80, -150, -20) \ + X_INT(seeNoisyThreshold, -30, -80, 20) \ + X_INT(aspInitialDelta, 50, 20, 80) \ + X_INT(seePawnValue, 100, 80, 120) \ + X_INT(seeKnightValue, 300, 250, 350) \ + X_INT(seeBishopValue, 350, 300, 400) \ + X_INT(seeRookValue, 500, 400, 600) \ + X_INT(seeQueenValue, 900, 750, 1100) \ + X_INT(seeQsThreshold, 0, -50, 50) \ + X_INT(seeOrderingThreshold, -100, -120, -80) \ + X_INT(historyBonusMultiplier, 300, 270, 330) \ + X_INT(historyBonusSub, 250, 230, 280) + +#ifdef TUNING + #define X_DOUBLE(name, default_val, min_val, max_val) \ + inline double name = default_val; \ + constexpr double name##_step = AUTO_STEP(min_val, max_val); + #define X_INT(name, default_val, min_val, max_val) \ + inline int name = default_val; \ + constexpr double name##_step = AUTO_STEP(min_val, max_val); +#else + #define X_DOUBLE(name, default_val, min_val, max_val) \ + constexpr double name = default_val; \ + constexpr double name##_step = AUTO_STEP(min_val, max_val); + #define X_INT(name, default_val, min_val, max_val) \ + constexpr int name = default_val; \ + constexpr double name##_step = AUTO_STEP(min_val, max_val); +#endif + +TUNABLE_LIST + +#undef X_DOUBLE +#undef X_INT + +inline void PrintTunables() { + #define X_DOUBLE(name, default_val, min_val, max_val) \ + std::cout << #name << ", float, " << name \ + << ", " << min_val \ + << ", " << max_val \ + << ", " << name##_step \ + << ", 0.002" << std::endl; + + #define X_INT(name, default_val, min_val, max_val) \ + std::cout << #name << ", int, " << name \ + << ", " << min_val \ + << ", " << max_val \ + << ", " << name##_step \ + << ", 0.002" << std::endl; + + TUNABLE_LIST + + #undef X_DOUBLE + #undef X_INT +} + +inline void PrintTunablesUCI() { + #define X_DOUBLE(name, default_val, min_val, max_val) \ + std::cout << "option name " #name \ + << " type string default " << name \ + << " min " << min_val \ + << " max " << max_val \ + << std::endl; + + #define X_INT(name, default_val, min_val, max_val) \ + std::cout << "option name " #name \ + << " type spin default " << name \ + << " min " << min_val \ + << " max " << max_val \ + << std::endl; + + TUNABLE_LIST + + #undef X_DOUBLE + #undef X_INT +} diff --git a/source/uci.cpp b/source/uci.cpp index 9b28d53..4bba4e9 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 @@ -47,35 +48,44 @@ static void ParsePosition(Board &board, std::string_view command, SEARCH::Search MOVEGEN::GenerateMoves(board); } -static int ReadParam(std::string param, std::string &command) { +static double ReadParam(const std::string& param, const std::string &command) { size_t pos = command.find(param); - if (pos != std::string::npos) { - pos += param.length() + 1; - bool isNegative = false; - std::string result = ""; + if (pos == std::string::npos) + return 0.0; - // Check for negative sign - if (pos < command.length() && command[pos] == '-') { - isNegative = true; - pos++; + pos += param.length() + 1; // skip "param " + std::string result; + + // optional sign + if (pos < command.length() && (command[pos] == '-' || command[pos] == '+')) { + result += command[pos]; + pos++; + } + + // digits and optional decimal point + bool decimalSeen = false; + while (pos < command.length() && (std::isdigit(command[pos]) || command[pos] == '.')) { + if (command[pos] == '.') { + if (decimalSeen) break; // only one decimal point + decimalSeen = true; } + result += command[pos++]; + } - // Extract digits - while (pos < command.length() && std::isdigit(command[pos])) { - result += command[pos]; - pos++; - } - - // Ensure result is not empty before calling stoi - if (!result.empty()) { - int value = std::stoi(result); - return isNegative ? -value : value; + if (!result.empty()) { + try { + return std::stod(result); + } catch (const std::out_of_range&) { + return (result[0] == '-') ? -1e308 : 1e308; // clamp to double range + } catch (const std::invalid_argument&) { + return 0.0; } } - return 0; + return 0.0; } + #ifdef _WIN32 // Windows implementation using std::thread template @@ -146,11 +156,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); + + double rawValue = ReadParam("value", command); + + #define X_DOUBLE(paramName, default_val, min_val, max_val) \ + if (name == #paramName) { \ + double v = 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) \ + if (name == #paramName) { \ + int v = static_cast(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() { @@ -158,6 +210,11 @@ 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 1" << std::endl; + + #ifdef TUNING + PrintTunablesUCI(); + #endif + std::cout << "uciok" << std::endl; } @@ -257,9 +314,14 @@ 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; } } -} \ No newline at end of file +}