Skip to content

Commit

Permalink
Add basic capture history
Browse files Browse the repository at this point in the history
  • Loading branch information
eduherminio committed Jan 30, 2024
1 parent 3547d4d commit f7d0f4c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
16 changes: 13 additions & 3 deletions src/Lynx/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,20 @@ public Engine(ChannelWriter<string> engineWriter)
_absoluteSearchCancellationTokenSource = new();
_engineWriter = engineWriter;

_historyMoves = new int[12][];
for (int i = 0; i < _historyMoves.Length; ++i)
_quietHistory = new int[12][];
for (int i = 0; i < _quietHistory.Length; ++i)
{
_historyMoves[i] = new int[64];
_quietHistory[i] = new int[64];
}

_captureHistory = new int[12][][];
for (int i = 0; i < 12; ++i)
{
_captureHistory[i] = new int[64][];
for (var j = 0; j < 64; ++j)
{
_captureHistory[i][j] = new int[12];
}
}

InitializeTT();
Expand Down
14 changes: 9 additions & 5 deletions src/Lynx/Search/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public sealed partial class Engine
private const int MaxValue = short.MaxValue;

/// <summary>
/// Returns the score evaluation of a move taking into account <see cref="_isScoringPV"/>, <paramref name="bestMoveTTCandidate"/>, <see cref="EvaluationConstants.MostValueableVictimLeastValuableAttacker"/>, <see cref="_killerMoves"/> and <see cref="_historyMoves"/>
/// Returns the score evaluation of a move taking into account <see cref="_isScoringPV"/>, <paramref name="bestMoveTTCandidate"/>, <see cref="EvaluationConstants.MostValueableVictimLeastValuableAttacker"/>, <see cref="_killerMoves"/> and <see cref="_quietHistory"/>
/// </summary>
/// <param name="move"></param>
/// <param name="depth"></param>
Expand Down Expand Up @@ -99,12 +99,16 @@ internal int ScoreMove(Move move, int depth, bool isNotQSearch, ShortMove bestMo

if (isCapture)
{

var baseCaptureScore = (isPromotion || move.IsEnPassant() || SEE.IsGoodCapture(Game.CurrentPosition, move))
? EvaluationConstants.GoodCaptureMoveBaseScoreValue
: EvaluationConstants.BadCaptureMoveBaseScoreValue;

return baseCaptureScore + EvaluationConstants.MostValueableVictimLeastValuableAttacker[move.Piece()][move.CapturedPiece()];
var piece = move.Piece();

return baseCaptureScore
+ EvaluationConstants.MostValueableVictimLeastValuableAttacker[piece][move.CapturedPiece()]
//+ EvaluationConstants.MVV_PieceValues[move.CapturedPiece()]
+ _captureHistory[piece][move.TargetSquare()][move.CapturedPiece()];
}

if (isPromotion)
Expand All @@ -131,7 +135,7 @@ internal int ScoreMove(Move move, int depth, bool isNotQSearch, ShortMove bestMo
}

// History move or 0 if not found
return EvaluationConstants.BaseMoveScore + _historyMoves[move.Piece()][move.TargetSquare()];
return EvaluationConstants.BaseMoveScore + _quietHistory[move.Piece()][move.TargetSquare()];
}

return EvaluationConstants.BaseMoveScore;
Expand Down Expand Up @@ -351,7 +355,7 @@ internal void PrintHistoryMoves()

for (int i = 0; i < 12; ++i)
{
var tmp = _historyMoves[i];
var tmp = _quietHistory[i];
for (int j = 0; j < 64; ++i)
{
var item = tmp[j];
Expand Down
19 changes: 16 additions & 3 deletions src/Lynx/Search/IDDFS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ public sealed partial class Engine
/// <summary>
/// 12x64
/// </summary>
private readonly int[][] _historyMoves;
private readonly int[][] _quietHistory;

/// <summary>
/// 12x64x12
/// </summary>
private readonly int[][][] _captureHistory;

private readonly int[] _maxDepthReached = new int[Constants.AbsoluteMaxDepth];
private TranspositionTable _tt = [];
Expand Down Expand Up @@ -298,9 +303,17 @@ private int CheckPonderHit(ref SearchResult? lastSearchResult, int depth)
Array.Clear(_killerMoves[2]);
Debug.Assert(_killerMoves.Length == 3);

for (int i = 0; i < _historyMoves.Length; i++)
for (int i = 0; i < _quietHistory.Length; i++)
{
Array.Clear(_quietHistory[i]);
}

for (int i = 0; i < _captureHistory.Length; i++)
{
Array.Clear(_historyMoves[i]);
for (int j = 0; j < _captureHistory[i].Length; j++)
{
Array.Clear(_captureHistory[i][j]);
}
}
}

Expand Down
22 changes: 16 additions & 6 deletions src/Lynx/Search/NegaMax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM
}

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

// Don't allow LMR to drop into qsearch or increase the depth
// depth - 1 - depth +2 = 1, min depth we want
Expand Down Expand Up @@ -338,15 +338,25 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM
{
PrintMessage($"Pruning: {move} is enough");

if (!move.IsCapture())
if (move.IsCapture())
{
var piece = move.Piece();
var targetSquare = move.TargetSquare();
var capturedPiece = move.CapturedPiece();

_captureHistory[piece][targetSquare][capturedPiece] = ScoreHistoryMove(
_captureHistory[piece][targetSquare][capturedPiece],
EvaluationConstants.HistoryBonus[depth]);
}
else
{
// 🔍 Quiet history moves
// Doing this only in beta cutoffs (instead of when eval > alpha) was suggested by Sirius author
var piece = move.Piece();
var targetSquare = move.TargetSquare();

_historyMoves[piece][targetSquare] = ScoreHistoryMove(
_historyMoves[piece][targetSquare],
_quietHistory[piece][targetSquare] = ScoreHistoryMove(
_quietHistory[piece][targetSquare],
EvaluationConstants.HistoryBonus[depth]);

// 🔍 History penalty/malus
Expand All @@ -359,8 +369,8 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM
var visitedMovePiece = visitedMove.Piece();
var visitedMoveTargetSquare = visitedMove.TargetSquare();

_historyMoves[visitedMovePiece][visitedMoveTargetSquare] = ScoreHistoryMove(
_historyMoves[visitedMovePiece][visitedMoveTargetSquare],
_quietHistory[visitedMovePiece][visitedMoveTargetSquare] = ScoreHistoryMove(
_quietHistory[visitedMovePiece][visitedMoveTargetSquare],
-EvaluationConstants.HistoryBonus[depth]);
}
}
Expand Down

0 comments on commit f7d0f4c

Please sign in to comment.