From 5f151e5517b4a9d460aac694b9f5f6ffb603b9c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20C=C3=A1ceres?= Date: Wed, 13 Nov 2024 15:38:20 +0100 Subject: [PATCH 1/2] Scale eval with material --- src/Lynx/Model/Position.cs | 16 ++++++++++++++++ src/Lynx/SEE.cs | 18 +++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/Lynx/Model/Position.cs b/src/Lynx/Model/Position.cs index 46f66a752..7a8872e5c 100644 --- a/src/Lynx/Model/Position.cs +++ b/src/Lynx/Model/Position.cs @@ -560,6 +560,7 @@ public bool WasProduceByAValidMove() eval = (int)(eval * ((80 + (totalPawnsCount * 7)) / 128.0)); eval = ScaleEvalWith50MovesDrawDistance(eval, movesWithoutCaptureOrPawnMove); + eval = ScaleEvalWithMaterial(eval); eval = Math.Clamp(eval, MinStaticEval, MaxStaticEval); @@ -856,6 +857,21 @@ internal int KingAdditionalEvaluation(int squareIndex, int pieceSide, BitBoard e internal static int ScaleEvalWith50MovesDrawDistance(int eval, int movesWithoutCaptureOrPawnMove) => eval * (200 - movesWithoutCaptureOrPawnMove) / 200; + /// + /// Formula based on Stormphrax + /// + internal int ScaleEvalWithMaterial(int eval) + { + var material_phase = + (SEE.PieceValues[(int)Piece.P] * (PieceBitBoards[(int)Piece.P] & PieceBitBoards[(int)Piece.p]).CountBits()) + + (SEE.PieceValues[(int)Piece.N] * (PieceBitBoards[(int)Piece.N] & PieceBitBoards[(int)Piece.n]).CountBits()) + + (SEE.PieceValues[(int)Piece.B] * (PieceBitBoards[(int)Piece.B] & PieceBitBoards[(int)Piece.b]).CountBits()) + + (SEE.PieceValues[(int)Piece.R] * (PieceBitBoards[(int)Piece.R] & PieceBitBoards[(int)Piece.r]).CountBits()) + + (SEE.PieceValues[(int)Piece.Q] * (PieceBitBoards[(int)Piece.Q] & PieceBitBoards[(int)Piece.q]).CountBits()); + + return eval * (26500 + material_phase) / 32768; + } + #endregion #region Attacks diff --git a/src/Lynx/SEE.cs b/src/Lynx/SEE.cs index d1c76fde5..556822148 100644 --- a/src/Lynx/SEE.cs +++ b/src/Lynx/SEE.cs @@ -10,7 +10,7 @@ public static class SEE { #pragma warning disable IDE0055 // Discard formatting in this region - private static ReadOnlySpan _pieceValues => + public static ReadOnlySpan PieceValues => [ 100, 450, 450, 650, 1250, 0, 100, 450, 450, 650, 1250, 0, @@ -35,7 +35,7 @@ public static bool IsGoodCapture(Position position, Move move, int threshold = 0 var sideToMove = position.Side; - var score = _pieceValues[move.CapturedPiece()] - threshold; // Gain() - threshold + var score = PieceValues[move.CapturedPiece()] - threshold; // Gain() - threshold // If taking the opponent's piece without any risk is still negative if (score < 0) @@ -44,7 +44,7 @@ public static bool IsGoodCapture(Position position, Move move, int threshold = 0 } var next = move.Piece(); - score -= _pieceValues[next]; + score -= PieceValues[next]; // If risking our piece being fully lost and the exchange value is still >= 0 if (score >= 0) @@ -91,7 +91,7 @@ public static bool IsGoodCapture(Position position, Move move, int threshold = 0 // Removing used pieces from attackers attackers &= occupancy; - score = -score - 1 - _pieceValues[nextPiece]; + score = -score - 1 - PieceValues[nextPiece]; us = Utils.OppositeSide(us); if (score >= 0) @@ -134,7 +134,7 @@ public static bool HasPositiveScore(Position position, Move move, int threshold ? move.PromotedPiece() : move.Piece(); - score -= _pieceValues[next]; + score -= PieceValues[next]; // If risking our piece being fully lost and the exchange value is still >= 0 if (score >= 0) @@ -181,7 +181,7 @@ public static bool HasPositiveScore(Position position, Move move, int threshold // Removing used pieces from attackers attackers &= occupancy; - score = -score - 1 - _pieceValues[nextPiece]; + score = -score - 1 - PieceValues[nextPiece]; us = Utils.OppositeSide(us); if (score >= 0) @@ -209,15 +209,15 @@ private static int Gain(Move move) } else if (move.IsEnPassant()) { - return _pieceValues[(int)Piece.P]; + return PieceValues[(int)Piece.P]; } var promotedPiece = move.PromotedPiece(); #pragma warning disable S3358 // Ternary operators should not be nested return promotedPiece == default - ? _pieceValues[move.CapturedPiece()] - : _pieceValues[promotedPiece] - _pieceValues[(int)Piece.P] + (move.IsCapture() ? _pieceValues[move.CapturedPiece()] : 0); + ? PieceValues[move.CapturedPiece()] + : PieceValues[promotedPiece] - PieceValues[(int)Piece.P] + (move.IsCapture() ? PieceValues[move.CapturedPiece()] : 0); #pragma warning restore S3358 // Ternary operators should not be nested } From 3681e38a9cc990225878e53b4d13e93c708fc75c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20C=C3=A1ceres?= Date: Wed, 13 Nov 2024 16:17:40 +0100 Subject: [PATCH 2/2] You dumbass --- src/Lynx/Model/Position.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Lynx/Model/Position.cs b/src/Lynx/Model/Position.cs index 7a8872e5c..d76a549d0 100644 --- a/src/Lynx/Model/Position.cs +++ b/src/Lynx/Model/Position.cs @@ -860,14 +860,14 @@ internal static int ScaleEvalWith50MovesDrawDistance(int eval, int movesWithoutC /// /// Formula based on Stormphrax /// - internal int ScaleEvalWithMaterial(int eval) + internal int ScaleEvalWithMaterial(int eval) { var material_phase = - (SEE.PieceValues[(int)Piece.P] * (PieceBitBoards[(int)Piece.P] & PieceBitBoards[(int)Piece.p]).CountBits()) - + (SEE.PieceValues[(int)Piece.N] * (PieceBitBoards[(int)Piece.N] & PieceBitBoards[(int)Piece.n]).CountBits()) - + (SEE.PieceValues[(int)Piece.B] * (PieceBitBoards[(int)Piece.B] & PieceBitBoards[(int)Piece.b]).CountBits()) - + (SEE.PieceValues[(int)Piece.R] * (PieceBitBoards[(int)Piece.R] & PieceBitBoards[(int)Piece.r]).CountBits()) - + (SEE.PieceValues[(int)Piece.Q] * (PieceBitBoards[(int)Piece.Q] & PieceBitBoards[(int)Piece.q]).CountBits()); + (SEE.PieceValues[(int)Piece.P] * (PieceBitBoards[(int)Piece.P] | PieceBitBoards[(int)Piece.p]).CountBits()) + + (SEE.PieceValues[(int)Piece.N] * (PieceBitBoards[(int)Piece.N] | PieceBitBoards[(int)Piece.n]).CountBits()) + + (SEE.PieceValues[(int)Piece.B] * (PieceBitBoards[(int)Piece.B] | PieceBitBoards[(int)Piece.b]).CountBits()) + + (SEE.PieceValues[(int)Piece.R] * (PieceBitBoards[(int)Piece.R] | PieceBitBoards[(int)Piece.r]).CountBits()) + + (SEE.PieceValues[(int)Piece.Q] * (PieceBitBoards[(int)Piece.Q] | PieceBitBoards[(int)Piece.q]).CountBits()); return eval * (26500 + material_phase) / 32768; }