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

⚖️ Material scaling: SP formula #1163

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading