Skip to content

Commit

Permalink
adjust TT score for mates & collect PV
Browse files Browse the repository at this point in the history
  • Loading branch information
znxftw committed Dec 22, 2024
1 parent bce723e commit 447c5f2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
8 changes: 4 additions & 4 deletions Rudim.Test/UnitTest/Board/TraversalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public class TraversalTest
// This helps keep track if certain optimizations are good enough to make up for the extra time spent
// Compare time spent with and without the change before updating the keys
[Theory]
[InlineData(Helpers.StartingFEN, 2170912, 7, 8)]
[InlineData(Helpers.EndgameFEN, 450006, 36, 9)]
[InlineData(Helpers.AdvancedMoveFEN, 4800762, 1750, 8)]
[InlineData(Helpers.KiwiPeteFEN, 11787207, -42, 8)]
[InlineData(Helpers.StartingFEN, 3513132, 7, 8)]
[InlineData(Helpers.EndgameFEN, 782171, 36, 9)]
[InlineData(Helpers.AdvancedMoveFEN, 6979392, 1750, 8)]
[InlineData(Helpers.KiwiPeteFEN, 15534609, -42, 8)]
public void ShouldTraverseDeterministically(string position, int expectedNodes, int expectedScore, int depth)
{
Global.Reset();
Expand Down
48 changes: 48 additions & 0 deletions Rudim/Common/TranspositionTable.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Rudim.Board;
using System;
using System.Collections.Generic;

namespace Rudim.Common
{
Expand Down Expand Up @@ -51,6 +53,52 @@ public static void SubmitEntry(ulong hash, int score, int depth, Move bestMove,
return;
Entries[index] = new TranspositionTableEntry { Hash = hash, Score = score, Depth = depth, BestMove = bestMove, Type = entryType };
}

public static List<Move> CollectPrincipalVariation(BoardState boardState)
{
List<Move> pv = new();
while (true)
{
TranspositionTableEntry entry = Entries[boardState.BoardHash & (Capacity - 1)];
if (entry == null || entry.Hash != boardState.BoardHash || entry.Type != TranspositionEntryType.Exact)
{
break;
}
pv.Add(entry.BestMove);
boardState.MakeMove(entry.BestMove);
}

for (int i = pv.Count - 1; i >= 0; i--)
{
boardState.UnmakeMove(pv[i]);
}
return pv;
}

public static int AdjustScore(int score, int ply)
{
if (!IsCloseToCheckmate(score))
{
return score;
}

return score + (score > 0 ? +ply : -ply);
}

public static int RetrieveScore(int score, int ply)
{
if (!IsCloseToCheckmate(score))
{
return score;
}

return score + (score > 0 ? -ply : +ply);
}

private static bool IsCloseToCheckmate(int score)
{
return Constants.MaxCentipawnEval - Math.Abs(score) <= Constants.MaxPly;
}
}

public class TranspositionTableEntry
Expand Down
7 changes: 6 additions & 1 deletion Rudim/Search/IterativeDeepening.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using Rudim.CLI;
using Rudim.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;

namespace Rudim.Search
Expand Down Expand Up @@ -37,7 +39,10 @@ public static void Search(BoardState boardState, int depth, CancellationToken ca

if (debugMode)
{
CliClient.WriteLine($"info depth {i} score cp {Score} nodes {nodesTraversed} time {time} nps {nps}");
List<Move> pv = TranspositionTable.CollectPrincipalVariation(boardState);
string pvString = string.Join(' ', pv.Select(move =>
move.Source.ToString() + move.Target.ToString() + move.GetPromotionChar()));
CliClient.WriteLine($"info depth {i} score cp {Score} nodes {nodesTraversed} time {time} nps {nps} pv {pvString}");
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions Rudim/Search/Negamax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ public static int Search(BoardState boardState, int depth, CancellationToken can

private static int Search(BoardState boardState, int depth, int alpha, int beta, CancellationToken cancellationToken)
{
int ply = _searchDepth - depth;
Nodes++;

(bool hasValue, int ttScore, Move bestEvaluation) = TranspositionTable.GetEntry(boardState.BoardHash, alpha, beta, depth);
if (hasValue)
{
BestMove = bestEvaluation;
return ttScore;
return TranspositionTable.RetrieveScore(ttScore, ply);
}

if (boardState.IsDraw())
Expand All @@ -42,10 +45,8 @@ private static int Search(BoardState boardState, int depth, int alpha, int beta,
return Quiescence.Search(boardState, alpha, beta, cancellationToken);

int originalAlpha = alpha;
int ply = _searchDepth - depth;
bool foundPv = false;
TranspositionEntryType entryType = TranspositionEntryType.Alpha;
Nodes++;

boardState.GenerateMoves();
PopulateMoveScores(boardState, ply);
Expand All @@ -70,7 +71,7 @@ private static int Search(BoardState boardState, int depth, int alpha, int beta,
boardState.UnmakeMove(move);
if (score >= beta)
{
TranspositionTable.SubmitEntry(boardState.BoardHash, beta, depth, move, TranspositionEntryType.Beta);
TranspositionTable.SubmitEntry(boardState.BoardHash, TranspositionTable.AdjustScore(beta, ply), depth, move, TranspositionEntryType.Beta);
return BetaCutoff(beta, move, ply);
}
if (score > alpha)
Expand All @@ -90,7 +91,7 @@ private static int Search(BoardState boardState, int depth, int alpha, int beta,
if (alpha != originalAlpha)
BestMove = bestEvaluation;

TranspositionTable.SubmitEntry(boardState.BoardHash, alpha, depth, bestEvaluation, entryType);
TranspositionTable.SubmitEntry(boardState.BoardHash, TranspositionTable.AdjustScore(alpha, ply), depth, bestEvaluation, entryType);

return alpha;
}
Expand Down

0 comments on commit 447c5f2

Please sign in to comment.