Skip to content

Commit

Permalink
⚡ Simplify triple repetition detection logic, removing some branching (
Browse files Browse the repository at this point in the history
  • Loading branch information
eduherminio committed Jan 26, 2024
1 parent 46c80bb commit 40475e2
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/Lynx/Search/NegaMax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,32 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM
var isThreeFoldRepetition = !Game.PositionHashHistory.Add(position.UniqueIdentifier);

int evaluation;
if (isThreeFoldRepetition || Game.Is50MovesRepetition())
if (isThreeFoldRepetition)
{
evaluation = 0;

// We don't need to evaluate further down to know it's a draw.
// Since we won't be evaluating further down, we need to clear the PV table because those moves there
// don't belong to this line and if this move were to beat alpha, they'd incorrectly copied to pv line.
Array.Clear(_pVTable, nextPvIndex, _pVTable.Length - nextPvIndex);

// This is the only case were we don't clear position.UniqueIdentifier from Game.PositionHashHistory, because it was already there before making the move
}
else if (Game.Is50MovesRepetition())
{
evaluation = 0;

// We don't need to evaluate further down to know it's a draw.
// Since we won't be evaluating further down, we need to clear the PV table because those moves there
// don't belong to this line and if this move were to beat alpha, they'd incorrectly copied to pv line.
Array.Clear(_pVTable, nextPvIndex, _pVTable.Length - nextPvIndex);

Game.PositionHashHistory.Remove(position.UniqueIdentifier);
}
else if (pvNode && movesSearched == 0)
{
evaluation = -NegaMax(depth - 1, ply + 1, -beta, -alpha);
Game.PositionHashHistory.Remove(position.UniqueIdentifier);
}
else
{
Expand All @@ -246,10 +260,7 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM
{
// After making a move
Game.HalfMovesWithoutCaptureOrPawnMove = oldValue;
if (!isThreeFoldRepetition)
{
Game.PositionHashHistory.Remove(position.UniqueIdentifier);
}
Game.PositionHashHistory.Remove(position.UniqueIdentifier); // We know that there's no triple repetition here
position.UnmakeMove(move, gameState);

break;
Expand Down Expand Up @@ -311,14 +322,13 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM
// PVS Hipothesis invalidated -> search with full depth and full score bandwidth
evaluation = -NegaMax(depth - 1, ply + 1, -beta, -alpha);
}

Game.PositionHashHistory.Remove(position.UniqueIdentifier);
}

// After making a move
// Game.PositionHashHistory is update above
Game.HalfMovesWithoutCaptureOrPawnMove = oldValue;
if (!isThreeFoldRepetition)
{
Game.PositionHashHistory.Remove(position.UniqueIdentifier);
}
position.UnmakeMove(move, gameState);

PrintMove(ply, move, evaluation);
Expand Down

0 comments on commit 40475e2

Please sign in to comment.