From 7c4fbc1f6dfd1837114f45f0eeb83da25913e653 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sat, 13 Jul 2024 22:20:15 +0200 Subject: [PATCH] added Transposition tables --- source/search.cpp | 56 ++++++++++++++++++++++++++++++++++------------- source/tt.cpp | 30 +++++++++++++++++++++++++ source/tt.h | 16 ++++++++++++++ source/types.h | 6 +++++ 4 files changed, 93 insertions(+), 15 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 6ea69fd..71825a9 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -2,6 +2,7 @@ #include "movegen.h" #include "evaluate.h" #include +#include "tt.h" static inline int nodes = 0; static inline int ply = 0; @@ -65,16 +66,26 @@ void ListScores(Board &board) { for (int i = 0; i < board.currentMoveIndex; i++) { Move currentMove = board.moveList[i]; - std::cout << squareCoords[currentMove.MoveFrom()] << squareCoords[currentMove.MoveTo()]; std::cout << ": " << ScoreMove(board, currentMove) << '\n'; + std::cout << squareCoords[currentMove.MoveFrom()] << squareCoords[currentMove.MoveTo()]; + std::cout << ": " << ScoreMove(board, currentMove) << '\n'; } } static int Quiescence(Board board, int alpha, int beta) { + int score = ReadEntry(board.hashKey, ply, alpha, beta); + + if (score != 10000) { + return score; + } + + int nodeType = AllNode; + nodes++; int staticScore = Evaluate(board); if (staticScore >= beta) { + WriteEntry(board.hashKey, ply, beta, PV); return beta; } @@ -90,25 +101,39 @@ static int Quiescence(Board board, int alpha, int beta) { if (board.moveList[i].IsCapture()) { Board copy = board; board.MakeMove(board.moveList[i]); - int score = -Quiescence(board, -beta, -alpha); + score = -Quiescence(board, -beta, -alpha); board = copy; if (score >= beta) { + WriteEntry(board.hashKey, ply, beta, CutNode); return beta; } if (score > alpha) { + nodeType = PV; alpha = score; } } } + WriteEntry(board.hashKey, ply, alpha, nodeType); + return alpha; } static int NegaMax(Board board, int depth, int alpha, int beta, bool isRoot) { + int score = ReadEntry(board.hashKey, depth, alpha, beta); + if (score != 10000) { + return score; + } + + int nodeType = AllNode; + if (depth == 0) { - return Quiescence(board, alpha, beta); + score = Quiescence(board, alpha, beta); + // Write to TT (PV) + WriteEntry(board.hashKey, depth, score, PV); + return score; } nodes++; @@ -127,15 +152,13 @@ static int NegaMax(Board board, int depth, int alpha, int beta, bool isRoot) { } } - - int max = -50000; - for (int i = 0; i < board.currentMoveIndex; i++) { Board copy = board; board.MakeMove(board.moveList[i]); ply++; - int score = -NegaMax(board, depth - 1, -beta, -alpha, false); - board = copy; ply--; + score = -NegaMax(board, depth - 1, -beta, -alpha, false); + board = copy; + ply--; if (score >= beta) { if (board.moveList[i].IsCapture()) { @@ -144,29 +167,31 @@ static int NegaMax(Board board, int depth, int alpha, int beta, bool isRoot) { killerMoves[0][ply] = board.moveList[i]; } + // Write to TT (CutNode) + WriteEntry(board.hashKey, depth, beta, CutNode); return beta; } if (score > alpha) { alpha = score; - } - if (score > max) { - max = score; + // Write to TT (PV) + // if this doesnt occur, then it remains AllNode + nodeType = PV; } if (isRoot) { - if (max > bestScore) { - bestScore = max; + if (alpha > bestScore) { + bestScore = alpha; bestMove = board.moveList[i]; } } } - return max; + WriteEntry(board.hashKey, depth, alpha, nodeType); + return alpha; } - void SearchPosition(Board &board, int depth) { bestScore = -50000; bestMove = Move(); @@ -174,4 +199,5 @@ void SearchPosition(Board &board, int depth) { std::cout << "info depth " << depth << " nodes " << nodes << '\n'; std::cout << "bestmove " << squareCoords[bestMove.MoveFrom()] << squareCoords[bestMove.MoveTo()] << '\n'; + nodes = 0; } diff --git a/source/tt.cpp b/source/tt.cpp index afeab9a..761b543 100644 --- a/source/tt.cpp +++ b/source/tt.cpp @@ -2,6 +2,8 @@ static U64 initialSeed = 0x60919C48E57863B9; +TTEntry TTable[hashSize]; + // 2[colors] * 6[pieces] * 64[squares] U64 zKeys[2][6][64]; @@ -76,3 +78,31 @@ U64 GetHashKey(Board &board) { return key; } + +int ReadEntry(U64 &hashKey, int depth, int alpha, int beta) { + TTEntry *current = &TTable[hashKey % hashSize]; + + if (current->hashKey == hashKey) { + if (current->depth >= depth) { + if (current->nodeType == PV){ + return current->score; + } else if (current->nodeType == AllNode && current->score <= alpha) { + return alpha; + } else if (current->nodeType == CutNode && current->score >= beta) { + return beta; + } + } + } + + // returning unknown value + return 10000; +} + +void WriteEntry(U64 &hashKey, int depth, int score, int nodeType) { + TTEntry *current = &TTable[hashKey % hashSize]; + + current->hashKey = hashKey; + current->nodeType = nodeType; + current->score = score; + current->depth = depth; +} diff --git a/source/tt.h b/source/tt.h index f284d6c..2adb8ea 100644 --- a/source/tt.h +++ b/source/tt.h @@ -1,6 +1,9 @@ #pragma once #include "board.h" +// 5 MB +constexpr int hashSize = 0x500000; + // 2[colors] * 6[pieces] * 64[squares] extern U64 zKeys[2][6][64]; @@ -14,3 +17,16 @@ extern U64 zSide; void InitZobrist(); U64 GetHashKey(Board &board); + +class TTEntry { +public: + U64 hashKey; + int depth; + int score; + int nodeType; +}; + +extern TTEntry TTable[hashSize]; + +int ReadEntry(U64 &hashKey, int depth, int alpha, int beta); +void WriteEntry(U64 &hashKey, int depth, int score, int nodeType); diff --git a/source/types.h b/source/types.h index dcd8bef..7cc13b4 100644 --- a/source/types.h +++ b/source/types.h @@ -29,6 +29,12 @@ enum CASTLING_RIGHTS { blackQueenRight = 8 }; +enum NODE_TYPES { + PV, + CutNode, + AllNode +}; + constexpr std::array PIECE_LETTERS = { 'P', 'p', 'N', 'n',