From defba40979772cde0a9a716d3d3988db9453e311 Mon Sep 17 00:00:00 2001 From: rektdie Date: Thu, 27 Feb 2025 03:47:56 +0100 Subject: [PATCH] connected TT to search --- source/search.cpp | 25 +++++++++++++++++++------ source/search.h | 4 ++++ source/tt.cpp | 6 +++--- source/tt.h | 3 ++- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 9b85db9..f8fb4cf 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -128,17 +128,27 @@ SearchResults PVS(Board board, int depth, int alpha, int beta) { } } - nodes++; - if (depth == 0) return Quiescence(board, alpha, beta).score; + // if NOT PV node then we try to hit the TTable + if (beta - alpha == 1) { + SearchResults entry = ReadEntry(board.hashKey, depth, alpha, beta); + if (entry.score != invalidEntry) { + return entry; + } + } - int score = -50000; + if (depth == 0) return Quiescence(board, alpha, beta); + + int score = -inf; + int nodeType = AllNode; + + nodes++; GenerateMoves(board, board.sideToMove); SortMoves(board); if (board.currentMoveIndex == 0) { if (board.InCheck(board.sideToMove)) { // checkmate - return -49000 + ply; + return -99000 + ply; } else { // stalemate return 0; } @@ -172,10 +182,12 @@ SearchResults PVS(Board board, int depth, int alpha, int beta) { } if (score >= beta) { + WriteEntry(board.hashKey, depth, beta, CutNode); return beta; } if (score > alpha) { + nodeType = PV; alpha = score; results.bestMove = board.moveList[i]; } @@ -183,6 +195,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta) { results.score = alpha; + WriteEntry(board.hashKey, depth, results.score, nodeType); return results; } @@ -194,13 +207,13 @@ static SearchResults ID(Board &board, SearchParams params) { int inc = board.sideToMove ? params.binc : params.winc; SearchResults safeResults; - safeResults.score = -50000; + safeResults.score = -inf; for (int depth = 1; depth <= 99; depth++) { int timeRemaining = (fullTime / 20) + (inc / 2); timeToSearch = timeRemaining; - SearchResults currentResults = PVS(board, depth, -50000, 50000); + SearchResults currentResults = PVS(board, depth, -inf, inf); if (currentResults.score >= safeResults.score) { safeResults = currentResults; } diff --git a/source/search.h b/source/search.h index f55806f..64f8d0e 100644 --- a/source/search.h +++ b/source/search.h @@ -5,6 +5,10 @@ extern int nodes; extern bool benchStarted; +constexpr int invalidEntry = 111111; +constexpr int inf = 100000; + + class SearchResults { public: int score = 0; diff --git a/source/tt.cpp b/source/tt.cpp index 761b543..65610bf 100644 --- a/source/tt.cpp +++ b/source/tt.cpp @@ -79,7 +79,7 @@ U64 GetHashKey(Board &board) { return key; } -int ReadEntry(U64 &hashKey, int depth, int alpha, int beta) { +SearchResults ReadEntry(U64 &hashKey, int depth, int alpha, int beta) { TTEntry *current = &TTable[hashKey % hashSize]; if (current->hashKey == hashKey) { @@ -94,8 +94,8 @@ int ReadEntry(U64 &hashKey, int depth, int alpha, int beta) { } } - // returning unknown value - return 10000; + // returning invalid value + return invalidEntry; } void WriteEntry(U64 &hashKey, int depth, int score, int nodeType) { diff --git a/source/tt.h b/source/tt.h index 2adb8ea..9cb656c 100644 --- a/source/tt.h +++ b/source/tt.h @@ -1,5 +1,6 @@ #pragma once #include "board.h" +#include "search.h" // 5 MB constexpr int hashSize = 0x500000; @@ -28,5 +29,5 @@ public: extern TTEntry TTable[hashSize]; -int ReadEntry(U64 &hashKey, int depth, int alpha, int beta); +SearchResults ReadEntry(U64 &hashKey, int depth, int alpha, int beta); void WriteEntry(U64 &hashKey, int depth, int score, int nodeType);