去除部分语句的方式链接通过,暂时先这样处理

This commit is contained in:
CalciteM Team 2019-07-14 02:01:38 +08:00
parent 0421fa0494
commit 8faa9baa38
5 changed files with 112 additions and 81 deletions

View File

@ -1,15 +1,15 @@
#ifndef CONFIG_H
#define CONFIG_H
//#define DEBUG
#define DEBUG
#define RANDOM_MOVE
//#define RANDOM_MOVE
#define DEAL_WITH_HORIZON_EFFECT
//#define DEAL_WITH_HORIZON_EFFECT
//#define RANDOM_BEST_MOVE
//#define HASH_MAP_ENABLE
#define HASH_MAP_ENABLE
//#define DONOT_DELETE_TREE
@ -48,7 +48,7 @@
#define DRAW_SEAT_NUMBER
#endif
#define IDS_SUPPORT
//#define IDS_SUPPORT
#define SAVE_CHESSBOOK_WHEN_ACTION_NEW_TRIGGERED

View File

@ -1,48 +1,37 @@
#include "hashmap.h"
template <typename T>
HashMap<T>::HashMap():
capacity(0),
size(0),
pool(nullptr)
{
this->capacity = 0x20000000; // TODO
}
template <typename T>
HashMap<T>::HashMap(size_t capacity)
{
this->capacity = capacity;
this->size = 0;
construct();
}
// template <typename T>
// HashMap<T>(uint64_t capacity, uint64_t size, T *pool)
// {
// this->capacity = capacity;
// size = size;
// //HashMap<T>::construct();
// }
template <typename T>
HashMap<T>::~HashMap()
{
clear();
}
template <typename T>
bool HashMap<T>::construct()
{
pool = new T[capacity];
HashMap<T>::pool = new T[capacity];
if (pool == nullptr) {
if (HashMap<T>::pool == nullptr) {
return false;
}
return true;
}
template <typename T>
T& HashMap<T>::at(uint64_t i)
{
if (i >= capacity) {
qDebug() << "Error";
return pool[0];
return HashMap<T>::pool[0];
}
return pool[i];
return HashMap<T>::pool[i];
}
template <typename T>
@ -52,7 +41,7 @@ size_t HashMap<T>::getSize()
}
template <typename T>
size_t HashMap<T>::getCapacity()
uint64_t HashMap<T>::getCapacity()
{
return capacity;
}
@ -64,7 +53,7 @@ uint64_t HashMap<T>::hashToAddr(uint64_t hash)
}
template <typename T>
void HashMap<T>::insert(uint64_t hash, const T &hashValue)
void HashMap<T>::insert(uint64_t hash, T &hashValue)
{
uint64_t addr = hashToAddr(hash);
@ -78,3 +67,6 @@ void HashMap<T>::clear()
pool = nullptr;
}
template <typename T>
HashMap<T>* HashMap<T>::instance = new HashMap<T>(capacity = 1024, size = 0);

View File

@ -2,55 +2,87 @@
#define HASHMAP_H
#include <limits>
#include <memory>
#include <mutex>
#include <qDebug>
template <typename T>
class HashMap
{
public:
HashMap();
HashMap(size_t capacity);
~HashMap();
//HashMap(size_t capacity, size_t size, T* pool);
//~HashMap();
HashMap() = default;
enum FindResult
static HashMap *getInstance()
{
HASHMAP_NOTFOUND = INT32_MAX,
};
#if 0
static std::once_flag s_flag;
std::call_once(s_flag, [&]() {
instance.reset(new HashMap);
});
#endif
if (instance)
T& at(uint64_t i);
return *instance;
}
static void lock()
{
hashMapMutex.lock();
}
static void unlock()
{
hashMapMutex.unlock();
}
static T& at(uint64_t i);
#if 0
T& operator[](uint64_t hash)
{
uint64_t addr = hashToAddr(hash);
return pool[addr];
}
#endif
uint64_t hashToAddr(uint64_t hash);
static uint64_t hashToAddr(uint64_t hash);
T &find(uint64_t hash)
static char* find(uint64_t hash)
{
uint64_t addr = hashToAddr(hash);
// uint64_t addr = hashToAddr(hash);
return pool[addr];
return pool[hash <<32 >>32];
}
size_t getSize();
size_t getCapacity();
static size_t getSize();
static size_t getCapacity();
void clear();
static void clear();
void insert(uint64_t hash, const T &hashValue);
static void insert(uint64_t hash, T &hashValue);
bool construct();
static bool construct();
private:
size_t capacity;
size_t size;
public:
static const uint64_t capacity;
static uint64_t size;
T *pool;
static char *pool;
static std::mutex hashMapMutex;
//static std::auto_ptr<HashMap<T>> instance;
static HashMap<T>* instance;
public:
// 防止外部构造。
//HashMap() = default;
// 防止拷贝和赋值。
HashMap &operator=(const HashMap &) = delete; HashMap(const HashMap &another) = delete;
};
#endif // HASHMAP_H

View File

