-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtt.py
More file actions
80 lines (64 loc) · 2.22 KB
/
Copy pathtt.py
File metadata and controls
80 lines (64 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Credits:
# Disservin: https://github.com/Disservin/python-chess-engine/tree/master
from helpers import *
# External
import chess
from enum import Enum
Flag = Enum("Flag", ["NONEBOUND", "UPPERBOUND", "LOWERBOUND", "EXACTBOUND"])
"""
This is an entry in our TT, it saves
information about the score, flag and most
importantly the move.
"""
class TEntry:
def __init__(self) -> None:
self.key = 0
self.depth = 0
self.flag = Flag.NONEBOUND
self.score = VALUE_NONE
self.move = chess.Move.null()
class TranspositionTable:
def __init__(self) -> None:
# Higher values take rather long to initialize
self.tt_size = 2**19 - 1
self.transposition_table = [TEntry() for _ in range(self.tt_size)]
# Calculate "array" index
def ttIndex(self, key: int) -> int:
return key % self.tt_size
# store an entry in the TT
def storeEntry(
self, key: int, depth: int, flag: Flag, score: int, move: chess.Move, ply: int
) -> None:
index = self.ttIndex(key)
entry = self.transposition_table[index]
# Replacement schema
if entry.key != key or entry.move != move:
entry.move = move
if entry.key != key or flag == Flag.EXACTBOUND or depth + 4 > entry.depth:
entry.depth = depth
entry.score = self.scoreToTT(score, ply)
entry.key = key
entry.flag = flag
# self.transposition_table[index] = entry
def probeEntry(self, key: int) -> TEntry:
index = self.ttIndex(key)
entry = self.transposition_table[index]
return entry
# if we want to save correct mate scores we have to adjust the distance
def scoreToTT(self, s: int, plies: int) -> int:
if s >= VALUE_TB_WIN_IN_MAX_PLY:
return s + plies
else:
if s <= VALUE_TB_LOSS_IN_MAX_PLY:
return s - plies
else:
return s
# undo the previous adjustment
def scoreFromTT(self, s: int, plies: int) -> int:
if s >= VALUE_TB_WIN_IN_MAX_PLY:
return s - plies
else:
if s <= VALUE_TB_LOSS_IN_MAX_PLY:
return s + plies
else:
return s