-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.cpp
More file actions
254 lines (225 loc) · 8.08 KB
/
search.cpp
File metadata and controls
254 lines (225 loc) · 8.08 KB
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
#include "search.h"
#include "evaluate.h"
#include <memory>
#include <vector>
#include <chrono>
#include <iostream>
#include <atomic>
namespace search {
using namespace Stockfish; // maybe....
using Move = uint16_t;
std::unique_ptr<Stockfish::Eval::NNUE::Networks> nn;
std::unique_ptr<Stockfish::Eval::NNUE::AccumulatorCaches> cache;
TranspositionTable tt;
std::chrono::time_point<std::chrono::steady_clock> start;
std::atomic<bool> stop_requested;
int seldepth = 0;
struct SearchStackEntry {
std::unique_ptr<Move[]> pv;
Value eval = Stockfish::VALUE_NONE;
SearchStackEntry() :
pv(std::make_unique<Move[]>(Stockfish::MAX_PLY)) {}
};
inline Value value_draw(size_t nodes) { return VALUE_DRAW - 1 + Value(nodes & 0x2); }
inline Value value_to_tt(Value v, int ply) {
return is_win(v) ? v + ply : is_loss(v) ? v - ply : v;
}
static Value value_from_tt(Value v, int ply, int r50c) {
if (is_win(v))
{
if (v >= VALUE_MATE_IN_MAX_PLY && VALUE_MATE - v > 100 - r50c)
return VALUE_TB_WIN_IN_MAX_PLY - 1;
if (VALUE_TB - v > 100 - r50c)
return VALUE_TB_WIN_IN_MAX_PLY - 1;
return v - ply;
}
if (is_loss(v))
{
if (v <= VALUE_MATED_IN_MAX_PLY && VALUE_MATE + v > 100 - r50c)
return VALUE_TB_LOSS_IN_MAX_PLY + 1;
if (VALUE_TB + v > 100 - r50c)
return VALUE_TB_LOSS_IN_MAX_PLY + 1;
return v + ply;
}
return v;
}
static void update_pv(Move* pv, Move move, const Move* childPv) {
*pv++ = move;
while (childPv && *childPv)
*pv++ = *childPv++;
*pv = 0;
}
static Value qsearch(Position& pos, Value alpha, Value beta, int ply, SearchStackEntry* ss) {
seldepth = std::max(seldepth, ply+1);
Value stand_pat = -VALUE_INFINITE;
if (!pos.b.inCheck())
{
stand_pat = Stockfish::Eval::evaluate(*nn, pos, pos.stack, *cache, 0);
ss->eval = stand_pat;
if (stand_pat >= beta)
return stand_pat;
if (stand_pat > alpha)
alpha = stand_pat;
}
chess::Movelist list, list2;
chess::movegen::legalmoves(list, pos.b);
if (list.size() == 0)
return pos.b.inCheck() ? mated_in(ply + 1) : value_draw(nodes.load());
for (int i = 0; i < list.size(); ++i)
{
chess::Move mv = list[i];
if (pos.b.isCapture(mv) || pos.b.givesCheck(mv) != chess::CheckType::NO_CHECK
|| mv.typeOf() == mv.PROMOTION)
list2.add(mv);
}
movepick::qOrderMoves(pos.b, list2);
for (int i = 0; i < list2.size(); ++i)
{
chess::Move mv = list2[i];
pos.do_move(mv);
++nodes;
Value score = -qsearch(pos, -beta, -alpha, ply + 1, ss + 1);
pos.undo_move(mv);
if (score >= beta)
return score;
if (score > alpha)
alpha = score;
}
return alpha;
}
static Value
negamax(Position& pos, int depth, Value alpha, Value beta, int ply, SearchStackEntry* ss) {
seldepth = std::max(seldepth, ply+1);
if (stop_requested) return VALUE_NONE;
if (pos.b.isRepetition(1) || pos.b.halfMoveClock() >= 99)
return VALUE_DRAW;
uint64_t key = pos.b.hash();
ss->pv[0] = 0;
TTEntry* entry = tt.lookup(key);
if (entry && entry->depth >= depth)
{
Value tt_score = value_from_tt(entry->score, ply, pos.b.halfMoveClock());
if ((entry->flag == TTFlag::EXACT)
|| (entry->flag == TTFlag::LOWERBOUND && tt_score >= beta)
|| (entry->flag == TTFlag::UPPERBOUND && tt_score <= alpha))
return tt_score;
}
if (depth <= 0)
return qsearch(pos, alpha, beta, ply, ss);
chess::Movelist list;
chess::movegen::legalmoves(list, pos.b);
if (list.empty())
return pos.b.inCheck() ? mated_in(ply + 1) : value_draw(nodes.load());
chess::Move bestMove;
Value best = -VALUE_INFINITE, orig_alpha = alpha;
movepick::orderMoves(pos.b, list, entry ? entry->move : chess::Move::NULL_MOVE, ply);
for (int i = 0; i < list.size(); ++i)
{
chess::Move mv = list[i];
if (ply == 0){
auto end = std::chrono::steady_clock::now();
auto s = std::chrono::duration_cast<std::chrono::seconds>(end - start).count();
if (s>=1)
std::cout << "info currmove " << chess::uci::moveToUci(mv) << " currmovenum " << i + 1
<< std::endl;
}
pos.do_move(mv);
++nodes;
Value score = -negamax(pos, depth - 1, -beta, -alpha, ply + 1, ss + 1);
pos.undo_move(mv);
if (stop_requested) break;
if (score > best)
{
best = score;
bestMove = mv;
update_pv(ss->pv.get(), mv.move(), (ss + 1)->pv.get());
}
if (score > alpha)
alpha = score;
if (score >= beta)
{
if (!pos.b.isCapture(mv) && pos.b.givesCheck(mv) == chess::CheckType::NO_CHECK
&& mv.typeOf() != mv.PROMOTION)
{
movepick::updateHistoryHeuristic(mv, depth);
movepick::updateKillerMoves(mv, ply);
}
break;
}
}
if (!stop_requested){
TTFlag flag = (best >= beta) ? TTFlag::LOWERBOUND
: (best <= orig_alpha) ? TTFlag::UPPERBOUND
: TTFlag::EXACT;
tt.store(key, bestMove, value_to_tt(best, ply), depth, flag);
}
ss->eval = best;
return best;
}
void run_search(const chess::Board& board, const TimeControl& tc) {
std::vector<SearchStackEntry> ss(MAX_PLY);
int rundepth = tc.depth ? tc.depth : 5;
Position pos(board);
std::vector<Move> pv(MAX_PLY);
seldepth = 0;
start = std::chrono::steady_clock::now();
for (int d = 1; d <= rundepth && !stop_requested; ++d)
{
cache->clear(*nn);
for (auto& s : ss)
{
std::fill(s.pv.get(), s.pv.get() + MAX_PLY, 0);
s.eval = VALUE_NONE;
}
pos.stack.reset();
Value v = negamax(pos, d, -VALUE_INFINITE, VALUE_INFINITE, 0, ss.data());
auto end = std::chrono::steady_clock::now();
auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
std::cout << "info depth " << d << " seldepth " << seldepth << " score ";
if (is_decisive(v))
{
int m = (v > 0 ? VALUE_MATE - v : -VALUE_MATE - v);
std::cout << "mate " << (v > 0 ? (m + 1) / 2 : -(m + 1) / 2);
}
else
{
std::cout << "cp " << v;
}
std::cout << " nodes " << nodes << " nps " << (nodes * 1000000000 / nanos);
std::cout << " time " << nanos / 1000000 << " hashfull " << tt.hashfull() << " pv ";
for (int j = 0; j < MAX_PLY && ss[0].pv[j]; ++j)
std::cout << chess::uci::moveToUci(chess::Move(ss[0].pv[j])) << " ";
std::cout << std::endl;
if (!stop_requested) {
for (int i = 0; i < MAX_PLY; ++i) {
pv[i] = ss[0].pv[i];
}
}
}
if (pv[0])
std::cout << "bestmove " << chess::uci::moveToUci(chess::Move(pv[0])) << std::endl;
}
// ---------------------- Initialization ---------------------------
template<bool init_nn>
void init() {
if constexpr (init_nn)
{
Stockfish::Eval::NNUE::NetworkBig nBig(
Stockfish::Eval::NNUE::EvalFile{EvalFileDefaultNameBig, "", EvalFileDefaultNameBig},
Stockfish::Eval::NNUE::EmbeddedNNUEType::BIG);
Stockfish::Eval::NNUE::NetworkSmall nSmall(
Stockfish::Eval::NNUE::EvalFile{EvalFileDefaultNameSmall, "", EvalFileDefaultNameSmall},
Stockfish::Eval::NNUE::EmbeddedNNUEType::SMALL);
nn = std::make_unique<Stockfish::Eval::NNUE::Networks>(std::move(nBig), std::move(nSmall));
}
else
{
nn->big.verify(EvalFileDefaultNameBig, onVerify);
nn->small.verify(EvalFileDefaultNameSmall, onVerify);
cache = std::make_unique<Stockfish::Eval::NNUE::AccumulatorCaches>(*nn);
}
}
// Explicit instantiations for the linker
template void init<true>();
template void init<false>();
} // namespace search