Skip to content

Commit

Permalink
🔍 Implement TT in quiescence search (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduherminio committed Oct 23, 2023
1 parent 65a62fe commit 0c9c97c
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/Lynx/Search/NegaMax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool isVerifyingNul
}

var nodeType = NodeType.Alpha;

int movesSearched = 0;
Move? bestMove = null;
bool isAnyMoveValid = false;
Expand Down Expand Up @@ -352,6 +351,15 @@ public int QuiescenceSearch(int ply, int alpha, int beta)
var nextPvIndex = PVTable.Indexes[ply + 1];
_pVTable[pvIndex] = _defaultMove; // Nulling the first value before any returns

Move ttBestMove = default;

var ttProbeResult = _tt.ProbeHash(_ttMask, position, 0, ply, alpha, beta);
if (ttProbeResult.Evaluation != EvaluationConstants.NoHashEntry)
{
return ttProbeResult.Evaluation;
}
ttBestMove = ttProbeResult.BestMove;

_maxDepthReached[ply] = ply;

var staticEvaluation = position.StaticEvaluation(Game.HalfMovesWithoutCaptureOrPawnMove);
Expand All @@ -376,12 +384,13 @@ public int QuiescenceSearch(int ply, int alpha, int beta)
return staticEvaluation;
}

var movesToEvaluate = generatedMoves.OrderByDescending(move => ScoreMove(move, ply, false));

var nodeType = NodeType.Alpha;
Move? bestMove = null;
bool isThereAnyValidCapture = false;

foreach (var move in movesToEvaluate)
var pseudoLegalMoves = generatedMoves.OrderByDescending(move => ScoreMove(move, ply, false, ttBestMove));

foreach (var move in pseudoLegalMoves)
{
var gameState = position.MakeMove(move);
if (!position.WasProduceByAValidMove())
Expand Down Expand Up @@ -429,6 +438,9 @@ public int QuiescenceSearch(int ply, int alpha, int beta)
if (evaluation >= beta)
{
PrintMessage($"Pruning: {move} is enough to discard this line");

_tt.RecordHash(_ttMask, position, 0, ply, beta, NodeType.Beta, bestMove);

return evaluation; // The refutation doesn't matter, since it'll be pruned
}

Expand All @@ -439,17 +451,23 @@ public int QuiescenceSearch(int ply, int alpha, int beta)

_pVTable[pvIndex] = move;
CopyPVTableMoves(pvIndex + 1, nextPvIndex, Configuration.EngineSettings.MaxDepth - ply - 1);

nodeType = NodeType.Exact;
}
}

if (bestMove is null)
if (bestMove is null
&& !isThereAnyValidCapture
&& !MoveGenerator.CanGenerateAtLeastAValidMove(position))
{
return isThereAnyValidCapture || MoveGenerator.CanGenerateAtLeastAValidMove(position)
? alpha
: Position.EvaluateFinalPosition(ply, position.IsInCheck());
var finalEval = Position.EvaluateFinalPosition(ply, position.IsInCheck());
_tt.RecordHash(_ttMask, position, 0, ply, finalEval, NodeType.Exact);

return finalEval;
}

// Node fails low
_tt.RecordHash(_ttMask, position, 0, ply, alpha, nodeType, bestMove);

return alpha;
}
}

0 comments on commit 0c9c97c

Please sign in to comment.