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 Feb 6, 2023
1 parent 375ee21 commit 438f255
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 45 deletions.
39 changes: 20 additions & 19 deletions ChineseChess.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 7.0.2, 2022-07-10T21:38:31. -->
<!-- Written by QtCreator 9.0.1, 2023-02-06T23:44:51. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down Expand Up @@ -60,6 +60,7 @@
<value type="QString" key="EditorConfiguration.ignoreFileTypes">*.md, *.MD, Makefile</value>
<value type="bool" key="EditorConfiguration.inEntireDocument">false</value>
<value type="bool" key="EditorConfiguration.skipTrailingWhitespace">true</value>
<value type="bool" key="EditorConfiguration.tintMarginArea">true</value>
</valuemap>
</data>
<data>
Expand Down Expand Up @@ -100,16 +101,16 @@
<variable>ProjectExplorer.Project.Target.0</variable>
<valuemap type="QVariantMap">
<value type="QString" key="DeviceType">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 6.3.1 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 6.3.1 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt6.631.win64_mingw_kit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 6.4.2 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 6.4.2 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt6.642.win64_mingw_kit</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
<value type="int" key="EnableQmlDebugging">0</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:\Qt\QtProjects\build-ChineseChess-Desktop_Qt_6_3_1_MinGW_64_bit-Debug</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory.shadowDir">D:/Qt/QtProjects/build-ChineseChess-Desktop_Qt_6_3_1_MinGW_64_bit-Debug</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:\Qt\QtProjects\ChineseChess\..\build-ChineseChess-Desktop_Qt_6_4_2_MinGW_64_bit-Debug</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory.shadowDir">D:/Qt/QtProjects/build-ChineseChess-Desktop_Qt_6_4_2_MinGW_64_bit-Debug</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
Expand All @@ -122,8 +123,8 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">构建</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">构建</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
Expand All @@ -147,8 +148,8 @@
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">2</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:\Qt\QtProjects\build-ChineseChess-Desktop_Qt_6_3_1_MinGW_64_bit-Release</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory.shadowDir">D:/Qt/QtProjects/build-ChineseChess-Desktop_Qt_6_3_1_MinGW_64_bit-Release</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:\Qt\QtProjects\ChineseChess\..\build-ChineseChess-Desktop_Qt_6_4_2_MinGW_64_bit-Release</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory.shadowDir">D:/Qt/QtProjects/build-ChineseChess-Desktop_Qt_6_4_2_MinGW_64_bit-Release</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
Expand All @@ -161,8 +162,8 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">构建</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">构建</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
Expand All @@ -188,8 +189,8 @@
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.2">
<value type="int" key="EnableQmlDebugging">0</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:\Qt\QtProjects\build-ChineseChess-Desktop_Qt_6_3_1_MinGW_64_bit-Profile</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory.shadowDir">D:/Qt/QtProjects/build-ChineseChess-Desktop_Qt_6_3_1_MinGW_64_bit-Profile</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:\Qt\QtProjects\ChineseChess\..\build-ChineseChess-Desktop_Qt_6_4_2_MinGW_64_bit-Profile</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory.shadowDir">D:/Qt/QtProjects/build-ChineseChess-Desktop_Qt_6_4_2_MinGW_64_bit-Profile</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
Expand All @@ -202,8 +203,8 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">构建</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">构建</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
Expand Down Expand Up @@ -232,8 +233,8 @@
<valuemap type="QVariantMap" key="ProjectExplorer.Target.DeployConfiguration.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">0</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Deploy</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Deploy</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">部署</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">部署</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Deploy</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">1</value>
Expand All @@ -256,7 +257,7 @@
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">D:/Qt/QtProjects/build-ChineseChess-Desktop_Qt_6_3_1_MinGW_64_bit-Release</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">D:/Qt/QtProjects/build-ChineseChess-Desktop_Qt_6_4_2_MinGW_64_bit-Release</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>
Expand Down
14 changes: 3 additions & 11 deletions src/GUI/dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,17 +439,8 @@ void Dialog::on_PlayerSide_currentIndexChanged(int index) {

// 电脑难度选择
void Dialog::on_ComputerHard_currentIndexChanged(int index) {
// 电脑每一步至少搜索多长时间(单位:毫秒)
switch (index) {
case 0:
this->m_chessEngine->setSearchTime(3000);
break;
case 1:
this->m_chessEngine->setSearchTime(6000);
break;
default:
this->m_chessEngine->setSearchTime(9000);
}
// 电脑每一步至少搜索多长时间(单位:毫秒)
this->m_chessEngine->setSearchTime(1000 * (index + 1));
}

// 翻转按钮点击槽函数
Expand Down Expand Up @@ -764,6 +755,7 @@ inline void Dialog::setMoving(const bool isMoving) {
// 查找云开局库
inline std::tuple<QString, Step> Dialog::searchBook() {
QNetworkAccessManager cloudBook;
cloudBook.setTransferTimeout(500);
// 搜索开局库
QNetworkReply *cloudReply = cloudBook.get(
QNetworkRequest{"http://www.chessdb.cn/chessdb.php?action=queryall&board=" +
Expand Down
33 changes: 33 additions & 0 deletions src/GUI/dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -995,16 +995,49 @@ color:transparent;</string>
<property name="styleSheet">
<string notr="true">font:40px</string>
</property>
<property name="currentIndex">
<number>2</number>
</property>
<item>
<property name="text">
<string> 1 秒</string>
</property>
</item>
<item>
<property name="text">
<string> 2 秒</string>
</property>
</item>
<item>
<property name="text">
<string> 3 秒</string>
</property>
</item>
<item>
<property name="text">
<string> 4 秒</string>
</property>
</item>
<item>
<property name="text">
<string> 5 秒</string>
</property>
</item>
<item>
<property name="text">
<string> 6 秒</string>
</property>
</item>
<item>
<property name="text">
<string> 7 秒</string>
</property>
</item>
<item>
<property name="text">
<string> 8 秒</string>
</property>
</item>
<item>
<property name="text">
<string> 9 秒</string>
Expand Down
36 changes: 27 additions & 9 deletions src/search/chessengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ void ChessEngine::search() {

// 时间控制
auto startTimeStamp = clock();
auto timeMan = QtConcurrent::run([&] {
while (not searchInstances.front().isStopped() and
clock() - startTimeStamp < this->m_searchTime) {
std::this_thread::yield();
}
for (auto &searchInstance : searchInstances) {
searchInstance.stopSearch();
}
});

forever {
// 多线程搜索
Expand All @@ -41,26 +50,35 @@ void ChessEngine::search() {
searchInstance.searchRoot(this->m_currentDepth);
}

// 随便取出一个最好的分数查看一下
this->m_bestScore = searchInstances.front().bestScore();
this->m_bestMove = searchInstances.front().bestMove();

// 如果只有一个合法走法或者赢了或者输了或者产生了长将局面或者超过时间就停止搜索就不用再往下搜索了
if (searchInstances.front().legalMove() == 1 or
this->m_bestScore < LOST_SCORE or this->m_bestScore > WIN_SCORE or
clock() - startTimeStamp > this->m_searchTime) {
// 从所有的线程中选出分数最高的那一个走法来走
// 记录所有的线程中最高分数与对应的走法
if (not searchInstances.front().isStopped()) {
this->m_bestScore = searchInstances.front().bestScore();
this->m_bestMove = searchInstances.front().bestMove();
for (const auto &searchInstance : searchInstances) {
qint16 threadScore { searchInstance.bestScore() };
if (this->m_bestScore < threadScore) {
this->m_bestScore = threadScore;
this->m_bestMove = searchInstance.bestMove();
}
}
} else {
// 如果被迫停止,那么这一层没有搜索完成
--this->m_currentDepth;
}

// 如果只有一个合法走法或者超过了最大层数或者超过时间就停止搜索就不用再往下搜索了
if (searchInstances.front().isStopped() or
searchInstances.front().legalMove() == 1 or
this->m_currentDepth >= 99 or
clock() - startTimeStamp > this->m_searchTime) {
// 停止时间管理
searchInstances.front().stopSearch();
timeMan.waitForFinished();
// 走棋
this->m_chessboard.makeMove(this->m_bestMove);
break;
}

// 增加层数
++this->m_currentDepth;
}
Expand Down
10 changes: 7 additions & 3 deletions src/search/searchinstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace PikaChess {
/** 搜索的衰减层数 [第几层][第几个走法] */
quint16 REDUCTIONS[64][128];
quint16 REDUCTIONS[100][128];

/** 延迟走法裁剪的裁剪层数 [第几层] */
quint16 LMP_MOVE_COUNT[64];
quint16 LMP_MOVE_COUNT[100];

SearchInstance::SearchInstance(const Chessboard &chessboard, HashTable &hashTable)
: m_chessboard { chessboard }, m_hashTable { hashTable } { }
Expand Down Expand Up @@ -74,7 +74,7 @@ qint16 SearchInstance::searchFull(qint16 alpha, const qint16 beta,
this->m_iidMove = INVALID_MOVE;

// 达到深度就返回静态评价,由于空着裁剪,深度可能小于-1
if (depth <= 0) return searchQuiescence(alpha, beta);
if (depth <= 0 or this->m_stop) return searchQuiescence(alpha, beta);

// 杀棋步数裁剪
qint16 tryScore = LOSS_SCORE + this->m_distance;
Expand Down Expand Up @@ -278,6 +278,10 @@ Move SearchInstance::bestMove() const { return this->m_bestMove; }

quint8 SearchInstance::legalMove() const { return this->m_legalMove; }

void SearchInstance::stopSearch() { this->m_stop = true; }

bool SearchInstance::isStopped() const { return this->m_stop; }

bool SearchInstance::makeMove(Move &move) {
bool result { this->m_chessboard.makeMove(move) };
if (result) ++this->m_distance;
Expand Down
9 changes: 9 additions & 0 deletions src/search/searchinstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class SearchInstance {
/** 当前局面有效的走法数 */
quint8 legalMove() const;

/** 让线程停止搜索 */
void stopSearch();

/** 查看当前线程是否已经停止搜索 */
bool isStopped() const;

protected:
/** 走一步 */
bool makeMove(Move &move);
Expand All @@ -51,6 +57,9 @@ class SearchInstance {
void setBestMove(const Move &move, qint8 depth);

private:
/** 停止标志 */
volatile bool m_stop { false };

/** 距离根节点的距离 */
quint8 m_distance { 0 };

Expand Down
6 changes: 3 additions & 3 deletions src/table/pregen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ extern __m128i BITBOARD_MASK[90];
extern __m128i BITBOARD_NOT_MASK[90];

/** 延迟走法衰减的衰减层数 */
extern quint16 REDUCTIONS[64][128];
extern quint16 REDUCTIONS[100][128];

/** 延迟走法裁剪的裁剪层数 [第几层] */
extern quint16 LMP_MOVE_COUNT[64];
extern quint16 LMP_MOVE_COUNT[100];

PreGen::PreGen() {
// 位棋盘掩码初始化
Expand Down Expand Up @@ -92,7 +92,7 @@ PreGen::PreGen() {
quint16 reduce[128];
for (quint8 i { 1 }; i < 128; ++i) reduce[i] = int(21.9 * std::log(i));

for (quint8 depth = 1; depth < 64; ++depth) {
for (quint8 depth = 1; depth < 100; ++depth) {
LMP_MOVE_COUNT[depth] = 3 + depth * depth;
for (quint8 moveCount = 1; moveCount < 128; ++moveCount) {
int r = reduce[depth] * reduce[moveCount];
Expand Down

0 comments on commit 438f255

Please sign in to comment.