Skip to content

Commit

Permalink
Scale eval with material
Browse files Browse the repository at this point in the history
  • Loading branch information
eduherminio committed Nov 13, 2024
1 parent c590b71 commit 5f151e5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
16 changes: 16 additions & 0 deletions src/Lynx/Model/Position.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;

/// <summary>
/// Formula based on Stormphrax
/// </summary>
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
Expand Down
18 changes: 9 additions & 9 deletions src/Lynx/SEE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class SEE
{
#pragma warning disable IDE0055 // Discard formatting in this region

private static ReadOnlySpan<int> _pieceValues =>
public static ReadOnlySpan<int> PieceValues =>
[
100, 450, 450, 650, 1250, 0,
100, 450, 450, 650, 1250, 0,
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 5f151e5

Please sign in to comment.