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

🔍 Improve TT replacement scheme: add required conditions for always replace #526

Merged
merged 16 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/Lynx/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public async Task<SearchResult> BestMove(GoCommand goCommand)
{
_searchCancellationTokenSource = new();
_absoluteSearchCancellationTokenSource = new();
int? maxDepth = null;
int? maxDepth = Configuration.EngineSettings.MaxDepth;
int? decisionTime = null;

int millisecondsLeft;
Expand All @@ -91,8 +91,6 @@ public async Task<SearchResult> BestMove(GoCommand goCommand)
millisecondsIncrement = goCommand.BlackIncrement;
}

maxDepth = Configuration.EngineSettings.MaxDepth;

if (millisecondsLeft > 0)
{
if (goCommand.MovesToGo == default)
Expand Down
2 changes: 0 additions & 2 deletions src/Lynx/Model/Position.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;

Expand Down
25 changes: 19 additions & 6 deletions src/Lynx/Model/TranspositionTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public struct TranspositionTableElement
/// <summary>
/// Full Zobrist key
/// </summary>
public long Key { get; set; }
private int _key;

/// <summary>
/// Best move found in a position. 0 if the position failed low (score <= alpha)
Expand All @@ -30,8 +30,6 @@ public struct TranspositionTableElement

private byte _depth;

//private byte _age;

/// <summary>
/// Node (position) type:
/// <see cref="NodeType.Exact"/>: == <see cref="Score"/>,
Expand All @@ -50,7 +48,7 @@ public struct TranspositionTableElement
/// </summary>
public int Score { readonly get => _score; set => _score = (short)value; }

//public int Age { readonly get => _age; set => _age = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref value, 1))[0]; }
public long Key { readonly get => _key; set => _key = (int)(value >> 32); }

public void Clear()
{
Expand All @@ -70,7 +68,11 @@ public static class TranspositionTableExtensions
public static (int Length, int Mask) CalculateLength(int size)
{
var sizeBytes = size * 1024 * 1024;
var ttLength = (int)BitOperations.RoundUpToPowerOf2((uint)(sizeBytes / _ttElementSize));
var ttLength = sizeBytes / _ttElementSize;
if (!BitOperations.IsPow2(ttLength))
{
ttLength = (int)BitOperations.RoundUpToPowerOf2((uint)ttLength) / 2;
}
var ttLengthMb = ttLength / 1024 / 1024;

var mask = ttLength - 1;
Expand Down Expand Up @@ -114,7 +116,7 @@ public static (int Evaluation, Move BestMove, NodeType NodeType) ProbeHash(this

ref var entry = ref tt[position.UniqueIdentifier & ttMask];

if (position.UniqueIdentifier != entry.Key)
if ((position.UniqueIdentifier >> 32) != entry.Key)
{
return (EvaluationConstants.NoHashEntry, default, default);
}
Expand Down Expand Up @@ -165,6 +167,17 @@ public static void RecordHash(this TranspositionTable tt, int ttMask, Position p
// _logger.Warn("TT collision");
//}

bool shouldReplace =
entry.Key == 0 // No actual entry
|| (position.UniqueIdentifier >> 32) != entry.Key // Different key: collision
|| nodeType == NodeType.Exact // Entering PV data
|| depth >= entry.Depth; // Higher depth

if (!shouldReplace)
{
return;
}

// We want to store the distance to the checkmate position relative to the current node, independently from the root
// If the evaluated score is a checkmate in 8 and we're at depth 5, we want to store checkmate value in 3
var score = RecalculateMateScores(eval, -ply);
Expand Down
4 changes: 1 addition & 3 deletions src/Lynx/Search/NegaMax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,12 @@ 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;
Move ttBestMove = ttProbeResult.BestMove;

_maxDepthReached[ply] = ply;

Expand Down