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

⚡ Make search config params constant #847

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion src/Lynx.Benchmark/UCI_Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ public class UCI_Benchmark : BaseBenchmark
public (long, long) Bench_DefaultDepth()
{
var engine = new Engine(_channel.Writer);
return engine.Bench(Configuration.EngineSettings.BenchDepth);
return engine.Bench(EngineSettings.BenchDepth);
}
}
161 changes: 69 additions & 92 deletions src/Lynx/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Lynx;

public static class Configuration
{
public static EngineSettings EngineSettings { get; set; } = new EngineSettings();
public static GeneralSettings GeneralSettings { get; set; } = new GeneralSettings();
public static EngineSettings EngineSettings { get; } = new EngineSettings();
public static GeneralSettings GeneralSettings { get; } = new GeneralSettings();

private static int _isDebug = 0;
#pragma warning disable IDE1006 // Naming Styles
Expand Down Expand Up @@ -55,9 +55,9 @@ public static int Hash

public sealed class GeneralSettings
{
public bool EnableLogging { get; set; } = false;
public bool EnableLogging = false;

public bool EnableTuning { get; set; } = false;
public bool EnableTuning = false;
}

public sealed class EngineSettings
Expand All @@ -68,124 +68,101 @@ public sealed class EngineSettings
/// <summary>
/// Depth for bench command
/// </summary>
public int BenchDepth { get; set; } = 10;
public const int BenchDepth = 10;

/// <summary>
/// MB
/// </summary>
public int TranspositionTableSize { get; set; } = 256;
public int TranspositionTableSize = 256;

public bool UseOnlineTablebaseInRootPositions { get; set; } = false;
public bool UseOnlineTablebaseInRootPositions = false;

/// <summary>
/// Experimental, might misbehave due to tablebase API limits
/// </summary>
public bool UseOnlineTablebaseInSearch { get; set; } = false;
public bool UseOnlineTablebaseInSearch = false;

/// <summary>
/// This can also de used to reduce online probing
/// </summary>
public int OnlineTablebaseMaxSupportedPieces { get; set; } = 7;
public const int OnlineTablebaseMaxSupportedPieces = 7;

public bool ShowWDL { get; set; } = false;
public bool ShowWDL = false;

public bool IsPonder { get; set; } = false;
public bool IsPonder = false;

public double SPSA_OB_R_end { get; set; } = 0.02;
public const double SPSA_OB_R_end = 0.02;

#region Time management

public double HardTimeBoundMultiplier { get; set; } = 0.52;
public const double HardTimeBoundMultiplier = 0.52;

public double SoftTimeBoundMultiplier { get; set; } = 1;
public const double SoftTimeBoundMultiplier = 1;

public int DefaultMovesToGo { get; set; } = 45;
public const int DefaultMovesToGo = 45;

public double SoftTimeBaseIncrementMultiplier { get; set; } = 0.8;
public const double SoftTimeBaseIncrementMultiplier = 0.8;

#endregion

[SPSAAttribute<int>(3, 10, 0.5)]
public int LMR_MinDepth { get; set; } = 3;
public const int LMR_MinDepth = 3;

[SPSAAttribute<int>(1, 10, 0.5)]
public int LMR_MinFullDepthSearchedMoves { get; set; } = 4;
public const int LMR_MinFullDepthSearchedMoves = 4;

/// <summary>
/// Value originally from Stormphrax, who apparently took it from Viridithas
/// </summary>
[SPSAAttribute<double>(0.1, 2, 0.10)]
public double LMR_Base { get; set; } = 0.91;
public const double LMR_Base = 0.91;

/// <summary>
/// Value originally from Akimbo
/// </summary>
[SPSAAttribute<double>(1, 5, 0.1)]
public double LMR_Divisor { get; set; } = 3.42;
public const double LMR_Divisor = 3.42;

[SPSAAttribute<int>(1, 10, 0.5)]
public int NMP_MinDepth { get; set; } = 2;
public const int NMP_MinDepth = 2;

[SPSAAttribute<int>(1, 5, 0.5)]
public int NMP_BaseDepthReduction { get; set; } = 2;
public const int NMP_BaseDepthReduction = 2;

[SPSAAttribute<int>(0, 10, 0.5)]
public int NMP_DepthIncrement { get; set; } = 1;
public const int NMP_DepthIncrement = 1;

[SPSAAttribute<int>(1, 10, 0.5)]
public int NMP_DepthDivisor { get; set; } = 4;
public const int NMP_DepthDivisor = 4;

[SPSAAttribute<int>(1, 100, 5)]
public int AspirationWindow_Delta { get; set; } = 13;
public const int AspirationWindow_Delta = 13;

[SPSAAttribute<int>(1, 20, 1)]
public int AspirationWindow_MinDepth { get; set; } = 8;
public const int AspirationWindow_MinDepth = 8;

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

[SPSAAttribute<int>(1, 300, 15)]
public int RFP_DepthScalingFactor { get; set; } = 82;
public const int RFP_DepthScalingFactor = 82;

[SPSAAttribute<int>(1, 10, 0.5)]
public int Razoring_MaxDepth { get; set; } = 1;
public const int Razoring_MaxDepth = 1;

[SPSAAttribute<int>(1, 300, 15)]
public int Razoring_Depth1Bonus { get; set; } = 129;
public const int Razoring_Depth1Bonus = 129;

[SPSAAttribute<int>(1, 300, 15)]
public int Razoring_NotDepth1Bonus { get; set; } = 178;
public const int Razoring_NotDepth1Bonus = 178;

[SPSAAttribute<int>(1, 10, 0.5)]
public int IIR_MinDepth { get; set; } = 3;
public const int IIR_MinDepth = 3;

[SPSAAttribute<int>(1, 10, 0.5)]
public int LMP_MaxDepth { get; set; } = 7;
public const int LMP_MaxDepth = 7;

[SPSAAttribute<int>(0, 10, 0.5)]
public int LMP_BaseMovesToTry { get; set; } = 0;
public const int LMP_BaseMovesToTry = 0;

[SPSAAttribute<int>(0, 10, 0.5)]
public int LMP_MovesDepthMultiplier { get; set; } = 4;
public const int LMP_MovesDepthMultiplier = 4;

public int History_MaxMoveValue { get; set; } = 8_192;
public const int History_MaxMoveValue = 8_192;

/// <summary>
/// 1896: constant from depth 12
/// </summary>
public int History_MaxMoveRawBonus { get; set; } = 1_896;
public const int History_MaxMoveRawBonus = 1_896;

[SPSAAttribute<int>(0, 6, 0.5)]
public int SEE_BadCaptureReduction { get; set; } = 2;
public const int SEE_BadCaptureReduction = 2;

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

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

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

#region Evaluation

Expand Down Expand Up @@ -220,34 +197,34 @@ public sealed class EngineSettings
new(0, 0));

