2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
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.
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.
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/>.
28 MAIN_TT, CAPTURE_INIT, GOOD_CAPTURE, REFUTATION, QUIET_INIT, QUIET, BAD_CAPTURE,
29 EVASION_TT, EVASION_INIT, EVASION,
30 PROBCUT_TT, PROBCUT_INIT, PROBCUT,
31 QSEARCH_TT, QCAPTURE_INIT, QCAPTURE, QCHECK_INIT, QCHECK
34 // partial_insertion_sort() sorts moves in descending order up to and including
35 // a given limit. The order of moves smaller than the limit is left unspecified.
36 void partial_insertion_sort(ExtMove* begin, ExtMove* end, int limit) {
38 for (ExtMove *sortedEnd = begin, *p = begin + 1; p < end; ++p)
39 if (p->value >= limit)
43 for (q = sortedEnd; q != begin && *(q - 1) < tmp; --q)
52 /// Constructors of the MovePicker class. As arguments we pass information
53 /// to help it to return the (presumably) good moves first, to decide which
54 /// moves to return (in the quiescence search, for instance, we only want to
55 /// search captures, promotions, and some checks) and how important good move
56 /// ordering is at the current node.
58 /// MovePicker constructor for the main search
59 MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh,
60 const CapturePieceToHistory* cph,
61 const PieceToHistory** ch,
64 : pos(p), mainHistory(mh), captureHistory(cph), continuationHistory(ch),
65 ttMove(ttm), refutations{{killers[0], 0}, {killers[1], 0}, {cm, 0}}, depth(d)
69 stage = (pos.checkers() ? EVASION_TT : MAIN_TT) +
70 !(ttm && pos.pseudo_legal(ttm));
73 /// MovePicker constructor for quiescence search
74 MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh,
75 const CapturePieceToHistory* cph,
76 const PieceToHistory** ch,
78 : pos(p), mainHistory(mh), captureHistory(cph), continuationHistory(ch), ttMove(ttm), recaptureSquare(rs), depth(d)
82 stage = (pos.checkers() ? EVASION_TT : QSEARCH_TT) +
84 && (pos.checkers() || depth > DEPTH_QS_RECAPTURES || to_sq(ttm) == recaptureSquare)
85 && pos.pseudo_legal(ttm));
88 /// MovePicker constructor for ProbCut: we generate captures with SEE greater
89 /// than or equal to the given threshold.
90 MovePicker::MovePicker(const Position& p, Move ttm, Value th, Depth d, const CapturePieceToHistory* cph)
91 : pos(p), captureHistory(cph), ttMove(ttm), threshold(th), depth(d)
93 assert(!pos.checkers());
95 stage = PROBCUT_TT + !(ttm && pos.capture(ttm)
96 && pos.pseudo_legal(ttm)
97 && pos.see_ge(ttm, threshold));
100 /// MovePicker::score() assigns a numerical value to each move in a list, used
101 /// for sorting. Captures are ordered by Most Valuable Victim (MVV), preferring
102 /// captures with a good history. Quiets moves are ordered using the histories.
103 template<GenType Type>
104 void MovePicker::score() {
106 static_assert(Type == CAPTURES || Type == QUIETS || Type == EVASIONS, "Wrong type");
108 for (auto& m : *this)
109 if constexpr (Type == CAPTURES)
110 m.value = 6 * int(PieceValue[MG][pos.piece_on(to_sq(m))])
111 + (*captureHistory)[pos.moved_piece(m)][to_sq(m)][type_of(pos.piece_on(to_sq(m)))];
113 else if constexpr (Type == QUIETS)
114 m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
115 + 2 * (*continuationHistory[0])[pos.moved_piece(m)][to_sq(m)]
116 + (*continuationHistory[1])[pos.moved_piece(m)][to_sq(m)]
117 + (*continuationHistory[3])[pos.moved_piece(m)][to_sq(m)]
118 + (*continuationHistory[5])[pos.moved_piece(m)][to_sq(m)];
120 else // Type == EVASIONS
123 m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
124 - Value(type_of(pos.moved_piece(m)));
126 m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
127 + 2 * (*continuationHistory[0])[pos.moved_piece(m)][to_sq(m)]
132 /// MovePicker::select() returns the next move satisfying a predicate function.
133 /// It never returns the TT move.
134 template<MovePicker::PickType T, typename Pred>
135 Move MovePicker::select(Pred filter) {
137 while (cur < endMoves)
140 std::swap(*cur, *std::max_element(cur, endMoves));
142 if (*cur != ttMove && filter())
150 /// MovePicker::next_move() is the most important method of the MovePicker class. It
151 /// returns a new pseudo-legal move every time it is called until there are no more
152 /// moves left, picking the move with the highest score from a list of generated moves.
153 Move MovePicker::next_move(bool skipQuiets) {
168 cur = endBadCaptures = moves;
169 endMoves = generate<CAPTURES>(pos, cur);
172 partial_insertion_sort(cur, endMoves, -3000 * depth);
177 if (select<Next>([&](){
178 return pos.see_ge(*cur, Value(-69 * cur->value / 1024)) ?
179 // Move losing capture to endBadCaptures to be tried later
180 true : (*endBadCaptures++ = *cur, false); }))
183 // Prepare the pointers to loop over the refutations array
184 cur = std::begin(refutations);
185 endMoves = std::end(refutations);
187 // If the countermove is the same as a killer, skip it
188 if ( refutations[0].move == refutations[2].move
189 || refutations[1].move == refutations[2].move)
196 if (select<Next>([&](){ return *cur != MOVE_NONE
197 && !pos.capture(*cur)
198 && pos.pseudo_legal(*cur); }))
206 cur = endBadCaptures;
207 endMoves = generate<QUIETS>(pos, cur);
210 partial_insertion_sort(cur, endMoves, -3000 * depth);
218 && select<Next>([&](){return *cur != refutations[0].move
219 && *cur != refutations[1].move
220 && *cur != refutations[2].move;}))
223 // Prepare the pointers to loop over the bad captures
225 endMoves = endBadCaptures;
231 return select<Next>([](){ return true; });
235 endMoves = generate<EVASIONS>(pos, cur);
242 return select<Best>([](){ return true; });
245 return select<Next>([&](){ return pos.see_ge(*cur, threshold); });
248 if (select<Next>([&](){ return depth > DEPTH_QS_RECAPTURES
249 || to_sq(*cur) == recaptureSquare; }))
252 // If we did not find any move and we do not try checks, we have finished
253 if (depth != DEPTH_QS_CHECKS)
261 endMoves = generate<QUIET_CHECKS>(pos, cur);
267 return select<Next>([](){ return true; });
271 return MOVE_NONE; // Silence warning
274 } // namespace Stockfish