每下一着之前才重新随机生成着法候选列表以提升效率

This commit is contained in:
CalciteM 2019-07-10 02:05:33 +08:00
parent 556bb7b94f
commit eb2121046d
2 changed files with 34 additions and 9 deletions

View File

@ -102,13 +102,10 @@ struct NineChessAi_ab::Node *NineChessAi_ab::addNode(Node *parent, int value, in
mutex NineChessAi_ab::hashMapMutex; mutex NineChessAi_ab::hashMapMutex;
unordered_map<uint64_t, NineChessAi_ab::HashValue> NineChessAi_ab::hashmap; unordered_map<uint64_t, NineChessAi_ab::HashValue> NineChessAi_ab::hashmap;
void NineChessAi_ab::generateLegalMoves(Node *node)
{
const int MOVE_PRIORITY_TABLE_SIZE = NineChess::N_RINGS * NineChess::N_SEATS;
int pos = 0;
#ifdef MOVE_PRIORITY_TABLE_SUPPORT #ifdef MOVE_PRIORITY_TABLE_SUPPORT
#ifdef RANDOM_MOVE #ifdef RANDOM_MOVE
void NineChessAi_ab::shuffleMovePriorityTable()
{
array<int, 4> movePriorityTable0 = { 17, 19, 21, 23 }; // 星位 array<int, 4> movePriorityTable0 = { 17, 19, 21, 23 }; // 星位
array<int, 8> movePriorityTable1 = { 25, 27, 29, 31, 9, 11, 13, 15 }; // 外圈和内圈四个顶点 array<int, 8> movePriorityTable1 = { 25, 27, 29, 31, 9, 11, 13, 15 }; // 外圈和内圈四个顶点
array<int, 4> movePriorityTable2 = { 16, 18, 20, 22 }; // 中圈十字架 array<int, 4> movePriorityTable2 = { 16, 18, 20, 22 }; // 中圈十字架
@ -121,14 +118,12 @@ void NineChessAi_ab::generateLegalMoves(Node *node)
std::shuffle(movePriorityTable2.begin(), movePriorityTable2.end(), std::default_random_engine(seed)); std::shuffle(movePriorityTable2.begin(), movePriorityTable2.end(), std::default_random_engine(seed));
std::shuffle(movePriorityTable3.begin(), movePriorityTable3.end(), std::default_random_engine(seed)); std::shuffle(movePriorityTable3.begin(), movePriorityTable3.end(), std::default_random_engine(seed));
array<int, 24> movePriorityTable;
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
movePriorityTable[i + 0] = movePriorityTable0[i]; movePriorityTable[i + 0] = movePriorityTable0[i];
} }
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
movePriorityTable[i + 4] = movePriorityTable1[i]; movePriorityTable[i + 4] = movePriorityTable1[i];
} }
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
@ -138,6 +133,18 @@ void NineChessAi_ab::generateLegalMoves(Node *node)
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
movePriorityTable[i + 16] = movePriorityTable3[i]; movePriorityTable[i + 16] = movePriorityTable3[i];
} }
}
#endif // #ifdef RANDOM_MOVE
#endif // MOVE_PRIORITY_TABLE_SUPPORT
void NineChessAi_ab::generateLegalMoves(Node *node)
{
const int MOVE_PRIORITY_TABLE_SIZE = NineChess::N_RINGS * NineChess::N_SEATS;
int pos = 0;
#ifdef MOVE_PRIORITY_TABLE_SUPPORT
#ifdef RANDOM_MOVE
#else // RANDOM_MOVE #else // RANDOM_MOVE
int movePriorityTable[MOVE_PRIORITY_TABLE_SIZE] = { int movePriorityTable[MOVE_PRIORITY_TABLE_SIZE] = {
17, 19, 21, 23, // 星位 17, 19, 21, 23, // 星位
@ -579,6 +586,12 @@ int NineChessAi_ab::alphaBetaPruning(int depth)
srand(time0); srand(time0);
time1.start(); time1.start();
#ifdef MOVE_PRIORITY_TABLE_SUPPORT
#ifdef RANDOM_MOVE
shuffleMovePriorityTable();
#endif
#endif
#ifdef IDS_SUPPORT #ifdef IDS_SUPPORT
// 深化迭代 // 深化迭代

View File

@ -13,6 +13,7 @@
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <Qdebug> #include <Qdebug>
#include <array>
#include "ninechess.h" #include "ninechess.h"
@ -127,6 +128,13 @@ protected:
// 篡改深度 // 篡改深度
int changeDepth(int originalDepth); int changeDepth(int originalDepth);
// 随机打乱着法搜索顺序
#ifdef MOVE_PRIORITY_TABLE_SUPPORT
#ifdef RANDOM_MOVE
void shuffleMovePriorityTable();
#endif
#endif
// 判断是否在哈希表中 // 判断是否在哈希表中
unordered_map<uint64_t, NineChessAi_ab::HashValue>::iterator findHash(uint64_t hash); unordered_map<uint64_t, NineChessAi_ab::HashValue>::iterator findHash(uint64_t hash);
@ -166,6 +174,10 @@ private:
// 局面数据哈希表 // 局面数据哈希表
static unordered_map<uint64_t, HashValue> hashmap; static unordered_map<uint64_t, HashValue> hashmap;
#ifdef MOVE_PRIORITY_TABLE_SUPPORT
array<int, NineChess::N_RINGS *NineChess::N_SEATS> movePriorityTable;
#endif // MOVE_PRIORITY_TABLE_SUPPORT
// 哈希表的默认大小 // 哈希表的默认大小
static const size_t maxHashCount = 1024 * 1024; static const size_t maxHashCount = 1024 * 1024;