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/>.
29 MAIN_TT, CAPTURE_INIT, GOOD_CAPTURE, REFUTATION, QUIET_INIT, QUIET, BAD_CAPTURE,
30 EVASION_TT, EVASION_INIT, EVASION,
31 PROBCUT_TT, PROBCUT_INIT, PROBCUT,
32 QSEARCH_TT, QCAPTURE_INIT, QCAPTURE, QCHECK_INIT, QCHECK
35 // partial_insertion_sort() sorts moves in descending order up to and including
36 // a given limit. The order of moves smaller than the limit is left unspecified.
37 void partial_insertion_sort(ExtMove* begin, ExtMove* end, int limit) {
39 for (ExtMove *sortedEnd = begin, *p = begin + 1; p < end; ++p)
40 if (p->value >= limit)
44 for (q = sortedEnd; q != begin && *(q - 1) < tmp; --q)
53 /// Constructors of the MovePicker class. As arguments we pass information
54 /// to help it to return the (presumably) good moves first, to decide which
55 /// moves to return (in the quiescence search, for instance, we only want to
56 /// search captures, promotions, and some checks) and how important good move
57 /// ordering is at the current node.
59 /// MovePicker constructor for the main search
60 MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh,
61 const CapturePieceToHistory* cph,
62 const PieceToHistory** ch,
65 : pos(p), mainHistory(mh), captureHistory(cph), continuationHistory(ch),
66 ttMove(ttm), refutations{{killers[0], 0}, {killers[1], 0}, {cm, 0}}, depth(d)
70 stage = (pos.checkers() ? EVASION_TT : MAIN_TT) +
71 !(ttm && pos.pseudo_legal(ttm));
74 /// MovePicker constructor for quiescence search
75 MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh,
76 const CapturePieceToHistory* cph,
77 const PieceToHistory** ch,
79 : pos(p), mainHistory(mh), captureHistory(cph), continuationHistory(ch), ttMove(ttm), recaptureSquare(rs), depth(d)
83 stage = (pos.checkers() ? EVASION_TT : QSEARCH_TT) +
85 && (pos.checkers() || depth > DEPTH_QS_RECAPTURES || to_sq(ttm) == recaptureSquare)
86 && pos.pseudo_legal(ttm));
89 /// MovePicker constructor for ProbCut: we generate captures with SEE greater
90 /// than or equal to the given threshold.
91 MovePicker::MovePicker(const Position& p, Move ttm, Value th, Depth d, const CapturePieceToHistory* cph)
92 : pos(p), captureHistory(cph), ttMove(ttm), threshold(th), depth(d)
94 assert(!pos.checkers());
96 stage = PROBCUT_TT + !(ttm && pos.capture(ttm)
97 && pos.pseudo_legal(ttm)
98 && pos.see_ge(ttm, threshold));
101 /// MovePicker::score() assigns a numerical value to each move in a list, used
102 /// for sorting. Captures are ordered by Most Valuable Victim (MVV), preferring
103 /// captures with a good history. Quiets moves are ordered using the histories.
104 template<GenType Type>
105 void MovePicker::score() {
107 static_assert(Type == CAPTURES || Type == QUIETS || Type == EVASIONS, "Wrong type");
109 Bitboard threatened, threatenedByPawn, threatenedByMinor, threatenedByRook;
110 if constexpr (Type == QUIETS)
112 Color us = pos.side_to_move();
113 // squares threatened by pawns
114 threatenedByPawn = pos.attacks_by<PAWN>(~us);
115 // squares threatened by minors or pawns
116 threatenedByMinor = pos.attacks_by<KNIGHT>(~us) | pos.attacks_by<BISHOP>(~us) | threatenedByPawn;
117 // squares threatened by rooks, minors or pawns
118 threatenedByRook = pos.attacks_by<ROOK>(~us) | threatenedByMinor;
120 // pieces threatened by pieces of lesser material value
121 threatened = (pos.pieces(us, QUEEN) & threatenedByRook)
122 | (pos.pieces(us, ROOK) & threatenedByMinor)
123 | (pos.pieces(us, KNIGHT, BISHOP) & threatenedByPawn);
127 // Silence unused variable warnings
129 (void) threatenedByPawn;
130 (void) threatenedByMinor;
131 (void) threatenedByRook;
134 for (auto& m : *this)
135 if constexpr (Type == CAPTURES)
136 m.value = 6 * int(PieceValue[MG][pos.piece_on(to_sq(m))])
137 + (*captureHistory)[pos.moved_piece(m)][to_sq(m)][type_of(pos.piece_on(to_sq(m)))];
139 else if constexpr (Type == QUIETS)
140 m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
141 + 2 * (*continuationHistory[0])[pos.moved_piece(m)][to_sq(m)]
142 + (*continuationHistory[1])[pos.moved_piece(m)][to_sq(m)]
143 + (*continuationHistory[3])[pos.moved_piece(m)][to_sq(m)]
144 + (*continuationHistory[5])[pos.moved_piece(m)][to_sq(m)]
145 + (threatened & from_sq(m) ?
146 (type_of(pos.moved_piece(m)) == QUEEN && !(to_sq(m) & threatenedByRook) ? 50000
147 : type_of(pos.moved_piece(m)) == ROOK && !(to_sq(m) & threatenedByMinor) ? 25000
148 : !(to_sq(m) & threatenedByPawn) ? 15000
152 else // Type == EVASIONS
155 m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
156 - Value(type_of(pos.moved_piece(m)));
158 m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
159 + 2 * (*continuationHistory[0])[pos.moved_piece(m)][to_sq(m)]
164 /// MovePicker::select() returns the next move satisfying a predicate function.
165 /// It never returns the TT move.
166 template<MovePicker::PickType T, typename Pred>
167 Move MovePicker::select(Pred filter) {
169 while (cur < endMoves)
172 std::swap(*cur, *std::max_element(cur, endMoves));
174 if (*cur != ttMove && filter())
182 /// MovePicker::next_move() is the most important method of the MovePicker class. It
183 /// returns a new pseudo-legal move every time it is called until there are no more
184 /// moves left, picking the move with the highest score from a list of generated moves.
185 Move MovePicker::next_move(bool skipQuiets) {
200 cur = endBadCaptures = moves;
201 endMoves = generate<CAPTURES>(pos, cur);
204 partial_insertion_sort(cur, endMoves, -3000 * depth);
209 if (select<Next>([&](){
210 return pos.see_ge(*cur, Value(-69 * cur->value / 1024)) ?
211 // Move losing capture to endBadCaptures to be tried later
212 true : (*endBadCaptures++ = *cur, false); }))
215 // Prepare the pointers to loop over the refutations array
216 cur = std::begin(refutations);
217 endMoves = std::end(refutations);
219 // If the countermove is the same as a killer, skip it
220 if ( refutations[0].move == refutations[2].move
221 || refutations[1].move == refutations[2].move)
228 if (select<Next>([&](){ return *cur != MOVE_NONE
229 && !pos.capture(*cur)
230 && pos.pseudo_legal(*cur); }))
238 cur = endBadCaptures;
239 endMoves = generate<QUIETS>(pos, cur);
242 partial_insertion_sort(cur, endMoves, -3000 * depth);
250 && select<Next>([&](){return *cur != refutations[0].move
251 && *cur != refutations[1].move
252 && *cur != refutations[2].move;}))
255 // Prepare the pointers to loop over the bad captures
257 endMoves = endBadCaptures;
263 return select<Next>([](){ return true; });
267 endMoves = generate<EVASIONS>(pos, cur);
274 return select<Best>([](){ return true; });
277 return select<Next>([&](){ return pos.see_ge(*cur, threshold); });
280 if (select<Next>([&](){ return depth > DEPTH_QS_RECAPTURES
281 || to_sq(*cur) == recaptureSquare; }))
284 // If we did not find any move and we do not try checks, we have finished
285 if (depth != DEPTH_QS_CHECKS)
293 endMoves = generate<QUIET_CHECKS>(pos, cur);
299 return select<Next>([](){ return true; });
303 return MOVE_NONE; // Silence warning
306 } // namespace Stockfish