refactor: TranspositionTable 简写为 TT
This commit is contained in:
parent
40971cb3fa
commit
d4407e80ea
|
@ -289,7 +289,7 @@ void AIAlgorithm::setGame(const Game &g)
|
||||||
// 如果规则改变,重建hashmap
|
// 如果规则改变,重建hashmap
|
||||||
if (strcmp(rule.name, rule.name) != 0) {
|
if (strcmp(rule.name, rule.name) != 0) {
|
||||||
#ifdef TRANSPOSITION_TABLE_ENABLE
|
#ifdef TRANSPOSITION_TABLE_ENABLE
|
||||||
TranspositionTable::clear();
|
TT::clear();
|
||||||
#endif // TRANSPOSITION_TABLE_ENABLE
|
#endif // TRANSPOSITION_TABLE_ENABLE
|
||||||
|
|
||||||
#ifdef ENDGAME_LEARNING
|
#ifdef ENDGAME_LEARNING
|
||||||
|
@ -371,7 +371,7 @@ int AIAlgorithm::search(depth_t depth)
|
||||||
for (depth_t i = depthBegin; i < d; i += 1) {
|
for (depth_t i = depthBegin; i < d; i += 1) {
|
||||||
#ifdef TRANSPOSITION_TABLE_ENABLE
|
#ifdef TRANSPOSITION_TABLE_ENABLE
|
||||||
#ifdef CLEAR_TRANSPOSITION_TABLE
|
#ifdef CLEAR_TRANSPOSITION_TABLE
|
||||||
TranspositionTable::clear(); // 每次走子前清空哈希表
|
TT::clear(); // 每次走子前清空哈希表
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
value = search(i, alpha, beta, root);
|
value = search(i, alpha, beta, root);
|
||||||
|
@ -401,7 +401,7 @@ int AIAlgorithm::search(depth_t depth)
|
||||||
|
|
||||||
#ifdef TRANSPOSITION_TABLE_ENABLE
|
#ifdef TRANSPOSITION_TABLE_ENABLE
|
||||||
#ifdef CLEAR_TRANSPOSITION_TABLE
|
#ifdef CLEAR_TRANSPOSITION_TABLE
|
||||||
TranspositionTable::clear(); // 每次走子前清空哈希表
|
TT::clear(); // 每次走子前清空哈希表
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -463,15 +463,15 @@ value_t AIAlgorithm::search(depth_t depth, value_t alpha, value_t beta, Node *no
|
||||||
|
|
||||||
#ifdef TRANSPOSITION_TABLE_ENABLE
|
#ifdef TRANSPOSITION_TABLE_ENABLE
|
||||||
// 哈希类型
|
// 哈希类型
|
||||||
enum TranspositionTable::HashType hashf = TranspositionTable::hashfALPHA;
|
enum TT::HashType hashf = TT::hashfALPHA;
|
||||||
|
|
||||||
#ifdef DEBUG_AB_TREE
|
#ifdef DEBUG_AB_TREE
|
||||||
node->hash = hash;
|
node->hash = hash;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
TranspositionTable::HashType type = TranspositionTable::hashfEMPTY;
|
TT::HashType type = TT::hashfEMPTY;
|
||||||
|
|
||||||
value_t probeVal = TranspositionTable::probeHash(hash, depth, alpha, beta, bestMove, type);
|
value_t probeVal = TT::probeHash(hash, depth, alpha, beta, bestMove, type);
|
||||||
|
|
||||||
if (probeVal != INT16_MIN /* TODO: valUNKOWN */ && node != root) {
|
if (probeVal != INT16_MIN /* TODO: valUNKOWN */ && node != root) {
|
||||||
#ifdef TRANSPOSITION_TABLE_DEBUG
|
#ifdef TRANSPOSITION_TABLE_DEBUG
|
||||||
|
@ -483,7 +483,7 @@ value_t AIAlgorithm::search(depth_t depth, value_t alpha, value_t beta, Node *no
|
||||||
node->value = probeVal;
|
node->value = probeVal;
|
||||||
|
|
||||||
#ifdef SORT_CONSIDER_PRUNED
|
#ifdef SORT_CONSIDER_PRUNED
|
||||||
if (type != TranspositionTable::hashfEXACT && type != TranspositionTable::hashfEMPTY) {
|
if (type != TT::hashfEXACT && type != TT::hashfEMPTY) {
|
||||||
node->pruned = true; // TODO: 是否有用?
|
node->pruned = true; // TODO: 是否有用?
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -535,7 +535,7 @@ value_t AIAlgorithm::search(depth_t depth, value_t alpha, value_t beta, Node *no
|
||||||
|
|
||||||
#ifdef TRANSPOSITION_TABLE_ENABLE
|
#ifdef TRANSPOSITION_TABLE_ENABLE
|
||||||
// 记录确切的哈希值
|
// 记录确切的哈希值
|
||||||
TranspositionTable::recordHash(node->value, depth, TranspositionTable::hashfEXACT, hash, MOVE_NONE);
|
TT::recordHash(node->value, depth, TT::hashfEXACT, hash, MOVE_NONE);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return node->value;
|
return node->value;
|
||||||
|
@ -559,7 +559,7 @@ value_t AIAlgorithm::search(depth_t depth, value_t alpha, value_t beta, Node *no
|
||||||
|
|
||||||
#ifdef TRANSPOSITION_TABLE_ENABLE
|
#ifdef TRANSPOSITION_TABLE_ENABLE
|
||||||
// 记录确切的哈希值
|
// 记录确切的哈希值
|
||||||
TranspositionTable::recordHash(node->value, depth, TranspositionTable::hashfEXACT, hash, MOVE_NONE);
|
TT::recordHash(node->value, depth, TT::hashfEXACT, hash, MOVE_NONE);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return node->value;
|
return node->value;
|
||||||
|
@ -612,7 +612,7 @@ value_t AIAlgorithm::search(depth_t depth, value_t alpha, value_t beta, Node *no
|
||||||
|
|
||||||
if (value > alpha) {
|
if (value > alpha) {
|
||||||
#ifdef TRANSPOSITION_TABLE_ENABLE
|
#ifdef TRANSPOSITION_TABLE_ENABLE
|
||||||
hashf = TranspositionTable::hashfEXACT;
|
hashf = TT::hashfEXACT;
|
||||||
#endif
|
#endif
|
||||||
alpha = value;
|
alpha = value;
|
||||||
}
|
}
|
||||||
|
@ -682,7 +682,7 @@ value_t AIAlgorithm::search(depth_t depth, value_t alpha, value_t beta, Node *no
|
||||||
|
|
||||||
#ifdef TRANSPOSITION_TABLE_ENABLE
|
#ifdef TRANSPOSITION_TABLE_ENABLE
|
||||||
// 记录不一定确切的哈希值
|
// 记录不一定确切的哈希值
|
||||||
TranspositionTable::recordHash(node->value, depth, hashf, hash, node->children[0]->move);
|
TT::recordHash(node->value, depth, hashf, hash, node->children[0]->move);
|
||||||
#endif /* TRANSPOSITION_TABLE_ENABLE */
|
#endif /* TRANSPOSITION_TABLE_ENABLE */
|
||||||
|
|
||||||
// 返回
|
// 返回
|
||||||
|
|
|
@ -116,7 +116,7 @@ public:
|
||||||
|
|
||||||
#ifdef TRANSPOSITION_TABLE_ENABLE
|
#ifdef TRANSPOSITION_TABLE_ENABLE
|
||||||
// 清空哈希表
|
// 清空哈希表
|
||||||
void clearTranspositionTable();
|
void clearTT();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// 比较函数
|
// 比较函数
|
||||||
|
|
|
@ -2,9 +2,9 @@
|
||||||
|
|
||||||
#ifdef TRANSPOSITION_TABLE_ENABLE
|
#ifdef TRANSPOSITION_TABLE_ENABLE
|
||||||
static constexpr int TRANSPOSITION_TABLE_SIZE = 0x2000000; // 8-128M:102s, 4-64M:93s 2-32M:91s 1-16M: 冲突
|
static constexpr int TRANSPOSITION_TABLE_SIZE = 0x2000000; // 8-128M:102s, 4-64M:93s 2-32M:91s 1-16M: 冲突
|
||||||
HashMap<hash_t, TranspositionTable::HashValue> transpositionTable(TRANSPOSITION_TABLE_SIZE);
|
HashMap<hash_t, TT::HashValue> transpositionTable(TRANSPOSITION_TABLE_SIZE);
|
||||||
|
|
||||||
value_t TranspositionTable::probeHash(hash_t hash,
|
value_t TT::probeHash(hash_t hash,
|
||||||
depth_t depth, value_t alpha, value_t beta,
|
depth_t depth, value_t alpha, value_t beta,
|
||||||
move_t &bestMove, HashType &type)
|
move_t &bestMove, HashType &type)
|
||||||
{
|
{
|
||||||
|
@ -39,7 +39,7 @@ out:
|
||||||
return VALUE_UNKNOWN;
|
return VALUE_UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TranspositionTable::findHash(hash_t hash, TranspositionTable::HashValue &hashValue)
|
bool TT::findHash(hash_t hash, TT::HashValue &hashValue)
|
||||||
{
|
{
|
||||||
return transpositionTable.find(hash, hashValue);
|
return transpositionTable.find(hash, hashValue);
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ bool TranspositionTable::findHash(hash_t hash, TranspositionTable::HashValue &ha
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int TranspositionTable::recordHash(value_t value, depth_t depth, TranspositionTable::HashType type, hash_t hash, move_t bestMove)
|
int TT::recordHash(value_t value, depth_t depth, TT::HashType type, hash_t hash, move_t bestMove)
|
||||||
{
|
{
|
||||||
// 同样深度或更深时替换
|
// 同样深度或更深时替换
|
||||||
// 注意: 每走一步以前都必须把散列表中所有的标志项置为 hashfEMPTY
|
// 注意: 每走一步以前都必须把散列表中所有的标志项置为 hashfEMPTY
|
||||||
|
@ -94,7 +94,7 @@ int TranspositionTable::recordHash(value_t value, depth_t depth, TranspositionTa
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TranspositionTable::clear()
|
void TT::clear()
|
||||||
{
|
{
|
||||||
transpositionTable.clear();
|
transpositionTable.clear();
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,7 +45,9 @@ public:
|
||||||
static void clear();
|
static void clear();
|
||||||
};
|
};
|
||||||
|
|
||||||
extern HashMap<hash_t, TranspositionTable::HashValue> transpositionTable;
|
using TT = TranspositionTable;
|
||||||
|
|
||||||
|
extern HashMap<hash_t, TT::HashValue> transpositionTable;
|
||||||
|
|
||||||
#endif // TRANSPOSITION_TABLE_ENABLE
|
#endif // TRANSPOSITION_TABLE_ENABLE
|
||||||
|
|
||||||
|
|
|
@ -71,7 +71,7 @@ void AiThread::setAi(const Game &g)
|
||||||
#ifdef TRANSPOSITION_TABLE_ENABLE
|
#ifdef TRANSPOSITION_TABLE_ENABLE
|
||||||
// 新下一盘前清除哈希表 (注意可能同时存在每步之前清除)
|
// 新下一盘前清除哈希表 (注意可能同时存在每步之前清除)
|
||||||
#ifdef CLEAR_TRANSPOSITION_TABLE
|
#ifdef CLEAR_TRANSPOSITION_TABLE
|
||||||
TranspositionTable::clear();
|
TT::clear();
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue