Switched to PVS from NegaMax, removed TT temporarily

This commit is contained in:
rektdie committed 2024-09-10 09:53:54 +02:00
1 parent 7c4fbc1f6d
commit a0adf43891
2 files changed
+26 -76

No files matched your search

+25 -75
View File
@@ -5,28 +5,15 @@
#include "tt.h" #include "tt.h"
static inline int nodes = 0; static inline int nodes = 0;
static inline int ply = 0;
static inline Move bestMove = Move(); static inline Move bestMove = Move();
static inline int bestScore = -50000; static inline int bestScore = -50000;
// killer moves [id][ply]
static inline Move killerMoves[2][64];
static int ScoreMove(Board &board, Move &move) { static int ScoreMove(Board &board, Move &move) {
const int attackerType = board.GetPieceType(move.MoveFrom()); const int attackerType = board.GetPieceType(move.MoveFrom());
const int targetType = board.GetPieceType(move.MoveTo()); const int targetType = board.GetPieceType(move.MoveTo());
if (move.IsCapture()) { if (move.IsCapture()) {
return moveScoreTable[attackerType][targetType] + 10000; return moveScoreTable[attackerType][targetType] + 10000;
} else {
// 1st killer move
if (killerMoves[0][ply] == move) {
return 9000;
}
// 2nd killer move
else if (killerMoves[1][ply] == move) {
return 8000;
}
} }
return 0; return 0;
@@ -72,20 +59,11 @@ void ListScores(Board &board) {
} }
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;
} }
@@ -101,101 +79,73 @@ 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]);
score = -Quiescence(board, -beta, -alpha); int 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 PVS(Board board, int depth, int alpha, int beta, int isRoot) {
int score = ReadEntry(board.hashKey, depth, alpha, beta);
if (score != 10000) {
return score;
}
int nodeType = AllNode;
if (depth == 0) {
score = Quiescence(board, alpha, beta);
// Write to TT (PV)
WriteEntry(board.hashKey, depth, score, PV);
return score;
}
nodes++; nodes++;
if (depth == 0) return Quiescence(board, alpha, beta);
int score = -50000;
GenerateMoves(board, board.sideToMove); GenerateMoves(board, board.sideToMove);
SortMoves(board); SortMoves(board);
if (board.InCheck(board.sideToMove)) depth++; // For all moves
if (board.currentMoveIndex == 0) {
if (board.InCheck(board.sideToMove)) { // checkmate
return -49000 + ply;
} else { // stalemate
return 0;
}
}
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]); copy.MakeMove(copy.moveList[i]);
ply++;
score = -NegaMax(board, depth - 1, -beta, -alpha, false); // First move (suspected PV node)
board = copy; if (!i) {
ply--; // Full search
score = -PVS(copy, depth - 1, -beta, -alpha, false);
} else {
// Quick search
score = -PVS(copy, depth - 1, -alpha-1, -alpha, false);
if (score > alpha && beta - alpha > 1) {
// We have to do full search
score = -PVS(copy, depth - 1, -beta, -alpha, false);
}
}
if (score >= beta) { if (score >= beta) {
if (board.moveList[i].IsCapture()) {
// store killer moves
killerMoves[1][ply] = killerMoves[0][ply];
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;
// Write to TT (PV)
// if this doesnt occur, then it remains AllNode
nodeType = PV;
} }
if (isRoot) { if (isRoot) {
if (alpha > bestScore) { if (alpha > bestScore) {
bestScore = alpha; bestScore = alpha;
bestMove = board.moveList[i]; bestMove = copy.moveList[i];
} }
} }
} }
WriteEntry(board.hashKey, depth, alpha, nodeType);
return alpha; return alpha;
} }
void SearchPosition(Board &board, int depth) { void SearchPosition(Board &board, int depth) {
bestScore = -50000; bestScore = -50000;
bestMove = Move(); bestMove = Move();
NegaMax(board, depth, -50000, 50000, true); PVS(board, depth, -50000, 50000, true);
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';
+1 -1
View File
@@ -26,7 +26,7 @@ Move ParseMove(Board &board, const char* moveString) {
it = std::find(squareCoords.begin(), squareCoords.end(), toString); it = std::find(squareCoords.begin(), squareCoords.end(), toString);
int to = std::distance(squareCoords.begin(), it); int to = std::distance(squareCoords.begin(), it);
std::string promotionString = ""; std::string promotionString = "";
// Finding the move from available moves // Finding the move from available moves
for (Move move : board.moveList) { for (Move move : board.moveList) {