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

🔍 Add basic (quiet) history malus/penalty #610

Merged
merged 12 commits into from
Jan 14, 2024
23 changes: 22 additions & 1 deletion src/Lynx/Search/NegaMax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM
}
}

Span<Move> visitedMoves = stackalloc Move[pseudoLegalMoves.Length];
int visitedMovesCounter = 0;

for (int moveIndex = 0; moveIndex < pseudoLegalMoves.Length; ++moveIndex)
{
// Incremental move sorting, inspired by https://github.com/jw1912/Chess-Challenge and suggested by toanth
Expand All @@ -205,6 +208,8 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM
continue;
}

visitedMoves[visitedMovesCounter++] = move;

++_nodes;
isAnyMoveValid = true;

Expand Down Expand Up @@ -322,7 +327,7 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM

if (!move.IsCapture())
{
// 🔍 History moves
// 🔍 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();
Expand All @@ -331,6 +336,22 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM
_historyMoves[piece][targetSquare],
EvaluationConstants.HistoryBonus[depth]);

// 🔍 History penalty/malus
// When a quiet move fails high, penalize previous visited quiet moves
for (int i = 0; i < visitedMovesCounter - 1; ++i)
{
var visitedMove = visitedMoves[i];
if (!visitedMove.IsCapture())
{
var visitedMovePiece = visitedMove.Piece();
var visitedMoveTargetSquare = visitedMove.TargetSquare();

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

// 🔍 Killer moves
if (move.PromotedPiece() == default && move != _killerMoves[0][ply])
{
Expand Down
Loading