Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
增加了多线程搜索的选举机制,选取分数最高的走法来走
Browse files Browse the repository at this point in the history
  • Loading branch information
PikaCat committed Sep 14, 2021
1 parent 541e280 commit 229dae4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
8 changes: 4 additions & 4 deletions dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,9 +643,9 @@ void Dialog::computerMove() {
ui->ComputerScore->setText(bookOK);
// 先尝试走一步
POSITION_INFO.makeMove(mapTo256(step), COMPUTER_SIDE);
// 如果走云端走法会产生重复局面,只有自己长将军时才不采纳云端走法
// 其他情况:对方长将军是有利于自己的,重复局面判和棋,以和为贵
if (POSITION_INFO.scoreRepeatFlag(POSITION_INFO.getRepeatFlag()) != BAN_SCORE_LOSS) {
// 如果走云端走法会产生重复局面,只有对方长将军才采纳云端走法
RepeatFlag repeatFlag { POSITION_INFO.getRepeatFlag() };
if (repeatFlag == 0 or POSITION_INFO.scoreRepeatFlag(repeatFlag) == BAN_SCORE_MATE) {
emit threadOK(step);
return;
}
Expand Down Expand Up @@ -693,7 +693,7 @@ void Dialog::computerMove() {
ui->ComputerScore->setText(QString::fromLocal8Bit("长将局面"));
} else {
ui->ComputerScore->setText(
QString::fromLocal8Bit("[") + QString::number(CURRENT_DEPTH - 1) +
QString::fromLocal8Bit("[") + QString::number(CURRENT_DEPTH) +
QString::fromLocal8Bit("层]") + QString::number(score));
}
emit threadOK(mapToStep(POSITION_INFO.mBestMove));
Expand Down
39 changes: 21 additions & 18 deletions pikachess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2182,8 +2182,7 @@ Score searchMain() {
CURRENT_DEPTH = 1;
Score bestScore{0};
// 多线程搜索,每个线程一个PositionInfo,只有置换表是共享的
// 为什么是MAX_CONCURRENT - 1?
// 因为本身开启其他线程去搜索的线程也要参与搜索,占用一个核心
// 为什么是MAX_CONCURRENT - 1? 因为本身开启其他线程去搜索的线程也要参与搜索,占用一个核心
QVector<PositionInfo> threadPositionInfos(MAX_CONCURRENT - 1);
// 初始化线程局面
for (auto &threadPositionInfo : threadPositionInfos) {
Expand All @@ -2193,31 +2192,35 @@ Score searchMain() {
auto startTimeStamp = clock();
forever {
// 多线程搜索
QVector<QFuture<void>> tasks;
QVector<QFuture<Score>> tasks;
tasks.reserve(MAX_CONCURRENT - 1);
for (auto &threadPositionInfo : threadPositionInfos) {
tasks.emplaceBack(QtConcurrent::run(
[&] { threadPositionInfo.searchRoot(CURRENT_DEPTH); }));
tasks.emplaceBack(QtConcurrent::run([&] {
return threadPositionInfo.searchRoot(CURRENT_DEPTH);
}));
}
// 自己也搜索
bestScore = POSITION_INFO.searchRoot(CURRENT_DEPTH);
// 等待线程结束
for (auto &task : tasks) {
task.waitForFinished();
for (auto &task : tasks) task.waitForFinished();
// 如果赢了或者输了或者产生了长将局面或者超过时间就停止搜索就不用再往下搜索了
if (bestScore < LOST_SCORE or bestScore > WIN_SCORE or
clock() - startTimeStamp > SEARCH_TIME) {
// 从所有的线程中选出分数最高的那一个走法来走
for (Count index { 0 }; index < size(tasks); ++index) {
Score threadScore { tasks[index].result() };
if (bestScore < threadScore) {
bestScore = threadScore;
POSITION_INFO.mBestMove = threadPositionInfos[index].mBestMove;
}
}
// 走棋
POSITION_INFO.makeMove(POSITION_INFO.mBestMove, COMPUTER_SIDE);
return bestScore;
}
// 增加层数
++CURRENT_DEPTH;
// 如果赢了或者输了或者产生了长将局面就不用再往下搜索了
if (bestScore < LOST_SCORE or bestScore > WIN_SCORE) {
break;
}
// 超过时间就停止搜索
if (clock() - startTimeStamp > SEARCH_TIME) {
break;
}
}
// 走棋
POSITION_INFO.makeMove(POSITION_INFO.mBestMove, COMPUTER_SIDE);
return bestScore;
}

// 用于初始化工作
Expand Down

0 comments on commit 229dae4

Please sign in to comment.