@ -13,18 +13,26 @@
#include <algorithm>
#include "ninechessai_ab.h"
#include "hashmap.h"
#ifdef HASH_MAP_ENABLE
static std::unique_ptr<HashMap<NineChessAi_ab::HashValue>> instance;
#endif
NineChessAi_ab::NineChessAi_ab() :
rootNode(nullptr),
requiredQuit(false),
nodeCount(0),
evaluatedNodeCount(0),
hashHitCount(0)
#ifdef HASH_MAP_ENABLE
hashHitCount(0),
//hashmap(HashMap<HashValue>::getInstance()),
#endif
evaluatedNodeCount(0)
{
buildRoot();
#ifdef HASH_MAP_ENABLE
hashMap.construct(); // TODO
//hashmap = HashMap<HashValue>::getInstance();
#endif
}
@ -99,7 +107,7 @@ struct NineChessAi_ab::Node *NineChessAi_ab::addNode(Node *parent, int value, in
// 静态hashmap初始化
//mutex NineChessAi_ab::hashMapMutex;
//HashMap<NineChessAi_ab::HashValue> NineChessAi_ab::hashmap;
//std::unique_ptr<HashMap<NineChessAi_ab::HashValue>> HashMap::instance;
#ifdef MOVE_PRIORITY_TABLE_SUPPORT
@ -636,9 +644,9 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node)
uint64_t hash = chessTemp.getHash();
node->hash = hash;
hashMapMutex.lock();
//hashMapMutex.lock();
HashValue hashValue = findHash(hash);
HashValue hashValue;// = NineChessAi_ab::findHash(hash_);
if (node != rootNode &&
hashValue.hash == hash &&
@ -655,13 +663,13 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node)
else
node->value -= hashValue.depth - depth;
hashMapMutex.unlock();
//hashMapMutex::unlock();
hashHitCount++;
return node->value;
}
hashMapMutex.unlock();
//hashMapMutex.unlock();
#endif /* HASH_MAP_ENABLE */
// 搜索到叶子节点(决胜局面)
@ -831,12 +839,12 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node)
}
// 更新更深层数据
else {
hashMapMutex.lock();
//hashMapMutex.lock();
if (hashValue.depth < depth) {
hashValue.value = node->value;
hashValue.depth = depth;
}
hashMapMutex.unlock();
//hashMapMutex.unlock();
}
#endif
@ -848,12 +856,13 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node)
}
#ifdef HASH_MAP_ENABLE
int NineChessAi_ab::recordHash(const HashValue &hashValue)
int NineChessAi_ab::recordHash(HashValue &hashValue)
{
#ifdef HASH_MAP_ENABLE
hashMapMutex.lock();
hashMap.insert(hashValue.hash, hashValue);
hashMapMutex.unlock();
//hashMapMutex.lock();
//HashMap<HashValue>::insert(hashValue.hash, hashValue);
//hashMapMutex.unlock();
#endif // HASH_MAP_ENABLE
return 0;
@ -956,9 +965,10 @@ const char *NineChessAi_ab::move2string(int move)
}
#ifdef HASH_MAP_ENABLE
#if 0
NineChessAi_ab::HashValue NineChessAi_ab::findHash(uint64_t hash)
{
NineChessAi_ab::HashValue hashValue = hashMap.find(hash);
// NineChessAi_ab::HashValue hashValue = hashmap.find(hash);
// TODO: 变换局面
#if 0
@ -984,13 +994,14 @@ NineChessAi_ab::HashValue NineChessAi_ab::findHash(uint64_t hash)
}
#endif
return hashValue;
return 0;
}
#endif
void NineChessAi_ab::clearHashMap()
{
hashMapMutex.lock();
hashMap.clear();
hashMapMutex.unlock();
//hashMapMutex.lock();
//hashMap.clear();
//hashMapMutex.unlock();
}
#endif /* HASH_MAP_ENABLE */
#endif /* HASH_MAP_ENABLE */

View File

@ -29,7 +29,7 @@ class NineChessAi_ab
public:
#ifdef HASH_MAP_ENABLE
// 定义哈希值的类型
enum HashType : int16_t
enum HashType
{
hashfEMPTY = 0,
hashfALPHA = 1,
@ -129,11 +129,6 @@ public:
static bool nodeLess(const Node *first, const Node *second);
static bool nodeGreater(const Node *first, const Node *second);
#ifdef HASH_MAP_ENABLE
static std::mutex hashMapMutex;
static HashMap<HashValue> hashMap;
#endif
protected:
// 生成所有合法的着法并建立子节点
void generateLegalMoves(Node *node);
@ -152,7 +147,7 @@ protected:
#ifdef HASH_MAP_ENABLE
// 插入哈希表
int recordHash(const HashValue &hashValue);
int recordHash(HashValue &hashValue);
#endif
// 评价函数
@ -213,9 +208,6 @@ private:
array<int, NineChess::N_RINGS *NineChess::N_SEATS> movePriorityTable;
#endif // MOVE_PRIORITY_TABLE_SUPPORT
// 哈希表的默认大小
static const size_t maxHashCount = 1024 * 1024;
// 定义极大值
static const int INF_VALUE = 0x1 << 30;
@ -225,6 +217,10 @@ private:
private:
// 命令行
char cmdline[32];
#ifdef HASH_MAP_ENABLE
HashMap<struct HashValue> hashmap;
#endif
};
#endif