From c08488374cb6913373a49d58bba9b83c21a8e175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20C=C3=A1ceres?= Date: Fri, 10 Jan 2025 23:57:55 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20(Re)calculate=20incremental=20eval?= =?UTF-8?q?=20accumulator=20on=20`StaticEvaluation`=20when=20needed=20(#13?= =?UTF-8?q?53)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/Lynx/Model/Position.cs | 71 ++++++-------------------------------- 1 file changed, 11 insertions(+), 60 deletions(-) diff --git a/src/Lynx/Model/Position.cs b/src/Lynx/Model/Position.cs index 31af26b90..8628751ea 100644 --- a/src/Lynx/Model/Position.cs +++ b/src/Lynx/Model/Position.cs @@ -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; } /// @@ -536,59 +535,6 @@ public bool WasProduceByAValidMove() #region Evaluation - /// - /// Base incremental evaluation for the position. - /// - [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; - } - /// /// Evaluates material and position in a NegaMax style. /// That is, positive scores always favour playing . @@ -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) { @@ -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]; @@ -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]; @@ -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 +=