added Transposition tables

This commit is contained in:
rektdie committed 2024-07-13 22:20:15 +02:00
1 parent 2e984643c0
commit 7c4fbc1f6d
4 files changed
+93 -15

No files matched your search

+41 -15
View File
@@ -2,6 +2,7 @@
#include "movegen.h"
#include "evaluate.h"
#include <algorithm>
#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;
}
+30
View File
@@ -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;
}
+16
View File
@@ -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);
+6
View File
@@ -29,6 +29,12 @@ enum CASTLING_RIGHTS {
blackQueenRight = 8
};
enum NODE_TYPES {
PV,
CutNode,
AllNode
};
constexpr std::array<const char, 12> PIECE_LETTERS = {
'P', 'p',
'N', 'n',