2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4 Copyright (C) 2008-2013 Marco Costalba, Joona Kiiski, Tord Romstad
6 Stockfish is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
12 Stockfish is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 MAIN_SEARCH, CAPTURES_S1, KILLERS_S1, QUIETS_1_S1, QUIETS_2_S1, BAD_CAPTURES_S1,
31 QSEARCH_0, CAPTURES_S3, QUIET_CHECKS_S3,
32 QSEARCH_1, CAPTURES_S4,
34 RECAPTURE, CAPTURES_S6,
38 // Our insertion sort, guaranteed to be stable, as is needed
39 void insertion_sort(MoveStack* begin, MoveStack* end)
41 MoveStack tmp, *p, *q;
43 for (p = begin + 1; p < end; ++p)
46 for (q = p; q != begin && *(q-1) < tmp; --q)
52 // Unary predicate used by std::partition to split positive scores from remaining
53 // ones so to sort separately the two sets, and with the second sort delayed.
54 inline bool has_positive_score(const MoveStack& ms) { return ms.score > 0; }
56 // Picks and moves to the front the best move in the range [begin, end),
57 // it is faster than sorting all the moves in advance when moves are few, as
58 // normally are the possible captures.
59 inline MoveStack* pick_best(MoveStack* begin, MoveStack* end)
61 std::swap(*begin, *std::max_element(begin, end));
67 /// Constructors of the MovePicker class. As arguments we pass information
68 /// to help it to return the presumably good moves first, to decide which
69 /// moves to return (in the quiescence search, for instance, we only want to
70 /// search captures, promotions and some checks) and about how important good
71 /// move ordering is at the current node.
73 MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h, const CountermovesStats& cm,
74 Search::Stack* s, Value beta) : pos(p), history(h), depth(d) {
76 assert(d > DEPTH_ZERO);
80 endBadCaptures = moves + MAX_MOVES - 1;
90 killers[0].move = ss->killers[0];
91 killers[1].move = ss->killers[1];
92 Square prevSq = to_sq((ss-1)->currentMove);
93 killers[2].move = cm[pos.piece_on(prevSq)][prevSq];
95 // Consider sligtly negative captures as good if at low depth and far from beta
96 if (ss && ss->staticEval < beta - PawnValueMg && d < 3 * ONE_PLY)
97 captureThreshold = -PawnValueMg;
99 // Consider negative captures as good if still enough to reach beta
100 else if (ss && ss->staticEval > beta)
101 captureThreshold = beta - ss->staticEval;
104 ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
105 end += (ttMove != MOVE_NONE);
108 MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h,
109 Square sq) : pos(p), history(h), cur(moves), end(moves) {
111 assert(d <= DEPTH_ZERO);
116 else if (d > DEPTH_QS_NO_CHECKS)
119 else if (d > DEPTH_QS_RECAPTURES)
123 // Skip TT move if is not a capture or a promotion, this avoids qsearch
124 // tree explosion due to a possible perpetual check or similar rare cases
125 // when TT table is full.
126 if (ttm && !pos.is_capture_or_promotion(ttm))
132 recaptureSquare = sq;
136 ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
137 end += (ttMove != MOVE_NONE);
140 MovePicker::MovePicker(const Position& p, Move ttm, const HistoryStats& h, PieceType pt)
141 : pos(p), history(h), cur(moves), end(moves) {
143 assert(!pos.checkers());
147 // In ProbCut we generate only captures better than parent's captured piece
148 captureThreshold = PieceValue[MG][pt];
149 ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
151 if (ttMove && (!pos.is_capture(ttMove) || pos.see(ttMove) <= captureThreshold))
154 end += (ttMove != MOVE_NONE);
158 /// score() assign a numerical move ordering score to each move in a move list.
159 /// The moves with highest scores will be picked first.
161 void MovePicker::score<CAPTURES>() {
162 // Winning and equal captures in the main search are ordered by MVV/LVA.
163 // Suprisingly, this appears to perform slightly better than SEE based
164 // move ordering. The reason is probably that in a position with a winning
165 // capture, capturing a more valuable (but sufficiently defended) piece
166 // first usually doesn't hurt. The opponent will have to recapture, and
167 // the hanging piece will still be hanging (except in the unusual cases
168 // where it is possible to recapture with the hanging piece). Exchanging
169 // big pieces before capturing a hanging piece probably helps to reduce
171 // In main search we want to push captures with negative SEE values to
172 // badCaptures[] array, but instead of doing it now we delay till when
173 // the move has been picked up in pick_move_from_list(), this way we save
174 // some SEE calls in case we get a cutoff (idea from Pablo Vazquez).
177 for (MoveStack* it = moves; it != end; ++it)
180 it->score = PieceValue[MG][pos.piece_on(to_sq(m))]
181 - type_of(pos.piece_moved(m));
183 if (type_of(m) == PROMOTION)
184 it->score += PieceValue[MG][promotion_type(m)] - PieceValue[MG][PAWN];
186 else if (type_of(m) == ENPASSANT)
187 it->score += PieceValue[MG][PAWN];
192 void MovePicker::score<QUIETS>() {
196 for (MoveStack* it = moves; it != end; ++it)
199 it->score = history[pos.piece_moved(m)][to_sq(m)];
204 void MovePicker::score<EVASIONS>() {
205 // Try good captures ordered by MVV/LVA, then non-captures if destination square
206 // is not under attack, ordered by history value, then bad-captures and quiet
207 // moves with a negative SEE. This last group is ordered by the SEE score.
211 for (MoveStack* it = moves; it != end; ++it)
214 if ((seeScore = pos.see_sign(m)) < 0)
215 it->score = seeScore - HistoryStats::Max; // At the bottom
217 else if (pos.is_capture(m))
218 it->score = PieceValue[MG][pos.piece_on(to_sq(m))]
219 - type_of(pos.piece_moved(m)) + HistoryStats::Max;
221 it->score = history[pos.piece_moved(m)][to_sq(m)];
226 /// generate_next() generates, scores and sorts the next bunch of moves, when
227 /// there are no more moves to try for the current phase.
229 void MovePicker::generate_next() {
235 case CAPTURES_S1: case CAPTURES_S3: case CAPTURES_S4: case CAPTURES_S5: case CAPTURES_S6:
236 end = generate<CAPTURES>(pos, moves);
242 end = cur + 3 - (killers[2].move == killers[0].move || killers[2].move == killers[1].move);
246 endQuiets = end = generate<QUIETS>(pos, moves);
248 end = std::partition(cur, end, has_positive_score);
249 insertion_sort(cur, end);
255 if (depth >= 3 * ONE_PLY)
256 insertion_sort(cur, end);
259 case BAD_CAPTURES_S1:
260 // Just pick them in reverse order to get MVV/LVA ordering
261 cur = moves + MAX_MOVES - 1;
262 end = endBadCaptures;
266 end = generate<EVASIONS>(pos, moves);
271 case QUIET_CHECKS_S3:
272 end = generate<QUIET_CHECKS>(pos, moves);
275 case EVASION: case QSEARCH_0: case QSEARCH_1: case PROBCUT: case RECAPTURE:
278 end = cur + 1; // Avoid another next_phase() call
287 /// next_move() is the most important method of the MovePicker class. It returns
288 /// a new pseudo legal move every time is called, until there are no more moves
289 /// left. It picks the move with the biggest score from a list of generated moves
290 /// taking care not returning the ttMove if has already been searched previously.
292 Move MovePicker::next_move<false>() {
303 case MAIN_SEARCH: case EVASION: case QSEARCH_0: case QSEARCH_1: case PROBCUT:
308 move = pick_best(cur++, end)->move;
311 assert(captureThreshold <= 0); // Otherwise we cannot use see_sign()
313 if (pos.see_sign(move) >= captureThreshold)
316 // Losing capture, move it to the tail of the array
317 (endBadCaptures--)->move = move;
322 move = (cur++)->move;
323 if ( move != MOVE_NONE
324 && pos.is_pseudo_legal(move)
326 && !pos.is_capture(move))
330 case QUIETS_1_S1: case QUIETS_2_S1:
331 move = (cur++)->move;
333 && move != killers[0].move
334 && move != killers[1].move
335 && move != killers[2].move)
339 case BAD_CAPTURES_S1:
340 return (cur--)->move;
342 case EVASIONS_S2: case CAPTURES_S3: case CAPTURES_S4:
343 move = pick_best(cur++, end)->move;
349 move = pick_best(cur++, end)->move;
350 if (move != ttMove && pos.see(move) > captureThreshold)
355 move = pick_best(cur++, end)->move;
356 if (to_sq(move) == recaptureSquare)
360 case QUIET_CHECKS_S3:
361 move = (cur++)->move;
376 /// Version of next_move() to use at split point nodes where the move is grabbed
377 /// from the split point's shared MovePicker object. This function is not thread
378 /// safe so must be lock protected by the caller.
380 Move MovePicker::next_move<true>() { return ss->splitPoint->movePicker->next_move<false>(); }