Skip to content

Commit

Permalink
🧹 Clean Position.StaticEval() (#480)
Browse files Browse the repository at this point in the history
Stop passing unused `movesWithoutCaptureOrPawnMove` to `Position.StaticEval()`
  • Loading branch information
eduherminio authored Oct 30, 2023
1 parent c7ccd86 commit 6155f47
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
20 changes: 10 additions & 10 deletions src/Lynx.Dev/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -956,56 +956,56 @@ static void FileAndRankMasks()
static void EnhancedPawnEvaluation()
{
var position = new Position("4k3/ppp5/8/8/8/P7/PP6/4K3 w - - 0 1");
var eval = position.StaticEvaluation(default);
var eval = position.StaticEvaluation();
position.Print();
Console.WriteLine(eval);

position = new Position("4k3/pp4pp/p7/8/8/P7/PPP3P1/4K3 w - - 0 1");
position.Print();
eval = position.StaticEvaluation(default);
eval = position.StaticEvaluation();
Console.WriteLine(eval);

position = new Position("4k3/pp3pp1/p7/8/8/P7/PPP4P/4K3 w - - 0 1");
position.Print();
eval = position.StaticEvaluation(default);
eval = position.StaticEvaluation();
Console.WriteLine(eval);

position = new Position("4k3/pp3pp1/p7/8/8/P7/PP3P1P/4K3 w - - 0 1");
position.Print();
eval = position.StaticEvaluation(default);
eval = position.StaticEvaluation();
Console.WriteLine(eval);

position = new Position("4k3/pp2pp2/p7/8/8/P7/PP3P1P/4K3 w - - 0 1");
position.Print();
eval = position.StaticEvaluation(default);
eval = position.StaticEvaluation();
Console.WriteLine(eval);

position = new Position("4k3/pp2pp2/p7/8/7P/P7/PP3P2/4K3 w - - 0 1");
position.Print();
eval = position.StaticEvaluation(default);
eval = position.StaticEvaluation();
Console.WriteLine(eval);

position = new Position("4k3/pp2pp1P/p7/8/8/P7/PP3P2/4K3 w - - 0 1");
position.Print();
eval = position.StaticEvaluation(default);
eval = position.StaticEvaluation();
Console.WriteLine(eval);
}

static void RookEvaluation()
{
var position = new Position("r3k3/7p/8/8/8/8/7P/4K2R w - - 0 1");
position.Print();
var eval = position.StaticEvaluation(default);
var eval = position.StaticEvaluation();
Console.WriteLine(eval);

position = new Position("r3k3/1p6/8/8/8/8/7P/4K2R w - - 0 1");
position.Print();
eval = position.StaticEvaluation(default);
eval = position.StaticEvaluation();
Console.WriteLine(eval);

position = new Position("r3k3/1P6/8/8/8/8/7p/4K2R w - - 0 1");
position.Print();
eval = position.StaticEvaluation(default);
eval = position.StaticEvaluation();
Console.WriteLine(eval);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Lynx/LynxDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ private async ValueTask HandleStaticEval(string rawCommand, CancellationToken ca
_logger.Debug("Raw fen: {0}, parsed fen: {1}", fen, ourFen);
}

var eval = WDL.NormalizeScore(position.StaticEvaluation(0));
var eval = WDL.NormalizeScore(position.StaticEvaluation());
if (position.Side == Side.Black)
{
eval = -eval; // White perspective
Expand Down
2 changes: 1 addition & 1 deletion src/Lynx/Model/Position.cs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ public string FEN(int halfMovesWithoutCaptureOrPawnMove = 0, int fullMoveClock =
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int StaticEvaluation(int movesWithoutCaptureOrPawnMove)
public int StaticEvaluation()
{
//var result = OnlineTablebaseProber.EvaluationSearch(this, movesWithoutCaptureOrPawnMove, cancellationToken);
//Debug.Assert(result < EvaluationConstants.CheckMateBaseEvaluation, $"position {FEN()} returned tb eval out of bounds: {result}");
Expand Down
8 changes: 4 additions & 4 deletions src/Lynx/Search/NegaMax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM
if (ply >= Configuration.EngineSettings.MaxDepth)
{
_logger.Info("Max depth {0} reached", Configuration.EngineSettings.MaxDepth);
return position.StaticEvaluation(Game.HalfMovesWithoutCaptureOrPawnMove);
return position.StaticEvaluation();
}

_maxDepthReached[ply] = ply;
Expand Down Expand Up @@ -73,7 +73,7 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM

if (!pvNode && !isInCheck)
{
var staticEval = position.StaticEvaluation(Game.HalfMovesWithoutCaptureOrPawnMove);
var staticEval = position.StaticEvaluation();

// 🔍 Null Move Pruning (NMP) - our position is so good that we can potentially afford giving ouropponent a double move and still remain ahead of beta
if (depth >= Configuration.EngineSettings.NMP_MinDepth
Expand Down Expand Up @@ -351,7 +351,7 @@ public int QuiescenceSearch(int ply, int alpha, int beta)
if (ply >= Configuration.EngineSettings.MaxDepth)
{
_logger.Info("Max depth {0} reached", Configuration.EngineSettings.MaxDepth);
return position.StaticEvaluation(Game.HalfMovesWithoutCaptureOrPawnMove);
return position.StaticEvaluation();
}

var pvIndex = PVTable.Indexes[ply];
Expand All @@ -369,7 +369,7 @@ public int QuiescenceSearch(int ply, int alpha, int beta)

_maxDepthReached[ply] = ply;

var staticEvaluation = position.StaticEvaluation(Game.HalfMovesWithoutCaptureOrPawnMove);
var staticEvaluation = position.StaticEvaluation();

// Fail-hard beta-cutoff (updating alpha after this check)
if (staticEvaluation >= beta)
Expand Down
2 changes: 1 addition & 1 deletion tests/Lynx.Test/GameTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void IsThreefoldRepetition_CastleRightsRemoval()
Assert.DoesNotThrow(() => game.MakeMove(repeatedMoves[^1]));
Assert.AreEqual(repeatedMoves.Count, game.MoveHistory.Count);

var eval = winningPosition.StaticEvaluation(default);
var eval = winningPosition.StaticEvaluation();
Assert.AreNotEqual(0, eval);

Assert.DoesNotThrow(() => game.MakeMove(repeatedMoves[5]));
Expand Down
4 changes: 2 additions & 2 deletions tests/Lynx.Test/Model/PositionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,15 @@ public void StaticEvaluation_DrawDueToLackOfMaterial(string fen)
{
var position = new Position(fen);

Assert.AreEqual(0, position.StaticEvaluation(default));
Assert.AreEqual(0, position.StaticEvaluation());
}

[TestCase("4k3/8/8/7Q/7q/8/4K3/8 w - - 0 1", "4k3/8/8/7Q/7q/8/8/4K3 w - - 0 1", Description = "King in 7th rank with queens > King in 8th rank with queens")]
[TestCase("4k3/p7/8/8/8/8/P3K3/8 w - - 0 1", "4k3/p7/8/8/8/8/P7/4K3 w - - 0 1", Description = "King in 7th rank without queens > King in 8th rank without queens")]
[TestCase("4k3/7p/8/8/4K3/8/7P/8 w - - 0 1", "4k3/7p/8/q7/4K3/Q7/7P/8 w - - 0 1", Description = "King in the center without queens > King in the center with queens")]
public void StaticEvaluation_KingEndgame(string fen1, string fen2)
{
Assert.Greater(new Position(fen1).StaticEvaluation(default), new Position(fen2).StaticEvaluation(default));
Assert.Greater(new Position(fen1).StaticEvaluation(), new Position(fen2).StaticEvaluation());
}

/// <summary>
Expand Down

0 comments on commit 6155f47

Please sign in to comment.