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

🔍 Futility pruning #733

Merged
merged 4 commits into from
May 14, 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
4 changes: 4 additions & 0 deletions src/Lynx.Cli/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@

"SEE_BadCaptureReduction": 2,

"FP_MaxDepth": 6,
"FP_DepthScalingFactor": 60,
"FP_Margin": 250,

// Evaluation
"DoubledPawnPenalty": {
"MG": -6,
Expand Down
11 changes: 10 additions & 1 deletion src/Lynx/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public sealed class EngineSettings
[SPSAAttribute<int>(1, 20, 1)]
public int RFP_MaxDepth { get; set; } = 6;

[SPSAAttribute<int>(1, 300, 5)]
[SPSAAttribute<int>(1, 300, 10)]
public int RFP_DepthScalingFactor { get; set; } = 107;

[SPSAAttribute<int>(1, 10, 1)]
Expand Down Expand Up @@ -192,6 +192,15 @@ public sealed class EngineSettings
[SPSAAttribute<int>(0, 6, 1)]
public int SEE_BadCaptureReduction { get; set; } = 2;

[SPSAAttribute<int>(1, 10, 1)]
public int FP_MaxDepth { get; set; } = 6;

[SPSAAttribute<int>(1, 200, 10)]
public int FP_DepthScalingFactor { get; set; } = 60;

[SPSAAttribute<int>(0, 500, 25)]
public int FP_Margin { get; set; } = 250;

#region Evaluation

public TaperedEvaluationTerm DoubledPawnPenalty { get; set; } = new(-6, -12);
Expand Down
46 changes: 33 additions & 13 deletions src/Lynx/Search/NegaMax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM
_searchCancellationTokenSource.Token.ThrowIfCancellationRequested();

bool isInCheck = position.IsInCheck();
int staticEval = int.MaxValue, phase = int.MaxValue;

if (isInCheck)
{
Expand All @@ -84,7 +85,7 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM
}
else if (!pvNode)
{
var (staticEval, phase) = position.StaticEvaluation();
(staticEval, phase) = position.StaticEvaluation();

// From smol.cs
// ttEvaluation can be used as a better positional evaluation:
Expand All @@ -102,7 +103,9 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM
// Return formula by Ciekce, instead of just returning static eval
if (staticEval - (Configuration.EngineSettings.RFP_DepthScalingFactor * depth) >= beta)
{
#pragma warning disable S3949 // Calculations should not overflow - value is being set at the beginning of the else if (!pvNode)
return (staticEval + beta) / 2;
#pragma warning restore S3949 // Calculations should not overflow
}

// 🔍 Razoring - Strelka impl (CPW) - https://www.chessprogramming.org/Razoring#Strelka
Expand Down Expand Up @@ -249,20 +252,37 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM
}
else
{
// Late Move Pruning (LMP) - all quiet moves can be pruned
// after searching the first few given by the move ordering algorithm
if (!pvNode
&& !isInCheck
&& depth <= Configuration.EngineSettings.LMP_MaxDepth
&& scores[moveIndex] < EvaluationConstants.PromotionMoveScoreValue // Quiet moves
&& moveIndex >= Configuration.EngineSettings.LMP_BaseMovesToTry + (Configuration.EngineSettings.LMP_MovesDepthMultiplier * depth)) // Based on formula suggested by Antares
if (!pvNode && !isInCheck
&& scores[moveIndex] < EvaluationConstants.PromotionMoveScoreValue) // Quiet move
{
// After making a move
Game.HalfMovesWithoutCaptureOrPawnMove = oldHalfMovesWithoutCaptureOrPawnMove;
Game.PositionHashHistory.RemoveAt(Game.PositionHashHistory.Count - 1);
position.UnmakeMove(move, gameState);
// Late Move Pruning (LMP) - all quiet moves can be pruned
// after searching the first few given by the move ordering algorithm
if (depth <= Configuration.EngineSettings.LMP_MaxDepth
&& moveIndex >= Configuration.EngineSettings.LMP_BaseMovesToTry + (Configuration.EngineSettings.LMP_MovesDepthMultiplier * depth)) // Based on formula suggested by Antares
{
// After making a move
Game.HalfMovesWithoutCaptureOrPawnMove = oldHalfMovesWithoutCaptureOrPawnMove;
Game.PositionHashHistory.RemoveAt(Game.PositionHashHistory.Count - 1);
position.UnmakeMove(move, gameState);

break;
}

break;
// Futility Pruning (FP) - all quiet moves can be pruned
// once it's considered that they don't have potential to raise alpha
if (movesSearched > 0
//&& alpha < EvaluationConstants.PositiveCheckmateDetectionLimit
//&& beta > EvaluationConstants.NegativeCheckmateDetectionLimit
&& depth <= Configuration.EngineSettings.FP_MaxDepth
&& staticEval + Configuration.EngineSettings.FP_Margin + (Configuration.EngineSettings.FP_DepthScalingFactor * depth) <= alpha)
{
// After making a move
Game.HalfMovesWithoutCaptureOrPawnMove = oldHalfMovesWithoutCaptureOrPawnMove;
Game.PositionHashHistory.RemoveAt(Game.PositionHashHistory.Count - 1);
position.UnmakeMove(move, gameState);

break;
}
}

PrefetchTTEntry();
Expand Down
24 changes: 24 additions & 0 deletions src/Lynx/UCIHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,30 @@ private void HandleSetOption(ReadOnlySpan<char> command)
}
break;
}
case "fp_maxdepth":
{
if (length > 4 && int.TryParse(command[commandItems[4]], out var value))
{
Configuration.EngineSettings.FP_MaxDepth = value;
}
break;
}
case "fp_depthscalingfactor":
{
if (length > 4 && int.TryParse(command[commandItems[4]], out var value))
{
Configuration.EngineSettings.FP_DepthScalingFactor = value;
}
break;
}
case "fp_margin":
{
if (length > 4 && int.TryParse(command[commandItems[4]], out var value))
{
Configuration.EngineSettings.FP_Margin = value;
}
break;
}

#endregion

Expand Down
Loading