Skip to content

Commit

Permalink
Remove CheckmateDepthFactor
Browse files Browse the repository at this point in the history
  • Loading branch information
eduherminio committed Jan 3, 2025
1 parent 7dc51c7 commit ed175a5
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 42 deletions.
5 changes: 0 additions & 5 deletions src/Lynx/EvaluationConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@ static EvaluationConstants()
/// </summary>
public const int MinEval = -32_000; // -CheckMateBaseEvaluation - (Constants.AbsoluteMaxDepth + 45) * DepthCheckmateFactor;

/// <summary>
/// This value combined with <see cref="PositiveCheckmateDetectionLimit"/> and <see cref="NegativeCheckmateDetectionLimit"/> should allow mates up to in <see cref="Constants.AbsoluteMaxDepth"/> moves.
/// </summary>
public const int CheckmateDepthFactor = 1;

/// <summary>
/// Minimum evaluation for a position to be White checkmate
/// </summary>
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 @@ -604,7 +604,7 @@ public static int EvaluateFinalPosition(int ply, bool isInCheck)
if (isInCheck)
{
// Checkmate evaluation, but not as bad/shallow as it looks like since we're already searching at a certain depth
return -CheckMateBaseEvaluation + (CheckmateDepthFactor * ply);
return -CheckMateBaseEvaluation + ply;
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/Lynx/Model/TranspositionTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ internal static int RecalculateMateScores(int score, int ply)
{
if (score > EvaluationConstants.PositiveCheckmateDetectionLimit)
{
return score - (EvaluationConstants.CheckmateDepthFactor * ply);
return score - ply;
}
else if (score < EvaluationConstants.NegativeCheckmateDetectionLimit)
{
return score + (EvaluationConstants.CheckmateDepthFactor * ply);
return score + ply;
}

return score;
Expand Down
8 changes: 4 additions & 4 deletions src/Lynx/OnlineTablebaseProber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,14 @@ public static int EvaluationSearch(Position position, int halfMovesWithoutCaptur
Math.Abs(result.DistanceToZero ?? 0) + halfMovesWithoutCaptureOrPawnMove > 100
? 0
: result.DistanceToMate.HasValue
? EvaluationConstants.CheckMateBaseEvaluation - (EvaluationConstants.CheckmateDepthFactor * (int)Math.Ceiling(0.5 * Math.Abs(result.DistanceToMate.Value)))
: EvaluationConstants.CheckMateBaseEvaluation - (49 * EvaluationConstants.CheckmateDepthFactor),
? EvaluationConstants.CheckMateBaseEvaluation - (int)Math.Ceiling(0.5 * Math.Abs(result.DistanceToMate.Value))
: EvaluationConstants.CheckMateBaseEvaluation - 49 ,
TablebaseEvaluationCategory.Loss or TablebaseEvaluationCategory.MaybeLoss =>
Math.Abs(result.DistanceToZero ?? 0) + halfMovesWithoutCaptureOrPawnMove > 100
? 0
: result.DistanceToMate.HasValue
? -EvaluationConstants.CheckMateBaseEvaluation + (EvaluationConstants.CheckmateDepthFactor * (int)Math.Ceiling(0.5 * Math.Abs(result.DistanceToMate.Value)))
: -EvaluationConstants.CheckMateBaseEvaluation + (49 * EvaluationConstants.CheckmateDepthFactor),
? -EvaluationConstants.CheckMateBaseEvaluation + (int)Math.Ceiling(0.5 * Math.Abs(result.DistanceToMate.Value))
: -EvaluationConstants.CheckMateBaseEvaluation + 49,
_ => NoResult
};
#pragma warning restore S3358 // Ternary operators should not be nested
Expand Down
4 changes: 2 additions & 2 deletions src/Lynx/Search/OnlineTablebase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public sealed partial class Engine
tablebaseResult.BestMove, score: 0, targetDepth: 0, [tablebaseResult.BestMove], mate: tablebaseResult.MateScore)
{
DepthReached = 0,
Nodes = 666, // In case some guis proritize the info command with biggest depth
Depth = 666, // In case some guis proritize the info command with biggest depth
Time = Utils.CalculateUCITime(elapsedSeconds),
NodesPerSecond = 0,
HashfullPermill = _tt.HashfullPermillApprox(),
WDL = WDL.WDLModel(
(int)Math.CopySign(
EvaluationConstants.PositiveCheckmateDetectionLimit + (EvaluationConstants.CheckmateDepthFactor * tablebaseResult.MateScore),
EvaluationConstants.PositiveCheckmateDetectionLimit + tablebaseResult.MateScore,
tablebaseResult.MateScore),
0)
};
Expand Down
2 changes: 1 addition & 1 deletion src/Lynx/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public static int Update50movesRule(Move moveToPlay, int halfMovesWithoutCapture
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int CalculateMateInX(int score, int bestScoreAbs)
{
int mate = (int)Math.Ceiling(0.5 * ((EvaluationConstants.CheckMateBaseEvaluation - bestScoreAbs) / EvaluationConstants.CheckmateDepthFactor));
int mate = (int)Math.Ceiling(0.5 * (EvaluationConstants.CheckMateBaseEvaluation - bestScoreAbs));

return (int)Math.CopySign(mate, score);
}
Expand Down
16 changes: 8 additions & 8 deletions tests/Lynx.Test/EvaluationConstantsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class EvaluationConstantsTest
[Test]
public void PositiveCheckmateDetectionLimitTest()
{
Assert.Greater(CheckMateBaseEvaluation - ((Constants.AbsoluteMaxDepth + 10) * CheckmateDepthFactor),
Assert.Greater(CheckMateBaseEvaluation - (Constants.AbsoluteMaxDepth + 10),
PositiveCheckmateDetectionLimit);

Assert.Greater(PositiveCheckmateDetectionLimit, _sensibleEvaluation);
Expand All @@ -35,7 +35,7 @@ public void PositiveCheckmateDetectionLimitTest()
[Test]
public void NegativeCheckmateDetectionLimitTest()
{
Assert.Less(-(CheckMateBaseEvaluation - ((Constants.AbsoluteMaxDepth + 10) * CheckmateDepthFactor)),
Assert.Less(-(CheckMateBaseEvaluation - (Constants.AbsoluteMaxDepth + 10)),
NegativeCheckmateDetectionLimit);

Assert.Less(NegativeCheckmateDetectionLimit, -_sensibleEvaluation);
Expand All @@ -46,14 +46,14 @@ public void NegativeCheckmateDetectionLimitTest()
[Test]
public void CheckmateDepthFactorTest()
{
const int maxCheckmateValue = CheckMateBaseEvaluation - (Constants.AbsoluteMaxDepth * CheckmateDepthFactor);
const int maxCheckmateValue = CheckMateBaseEvaluation - Constants.AbsoluteMaxDepth ;
Assert.Less(maxCheckmateValue, MaxEval);
Assert.Greater(maxCheckmateValue, MinEval);

Assert.Greater(maxCheckmateValue, PositiveCheckmateDetectionLimit);
Assert.Greater(maxCheckmateValue, NegativeCheckmateDetectionLimit);

const int minCheckmateValue = -CheckMateBaseEvaluation + (Constants.AbsoluteMaxDepth * CheckmateDepthFactor);
const int minCheckmateValue = -CheckMateBaseEvaluation + Constants.AbsoluteMaxDepth;
Assert.Less(minCheckmateValue, MaxEval);
Assert.Greater(minCheckmateValue, MinEval);

Expand Down Expand Up @@ -92,8 +92,8 @@ public void CheckmateDepthFactorTest()
[Test]
public void MaxEvalTest()
{
Assert.Greater(MaxEval, PositiveCheckmateDetectionLimit + ((Constants.AbsoluteMaxDepth + 10) * CheckmateDepthFactor));
Assert.Greater(MaxEval, CheckMateBaseEvaluation + ((Constants.AbsoluteMaxDepth + 10) * CheckmateDepthFactor));
Assert.Greater(MaxEval, PositiveCheckmateDetectionLimit + Constants.AbsoluteMaxDepth + 10);
Assert.Greater(MaxEval, CheckMateBaseEvaluation + Constants.AbsoluteMaxDepth + 10);
Assert.Greater(MaxEval, TranspositionTable.RecalculateMateScores(CheckMateBaseEvaluation, Constants.AbsoluteMaxDepth));
Assert.Greater(MaxEval, TranspositionTable.RecalculateMateScores(CheckMateBaseEvaluation, -Constants.AbsoluteMaxDepth));
Assert.Less(MaxEval, short.MaxValue);
Expand All @@ -102,8 +102,8 @@ public void MaxEvalTest()
[Test]
public void MinEvalTest()
{
Assert.Less(MinEval, NegativeCheckmateDetectionLimit - ((Constants.AbsoluteMaxDepth + 10) * CheckmateDepthFactor));
Assert.Less(MinEval, -CheckMateBaseEvaluation - ((Constants.AbsoluteMaxDepth + 10) * CheckmateDepthFactor));
Assert.Less(MinEval, NegativeCheckmateDetectionLimit - (Constants.AbsoluteMaxDepth + 10));
Assert.Less(MinEval, -CheckMateBaseEvaluation - (Constants.AbsoluteMaxDepth + 10));
Assert.Less(MinEval, TranspositionTable.RecalculateMateScores(-CheckMateBaseEvaluation, Constants.AbsoluteMaxDepth));
Assert.Less(MinEval, TranspositionTable.RecalculateMateScores(-CheckMateBaseEvaluation, -Constants.AbsoluteMaxDepth));
Assert.Greater(MinEval, short.MinValue);
Expand Down
20 changes: 10 additions & 10 deletions tests/Lynx.Test/Model/TranspositionTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public class TranspositionTableTests
[TestCase(-10_000, 5, -10_000)]
[TestCase(NegativeCheckmateDetectionLimit + 1, 5, NegativeCheckmateDetectionLimit + 1)]

[TestCase(CheckMateBaseEvaluation - (5 * CheckmateDepthFactor), 2, CheckMateBaseEvaluation - (7 * CheckmateDepthFactor))]
[TestCase(CheckMateBaseEvaluation - (2 * CheckmateDepthFactor), 4, CheckMateBaseEvaluation - (6 * CheckmateDepthFactor))]
[TestCase(CheckMateBaseEvaluation - 5, 2, CheckMateBaseEvaluation - 7)]
[TestCase(CheckMateBaseEvaluation - 2, 4, CheckMateBaseEvaluation - 6)]

[TestCase(-CheckMateBaseEvaluation + (5 * CheckmateDepthFactor), 2, -CheckMateBaseEvaluation + (7 * CheckmateDepthFactor))]
[TestCase(-CheckMateBaseEvaluation + (2 * CheckmateDepthFactor), 4, -CheckMateBaseEvaluation + (6 * CheckmateDepthFactor))]
[TestCase(-CheckMateBaseEvaluation + 5, 2, -CheckMateBaseEvaluation + 7)]
[TestCase(-CheckMateBaseEvaluation + 2, 4, -CheckMateBaseEvaluation + 6)]
public void RecalculateMateScores(int evaluation, int depth, int expectedEvaluation)
{
Assert.AreEqual(expectedEvaluation, TranspositionTable.RecalculateMateScores(evaluation, depth));
Expand All @@ -40,8 +40,8 @@ public void RecordHash_ProbeHash(int recordedEval, NodeType recordNodeType, int
Assert.AreEqual(staticEval, ttEntry.StaticEval);
}

[TestCase(CheckMateBaseEvaluation - (8 * CheckmateDepthFactor))]
[TestCase(-CheckMateBaseEvaluation + (3 * CheckmateDepthFactor))]
[TestCase(CheckMateBaseEvaluation - 8)]
[TestCase(-CheckMateBaseEvaluation + 3)]
public void RecordHash_ProbeHash_CheckmateSameDepth(int recordedEval)
{
const int sharedDepth = 5;
Expand All @@ -55,10 +55,10 @@ public void RecordHash_ProbeHash_CheckmateSameDepth(int recordedEval)
Assert.AreEqual(recordedEval, ttEntry.StaticEval);
}

[TestCase(CheckMateBaseEvaluation - (8 * CheckmateDepthFactor), 5, 4, CheckMateBaseEvaluation - (7 * CheckmateDepthFactor))]
[TestCase(CheckMateBaseEvaluation - (8 * CheckmateDepthFactor), 5, 6, CheckMateBaseEvaluation - (9 * CheckmateDepthFactor))]
[TestCase(-CheckMateBaseEvaluation + (8 * CheckmateDepthFactor), 5, 4, -CheckMateBaseEvaluation + (7 * CheckmateDepthFactor))]
[TestCase(-CheckMateBaseEvaluation + (8 * CheckmateDepthFactor), 5, 6, -CheckMateBaseEvaluation + (9 * CheckmateDepthFactor))]
[TestCase(CheckMateBaseEvaluation - 8, 5, 4, CheckMateBaseEvaluation - 7)]
[TestCase(CheckMateBaseEvaluation - 8, 5, 6, CheckMateBaseEvaluation - 9)]
[TestCase(-CheckMateBaseEvaluation + 8 , 5, 4, -CheckMateBaseEvaluation + 7)]
[TestCase(-CheckMateBaseEvaluation + 8 , 5, 6, -CheckMateBaseEvaluation + 9)]
public void RecordHash_ProbeHash_CheckmateDifferentDepth(int recordedEval, int recordedDeph, int probeDepth, int expectedProbeEval)
{
var position = new Position(Constants.InitialPositionFEN);
Expand Down
18 changes: 9 additions & 9 deletions tests/Lynx.Test/OnlineTablebaseProberTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public void EvaluationSearch_LossButHoldingAsLongAsPossible(string fen, int dist
{
var position = new Position(fen);
var result = OnlineTablebaseProber.EvaluationSearch(position, 0, default);
Assert.AreEqual(-CheckMateBaseEvaluation + (distanceToMate * CheckmateDepthFactor), result);
Assert.AreEqual(-CheckMateBaseEvaluation + distanceToMate, result);
}

[TestCase("8/3P3K/3p1k2/8/8/8/3p4/8 w - - 1 1", 14, "d7d8q")] // Win or lose
Expand Down Expand Up @@ -226,9 +226,9 @@ public async Task RootSearch_DrawWinningPositionDueToExistingHalfMovesWithoutCap
/// <summary>
/// In evaluation search we need to check <paramref name="halfMovesWithoutCaptureOrPawnMove"/> to make sure to apply the 50 moves rule correctly
/// </summary>
[TestCase("kN6/8/8/8/8/8/8/B3K3 w - - 0 1", 0, CheckMateBaseEvaluation - (27 * CheckmateDepthFactor))] // B+N, Mate in 27, DTZ==DTM=58
[TestCase("kN6/8/8/8/8/8/8/B3K3 w - - 0 1", 0, CheckMateBaseEvaluation - 27)] // B+N, Mate in 27, DTZ==DTM=58
[TestCase("kN6/8/8/8/8/8/8/B3K3 w - - 0 1", 50, 0)] // B+N, Mate in 27, DTM=58, DTZ >100
[TestCase("Kn6/8/8/8/8/8/8/b3k3 b - - 0 1", 0, +CheckMateBaseEvaluation - (27 * CheckmateDepthFactor))] // B+N, Mate in 27, DTZ?==DTM=58
[TestCase("Kn6/8/8/8/8/8/8/b3k3 b - - 0 1", 0, +CheckMateBaseEvaluation - 27)] // B+N, Mate in 27, DTZ?==DTM=58
[TestCase("Kn6/8/8/8/8/8/8/b3k3 b - - 0 1", 50, 0)] // B+N, Mate in 27, DTZ?==DTM=58, DTZ >100
public void EvaluationSearch_DrawWinningPositionDueToExistingHalfMovesWithoutCaptureOrPawnMove(string fen, int halfMovesWithoutCaptureOrPawnMove, int expectedEvaluation)
{
Expand All @@ -255,9 +255,9 @@ public async Task RootSearch_DrawLosingPositionDueToExistingHalfMovesWithoutCapt
/// <summary>
/// In evaluation search we need to check <paramref name="halfMovesWithoutCaptureOrPawnMove"/> to make sure to apply the 50 moves rule correctly
/// </summary>
[TestCase("6kN/8/8/8/8/8/8/B3K3 b - - 0 1", 0, -CheckMateBaseEvaluation + (30 * CheckmateDepthFactor))] // B+N, Mate in 30, DTZ?==DTM=60
[TestCase("6kN/8/8/8/8/8/8/B3K3 b - - 0 1", 0, -CheckMateBaseEvaluation + 30)] // B+N, Mate in 30, DTZ?==DTM=60
[TestCase("6kN/8/8/8/8/8/8/B3K3 b - - 0 1", 42, 0)] // B+N, Mate in 30, DTZ?==DTM=60
[TestCase("6Kn/8/8/8/8/8/8/b3k3 w - - 0 1", 0, -CheckMateBaseEvaluation + (30 * CheckmateDepthFactor))] // B+N, Mate in 30, DTZ?==DTM=60
[TestCase("6Kn/8/8/8/8/8/8/b3k3 w - - 0 1", 0, -CheckMateBaseEvaluation + 30)] // B+N, Mate in 30, DTZ?==DTM=60
[TestCase("6Kn/8/8/8/8/8/8/b3k3 w - - 0 1", 42, 0)] // B+N, Mate in 30, DTZ?==DTM=60
public void EvaluationSearch_DrawLosingPositionDueToExistingHalfMovesWithoutCaptureOrPawnMove(string fen, int halfMovesWithoutCaptureOrPawnMove, int expectedEvaluation)
{
Expand All @@ -280,7 +280,7 @@ public void EvaluationSearch_NoDistanceToMateProvidedWhileWinning(string fen, in
{
var position = new Position(fen);
var result = OnlineTablebaseProber.EvaluationSearch(position, default, default);
Assert.AreEqual(+CheckMateBaseEvaluation - (CheckmateDepthFactor * distanceToMate), result);
Assert.AreEqual(+CheckMateBaseEvaluation - distanceToMate, result);
}

[TestCase("8/1k6/8/8/p1P4B/2K1Pp2/8/8 b - - 1 1", -49)]
Expand All @@ -297,7 +297,7 @@ public void EvaluationSearch_NoDistanceToMateProvidedWhileLosing(string fen, int
{
var position = new Position(fen);
var result = OnlineTablebaseProber.EvaluationSearch(position, default, default);
Assert.AreEqual(-CheckMateBaseEvaluation + (CheckmateDepthFactor * distanceToMate), result);
Assert.AreEqual(-CheckMateBaseEvaluation + distanceToMate, result);
}

[TestCase("8/8/1p6/8/P1P3k1/P7/P6K/8 b - - 66 1", -49, new[] { "g4f4", "g4f5" })]
Expand All @@ -314,7 +314,7 @@ public void EvaluationSearch_MaybeLosing(string fen, int distanceToMate, string[
{
var position = new Position(fen);
var result = OnlineTablebaseProber.EvaluationSearch(position, default, default);
Assert.AreEqual(-CheckMateBaseEvaluation + (CheckmateDepthFactor * distanceToMate), result);
Assert.AreEqual(-CheckMateBaseEvaluation + distanceToMate, result);
}

[TestCase("8/8/1p6/8/P1P2k2/P7/P6K/8 w - - 67 2", +49, new[] { "h2h3" })]
Expand All @@ -331,7 +331,7 @@ public void EvaluationSearch_MaybeWinning(string fen, int distanceToMate, string
{
var position = new Position(fen);
var result = OnlineTablebaseProber.EvaluationSearch(position, default, default);
Assert.AreEqual(CheckMateBaseEvaluation - (CheckmateDepthFactor * distanceToMate), result);
Assert.AreEqual(CheckMateBaseEvaluation - distanceToMate, result);
}

[TestCase("8/2PK4/1k6/8/8/8/8/8 w - - 0 1", 6, "c7c8q")]
Expand Down

0 comments on commit ed175a5

Please sign in to comment.