HASH: alphaBetaPruning 基本修改完成

This commit is contained in:
CalciteM Team 2019-07-14 16:06:34 +08:00
parent 6a7c8d1d59
commit acda6db11b
3 changed files with 44 additions and 27 deletions

View File

@ -2327,18 +2327,20 @@ void NineChess::constructHash()
//context.actionCaptureHash = rand64();
//context.player2sTurnHash = rand64();
uint64_t zobrist[N_POINTS][POINT_TYPE_COUNT];
//uint64_t zobrist[N_POINTS][POINT_TYPE_COUNT];
// 预留末8位后续填充局面特征标志
for (int p = 0; p < N_POINTS; p++) {
for (int t = NineChess::POINT_TYPE_EMPTY; t <= NineChess::POINT_TYPE_FORBIDDEN; t++) {
zobrist[p][t] = rand56();
context.zobrist[p][t] = rand56();
}
}
}
uint64_t NineChess::getHash()
{
updateHashMisc(); // 放在此处合适?
return context.hash;
}
@ -2356,7 +2358,7 @@ uint64_t NineChess::updateHash(int pos)
// PieceType is board_[pos]
// 0b00表示空白0b01=1 表示先手棋子0b10=2 表示后手棋子0b11=3 表示禁点
int pointType = board_[pos] & 0x30 >> 4;
int pointType = (board_[pos] & 0x30) >> 4;
//context.hashCheckCode |= (temp) << ((pos - 8) * 2 + 6);
// TODO: context.hash =
@ -2388,8 +2390,8 @@ uint64_t NineChess::updateHashMisc()
}
// TODO: 是否真的需要这几位?
context.hash |= (uint64_t)context.nPiecesNeedRemove << 4;
context.hash |= (uint64_t)context.nPiecesInHand_1;
context.hash |= (uint64_t)context.nPiecesNeedRemove << 2;
context.hash |= (uint64_t)context.nPiecesInHand_1 << 4;
return context.hash;
}

View File

@ -615,6 +615,10 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node)
int epsilon = 0;
#ifdef HASH_MAP_ENABLE
// 哈希值
HashValue hashValue;
memset(&hashValue, 0, sizeof(hashValue));
// 哈希类型
enum HashType hashf = hashfALPHA;
#endif
@ -640,11 +644,12 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node)
//hashMapMutex.lock();
HashValue hashValue;// = NineChessAi_ab::findHash(hash_);
// 从地址里一定可以读取出东西found 恒定为 true?
bool found = findHash(hash, hashValue);
if (node != rootNode &&
hashValue.hash == hash &&
hashValue.depth >= depth) {
hashValue.hash == hash && // 校验放在这里?
hashValue.depth >= depth) { // 大于还是大于或等于?
#ifdef DEBUG_AB_TREE
node->isHash = true;
#endif
@ -652,6 +657,7 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node)
// TODO: 处理 Alpha/Beta 确切值
node->value = hashValue.value;
// Why? 对 depth 的调整放在这里合适?
if (chessContext->turn == NineChess::PLAYER1)
node->value += hashValue.depth - depth;
else
@ -683,14 +689,7 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node)
#ifdef HASH_MAP_ENABLE
// 记录确切的哈希值
HashValue newHashValue;
newHashValue.alpha = alpha;
newHashValue.beta = beta;
newHashValue.depth = depth;
newHashValue.type = hashfEXACT;
newHashValue.hash = hash;
newHashValue.value = node->value;
recordHash(newHashValue);
recordHash(node->value, alpha, beta, depth, hashfEXACT, hash);
#endif
return node->value;
@ -701,7 +700,7 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node)
// 局面评估
node->value = evaluate(node);
// 为争取速胜value 值 +- 深度
// 为争取速胜value 值 +- 深度 (有必要?)
if (chessContext->turn == NineChess::PLAYER1)
node->value += depth;
else
@ -715,14 +714,7 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node)
#ifdef HASH_MAP_ENABLE
// 记录确切的哈希值
HashValue newHashValue;
newHashValue.alpha = alpha;
newHashValue.beta = beta;
newHashValue.depth = depth;
newHashValue.type = hashfEXACT;
newHashValue.hash = hash;
newHashValue.value = node->value;
recordHash(newHashValue);
recordHash(node->value, alpha, beta, depth, hashfEXACT, hash);
#endif
return node->value;
@ -820,6 +812,10 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node)
#endif // DONOT_DELETE_TREE
#ifdef HASH_MAP_ENABLE
// 记录不确切的哈希值
recordHash(node->value, alpha, beta, depth, hashf, hash);
#if 0
if (hashValue.hash != hash) {
// 添加到hashmap
HashValue newHashValue;
@ -841,6 +837,7 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node)
//hashMapMutex.unlock();
}
#endif
#endif /* HASH_MAP_ENABLE */
// 排序子节点树
sortLegalMoves(node); // (13%)
@ -978,12 +975,29 @@ bool NineChessAi_ab::findHash(uint64_t hash, HashValue &hashValue)
int NineChessAi_ab::recordHash(const HashValue &hashValue)
{
#ifdef HASH_MAP_ENABLE
//hashMapMutex.lock();
//HashMap<HashValue>::insert(hashValue.hash, hashValue);
hashmap.insert(hashValue.hash, hashValue);
//hashMapMutex.unlock();
#endif // HASH_MAP_ENABLE
return 0;
}
int NineChessAi_ab::recordHash(int value, int alpha, int beta, int depth, HashType type, uint64_t hash)
{
//hashMapMutex.lock();
HashValue hashValue;
hashValue.value = value;
hashValue.alpha = alpha;
hashValue.beta = beta;
hashValue.depth = depth;
hashValue.type = type;
hashValue.hash = hash;
hashmap.insert(hashValue.hash, hashValue);
//hashMapMutex.unlock();
return 0;
}

View File

@ -169,6 +169,7 @@ protected:
// 插入哈希表
int recordHash(const HashValue &hashValue);
int NineChessAi_ab::recordHash(int value, int alpha, int beta, int depth, HashType type, uint64_t hash);
#endif
private: