| | 1 | | using Rudim.Board; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | |
|
| | 5 | | namespace Rudim.Common |
| | 6 | | { |
| | 7 | | public static class TranspositionTable |
| | 8 | | { |
| | 9 | | // TODO : Calculate this based on a constant and in MiB, not hard numbers |
| | 10 | | private const int Capacity = 4096 * 16; |
| | 11 | | private static readonly TranspositionTableEntry[] Entries; |
| | 12 | |
|
| | 13 | | static TranspositionTable() |
| | 14 | | { |
| 1 | 15 | | Entries = new TranspositionTableEntry[Capacity]; |
| 1 | 16 | | } |
| | 17 | |
|
| | 18 | | public static void ClearTable() |
| | 19 | | { |
| 9 | 20 | | Array.Clear(Entries); |
| 9 | 21 | | } |
| | 22 | |
|
| | 23 | | public static Move GetHashMove(ulong hash) |
| | 24 | | { |
| 0 | 25 | | TranspositionTableEntry entry = Entries[hash & (Capacity - 1)]; |
| 0 | 26 | | return entry?.Hash == hash && entry.Type == TranspositionEntryType.Exact ? entry.BestMove : null; |
| | 27 | | } |
| | 28 | |
|
| | 29 | | public static (bool, int, Move) GetEntry(ulong hash, int alpha, int beta, int depth) |
| | 30 | | { |
| 1840423 | 31 | | TranspositionTableEntry entry = Entries[hash & (Capacity - 1)]; |
| | 32 | |
|
| 1840423 | 33 | | if (entry == null) |
| 984657 | 34 | | return (false, 0, null); |
| 855766 | 35 | | if (entry.Hash != hash) |
| 645115 | 36 | | return (false, 0, null); |
| 210651 | 37 | | if (entry.Depth < depth) |
| 94557 | 38 | | return (false, 0, null); |
| | 39 | |
|
| 116094 | 40 | | switch (entry.Type) |
| | 41 | | { |
| | 42 | | case TranspositionEntryType.Exact: |
| 227 | 43 | | return (true, entry.Score, entry.BestMove); |
| | 44 | | case TranspositionEntryType.Alpha: |
| 23823 | 45 | | if(entry.Score <= alpha) return (true, alpha, entry.BestMove); |
| | 46 | | break; |
| | 47 | | case TranspositionEntryType.Beta: |
| 169414 | 48 | | if(entry.Score >= beta) return (true, beta, entry.BestMove); |
| | 49 | | break; |
| | 50 | | } |
| | 51 | |
|
| 38497 | 52 | | return (false, 0, null); |
| | 53 | | } |
| | 54 | |
|
| | 55 | | public static void SubmitEntry(ulong hash, int score, int depth, Move bestMove, TranspositionEntryType entryType |
| | 56 | | { |
| 437099 | 57 | | var index = hash & (Capacity - 1); |
| 437099 | 58 | | if (Entries[index]?.Depth >= depth) |
| 206684 | 59 | | return; |
| 230415 | 60 | | Entries[index] = new TranspositionTableEntry { Hash = hash, Score = score, Depth = depth, BestMove = bestMov |
| 230415 | 61 | | } |
| | 62 | |
|
| | 63 | | public static List<Move> CollectPrincipalVariation(BoardState boardState) |
| | 64 | | { |
| 42 | 65 | | List<Move> pv = new(); |
| 104 | 66 | | while (true) |
| | 67 | | { |
| 146 | 68 | | TranspositionTableEntry entry = Entries[boardState.BoardHash & (Capacity - 1)]; |
| 146 | 69 | | if (entry == null || entry.Hash != boardState.BoardHash || entry.Type != TranspositionEntryType.Exact) |
| | 70 | | { |
| | 71 | | break; |
| | 72 | | } |
| | 73 | |
|
| 104 | 74 | | if (pv.Contains(entry.BestMove)) |
| | 75 | | break; |
| | 76 | |
|
| 104 | 77 | | boardState.MakeMove(entry.BestMove); |
| 104 | 78 | | if (boardState.IsInCheck(boardState.SideToMove.Other())) |
| | 79 | | { |
| 0 | 80 | | boardState.UnmakeMove(entry.BestMove); |
| 0 | 81 | | break; |
| | 82 | | } |
| 104 | 83 | | pv.Add(entry.BestMove); |
| | 84 | | } |
| | 85 | |
|
| 292 | 86 | | for (int i = pv.Count - 1; i >= 0; i--) |
| | 87 | | { |
| 104 | 88 | | boardState.UnmakeMove(pv[i]); |
| | 89 | | } |
| 42 | 90 | | return pv; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | public static int AdjustScore(int score, int ply) |
| | 94 | | { |
| 437099 | 95 | | if (!IsCloseToCheckmate(score)) |
| | 96 | | { |
| 429988 | 97 | | return score; |
| | 98 | | } |
| | 99 | |
|
| 7111 | 100 | | return score + (score > 0 ? +ply : -ply); |
| | 101 | | } |
| | 102 | |
|
| | 103 | | public static int RetrieveScore(int score, int ply) |
| | 104 | | { |
| 77597 | 105 | | if (!IsCloseToCheckmate(score)) |
| | 106 | | { |
| 75555 | 107 | | return score; |
| | 108 | | } |
| | 109 | |
|
| 2042 | 110 | | return score + (score > 0 ? -ply : +ply); |
| | 111 | | } |
| | 112 | |
|
| | 113 | | private static bool IsCloseToCheckmate(int score) |
| | 114 | | { |
| 514696 | 115 | | return Constants.MaxCentipawnEval - Math.Abs(score) <= Constants.MaxPly; |
| | 116 | | } |
| | 117 | | } |
| | 118 | |
|
| | 119 | | public class TranspositionTableEntry |
| | 120 | | { |
| | 121 | | public int Score { get; init; } |
| | 122 | | public ulong Hash { get; init; } |
| | 123 | | public int Depth { get; init; } |
| | 124 | | public Move BestMove { get; init; } |
| | 125 | | public TranspositionEntryType Type { get; init; } |
| | 126 | | } |
| | 127 | |
|
| | 128 | | public enum TranspositionEntryType |
| | 129 | | { |
| | 130 | | Exact, |
| | 131 | | Alpha, |
| | 132 | | Beta |
| | 133 | | } |
| | 134 | | } |