Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/Stockfish
Path: blob/master/src/search.h
376 views
1
/*
2
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3
Copyright (C) 2004-2025 The Stockfish developers (see AUTHORS file)
4
5
Stockfish is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9
10
Stockfish is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
14
15
You should have received a copy of the GNU General Public License
16
along with this program. If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
#ifndef SEARCH_H_INCLUDED
20
#define SEARCH_H_INCLUDED
21
22
#include <algorithm>
23
#include <array>
24
#include <atomic>
25
#include <cassert>
26
#include <cstddef>
27
#include <cstdint>
28
#include <functional>
29
#include <memory>
30
#include <string>
31
#include <string_view>
32
#include <vector>
33
34
#include "history.h"
35
#include "misc.h"
36
#include "nnue/network.h"
37
#include "nnue/nnue_accumulator.h"
38
#include "numa.h"
39
#include "position.h"
40
#include "score.h"
41
#include "syzygy/tbprobe.h"
42
#include "timeman.h"
43
#include "types.h"
44
45
namespace Stockfish {
46
47
// Different node types, used as a template parameter
48
enum NodeType {
49
NonPV,
50
PV,
51
Root
52
};
53
54
class TranspositionTable;
55
class ThreadPool;
56
class OptionsMap;
57
58
namespace Search {
59
60
// Stack struct keeps track of the information we need to remember from nodes
61
// shallower and deeper in the tree during the search. Each search thread has
62
// its own array of Stack objects, indexed by the current ply.
63
struct Stack {
64
Move* pv;
65
PieceToHistory* continuationHistory;
66
CorrectionHistory<PieceTo>* continuationCorrectionHistory;
67
int ply;
68
Move currentMove;
69
Move excludedMove;
70
Value staticEval;
71
int statScore;
72
int moveCount;
73
bool inCheck;
74
bool ttPv;
75
bool ttHit;
76
int cutoffCnt;
77
int reduction;
78
int quietMoveStreak;
79
};
80
81
82
// RootMove struct is used for moves at the root of the tree. For each root move
83
// we store a score and a PV (really a refutation in the case of moves which
84
// fail low). Score is normally set at -VALUE_INFINITE for all non-pv moves.
85
struct RootMove {
86
87
explicit RootMove(Move m) :
88
pv(1, m) {}
89
bool extract_ponder_from_tt(const TranspositionTable& tt, Position& pos);
90
bool operator==(const Move& m) const { return pv[0] == m; }
91
// Sort in descending order
92
bool operator<(const RootMove& m) const {
93
return m.score != score ? m.score < score : m.previousScore < previousScore;
94
}
95
96
uint64_t effort = 0;
97
Value score = -VALUE_INFINITE;
98
Value previousScore = -VALUE_INFINITE;
99
Value averageScore = -VALUE_INFINITE;
100
Value meanSquaredScore = -VALUE_INFINITE * VALUE_INFINITE;
101
Value uciScore = -VALUE_INFINITE;
102
bool scoreLowerbound = false;
103
bool scoreUpperbound = false;
104
int selDepth = 0;
105
int tbRank = 0;
106
Value tbScore;
107
std::vector<Move> pv;
108
};
109
110
using RootMoves = std::vector<RootMove>;
111
112
113
// LimitsType struct stores information sent by the caller about the analysis required.
114
struct LimitsType {
115
116
// Init explicitly due to broken value-initialization of non POD in MSVC
117
LimitsType() {
118
time[WHITE] = time[BLACK] = inc[WHITE] = inc[BLACK] = npmsec = movetime = TimePoint(0);
119
movestogo = depth = mate = perft = infinite = 0;
120
nodes = 0;
121
ponderMode = false;
122
}
123
124
bool use_time_management() const { return time[WHITE] || time[BLACK]; }
125
126
std::vector<std::string> searchmoves;
127
TimePoint time[COLOR_NB], inc[COLOR_NB], npmsec, movetime, startTime;
128
int movestogo, depth, mate, perft, infinite;
129
uint64_t nodes;
130
bool ponderMode;
131
};
132
133
134
// The UCI stores the uci options, thread pool, and transposition table.
135
// This struct is used to easily forward data to the Search::Worker class.
136
struct SharedState {
137
SharedState(const OptionsMap& optionsMap,
138
ThreadPool& threadPool,
139
TranspositionTable& transpositionTable,
140
const LazyNumaReplicated<Eval::NNUE::Networks>& nets) :
141
options(optionsMap),
142
threads(threadPool),
143
tt(transpositionTable),
144
networks(nets) {}
145
146
const OptionsMap& options;
147
ThreadPool& threads;
148
TranspositionTable& tt;
149
const LazyNumaReplicated<Eval::NNUE::Networks>& networks;
150
};
151
152
class Worker;
153
154
// Null Object Pattern, implement a common interface for the SearchManagers.
155
// A Null Object will be given to non-mainthread workers.
156
class ISearchManager {
157
public:
158
virtual ~ISearchManager() {}
159
virtual void check_time(Search::Worker&) = 0;
160
};
161
162
struct InfoShort {
163
int depth;
164
Score score;
165
};
166
167
struct InfoFull: InfoShort {
168
int selDepth;
169
size_t multiPV;
170
std::string_view wdl;
171
std::string_view bound;
172
size_t timeMs;
173
size_t nodes;
174
size_t nps;
175
size_t tbHits;
176
std::string_view pv;
177
int hashfull;
178
};
179
180
struct InfoIteration {
181
int depth;
182
std::string_view currmove;
183
size_t currmovenumber;
184
};
185
186
// Skill structure is used to implement strength limit. If we have a UCI_Elo,
187
// we convert it to an appropriate skill level, anchored to the Stash engine.
188
// This method is based on a fit of the Elo results for games played between
189
// Stockfish at various skill levels and various versions of the Stash engine.
190
// Skill 0 .. 19 now covers CCRL Blitz Elo from 1320 to 3190, approximately
191
// Reference: https://github.com/vondele/Stockfish/commit/a08b8d4e9711c2
192
struct Skill {
193
// Lowest and highest Elo ratings used in the skill level calculation
194
constexpr static int LowestElo = 1320;
195
constexpr static int HighestElo = 3190;
196
197
Skill(int skill_level, int uci_elo) {
198
if (uci_elo)
199
{
200
double e = double(uci_elo - LowestElo) / (HighestElo - LowestElo);
201
level = std::clamp((((37.2473 * e - 40.8525) * e + 22.2943) * e - 0.311438), 0.0, 19.0);
202
}
203
else
204
level = double(skill_level);
205
}
206
bool enabled() const { return level < 20.0; }
207
bool time_to_pick(Depth depth) const { return depth == 1 + int(level); }
208
Move pick_best(const RootMoves&, size_t multiPV);
209
210
double level;
211
Move best = Move::none();
212
};
213
214
// SearchManager manages the search from the main thread. It is responsible for
215
// keeping track of the time, and storing data strictly related to the main thread.
216
class SearchManager: public ISearchManager {
217
public:
218
using UpdateShort = std::function<void(const InfoShort&)>;
219
using UpdateFull = std::function<void(const InfoFull&)>;
220
using UpdateIter = std::function<void(const InfoIteration&)>;
221
using UpdateBestmove = std::function<void(std::string_view, std::string_view)>;
222
223
struct UpdateContext {
224
UpdateShort onUpdateNoMoves;
225
UpdateFull onUpdateFull;
226
UpdateIter onIter;
227
UpdateBestmove onBestmove;
228
};
229
230
231
SearchManager(const UpdateContext& updateContext) :
232
updates(updateContext) {}
233
234
void check_time(Search::Worker& worker) override;
235
236
void pv(Search::Worker& worker,
237
const ThreadPool& threads,
238
const TranspositionTable& tt,
239
Depth depth);
240
241
Stockfish::TimeManagement tm;
242
double originalTimeAdjust;
243
int callsCnt;
244
std::atomic_bool ponder;
245
246
std::array<Value, 4> iterValue;
247
double previousTimeReduction;
248
Value bestPreviousScore;
249
Value bestPreviousAverageScore;
250
bool stopOnPonderhit;
251
252
size_t id;
253
254
const UpdateContext& updates;
255
};
256
257
class NullSearchManager: public ISearchManager {
258
public:
259
void check_time(Search::Worker&) override {}
260
};
261
262
263
// Search::Worker is the class that does the actual search.
264
// It is instantiated once per thread, and it is responsible for keeping track
265
// of the search history, and storing data required for the search.
266
class Worker {
267
public:
268
Worker(SharedState&, std::unique_ptr<ISearchManager>, size_t, NumaReplicatedAccessToken);
269
270
// Called at instantiation to initialize reductions tables.
271
// Reset histories, usually before a new game.
272
void clear();
273
274
// Called when the program receives the UCI 'go' command.
275
// It searches from the root position and outputs the "bestmove".
276
void start_searching();
277
278
bool is_mainthread() const { return threadIdx == 0; }
279
280
void ensure_network_replicated();
281
282
// Public because they need to be updatable by the stats
283
ButterflyHistory mainHistory;
284
LowPlyHistory lowPlyHistory;
285
286
CapturePieceToHistory captureHistory;
287
ContinuationHistory continuationHistory[2][2];
288
PawnHistory pawnHistory;
289
290
CorrectionHistory<Pawn> pawnCorrectionHistory;
291
CorrectionHistory<Minor> minorPieceCorrectionHistory;
292
CorrectionHistory<NonPawn> nonPawnCorrectionHistory;
293
CorrectionHistory<Continuation> continuationCorrectionHistory;
294
295
TTMoveHistory ttMoveHistory;
296
297
private:
298
void iterative_deepening();
299
300
void do_move(Position& pos, const Move move, StateInfo& st, Stack* const ss);
301
void
302
do_move(Position& pos, const Move move, StateInfo& st, const bool givesCheck, Stack* const ss);
303
void do_null_move(Position& pos, StateInfo& st);
304
void undo_move(Position& pos, const Move move);
305
void undo_null_move(Position& pos);
306
307
// This is the main search function, for both PV and non-PV nodes
308
template<NodeType nodeType>
309
Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, bool cutNode);
310
311
// Quiescence search function, which is called by the main search
312
template<NodeType nodeType>
313
Value qsearch(Position& pos, Stack* ss, Value alpha, Value beta);
314
315
Depth reduction(bool i, Depth d, int mn, int delta) const;
316
317
// Pointer to the search manager, only allowed to be called by the main thread
318
SearchManager* main_manager() const {
319
assert(threadIdx == 0);
320
return static_cast<SearchManager*>(manager.get());
321
}
322
323
TimePoint elapsed() const;
324
TimePoint elapsed_time() const;
325
326
Value evaluate(const Position&);
327
328
LimitsType limits;
329
330
size_t pvIdx, pvLast;
331
std::atomic<uint64_t> nodes, tbHits, bestMoveChanges;
332
int selDepth, nmpMinPly;
333
334
Value optimism[COLOR_NB];
335
336
Position rootPos;
337
StateInfo rootState;
338
RootMoves rootMoves;
339
Depth rootDepth, completedDepth;
340
Value rootDelta;
341
342
size_t threadIdx;
343
NumaReplicatedAccessToken numaAccessToken;
344
345
// Reductions lookup table initialized at startup
346
std::array<int, MAX_MOVES> reductions; // [depth or moveNumber]
347
348
// The main thread has a SearchManager, the others have a NullSearchManager
349
std::unique_ptr<ISearchManager> manager;
350
351
Tablebases::Config tbConfig;
352
353
const OptionsMap& options;
354
ThreadPool& threads;
355
TranspositionTable& tt;
356
const LazyNumaReplicated<Eval::NNUE::Networks>& networks;
357
358
// Used by NNUE
359
Eval::NNUE::AccumulatorStack accumulatorStack;
360
Eval::NNUE::AccumulatorCaches refreshTable;
361
362
friend class Stockfish::ThreadPool;
363
friend class SearchManager;
364
};
365
366
struct ConthistBonus {
367
int index;
368
int weight;
369
};
370
371
372
} // namespace Search
373
374
} // namespace Stockfish
375
376
#endif // #ifndef SEARCH_H_INCLUDED
377
378