public TaperedEvaluationTermByCount27 VirtualKingMobilityBonus { get; set; } = new(
new(0, 0),
new(0, 0),
new(0, 0),
new(37, -5),
new(51, -8),
new(24, 24),
new(22, 13),
new(19, 3),
new(15, 6),
new(11, 5),
new(9, 9),
new(2, 13),
new(0, 9),
new(-5, 12),
new(-15, 15),
new(-26, 18),
new(-35, 15),
new(-46, 12),
new(-52, 10),
new(-60, 3),
new(-51, -5),
new(-46, -13),
new(-44, -24),
new(-39, -34),
new(-45, -44),
new(-22, -65),
new(-62, -72),
new(-36, -90));
new(0, 0),
new(0, 0),
new(0, 0),
new(37, -5),
new(51, -8),
new(24, 24),
new(22, 13),
new(19, 3),
new(15, 6),
new(11, 5),
new(9, 9),
new(2, 13),
new(0, 9),
new(-5, 12),
new(-15, 15),
new(-26, 18),
new(-35, 15),
new(-46, 12),
new(-52, 10),
new(-60, 3),
new(-51, -5),
new(-46, -13),
new(-44, -24),
new(-39, -34),
new(-45, -44),
new(-22, -65),
new(-62, -72),
new(-36, -90));

