Node子节点不用list改用vector使得提速20%
i7 CPU,自我对弈从 136s 缩短到 109s sortLegalMoves CPU性能占用率从13%下降到2.3%
This commit is contained in:
parent
332f3c181b
commit
bf26f291a1
|
@ -11,7 +11,8 @@
|
|||
#include <QTime>
|
||||
#include <array>
|
||||
#include <random>
|
||||
#include <chrono>
|
||||
#include <chrono>
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
NineChessAi_ab::NineChessAi_ab() :
|
||||
|
@ -267,6 +268,16 @@ void NineChessAi_ab::generateLegalMoves(Node *node)
|
|||
}
|
||||
}
|
||||
|
||||
bool NineChessAi_ab::nodeLess(const Node *first, const Node *second)
|
||||
{
|
||||
return first->value < second->value;
|
||||
}
|
||||
|
||||
bool NineChessAi_ab::nodeGreater(const Node *first, const Node *second)
|
||||
{
|
||||
return first->value > second->value;
|
||||
}
|
||||
|
||||
void NineChessAi_ab::sortLegalMoves(Node *node)
|
||||
{
|
||||
// 这个函数对效率的影响很大,排序好的话,剪枝较早,节省时间,但不能在此函数耗费太多时间
|
||||
|
@ -300,9 +311,11 @@ void NineChessAi_ab::sortLegalMoves(Node *node)
|
|||
#else
|
||||
|
||||
if (chessTemp.whosTurn() == NineChess::PLAYER1) {
|
||||
node->children.sort([](Node *n1, Node *n2) {return n1->value > n2->value; }); // (6%)
|
||||
//node->children.sort([](Node *n1, Node *n2) {return n1->value > n2->value; }); // (6%)
|
||||
std::stable_sort(node->children.begin(), node->children.end(), nodeGreater);
|
||||
} else {
|
||||
node->children.sort([](Node *n1, Node *n2) { return n1->value < n2->value; }); // (6%)
|
||||
//node->children.sort([](Node *n1, Node *n2) { return n1->value < n2->value; }); // (6%)
|
||||
std::stable_sort(node->children.begin(), node->children.end(), nodeLess);
|
||||
}
|
||||
|
||||
#if 0
|
||||
|
|
|
@ -47,9 +47,10 @@ public:
|
|||
// 定义一个节点结构体
|
||||
struct Node
|
||||
{
|
||||
public:
|
||||
int move; // 着法的命令行指令,图上标示为节点前的连线
|
||||
int value; // 节点的值
|
||||
list<struct Node*> children; // 子节点列表
|
||||
vector<struct Node*> children; // 子节点列表
|
||||
struct Node* parent; // 父节点
|
||||
size_t id; // 结点编号
|
||||
int rand; // 随机数,对于 value 一致的结点随机排序用
|
||||
|
@ -74,6 +75,23 @@ public:
|
|||
int result; // 终局结果,-1为负,0为未到终局,1为胜,走棋阶段被闷棋则为 -2/2,布局阶段闷棋为 -3
|
||||
struct Node* root; // 根节点
|
||||
#endif /* DEBUG_AB_TREE */
|
||||
|
||||
#if 0
|
||||
bool operator < (const Node &another) const
|
||||
{
|
||||
return this->value < another.value;
|
||||
}
|
||||
|
||||
bool operator > (const Node &another) const
|
||||
{
|
||||
return this->value > another.value;
|
||||
}
|
||||
|
||||
bool operator == (const Node &another) const
|
||||
{
|
||||
return this->value == another.value;
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
public:
|
||||
|
@ -97,6 +115,10 @@ public:
|
|||
// 清空哈希表
|
||||
void clearHashMap();
|
||||
|
||||
// 比较函数
|
||||
static bool nodeLess(const Node *first, const Node *second);
|
||||
static bool nodeGreater(const Node *first, const Node *second);
|
||||
|
||||
protected:
|
||||
// 生成所有合法的着法并建立子节点
|
||||
void generateLegalMoves(Node *node);
|
||||
|
|
Loading…
Reference in New Issue