forked from LeelaChessZero/lc0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.cc
379 lines (319 loc) · 13 KB
/
engine.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
This file is part of Leela Chess Zero.
Copyright (C) 2018-2019 The LCZero Authors
Leela Chess is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Leela Chess is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Leela Chess. If not, see <http://www.gnu.org/licenses/>.
Additional permission under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA
Toolkit and the NVIDIA CUDA Deep Neural Network library (or a
modified version of those libraries), containing parts covered by the
terms of the respective license agreement, the licensors of this
Program grant you additional permission to convey the resulting work.
*/
#include "engine.h"
#include <algorithm>
#include <cmath>
#include <functional>
#include "mcts/search.h"
#include "mcts/stoppers/factory.h"
#include "utils/configfile.h"
#include "utils/logging.h"
namespace lczero {
namespace {
const int kDefaultThreads = 2;
const OptionId kThreadsOptionId{"threads", "Threads",
"Number of (CPU) worker threads to use.", 't'};
const OptionId kLogFileId{"logfile", "LogFile",
"Write log to that file. Special value <stderr> to "
"output the log to the console.",
'l'};
const OptionId kSyzygyTablebaseId{
"syzygy-paths", "SyzygyPath",
"List of Syzygy tablebase directories, list entries separated by system "
"separator (\";\" for Windows, \":\" for Linux).",
's'};
const OptionId kPonderId{"ponder", "Ponder",
"This option is ignored. Here to please chess GUIs."};
const OptionId kUciChess960{
"chess960", "UCI_Chess960",
"Castling moves are encoded as \"king takes rook\"."};
const OptionId kShowWDL{"show-wdl", "UCI_ShowWDL",
"Show win, draw and lose probability."};
const OptionId kShowMovesleft{"show-movesleft", "UCI_ShowMovesLeft",
"Show estimated moves left."};
const OptionId kStrictUciTiming{"strict-uci-timing", "StrictTiming",
"The UCI host compensates for lag, waits for "
"the 'readyok' reply before sending 'go' and "
"only then starts timing."};
const OptionId kPreload{"preload", "",
"Initialize backend and load net on engine startup."};
MoveList StringsToMovelist(const std::vector<std::string>& moves,
const ChessBoard& board) {
MoveList result;
if (moves.size()) {
result.reserve(moves.size());
const auto legal_moves = board.GenerateLegalMoves();
const auto end = legal_moves.end();
for (const auto& move : moves) {
const auto m = board.GetModernMove({move, board.flipped()});
if (std::find(legal_moves.begin(), end, m) != end) result.emplace_back(m);
}
if (result.empty()) throw Exception("No legal searchmoves.");
}
return result;
}
} // namespace
EngineController::EngineController(std::unique_ptr<UciResponder> uci_responder,
const OptionsDict& options)
: options_(options),
uci_responder_(std::move(uci_responder)),
current_position_{ChessBoard::kStartposFen, {}} {}
void EngineController::PopulateOptions(OptionsParser* options) {
using namespace std::placeholders;
NetworkFactory::PopulateOptions(options);
options->Add<IntOption>(kThreadsOptionId, 1, 128) = kDefaultThreads;
options->Add<IntOption>(kNNCacheSizeId, 0, 999999999) = 2000000;
SearchParams::Populate(options);
options->Add<StringOption>(kSyzygyTablebaseId);
// Add "Ponder" option to signal to GUIs that we support pondering.
// This option is currently not used by lc0 in any way.
options->Add<BoolOption>(kPonderId) = true;
options->Add<BoolOption>(kUciChess960) = false;
options->Add<BoolOption>(kShowWDL) = false;
options->Add<BoolOption>(kShowMovesleft) = false;
ConfigFile::PopulateOptions(options);
PopulateTimeManagementOptions(RunType::kUci, options);
options->Add<BoolOption>(kStrictUciTiming) = false;
options->HideOption(kStrictUciTiming);
options->Add<BoolOption>(kPreload) = false;
}
void EngineController::ResetMoveTimer() {
move_start_time_ = std::chrono::steady_clock::now();
}
// Updates values from Uci options.
void EngineController::UpdateFromUciOptions() {
SharedLock lock(busy_mutex_);
// Syzygy tablebases.
std::string tb_paths = options_.Get<std::string>(kSyzygyTablebaseId);
if (!tb_paths.empty() && tb_paths != tb_paths_) {
syzygy_tb_ = std::make_unique<SyzygyTablebase>();
CERR << "Loading Syzygy tablebases from " << tb_paths;
if (!syzygy_tb_->init(tb_paths)) {
CERR << "Failed to load Syzygy tablebases!";
syzygy_tb_ = nullptr;
}
tb_paths_ = tb_paths;
} else if (tb_paths.empty()) {
syzygy_tb_ = nullptr;
tb_paths_.clear();
}
// Network.
const auto network_configuration =
NetworkFactory::BackendConfiguration(options_);
if (network_configuration_ != network_configuration) {
network_ = NetworkFactory::LoadNetwork(options_);
network_configuration_ = network_configuration;
}
// Cache size.
cache_.SetCapacity(options_.Get<int>(kNNCacheSizeId));
// Check whether we can update the move timer in "Go".
strict_uci_timing_ = options_.Get<bool>(kStrictUciTiming);
}
void EngineController::EnsureReady() {
std::unique_lock<RpSharedMutex> lock(busy_mutex_);
// If a UCI host is waiting for our ready response, we can consider the move
// not started until we're done ensuring ready.
ResetMoveTimer();
}
void EngineController::NewGame() {
// In case anything relies upon defaulting to default position and just calls
// newgame and goes straight into go.
ResetMoveTimer();
SharedLock lock(busy_mutex_);
cache_.Clear();
search_.reset();
tree_.reset();
CreateFreshTimeManager();
current_position_ = {ChessBoard::kStartposFen, {}};
UpdateFromUciOptions();
}
void EngineController::SetPosition(const std::string& fen,
const std::vector<std::string>& moves_str) {
// Some UCI hosts just call position then immediately call go, while starting
// the clock on calling 'position'.
ResetMoveTimer();
SharedLock lock(busy_mutex_);
current_position_ = CurrentPosition{fen, moves_str};
search_.reset();
}
Position EngineController::ApplyPositionMoves() {
ChessBoard board;
int no_capture_ply;
int game_move;
board.SetFromFen(current_position_.fen, &no_capture_ply, &game_move);
int game_ply = 2 * game_move - (board.flipped() ? 1 : 2);
Position pos(board, no_capture_ply, game_ply);
for (std::string move_str: current_position_.moves) {
Move move(move_str);
if (pos.IsBlackToMove()) move.Mirror();
pos = Position(pos, move);
}
return pos;
}
void EngineController::SetupPosition(
const std::string& fen, const std::vector<std::string>& moves_str) {
SharedLock lock(busy_mutex_);
search_.reset();
UpdateFromUciOptions();
if (!tree_) tree_ = std::make_unique<NodeTree>();
std::vector<Move> moves;
for (const auto& move : moves_str) moves.emplace_back(move);
const bool is_same_game = tree_->ResetToPosition(fen, moves);
if (!is_same_game) CreateFreshTimeManager();
}
void EngineController::CreateFreshTimeManager() {
time_manager_ = MakeTimeManager(options_);
}
namespace {
class PonderResponseTransformer : public TransformingUciResponder {
public:
PonderResponseTransformer(std::unique_ptr<UciResponder> parent,
std::string ponder_move)
: TransformingUciResponder(std::move(parent)),
ponder_move_(std::move(ponder_move)) {}
void TransformThinkingInfo(std::vector<ThinkingInfo>* infos) override {
// Output all stats from main variation (not necessary the ponder move)
// but PV only from ponder move.
ThinkingInfo ponder_info;
for (const auto& info : *infos) {
if (info.multipv <= 1) {
ponder_info = info;
if (ponder_info.mate) ponder_info.mate = -*ponder_info.mate;
if (ponder_info.score) ponder_info.score = -*ponder_info.score;
if (ponder_info.depth > 1) ponder_info.depth--;
if (ponder_info.seldepth > 1) ponder_info.seldepth--;
ponder_info.pv.clear();
}
if (!info.pv.empty() && info.pv[0].as_string() == ponder_move_) {
ponder_info.pv.assign(info.pv.begin() + 1, info.pv.end());
}
}
infos->clear();
infos->push_back(ponder_info);
}
private:
std::string ponder_move_;
};
} // namespace
void EngineController::Go(const GoParams& params) {
// TODO: should consecutive calls to go be considered to be a continuation and
// hence have the same start time like this behaves, or should we check start
// time hasn't changed since last call to go and capture the new start time
// now?
if (strict_uci_timing_ || !move_start_time_) ResetMoveTimer();
go_params_ = params;
std::unique_ptr<UciResponder> responder =
std::make_unique<NonOwningUciRespondForwarder>(uci_responder_.get());
// Setting up current position, now that it's known whether it's ponder or
// not.
if (params.ponder && !current_position_.moves.empty()) {
std::vector<std::string> moves(current_position_.moves);
std::string ponder_move = moves.back();
moves.pop_back();
SetupPosition(current_position_.fen, moves);
responder = std::make_unique<PonderResponseTransformer>(
std::move(responder), ponder_move);
} else {
SetupPosition(current_position_.fen, current_position_.moves);
}
if (!options_.Get<bool>(kUciChess960)) {
// Remap FRC castling to legacy castling.
responder = std::make_unique<Chess960Transformer>(
std::move(responder), tree_->HeadPosition().GetBoard());
}
if (!options_.Get<bool>(kShowWDL)) {
// Strip WDL information from the response.
responder = std::make_unique<WDLResponseFilter>(std::move(responder));
}
if (!options_.Get<bool>(kShowMovesleft)) {
// Strip movesleft information from the response.
responder = std::make_unique<MovesLeftResponseFilter>(std::move(responder));
}
auto stopper = time_manager_->GetStopper(params, *tree_.get());
search_ = std::make_unique<Search>(
*tree_, network_.get(), std::move(responder),
StringsToMovelist(params.searchmoves, tree_->HeadPosition().GetBoard()),
*move_start_time_, std::move(stopper), params.infinite || params.ponder,
options_, &cache_, syzygy_tb_.get());
LOGFILE << "Timer started at "
<< FormatTime(SteadyClockToSystemClock(*move_start_time_));
search_->StartThreads(options_.Get<int>(kThreadsOptionId));
}
void EngineController::PonderHit() {
ResetMoveTimer();
go_params_.ponder = false;
Go(go_params_);
}
void EngineController::Stop() {
if (search_) search_->Stop();
}
EngineLoop::EngineLoop()
: engine_(
std::make_unique<CallbackUciResponder>(
std::bind(&UciLoop::SendBestMove, this, std::placeholders::_1),
std::bind(&UciLoop::SendInfo, this, std::placeholders::_1)),
options_.GetOptionsDict()) {
engine_.PopulateOptions(&options_);
options_.Add<StringOption>(kLogFileId);
}
void EngineLoop::RunLoop() {
if (!ConfigFile::Init() || !options_.ProcessAllFlags()) return;
const auto options = options_.GetOptionsDict();
Logging::Get().SetFilename(options.Get<std::string>(kLogFileId));
if (options.Get<bool>(kPreload)) engine_.NewGame();
UciLoop::RunLoop();
}
void EngineLoop::CmdUci() {
SendId();
for (const auto& option : options_.ListOptionsUci()) {
SendResponse(option);
}
SendResponse("uciok");
}
void EngineLoop::CmdIsReady() {
engine_.EnsureReady();
SendResponse("readyok");
}
void EngineLoop::CmdSetOption(const std::string& name, const std::string& value,
const std::string& context) {
options_.SetUciOption(name, value, context);
// Set the log filename for the case it was set in UCI option.
Logging::Get().SetFilename(
options_.GetOptionsDict().Get<std::string>(kLogFileId));
}
void EngineLoop::CmdUciNewGame() { engine_.NewGame(); }
void EngineLoop::CmdPosition(const std::string& position,
const std::vector<std::string>& moves) {
std::string fen = position;
if (fen.empty()) {
fen = ChessBoard::kStartposFen;
}
engine_.SetPosition(fen, moves);
}
void EngineLoop::CmdFen() {
std::string fen = GetFen(engine_.ApplyPositionMoves());
return SendResponse(fen);
}
void EngineLoop::CmdGo(const GoParams& params) { engine_.Go(params); }
void EngineLoop::CmdPonderHit() { engine_.PonderHit(); }
void EngineLoop::CmdStop() { engine_.Stop(); }
} // namespace lczero