Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔍 Fail soft TT cutoffs #1044

Merged
merged 9 commits into from
Sep 22, 2024
24 changes: 12 additions & 12 deletions src/Lynx/Model/TranspositionTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static (int Length, int Mask) CalculateLength(int size)
/// <param name="beta"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static (int Evaluation, ShortMove BestMove, NodeType NodeType, int score) ProbeHash(this TranspositionTable tt, int ttMask, Position position, int depth, int ply, int alpha, int beta)
public static (int Score, ShortMove BestMove, NodeType NodeType, int RawScore) ProbeHash(this TranspositionTable tt, int ttMask, Position position, int depth, int ply, int alpha, int beta)
{
ref var entry = ref tt[position.UniqueIdentifier & ttMask];

Expand All @@ -103,24 +103,24 @@ public static (int Evaluation, ShortMove BestMove, NodeType NodeType, int score)
return (EvaluationConstants.NoHashEntry, default, default, default);
}

var eval = EvaluationConstants.NoHashEntry;
var rawScore = entry.Score;
var score = EvaluationConstants.NoHashEntry;

if (entry.Depth >= depth)
{
// We want to translate the checkmate position relative to the saved node to our root position from which we're searching
// If the recorded score is a checkmate in 3 and we are at depth 5, we want to read checkmate in 8
var score = RecalculateMateScores(entry.Score, ply);
var recalculatedScore = RecalculateMateScores(rawScore, ply);

eval = entry.Type switch
if (entry.Type == NodeType.Exact
|| (entry.Type == NodeType.Alpha && recalculatedScore <= alpha)
|| (entry.Type == NodeType.Beta && recalculatedScore >= beta))
{
NodeType.Exact => score,
NodeType.Alpha when score <= alpha => alpha,
NodeType.Beta when score >= beta => beta,
_ => EvaluationConstants.NoHashEntry
};
// We want to translate the checkmate position relative to the saved node to our root position from which we're searching
// If the recorded score is a checkmate in 3 and we are at depth 5, we want to read checkmate in 8
score = recalculatedScore;
}
}

return (eval, entry.Move, entry.Type, entry.Score);
return (score, entry.Move, entry.Type, rawScore);
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions src/Lynx/Search/NegaMax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM
bool pvNode = beta - alpha > 1;
ShortMove ttBestMove = default;
NodeType ttElementType = default;
int ttEvaluation = default;
int ttScore = default;
int ttEvaluation = default; // TODO rename ttScore
int ttScore = default; // TODO rename ttRawScore

if (!isRoot)
{
Expand Down Expand Up @@ -528,9 +528,9 @@ public int QuiescenceSearch(int ply, int alpha, int beta)
_pVTable[pvIndex] = _defaultMove; // Nulling the first value before any returns

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

Expand Down
10 changes: 5 additions & 5 deletions tests/Lynx.Test/Model/TranspositionTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ public void RecalculateMateScores(int evaluation, int depth, int expectedEvaluat
Assert.AreEqual(expectedEvaluation, TranspositionTableExtensions.RecalculateMateScores(evaluation, depth));
}

[TestCase(+19, NodeType.Alpha, +20, +30, 20)]
[TestCase(+19, NodeType.Alpha, +20, +30, +19)]
[TestCase(+21, NodeType.Alpha, +20, +30, NoHashEntry)]
[TestCase(+29, NodeType.Beta, +20, +30, NoHashEntry)]
[TestCase(+31, NodeType.Beta, +20, +30, 30)]
[TestCase(+31, NodeType.Beta, +20, +30, +31)]
public void RecordHash_ProbeHash(int recordedEval, NodeType recordNodeType, int probeAlpha, int probeBeta, int expectedProbeEval)
{
var position = new Position(Constants.InitialPositionFEN);
Expand All @@ -86,7 +86,7 @@ public void RecordHash_ProbeHash(int recordedEval, NodeType recordNodeType, int

transpositionTable.RecordHash(mask, position, depth: 5, ply: 3, eval: recordedEval, nodeType: recordNodeType, move: 1234);

Assert.AreEqual(expectedProbeEval, transpositionTable.ProbeHash(mask, position, depth: 5, ply: 3, alpha: probeAlpha, beta: probeBeta).Evaluation);
Assert.AreEqual(expectedProbeEval, transpositionTable.ProbeHash(mask, position, depth: 5, ply: 3, alpha: probeAlpha, beta: probeBeta).Score);
}

[TestCase(CheckMateBaseEvaluation - 8 * CheckmateDepthFactor)]
Expand All @@ -100,7 +100,7 @@ public void RecordHash_ProbeHash_CheckmateSameDepth(int recordedEval)

transpositionTable.RecordHash(mask, position, depth: 10, ply: sharedDepth, eval: recordedEval, nodeType: NodeType.Exact, move: 1234);

Assert.AreEqual(recordedEval, transpositionTable.ProbeHash(mask, position, depth: 7, ply: sharedDepth, alpha: 50, beta: 100).Evaluation);
Assert.AreEqual(recordedEval, transpositionTable.ProbeHash(mask, position, depth: 7, ply: sharedDepth, alpha: 50, beta: 100).Score);
}

[TestCase(CheckMateBaseEvaluation - 8 * CheckmateDepthFactor, 5, 4, CheckMateBaseEvaluation - 7 * CheckmateDepthFactor)]
Expand All @@ -115,6 +115,6 @@ public void RecordHash_ProbeHash_CheckmateDifferentDepth(int recordedEval, int r

transpositionTable.RecordHash(mask, position, depth: 10, ply: recordedDeph, eval: recordedEval, nodeType: NodeType.Exact, move: 1234);

Assert.AreEqual(expectedProbeEval, transpositionTable.ProbeHash(mask, position, depth: 7, ply: probeDepth, alpha: 50, beta: 100).Evaluation);
Assert.AreEqual(expectedProbeEval, transpositionTable.ProbeHash(mask, position, depth: 7, ply: probeDepth, alpha: 50, beta: 100).Score);
}
}
Loading