public TaperedEvaluationTermByCount8 KnightMobilityBonus { get; set; } = new(
new(0, 0),
Expand Down
10 changes: 5 additions & 5 deletions src/Lynx/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,16 @@ public SearchResult BestMove(GoCommand goCommand)
const int minSearchTime = 50;

var movesDivisor = goCommand.MovesToGo == 0
? Configuration.EngineSettings.DefaultMovesToGo
? EngineSettings.DefaultMovesToGo
: goCommand.MovesToGo;

millisecondsLeft -= engineGuiCommunicationTimeOverhead;
millisecondsLeft = Math.Clamp(millisecondsLeft, minSearchTime, int.MaxValue); // Avoiding 0/negative values

hardLimitTimeBound = (int)(millisecondsLeft * Configuration.EngineSettings.HardTimeBoundMultiplier);
hardLimitTimeBound = (int)(millisecondsLeft * EngineSettings.HardTimeBoundMultiplier);

var softLimitBase = (millisecondsLeft / movesDivisor) + (millisecondsIncrement * Configuration.EngineSettings.SoftTimeBaseIncrementMultiplier);
softLimitTimeBound = Math.Min(hardLimitTimeBound, (int)(softLimitBase * Configuration.EngineSettings.SoftTimeBoundMultiplier));
var softLimitBase = (millisecondsLeft / movesDivisor) + (millisecondsIncrement * EngineSettings.SoftTimeBaseIncrementMultiplier);
softLimitTimeBound = Math.Min(hardLimitTimeBound, (int)(softLimitBase * EngineSettings.SoftTimeBoundMultiplier));

_logger.Info("Soft time bound: {0}s", 0.001 * softLimitTimeBound);
_logger.Info("Hard time bound: {0}s", 0.001 * hardLimitTimeBound);
Expand Down Expand Up @@ -261,7 +261,7 @@ public SearchResult BestMove(GoCommand goCommand)

private async ValueTask<SearchResult> SearchBestMove(int maxDepth, int softLimitTimeBound)
{
if (!Configuration.EngineSettings.UseOnlineTablebaseInRootPositions || Game.CurrentPosition.CountPieces() > Configuration.EngineSettings.OnlineTablebaseMaxSupportedPieces)
if (!Configuration.EngineSettings.UseOnlineTablebaseInRootPositions || Game.CurrentPosition.CountPieces() > EngineSettings.OnlineTablebaseMaxSupportedPieces)
{
return IDDFS(maxDepth, softLimitTimeBound)!;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Lynx/EvaluationConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,11 @@ static EvaluationConstants()
for (int movesSearchedCount = 1; movesSearchedCount < Constants.MaxNumberOfPossibleMovesInAPosition; ++movesSearchedCount) // movesSearchedCount > 0 or we wouldn't be applying LMR
{
LMRReductions[searchDepth][movesSearchedCount] = Convert.ToInt32(Math.Round(
Configuration.EngineSettings.LMR_Base + (Math.Log(movesSearchedCount) * Math.Log(searchDepth) / Configuration.EngineSettings.LMR_Divisor)));
EngineSettings.LMR_Base + (Math.Log(movesSearchedCount) * Math.Log(searchDepth) / EngineSettings.LMR_Divisor)));
}

HistoryBonus[searchDepth] = Math.Min(
Configuration.EngineSettings.History_MaxMoveRawBonus,
EngineSettings.History_MaxMoveRawBonus,
(4 * searchDepth * searchDepth) + (120 * searchDepth) - 120); // Sirius, originally from Berserk
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Lynx/OnlineTablebaseProber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public static class OnlineTablebaseProber

public static int EvaluationSearch(Position position, int halfMovesWithoutCaptureOrPawnMove, CancellationToken cancellationToken)
{
if (!Configuration.EngineSettings.UseOnlineTablebaseInSearch || position.CountPieces() > Configuration.EngineSettings.OnlineTablebaseMaxSupportedPieces)
if (!Configuration.EngineSettings.UseOnlineTablebaseInSearch || position.CountPieces() > EngineSettings.OnlineTablebaseMaxSupportedPieces)
{
return NoResult;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Lynx/SPSAAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public string ToOBString(PropertyInfo property)
{
T val = GetPropertyValue(property);

return $"{property.Name}, int, {val}, {MinValue}, {MaxValue}, {Step}, {Configuration.EngineSettings.SPSA_OB_R_end}";
return $"{property.Name}, int, {val}, {MinValue}, {MaxValue}, {Step}, {EngineSettings.SPSA_OB_R_end}";
}

internal static T GetPropertyValue(PropertyInfo property)
Expand All @@ -75,7 +75,7 @@ public string ToOBPrettyString(PropertyInfo property)
T val = GetPropertyValue(property);
var percentage = 100 * (Step / double.Parse((MaxValue - MinValue).ToString()!));

return $"{property.Name,-35} {"int",-5} {val,-5} {MinValue,-5} {MaxValue,-5} {Step,-5} {$"{percentage:F2}%",-8}{Configuration.EngineSettings.SPSA_OB_R_end,-5}";
return $"{property.Name,-35} {"int",-5} {val,-5} {MinValue,-5} {MaxValue,-5} {Step,-5} {$"{percentage:F2}%",-8}{EngineSettings.SPSA_OB_R_end,-5}";
}

public KeyValuePair<string, JsonNode?> ToWeatherFactoryString(PropertyInfo property)
Expand Down
2 changes: 1 addition & 1 deletion src/Lynx/Search/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ internal int ScoreMove(Move move, int depth, bool isNotQSearch, ShortMove bestMo
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int ScoreHistoryMove(int score, int rawHistoryBonus)
{
return score + rawHistoryBonus - (score * Math.Abs(rawHistoryBonus) / Configuration.EngineSettings.History_MaxMoveValue);
return score + rawHistoryBonus - (score * Math.Abs(rawHistoryBonus) / EngineSettings.History_MaxMoveValue);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
4 changes: 2 additions & 2 deletions src/Lynx/Search/IDDFS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ public SearchResult IDDFS(int maxDepth, int softLimitTimeBound)
_searchCancellationTokenSource.Token.ThrowIfCancellationRequested();
_nodes = 0;

if (depth < Configuration.EngineSettings.AspirationWindow_MinDepth || lastSearchResult?.Evaluation is null)
if (depth < EngineSettings.AspirationWindow_MinDepth || lastSearchResult?.Evaluation is null)
{
bestEvaluation = NegaMax(depth: depth, ply: 0, alpha, beta);
}
else
{
// 🔍 Aspiration Windows
var window = Configuration.EngineSettings.AspirationWindow_Delta;
var window = EngineSettings.AspirationWindow_Delta;

alpha = Math.Max(MinValue, lastSearchResult.Evaluation - window);
beta = Math.Min(MaxValue, lastSearchResult.Evaluation + window);
Expand Down
Loading
Loading