-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranspositionTable.cpp
More file actions
165 lines (133 loc) · 4.44 KB
/
TranspositionTable.cpp
File metadata and controls
165 lines (133 loc) · 4.44 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
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
161
162
163
164
165
#pragma once
#include "TranspositionTable.h"
#include <iostream>
#include <string>
using namespace std;
RandomGenerator::RandomGenerator() : gen(rd()), dist(0, numeric_limits<uint64_t>::max()) {}
uint64_t RandomGenerator::generate64Bits() {
return dist(gen);
}
bool Transposition::IsQuiscence() {
return flag > 2;
}
// Initializes the transposition table with the specified size.
TranspositionTable::TranspositionTable(int sizeMB) {
initializePieceKeys();
tableSize = (sizeMB * 1024 * 1024) / sizeof(Transposition);
table.resize(tableSize);
}
// Initialize the zobrist keys used in hashing the transpositions.
void TranspositionTable::initializePieceKeys() {
blackToMove = randomGenerator.generate64Bits();
for (int i = 0; i < 2; i++)
for (int j = 0; j < 7; j++)
for (int k = 0; k < 8; k++)
for (int l = 0;l < 8;l++)
pieceKeys[i][j][k][l] = randomGenerator.generate64Bits();
}
void TranspositionTable::storeTransposition(uint64_t key, uint8_t flag, uint8_t depth, int value, Move move) {
int hash = key % tableSize;
// Clear the table if it's full.
if (getFillPercentage() > 99)
clear();
// First time for this hash.
if (table[hash].key == 0) {
entriesCount++;
table[hash] = { key, flag, depth, move, value };
}
else { // The entry exists in the table.
int originalHash = hash;
// Search linearly for the key.
while (table[hash].key != 0 && table[hash].key != key) {
hash = (hash + 1) % tableSize;
// Table is full.
if (hash == originalHash) return;
}
if (hash != originalHash) collisions++;
if (table[hash].key == 0) {
entriesCount++;
table[hash] = { key, flag, depth, move, value };
}
else {
bool isQuiescence = flag > 2;
bool storedIsQuiescence = table[hash].flag > 2;
// overwrite if better depth and the search type is equal, meaning The stored value was stored during main search
// and the current search is also the main search and same for quiescence.
bool betterDepth = table[hash].depth < depth && (storedIsQuiescence == isQuiescence);
// replacing upper and lower bound evaluations with exact ones.
bool exactEvaluation = (depth >= table[hash].depth && flag == Transposition::Exact);
// replaces values stored during quiescence search with a value from the main search.
bool replaceQuiescence = storedIsQuiescence && !isQuiescence;
if (betterDepth || exactEvaluation || replaceQuiescence) {
overwrites++;
table[hash] = { key, flag, depth, move, value };
}
}
}
}
// Checks if the transposition exists in the table.
bool TranspositionTable::probeTransposition(uint64_t key, Transposition& trans) {
int hash = key % tableSize;
int originalHash = hash;
while (table[hash].key != 0) {
if (table[hash].key == key) {
trans = table[hash];
return true;
}
hash = (hash + 1) % tableSize;
if (hash == originalHash) return false;
}
return false;
}
int TranspositionTable::lookupEvaluation(uint64_t key, int depth, int alpha, int beta, bool& found, bool Quiescence) {
Transposition pos;
if (probeTransposition(key, pos)) {
if (pos.IsQuiscence() == Quiescence && pos.depth >= depth || (!pos.IsQuiscence() && Quiescence)) {
if (pos.flag == pos.Exact || pos.flag == pos.QExact) {
found = true;
return pos.value;
}
if ((pos.flag == pos.Alpha || pos.flag == pos.QAlpha) && pos.value <= alpha) {
found = true;
return pos.value;
}
if ((pos.flag == pos.Beta || pos.flag == pos.QBeta) && pos.value >= beta) {
found = true;
return pos.value;
}
}
}
found = false;
return 0;
}
uint64_t TranspositionTable::generateZobristKey(int board[8][8]) {
uint64_t key = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
int piece = board[i][j];
if (piece > 0) {
key ^= pieceKeys[0][piece][i][j];
}
else if (piece < 0) {
key ^= pieceKeys[1][abs(piece)][i][j];
}
}
}
return key;
}
string TranspositionTable::getFillData() {
string output = "";
output += "Table Occupancy: " + to_string(entriesCount) + " : " + to_string((double(entriesCount) / double(tableSize)) * 100) + " %" + '\n';
output += "Table Overwrites: " + to_string(overwrites) + '\n';
output += "Table Collisions: " + to_string(collisions) + '\n';
return output;
}
double TranspositionTable::getFillPercentage() {
return (double(entriesCount) / double(tableSize)) * 100;
}
void TranspositionTable::clear() {
for (int i = 0; i < table.size(); i++) {
table[i] = Transposition();
}
overwrites = 0, collisions = 0, entriesCount = 0;
}