Skip to content

Commit

Permalink
⚡ (Re)calculate incremental eval accumulator on StaticEvaluation wh…
Browse files Browse the repository at this point in the history
…en needed (#1353)

This allows to keep having incremental eval after a king move, being that position right after the move the only one that isn't calculated incrementally.
  • Loading branch information
eduherminio authored Jan 10, 2025
1 parent 2bc3696 commit c084883
Showing 1 changed file with 11 additions and 60 deletions.
71 changes: 11 additions & 60 deletions src/Lynx/Model/Position.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public Position((BitBoard[] PieceBitBoards, BitBoard[] OccupancyBitBoards, int[]
UniqueIdentifier = ZobristTable.PositionHash(this);
#pragma warning restore S3366 // "this" should not be exposed from constructors

_isIncrementalEval = true;
_incrementalEvalAccumulator = InitialIncrementalStaticEvaluation();
_isIncrementalEval = false;
}

/// <summary>
Expand Down Expand Up @@ -536,59 +535,6 @@ public bool WasProduceByAValidMove()

#region Evaluation

/// <summary>
/// Base incremental evaluation for the position.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int InitialIncrementalStaticEvaluation()
{
int packedScore = 0;

var whiteKing = PieceBitBoards[(int)Piece.K].GetLS1BIndex();
var blackKing = PieceBitBoards[(int)Piece.k].GetLS1BIndex();

var whiteBucket = PSQTBucketLayout[whiteKing];
var blackBucket = PSQTBucketLayout[blackKing ^ 56];

// White pieces PSQTs, except king
for (int pieceIndex = (int)Piece.P; pieceIndex < (int)Piece.K; ++pieceIndex)
{
var bitboard = PieceBitBoards[pieceIndex];

while (bitboard != default)
{
var pieceSquareIndex = bitboard.GetLS1BIndex();
bitboard.ResetLS1B();

packedScore += PSQT(0, whiteBucket, pieceIndex, pieceSquareIndex)
+ PSQT(1, blackBucket, pieceIndex, pieceSquareIndex);
}
}

// Black pieces PSQTs, except king
for (int pieceIndex = (int)Piece.p; pieceIndex < (int)Piece.k; ++pieceIndex)
{
var bitboard = PieceBitBoards[pieceIndex];

while (bitboard != default)
{
var pieceSquareIndex = bitboard.GetLS1BIndex();
bitboard.ResetLS1B();

packedScore += PSQT(0, blackBucket, pieceIndex, pieceSquareIndex)
+ PSQT(1, whiteBucket, pieceIndex, pieceSquareIndex);
}
}

// Kings
packedScore += PSQT(0, whiteBucket, (int)Piece.K, whiteKing)
+ PSQT(0, blackBucket, (int)Piece.k, blackKing)
+ PSQT(1, blackBucket, (int)Piece.K, whiteKing)
+ PSQT(1, whiteBucket, (int)Piece.k, blackKing);

return packedScore;
}

/// <summary>
/// Evaluates material and position in a NegaMax style.
/// That is, positive scores always favour playing <see cref="Side"/>.
Expand Down Expand Up @@ -663,6 +609,8 @@ public int InitialIncrementalStaticEvaluation()
}
else
{
_incrementalEvalAccumulator = 0;

// White pieces PSQTs and additional eval, except king
for (int pieceIndex = (int)Piece.P; pieceIndex < (int)Piece.K; ++pieceIndex)
{
Expand All @@ -676,8 +624,8 @@ public int InitialIncrementalStaticEvaluation()
var pieceSquareIndex = bitboard.GetLS1BIndex();
bitboard.ResetLS1B();

packedScore += PSQT(0, whiteBucket, pieceIndex, pieceSquareIndex)
+ PSQT(1, blackBucket, pieceIndex, pieceSquareIndex);
_incrementalEvalAccumulator += PSQT(0, whiteBucket, pieceIndex, pieceSquareIndex)
+ PSQT(1, blackBucket, pieceIndex, pieceSquareIndex);

gamePhase += GamePhaseByPiece[pieceIndex];

Expand All @@ -699,8 +647,8 @@ public int InitialIncrementalStaticEvaluation()
var pieceSquareIndex = bitboard.GetLS1BIndex();
bitboard.ResetLS1B();

packedScore += PSQT(0, blackBucket, pieceIndex, pieceSquareIndex)
+ PSQT(1, whiteBucket, pieceIndex, pieceSquareIndex);
_incrementalEvalAccumulator += PSQT(0, blackBucket, pieceIndex, pieceSquareIndex)
+ PSQT(1, whiteBucket, pieceIndex, pieceSquareIndex);

gamePhase += GamePhaseByPiece[pieceIndex];

Expand All @@ -709,11 +657,14 @@ public int InitialIncrementalStaticEvaluation()
}

// Kings
packedScore +=
_incrementalEvalAccumulator +=
PSQT(0, whiteBucket, (int)Piece.K, whiteKing)
+ PSQT(1, blackBucket, (int)Piece.K, whiteKing)
+ PSQT(0, blackBucket, (int)Piece.k, blackKing)
+ PSQT(1, whiteBucket, (int)Piece.k, blackKing);

packedScore += _incrementalEvalAccumulator;
_isIncrementalEval = true;
}

packedScore +=
Expand Down

0 comments on commit c084883

Please sign in to comment.