added Transposition tables
This commit is contained in:
1 parent
2e984643c0
commit
7c4fbc1f6d
4 files changed
+93
-15
No files matched your search
+41
-15
@@ -2,6 +2,7 @@
|
|||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "evaluate.h"
|
#include "evaluate.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include "tt.h"
|
||||||
|
|
||||||
static inline int nodes = 0;
|
static inline int nodes = 0;
|
||||||
static inline int ply = 0;
|
static inline int ply = 0;
|
||||||
@@ -65,16 +66,26 @@ void ListScores(Board &board) {
|
|||||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
Move currentMove = board.moveList[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) {
|
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++;
|
nodes++;
|
||||||
|
|
||||||
int staticScore = Evaluate(board);
|
int staticScore = Evaluate(board);
|
||||||
|
|
||||||
if (staticScore >= beta) {
|
if (staticScore >= beta) {
|
||||||
|
WriteEntry(board.hashKey, ply, beta, PV);
|
||||||
return beta;
|
return beta;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,25 +101,39 @@ static int Quiescence(Board board, int alpha, int beta) {
|
|||||||
if (board.moveList[i].IsCapture()) {
|
if (board.moveList[i].IsCapture()) {
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
board.MakeMove(board.moveList[i]);
|
board.MakeMove(board.moveList[i]);
|
||||||
int score = -Quiescence(board, -beta, -alpha);
|
score = -Quiescence(board, -beta, -alpha);
|
||||||
board = copy;
|
board = copy;
|
||||||
|
|
||||||
if (score >= beta) {
|
if (score >= beta) {
|
||||||
|
WriteEntry(board.hashKey, ply, beta, CutNode);
|
||||||
return beta;
|
return beta;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (score > alpha) {
|
if (score > alpha) {
|
||||||
|
nodeType = PV;
|
||||||
alpha = score;
|
alpha = score;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WriteEntry(board.hashKey, ply, alpha, nodeType);
|
||||||
|
|
||||||
return alpha;
|
return alpha;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int NegaMax(Board board, int depth, int alpha, int beta, bool isRoot) {
|
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) {
|
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++;
|
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++) {
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
board.MakeMove(board.moveList[i]);
|
board.MakeMove(board.moveList[i]);
|
||||||
ply++;
|
ply++;
|
||||||
int score = -NegaMax(board, depth - 1, -beta, -alpha, false);
|
score = -NegaMax(board, depth - 1, -beta, -alpha, false);
|
||||||
board = copy; ply--;
|
board = copy;
|
||||||
|
ply--;
|
||||||
|
|
||||||
if (score >= beta) {
|
if (score >= beta) {
|
||||||
if (board.moveList[i].IsCapture()) {
|
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];
|
killerMoves[0][ply] = board.moveList[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Write to TT (CutNode)
|
||||||
|
WriteEntry(board.hashKey, depth, beta, CutNode);
|
||||||
return beta;
|
return beta;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (score > alpha) {
|
if (score > alpha) {
|
||||||
alpha = score;
|
alpha = score;
|
||||||
}
|
|
||||||
|
|
||||||
if (score > max) {
|
// Write to TT (PV)
|
||||||
max = score;
|
// if this doesnt occur, then it remains AllNode
|
||||||
|
nodeType = PV;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isRoot) {
|
if (isRoot) {
|
||||||
if (max > bestScore) {
|
if (alpha > bestScore) {
|
||||||
bestScore = max;
|
bestScore = alpha;
|
||||||
bestMove = board.moveList[i];
|
bestMove = board.moveList[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return max;
|
WriteEntry(board.hashKey, depth, alpha, nodeType);
|
||||||
|
return alpha;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void SearchPosition(Board &board, int depth) {
|
void SearchPosition(Board &board, int depth) {
|
||||||
bestScore = -50000;
|
bestScore = -50000;
|
||||||
bestMove = Move();
|
bestMove = Move();
|
||||||
@@ -174,4 +199,5 @@ void SearchPosition(Board &board, int depth) {
|
|||||||
std::cout << "info depth " << depth << " nodes " << nodes << '\n';
|
std::cout << "info depth " << depth << " nodes " << nodes << '\n';
|
||||||
std::cout << "bestmove " << squareCoords[bestMove.MoveFrom()]
|
std::cout << "bestmove " << squareCoords[bestMove.MoveFrom()]
|
||||||
<< squareCoords[bestMove.MoveTo()] << '\n';
|
<< squareCoords[bestMove.MoveTo()] << '\n';
|
||||||
|
nodes = 0;
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
static U64 initialSeed = 0x60919C48E57863B9;
|
static U64 initialSeed = 0x60919C48E57863B9;
|
||||||
|
|
||||||
|
TTEntry TTable[hashSize];
|
||||||
|
|
||||||
// 2[colors] * 6[pieces] * 64[squares]
|
// 2[colors] * 6[pieces] * 64[squares]
|
||||||
U64 zKeys[2][6][64];
|
U64 zKeys[2][6][64];
|
||||||
|
|
||||||
@@ -76,3 +78,31 @@ U64 GetHashKey(Board &board) {
|
|||||||
|
|
||||||
return key;
|
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
@@ -1,6 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "board.h"
|
#include "board.h"
|
||||||
|
|
||||||
|
// 5 MB
|
||||||
|
constexpr int hashSize = 0x500000;
|
||||||
|
|
||||||
// 2[colors] * 6[pieces] * 64[squares]
|
// 2[colors] * 6[pieces] * 64[squares]
|
||||||
extern U64 zKeys[2][6][64];
|
extern U64 zKeys[2][6][64];
|
||||||
|
|
||||||
@@ -14,3 +17,16 @@ extern U64 zSide;
|
|||||||
|
|
||||||
void InitZobrist();
|
void InitZobrist();
|
||||||
U64 GetHashKey(Board &board);
|
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);
|
||||||
@@ -29,6 +29,12 @@ enum CASTLING_RIGHTS {
|
|||||||
blackQueenRight = 8
|
blackQueenRight = 8
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum NODE_TYPES {
|
||||||
|
PV,
|
||||||
|
CutNode,
|
||||||
|
AllNode
|
||||||
|
};
|
||||||
|
|
||||||
constexpr std::array<const char, 12> PIECE_LETTERS = {
|
constexpr std::array<const char, 12> PIECE_LETTERS = {
|
||||||
'P', 'p',
|
'P', 'p',
|
||||||
'N', 'n',
|
'N', 'n',
|
||||||
|
|||||||
Reference in new issue
Block a user