-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransposition-table.js
160 lines (144 loc) · 3.86 KB
/
transposition-table.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const TranspositionFlag = {
EXACT: "EXACT",
LOWERBOUND: "LOWERBOUND",
UPPERBOUND: "UPPERBOUND",
};
class TranspositionTable {
constructor(size = 1 << 20) {
console.log("new TranspositionTable()");
// Default size is 2^20 entries
this.table = new Array(size);
this.size = size;
this.hits = 0;
this.misses = 0;
this.filled = 0;
this.filledExact = 0;
this.overwritten = 0;
}
printStats() {
const totalCalls = this.hits + this.misses;
const hitRatioPercent =
totalCalls === 0 ? 100 : Math.floor((this.hits * 100) / totalCalls);
const missesRatioPercent = 100 - hitRatioPercent;
console.log(
"Transposition stats: " +
this.filled +
" filled, exact=" +
this.filledExact +
", overwritten=" +
this.overwritten +
", " +
hitRatioPercent +
"% hit (" +
this.hits +
"), " +
missesRatioPercent +
"% missed (" +
this.misses +
"), total calls=" +
totalCalls +
", "
);
}
// Compute an index in the table using the Zobrist hash
getIndex(hash) {
return Number(hash % BigInt(this.size));
}
// Store an entry in the table
store(hash, depth, evaluation, flag, bestMove = undefined) {
const index = this.getIndex(hash);
// Replace the entry if it's empty or if the new depth is greater
const entry = this.table[index];
if (!entry || entry.depth <= depth) {
if (!entry) {
this.filled++;
if (flag === TranspositionFlag.EXACT) {
this.filledExact++;
}
} else {
this.overwritten++;
}
this.table[index] = {
hash,
depth,
evaluation,
flag,
bestMove,
};
}
}
retrieve(hash, requiredDepth) {
const index = this.getIndex(hash);
const entry = this.table[index];
// Verify that the entry corresponds to the current hash and depth is sufficient
if (entry && entry.hash === hash && entry.depth >= requiredDepth) {
this.hits++;
return entry;
}
this.misses++;
return undefined; // No valid entry found or insufficient depth
}
log(color, entry) {
if (verbose > 0 && entry.bestMove) {
console.log(
"Transposition table " +
PieceNames[color] +
": Found hash=" +
entry.hash +
", depth=" +
entry.depth +
", " +
entry.flag +
" evaluation=" +
entry.evaluation +
", bestMove=" +
(entry.bestMove
? entry.bestMove.toCoordinateNotation()
: "-")
);
}
}
use(color, hash, alpha, beta, depth) {
const entry = this.retrieve(hash, depth);
if (entry) {
switch (entry.flag) {
case "EXACT":
// Exact evaluation: return the stored value
this.log(color, entry)
return {
evaluation: entry.evaluation,
bestMove: entry.bestMove,
};
case "LOWERBOUND":
// Lower bound: update alpha
alpha = Math.max(alpha, entry.evaluation);
break;
case "UPPERBOUND":
// Upper bound: update beta
beta = Math.min(beta, entry.evaluation);
break;
}
// Prune the search if bounds overlap
if (alpha >= beta) {
this.log(color, entry);
return {
evaluation: entry.evaluation,
bestMove: entry.bestMove,
};
}
}
// No usable entry or search must continue
return undefined;
}
}
let TranspositionTableSingleton = undefined;
function TranspositionTableInstance() {
if (!TranspositionTableSingleton) {
TranspositionTableSingleton = new TranspositionTable();
}
return TranspositionTableSingleton;
}
function TranspositionTableReset() {
TranspositionTableSingleton = undefined;
return TranspositionTableInstance();
}