diff --git a/source/benchmark.cpp b/source/benchmark.cpp index 1699ab5..fce2932 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -63,7 +63,7 @@ constexpr std::array fenPositions = { void RunBenchmark() { Board board; - int depth = 6; + int depth = 8; int ply = 0; benchStarted = true; searchStopped = false; diff --git a/source/board.cpp b/source/board.cpp index 77444e4..6b1736f 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -22,7 +22,7 @@ void Board::Reset() { pieceThreats = std::array(); colorThreats = std::array(); - moveList = std::array(); + moveList = std::array(); currentMoveIndex = 0; diff --git a/source/board.h b/source/board.h index 5d4cc75..4223eca 100644 --- a/source/board.h +++ b/source/board.h @@ -7,6 +7,8 @@ constexpr int nullPieceType = 100; constexpr int noEPTarget = -1; +constexpr int MAX_MOVES = 218; + inline int positionIndex = 0; inline U64 positionHistory[1000]; @@ -24,7 +26,7 @@ public: std::array pieceThreats; std::array colorThreats; - std::array moveList; + std::array moveList; int currentMoveIndex = 0; bool sideToMove = White; diff --git a/source/main.cpp b/source/main.cpp index 93eb880..000c9d5 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -3,11 +3,13 @@ #include "uci.h" #include "utils.h" #include "benchmark.h" +#include "search.h" int main(int argc, char* argv[]) { MOVEGEN::initLeaperAttacks(); MOVEGEN::initSliderAttacks(); UTILS::InitZobrist(); + SEARCH::InitLMRTable(); Board board; diff --git a/source/search.cpp b/source/search.cpp index 9b9fafb..dd30f21 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -18,6 +18,21 @@ inline PVLine pvLine; Stopwatch sw; +void InitLMRTable() { + for (int depth = 0; depth <= MAX_DEPTH; depth++) { + for (int move = 0; move < MAX_MOVES; move++) { + 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); + } + } + } +} + static int ScoreMove(Board &board, Move &move, int ply) { TTEntry *current = TT.GetRawEntry(board.hashKey); if (current->bestMove == move) { @@ -48,8 +63,8 @@ static int ScoreMove(Board &board, Move &move, int ply) { } static void SortMoves(Board &board, int ply) { - std::array scores; - std::array indices; + std::array scores; + std::array indices; // Initialize scores and indices for (int i = 0; i < board.currentMoveIndex; i++) { @@ -64,7 +79,7 @@ static void SortMoves(Board &board, int ply) { }); // Create a temporary move list and reorder the moveList based on the sorted indices - std::array sortedMoves; + std::array sortedMoves; for (int i = 0; i < board.currentMoveIndex; i++) { sortedMoves[i] = board.moveList[indices[i]]; } @@ -107,10 +122,7 @@ static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int // Late Move Reduction if (depth >= 3 && moveSeen >= 3 && !move.IsCapture()) { - double base = 0.77; - double divisor = 2.36; - - reduction = std::floor(base + std::log(depth) * std::log(moveSeen) / divisor); + reduction = lmrTable[depth][moveSeen]; } return reduction; @@ -329,7 +341,7 @@ static SearchResults ID(Board &board, SearchParams params) { int elapsed = 0; sw.Restart(); - for (int depth = 1; depth <= 99; depth++) { + for (int depth = 1; depth <= MAX_DEPTH; depth++) { timeToSearch = std::max((fullTime / 20) + (inc / 2), 4); int softTime = timeToSearch * 0.65; diff --git a/source/search.h b/source/search.h index 1490fb4..f286246 100644 --- a/source/search.h +++ b/source/search.h @@ -8,8 +8,8 @@ namespace SEARCH { constexpr int MATE_SCORE = -99000; -constexpr int maxDepth = 64; -constexpr int maxHistory = 16384; +constexpr int MAX_DEPTH = 64; +constexpr int MAX_HISTORY = 16384; extern U64 nodes; extern bool benchStarted; @@ -21,6 +21,10 @@ inline bool searchStopped = false; // [id][ply] inline int killerMoves[2][64]; +inline int lmrTable[MAX_DEPTH+1][MAX_MOVES]; + +void InitLMRTable(); + class SearchResults { public: int score = 0; @@ -43,9 +47,9 @@ private: int historyMoves[2][64][64]; public: void Update(bool stm, Move move, int bonus) { - int clampedBonus = std::clamp(bonus, -maxHistory, maxHistory); + int clampedBonus = std::clamp(bonus, -MAX_HISTORY, MAX_HISTORY); historyMoves[stm][move.MoveFrom()][move.MoveTo()] += - clampedBonus - historyMoves[stm][move.MoveFrom()][move.MoveTo()] * std::abs(clampedBonus) / maxHistory; + clampedBonus - historyMoves[stm][move.MoveFrom()][move.MoveTo()] * std::abs(clampedBonus) / MAX_HISTORY; } void Clear() { @@ -67,8 +71,8 @@ inline History history; class PVLine { private: - int length[maxDepth] = {}; - Move table[maxDepth][maxDepth] = {}; + int length[MAX_DEPTH] = {}; + Move table[MAX_DEPTH][MAX_DEPTH] = {}; public: void SetLength(int ply) { length[ply] = ply; @@ -92,8 +96,8 @@ public: } void Clear() { - length[maxDepth - 1] = {}; - table[maxDepth - 1][maxDepth - 1] = {}; + length[MAX_DEPTH - 1] = {}; + table[MAX_DEPTH - 1][MAX_DEPTH - 1] = {}; } };