Skip to content

Commit

Permalink
Time management and Null Move Pruning fixes (#62)
Browse files Browse the repository at this point in the history
Bench: 3488872
  • Loading branch information
Dannyj1 authored Dec 14, 2023
1 parent 9cbe307 commit 2342632
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
12 changes: 1 addition & 11 deletions src/bitboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,6 @@ void Bitboard::makeNullMove() {
enPassantSquare = NO_SQUARE;
}

if (movingColor == BLACK) {
fullmoveClock += 1;
}

halfMoveClock += 1;
movingColor = getOppositeColor(movingColor);
zobristHash ^= zobristConstants[ZOBRIST_COLOR_INDEX];
previousMove = {NO_SQUARE, NO_SQUARE, EMPTY, 0, EMPTY, 0};
Expand All @@ -430,17 +425,12 @@ void Bitboard::makeNullMove() {
void Bitboard::unmakeNullMove() {
ply -= 1;
UndoData undoData = undoStack[ply];

halfMoveClock = undoData.halfMoveClock;

enPassantSquare = undoData.enPassantSquare;
castlingRights = undoData.castlingRights;
movingColor = getOppositeColor(movingColor);
zobristHash = undoData.zobristHash;
previousMove = undoData.previousMove;

if (movingColor == BLACK) {
fullmoveClock -= 1;
}
}

const Move& Bitboard::getPreviousMove() const { return previousMove; }
Expand Down
26 changes: 13 additions & 13 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,23 @@ Move getBestMove(senjo::GoParams params, ZagreusEngine& engine, Bitboard& board,
searchContext.startTime = startTime;
int depth = 0;
int bestScore = MAX_NEGATIVE;
Line bestPvLine{};
Line pvLine{};
pvLine.startPly = board.getPly();

tt->ageHistoryTable();

while (!engine.stopRequested()) {
auto currentTime = std::chrono::steady_clock::now();
// Update the endtime using new data
searchContext.endTime = getEndTime(searchContext, params, engine, board.getMovingColor());

auto currentTime = std::chrono::steady_clock::now();
if (currentTime > searchContext.endTime) {
engine.stopSearching();
break;
}

depth += 1;

if (depth + board.getPly() >= MAX_PLY) {
break;
}

searchStats.depth = depth;
searchStats.seldepth = 0;

Expand All @@ -72,7 +69,9 @@ Move getBestMove(senjo::GoParams params, ZagreusEngine& engine, Bitboard& board,
int score = search<color, ROOT>(board, MAX_NEGATIVE, MAX_POSITIVE, depth, searchContext,
searchStats, pvLine);

currentTime = std::chrono::steady_clock::now();
if (currentTime > searchContext.endTime) {
engine.stopSearching();
break;
}

Expand All @@ -99,12 +98,14 @@ Move getBestMove(senjo::GoParams params, ZagreusEngine& engine, Bitboard& board,
bestScore = score;
}

board.setPvLine(pvLine);
bestPvLine = pvLine;
board.setPvLine(bestPvLine);
searchStats.score = score;
printPv(searchStats, startTime, pvLine);
printPv(searchStats, startTime, bestPvLine);
}

return pvLine.moves[0];
engine.stopSearching();
return bestPvLine.moves[0];
}

template Move getBestMove<WHITE>(senjo::GoParams params, ZagreusEngine& engine, Bitboard& board,
Expand Down Expand Up @@ -132,7 +133,6 @@ int search(Bitboard& board, int alpha, int beta, int16_t depth,
}

searchStats.nodes += 1;
Move previousMove = board.getPreviousMove();

if (depth <= 0) {
pvLine.moveCount = 0;
Expand All @@ -148,14 +148,15 @@ int search(Bitboard& board, int alpha, int beta, int16_t depth,
}
}

Move previousMove = board.getPreviousMove();
bool isPreviousMoveNull = previousMove.from == NO_SQUARE && previousMove.to == NO_SQUARE;

// Null move pruning
if (!IS_PV_NODE && depth >= 3 && !isPreviousMoveNull && board.getAmountOfMinorOrMajorPieces<
color>() > 0) {
bool ownKingInCheck = board.isKingInCheck<color>();

if (!ownKingInCheck
&& Evaluation(board).evaluate() >= beta) {
if (!ownKingInCheck && Evaluation(board).evaluate() >= beta) {
int r = 3 + (depth >= 6) + (depth >= 12);

Line nullLine{};
Expand Down Expand Up @@ -196,7 +197,6 @@ int search(Bitboard& board, int alpha, int beta, int16_t depth,
}

legalMoveCount += 1;
previousMove = move;

int score;
if (IS_PV_NODE && doPvSearch) {
Expand Down

0 comments on commit 2342632

Please sign in to comment.