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

🔍 Split and tune max history values #1463

Closed
wants to merge 6 commits into from
Closed
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
11 changes: 9 additions & 2 deletions src/Lynx/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,21 @@ public int Threads
//[SPSA<int>(0, 10, 0.5)]
public int LMP_MovesDepthMultiplier { get; set; } = 3;

public int History_MaxMoveValue { get; set; } = 8_192;
[SPSA<int>(1_896, 16384, 1024)]
public int QuietHistory_MaxValue { get; set; } = 8_192;

[SPSA<int>(1_896, 16384, 1024)]
public int CaptureHistory_MaxValue { get; set; } = 16_384;

[SPSA<int>(1_896, 16384, 1024)]
public int ContinuationHistory_MaxValue { get; set; } = 16_384;

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

public int CounterMoves_MinDepth { get;set; } = 3;
public int CounterMoves_MinDepth { get; set; } = 3;

[SPSA<int>(0, 200, 10)]
public int History_BestScoreBetaMargin { get; set; } = 60;
Expand Down
31 changes: 21 additions & 10 deletions src/Lynx/Search/MoveOrdering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ private void UpdateMoveOrderingHeuristicsOnQuietBetaCutoff(int depth, int ply, R

_quietHistory[piece][targetSquare] = ScoreHistoryMove(
_quietHistory[piece][targetSquare],
HistoryBonus[depth]);
HistoryBonus[depth],
Configuration.EngineSettings.QuietHistory_MaxValue);

int continuationHistoryIndex;
int previousMovePiece = -1;
Expand All @@ -190,7 +191,8 @@ private void UpdateMoveOrderingHeuristicsOnQuietBetaCutoff(int depth, int ply, R

_continuationHistory[continuationHistoryIndex] = ScoreHistoryMove(
_continuationHistory[continuationHistoryIndex],
HistoryBonus[depth]);
HistoryBonus[depth],
Configuration.EngineSettings.ContinuationHistory_MaxValue);

// var previousPreviousMove = Game.MoveStack[ply - 2];
// var previousPreviousMovePiece = previousPreviousMove.Piece();
Expand All @@ -214,7 +216,8 @@ private void UpdateMoveOrderingHeuristicsOnQuietBetaCutoff(int depth, int ply, R
// When a quiet move fails high, penalize previous visited quiet moves
_quietHistory[visitedMovePiece][visitedMoveTargetSquare] = ScoreHistoryMove(
_quietHistory[visitedMovePiece][visitedMoveTargetSquare],
-HistoryBonus[depth]);
-HistoryBonus[depth],
Configuration.EngineSettings.QuietHistory_MaxValue);

if (!isRoot)
{
Expand All @@ -223,7 +226,8 @@ private void UpdateMoveOrderingHeuristicsOnQuietBetaCutoff(int depth, int ply, R

_continuationHistory[continuationHistoryIndex] = ScoreHistoryMove(
_continuationHistory[continuationHistoryIndex],
-HistoryBonus[depth]);
-HistoryBonus[depth],
Configuration.EngineSettings.ContinuationHistory_MaxValue);
}
}
}
Expand Down Expand Up @@ -263,7 +267,8 @@ private void UpdateMoveOrderingHeuristicsOnCaptureBetaCutoff(int depth, ReadOnly
var captureHistoryIndex = CaptureHistoryIndex(piece, targetSquare, capturedPiece);
_captureHistory[captureHistoryIndex] = ScoreHistoryMove(
_captureHistory[captureHistoryIndex],
HistoryBonus[depth]);
HistoryBonus[depth],
Configuration.EngineSettings.CaptureHistory_MaxValue);

// 🔍 Capture history penalty/malus
// When a capture fails high, penalize previous visited captures
Expand All @@ -281,18 +286,24 @@ private void UpdateMoveOrderingHeuristicsOnCaptureBetaCutoff(int depth, ReadOnly

_captureHistory[captureHistoryIndex] = ScoreHistoryMove(
_captureHistory[captureHistoryIndex],
-HistoryBonus[depth]);
-HistoryBonus[depth],
Configuration.EngineSettings.CaptureHistory_MaxValue);
}
}
}

/// <summary>
/// Soft caps history score
/// Formula taken from EP discord, https://discord.com/channels/1132289356011405342/1132289356447625298/1141102105847922839
/// History gravity: soft cap of history score
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int ScoreHistoryMove(int score, int rawHistoryBonus)
private static int ScoreHistoryMove(int score, int rawHistoryBonus, int maxHistoryValue)
{
return score + rawHistoryBonus - (score * Math.Abs(rawHistoryBonus) / Configuration.EngineSettings.History_MaxMoveValue);
Debug.Assert(Math.Abs(rawHistoryBonus) <= maxHistoryValue);

score = score + rawHistoryBonus - (score * Math.Abs(rawHistoryBonus) / maxHistoryValue);

Debug.Assert(Math.Abs(score) <= maxHistoryValue);

return score;
}
}
2 changes: 1 addition & 1 deletion src/Lynx/Search/NegaMax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ void RevertMove()
}

// -= history/(maxHistory/2)
reduction -= 2 * _quietHistory[move.Piece()][move.TargetSquare()] / Configuration.EngineSettings.History_MaxMoveValue;
reduction -= 2 * _quietHistory[move.Piece()][move.TargetSquare()] / Configuration.EngineSettings.QuietHistory_MaxValue;

// Don't allow LMR to drop into qsearch or increase the depth
// depth - 1 - depth +2 = 1, min depth we want
Expand Down
20 changes: 18 additions & 2 deletions src/Lynx/UCIHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,27 @@ private void HandleSetOption(ReadOnlySpan<char> command)
}
break;
}
case "history_maxmovevalue":
case "history_maxvalue":
{
if (length > 4 && int.TryParse(command[commandItems[4]], out var value))
{
Configuration.EngineSettings.History_MaxMoveValue = value;
Configuration.EngineSettings.QuietHistory_MaxValue = value;
}
break;
}
case "capturehistory_maxvalue":
{
if (length > 4 && int.TryParse(command[commandItems[4]], out var value))
{
Configuration.EngineSettings.CaptureHistory_MaxValue = value;
}
break;
}
case "continuationhistory_maxvalue":
{
if (length > 4 && int.TryParse(command[commandItems[4]], out var value))
{
Configuration.EngineSettings.ContinuationHistory_MaxValue = value;
}
break;
}
Expand Down
Loading