Position history encaps #52

Merged
rektdie merged 3 commits from positionHistory_encaps into main 2025-04-12 22:23:13 +02:00
8 changed files with 23 additions and 30 deletions

No files matched your search

-3
View File
@@ -65,14 +65,12 @@ void RunBenchmark() {
Board board;
int depth = BENCH_DEPTH;
int ply = 0;
positionIndex = 0;
// Clearing
TT.Clear();
SearchContext ctx;
Stopwatch sw;
for (int i = 0; i < fenPositions.size(); i++) {
board.SetByFen(fenPositions[i]);
@@ -81,5 +79,4 @@ void RunBenchmark() {
std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl;
TT.Clear();
positionIndex = 0;
}
+2 -1
View File
@@ -26,6 +26,8 @@ void Board::Reset() {
moveList = std::array<Move, MAX_MOVES>();
positionIndex = 0;
currentMoveIndex = 0;
hashKey = 0ULL;
@@ -391,7 +393,6 @@ bool Board::MakeMove(Move move) {
}
positionIndex++;
positionHistory[positionIndex] = hashKey;
return true;
}
+2 -3
View File
@@ -10,9 +10,6 @@ constexpr int noEPTarget = -1;
constexpr int MAX_MOVES = 218;
inline int positionIndex = 0;
inline U64 positionHistory[1000];
class Board {
public:
Board() {
@@ -37,6 +34,8 @@ public:
uint8_t castlingRights = 0;
int enPassantTarget = noEPTarget;
short positionIndex = 0;
int halfMoves = 0;
int fullMoves = 1;
+3 -7
View File
@@ -62,9 +62,8 @@ static void PlayRandMoves(Board &board) {
MOVEGEN::GenerateMoves<All>(board);
}
static bool IsGameOver(Board &board) {
bool isDraw = SEARCH::IsDraw(board);
if (isDraw) return true;
static bool IsGameOver(Board &board, SEARCH::SearchContext& ctx) {
if (SEARCH::IsDraw(board, ctx)) return true;
int moveSeen = 0;
@@ -73,7 +72,6 @@ static bool IsGameOver(Board &board) {
bool isLegal = copy.MakeMove(board.moveList[i]);
if (!isLegal) continue;
positionIndex--;
moveSeen++;
}
@@ -99,8 +97,6 @@ void PlayGame(std::atomic<int>& positions, U64 targetPositions, std::vector<Game
Board board;
uint8_t wdl = 1;
// Since positionIndex is thread-local, no need to protect it
positionIndex = 0;
SEARCH::SearchContext ctx;
@@ -110,7 +106,7 @@ void PlayGame(std::atomic<int>& positions, U64 targetPositions, std::vector<Game
SEARCH::SearchResults safeResults;
MOVEGEN::GenerateMoves<All>(board);
while (!IsGameOver(board)) {
while (!IsGameOver(board, ctx)) {
SEARCH::SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx);
if (!results.bestMove) break;
-2
View File
@@ -14,7 +14,6 @@ static U64 HelperPerft(Board board, int depth) {
bool isLegal = copy.MakeMove(board.moveList[i]);
if (!isLegal) continue;
nodes += HelperPerft(copy, depth - 1);
positionIndex--;
}
return nodes;
}
@@ -30,7 +29,6 @@ void Perft(Board &board, int depth) {
bool isLegal = copy.MakeMove(board.moveList[i]);
if (!isLegal) continue;
U64 nodeCount = HelperPerft(copy, depth - 1);
positionIndex--;
totalNodes += nodeCount;
board.moveList[i].PrintMove();
+8 -8
View File
@@ -92,9 +92,9 @@ void ListScores(Board &board, int ply, SearchContext& ctx) {
}
}
static bool IsThreefold(Board &board) {
for (int i = 0; i < positionIndex; i++) {
if (positionHistory[i] == board.hashKey) {
static bool IsThreefold(Board &board, SearchContext& ctx) {
for (int i = 0; i < board.positionIndex; i++) {
if (ctx.positionHistory[i] == board.hashKey) {
// repetition found
return true;
}
@@ -113,8 +113,8 @@ static bool IsInsuffMat(Board &board) {
&& !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook]));
}
bool IsDraw(Board &board) {
return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board);
bool IsDraw(Board &board, SearchContext& ctx) {
return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board, ctx);
}
static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply) {
@@ -172,9 +172,9 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
Board copy = board;
int isLegal = copy.MakeMove(board.moveList[i]);
if (!isLegal) continue;
ctx.positionHistory[copy.positionIndex] = copy.hashKey;
ctx.nodes++;
int score = -Quiescence<mode>(copy, -beta, -alpha, ply + 1, ctx).score;
positionIndex--;
if (score >= beta) {
return score;
@@ -217,7 +217,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
}
ctx.pvLine.SetLength(ply);
if (ply && (IsThreefold(board) || IsFifty(board) || IsInsuffMat(board))) return 0;
if (ply && (IsDraw(board, ctx))) return 0;
// if NOT PV node then we try to hit the TTable
@@ -287,6 +287,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
bool isLegal = copy.MakeMove(currMove);
if (!isLegal) continue;
ctx.positionHistory[copy.positionIndex] = copy.hashKey;
ctx.nodes++;
int reductions = GetReductions(board, currMove, depth, moveSeen, ply);
@@ -316,7 +317,6 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
}
moveSeen++;
positionIndex--;
if (ctx.searchStopped) return 0;
+4 -1
View File
@@ -114,6 +114,8 @@ public:
std::array<std::array<int, MAX_PLY>, 2> killerMoves{};
std::array<U64, 1000> positionHistory{};
Stopwatch sw;
SearchContext(){
@@ -121,6 +123,7 @@ public:
history.Clear();
sw.Restart();
killerMoves = {};
positionHistory = {};
}
};
@@ -130,7 +133,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
template <searchMode mode>
SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx);
bool IsDraw(Board &board);
bool IsDraw(Board &board, SearchContext& ctx);
constexpr int moveScoreTable[6][6] = {
105, 205, 305, 405, 505, 605,
+4 -5
View File
@@ -12,7 +12,7 @@
#include "perft.h"
#include "utils.h"
static void ParsePosition(Board &board, std::string_view command) {
static void ParsePosition(Board &board, std::string_view command, SEARCH::SearchContext& ctx) {
if (command.find("startpos") != std::string::npos) {
board.SetByFen(StartingFen);
}
@@ -33,6 +33,7 @@ static void ParsePosition(Board &board, std::string_view command) {
for (std::string_view move : moves) {
bool isLegal = board.MakeMove(UTILS::parseMove(board, move));
ctx.positionHistory[board.positionIndex] = board.hashKey;
}
}
@@ -124,8 +125,7 @@ void UCILoop(Board &board) {
// parse UCI "position" command
if (input.find("position") != std::string::npos) {
ParsePosition(board, input);
positionIndex = 0;
ParsePosition(board, input, ctx);
continue;
}
@@ -139,8 +139,7 @@ void UCILoop(Board &board) {
ctx.killerMoves = {};
ctx.history.Clear();
positionIndex = 0;
ctx.positionHistory = {};
continue;
}