sort: compare: 优化 compare 条件判断顺序并精简代码

目前自对弈中, 两个数左大于右或右大于左的概率之和小于两者相等, 3:4
的关系, 并且两者子结点是否剪枝, 不一致的情况明显小于一致的情况, 约
1:10的关系, 如果随机走子概率有所变化但总体结论不变. 故通过根据比例
计算总的判断次数, 进行优化.

自对弈加2层搜索实验, 搜索耗时由 102.511s 缩减到 91.947s, 提速 11.5%.
This commit is contained in:
Calcitem 2019-10-07 01:38:03 +08:00
parent a526dbff2c
commit 0b6e98b15d
1 changed files with 8 additions and 45 deletions

View File

@ -36,7 +36,7 @@
#define SORT_NAME nodep
#define SORT_TYPE AIAlgorithm::Node*
#define SORT_CMP(x, y) (-AIAlgorithm::nodeCompare((x), (y)))
#define SORT_CMP(x, y) (AIAlgorithm::nodeCompare((x), (y)))
player_t gSideToMove;
@ -245,56 +245,19 @@ struct AIAlgorithm::Node *AIAlgorithm::addNode(
return newNode;
}
int AIAlgorithm::nodeCompare(const Node * first, const Node * second)
int AIAlgorithm::nodeCompare(const Node *first, const Node *second)
{
int ret = 0;
if (gSideToMove == PLAYER_BLACK) {
if (first->value > second->value) {
ret = 1;
goto out;
if (first->value == second->value) {
if (first->pruned == second->pruned) {
return 0;
}
if (first->value < second->value) {
ret = -1;
goto out;
}
if (first->value == second->value) {
if (!first->pruned && second->pruned) {
ret = 1;
goto out;
} else if (first->pruned && !second->pruned) {
ret = -1;
goto out;
}
}
return (first->pruned ? 1 : -1);
}
if (gSideToMove == PLAYER_WHITE) {
if (first->value < second->value) {
ret = 1;
goto out;
}
int ret = (gSideToMove == PLAYER_BLACK ? 1 : -1);
if (first->value > second->value) {
ret = -1;
goto out;
}
if (first->value == second->value) {
if (!first->pruned && second->pruned) {
ret = 1;
goto out;
} else if (first->pruned && !second->pruned) {
ret = -1;
goto out;
}
}
}
out:
return ret;
return (first->value < second->value ? ret : -ret);
}
void AIAlgorithm::sortMoves(Node *node)