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

🔍 Don't always stop search when a mate is found #827

Merged
merged 2 commits into from
Jun 26, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Lynx/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,13 @@ public static class Constants
/// </summary>
public const int ArrayDepthMargin = 16;

/// <summary>
/// Given mate in <see cref="MateDistanceMarginToStopSearching"/> (so this is full moves)
/// </summary>
public const int MateDistanceMarginToStopSearching = 2;

public const int MaxMateDistanceToStopSearching = 100 - (2 * MateDistanceMarginToStopSearching);

/// <summary>
/// From https://lichess.org/RViT3UWL
/// Failes input parsing fail if default input buffer size is used (see Lynx.Cli.Listener
Expand Down
39 changes: 24 additions & 15 deletions src/Lynx/Search/IDDFS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public SearchResult IDDFS(int maxDepth, int softLimitTimeBound)
_engineWriter.TryWrite(InfoCommand.SearchResultInfo(lastSearchResult));
}

int mate = 0;

do
{
_absoluteSearchCancellationTokenSource.Token.ThrowIfCancellationRequested();
Expand Down Expand Up @@ -142,11 +144,14 @@ public SearchResult IDDFS(int maxDepth, int softLimitTimeBound)

var bestEvaluationAbs = Math.Abs(bestEvaluation);
isMateDetected = bestEvaluationAbs > EvaluationConstants.PositiveCheckmateDetectionLimit;
mate = isMateDetected
? Utils.CalculateMateInX(bestEvaluation, bestEvaluationAbs)
: 0;

lastSearchResult = UpdateLastSearchResult(lastSearchResult, bestEvaluation, alpha, beta, depth, isMateDetected, bestEvaluationAbs);
lastSearchResult = UpdateLastSearchResult(lastSearchResult, bestEvaluation, alpha, beta, depth, mate, bestEvaluationAbs);

_engineWriter.TryWrite(InfoCommand.SearchResultInfo(lastSearchResult));
} while (StopSearchCondition(++depth, maxDepth, isMateDetected, softLimitTimeBound));
} while (StopSearchCondition(++depth, maxDepth, mate, softLimitTimeBound));
}
catch (OperationCanceledException)
{
Expand All @@ -169,23 +174,33 @@ public SearchResult IDDFS(int maxDepth, int softLimitTimeBound)

var finalSearchResult = GenerateFinalSearchResult(lastSearchResult, bestEvaluation, alpha, beta, depth, firstLegalMove, isCancelled);

if (isMateDetected && finalSearchResult.Mate + Game.HalfMovesWithoutCaptureOrPawnMove < 96)
if (Configuration.EngineSettings.UseOnlineTablebaseInRootPositions
&& isMateDetected
&& (finalSearchResult.Mate * 2) + Game.HalfMovesWithoutCaptureOrPawnMove < Constants.MaxMateDistanceToStopSearching)
{
_logger.Info("Engine search found a short enough mate, cancelling online tb probing if still active");
_searchCancellationTokenSource.Cancel();
_logger.Info("Engine search found a short enough mate, cancelling online tb probing if still active");
}

_engineWriter.TryWrite(InfoCommand.SearchResultInfo(finalSearchResult));

return finalSearchResult;
}

private bool StopSearchCondition(int depth, int maxDepth, bool isMateDetected, int softLimitTimeBound)
private bool StopSearchCondition(int depth, int maxDepth, int mate, int softLimitTimeBound)
{
if (isMateDetected)
if (mate != 0)
{
_logger.Info("Stopping at depth {0}: mate detected", depth - 1);
return false;
var winningMateThreshold = (100 - Game.HalfMovesWithoutCaptureOrPawnMove) / 2;
_logger.Info("Depth {0}: mate in {1} detected ({2} moves until draw by repetition)", depth - 1, mate, winningMateThreshold);

if (mate < 0 || mate + Constants.MateDistanceMarginToStopSearching < winningMateThreshold)
{
_logger.Info("Stopping search: mate is short enough");
return false;
}

_logger.Info("Search continues, hoping to find a faster mate");
}

if (depth >= Configuration.EngineSettings.MaxDepth)
Expand Down Expand Up @@ -277,17 +292,11 @@ private bool OnlyOneLegalMove(ref Move firstLegalMove, [NotNullWhen(true)] out S

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private SearchResult UpdateLastSearchResult(SearchResult? lastSearchResult,
int bestEvaluation, int alpha, int beta, int depth, bool isMateDetected, int bestEvaluationAbs)
int bestEvaluation, int alpha, int beta, int depth, int mate, int bestEvaluationAbs)
{
var pvMoves = _pVTable.TakeWhile(m => m != default).ToList();
var maxDepthReached = _maxDepthReached.LastOrDefault(item => item != default);

int mate = default;
if (isMateDetected)
{
mate = Utils.CalculateMateInX(bestEvaluation, bestEvaluationAbs);
}

var elapsedTime = _stopWatch.ElapsedMilliseconds;

_previousSearchResult = lastSearchResult;
Expand Down
Loading