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-2012 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/>.
31 MAIN_SEARCH, CAPTURES_S1, KILLERS_S1, QUIETS_1_S1, QUIETS_2_S1, BAD_CAPTURES_S1,
33 QSEARCH_0, CAPTURES_S3, QUIET_CHECKS_S3,
34 QSEARCH_1, CAPTURES_S4,
36 RECAPTURE, CAPTURES_S6,
40 // Unary predicate used by std::partition to split positive scores from remaining
41 // ones so to sort separately the two sets, and with the second sort delayed.
42 inline bool has_positive_score(const MoveStack& ms) { return ms.score > 0; }
44 // Picks and moves to the front the best move in the range [begin, end),
45 // it is faster than sorting all the moves in advance when moves are few, as
46 // normally are the possible captures.
47 inline MoveStack* pick_best(MoveStack* begin, MoveStack* end)
49 std::swap(*begin, *std::max_element(begin, end));
55 /// History class method definitions
57 void History::clear() {
58 memset(history, 0, sizeof(history));
59 memset(gains, 0, sizeof(gains));
62 void History::update(Piece p, Square to, Value bonus) {
63 if (abs(history[p][to] + bonus) < History::Max)
64 history[p][to] += bonus;
67 void History::update_gain(Piece p, Square to, Value gain) {
68 gains[p][to] = std::max(gain, gains[p][to] - 1);
72 /// Constructors of the MovePicker class. As arguments we pass information
73 /// to help it to return the presumably good moves first, to decide which
74 /// moves to return (in the quiescence search, for instance, we only want to
75 /// search captures, promotions and some checks) and about how important good
76 /// move ordering is at the current node.
78 MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h,
79 Search::Stack* s, Value beta) : pos(p), H(h), depth(d) {
81 assert(d > DEPTH_ZERO);
85 endBadCaptures = moves + MAX_MOVES - 1;
95 killers[0].move = ss->killers[0];
96 killers[1].move = ss->killers[1];
98 // Consider sligtly negative captures as good if at low depth and far from beta
99 if (ss && ss->staticEval < beta - PawnValueMg && d < 3 * ONE_PLY)
100 captureThreshold = -PawnValueMg;
102 // Consider negative captures as good if still enough to reach beta
103 else if (ss && ss->staticEval > beta)
104 captureThreshold = beta - ss->staticEval;
107 ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
108 end += (ttMove != MOVE_NONE);
111 MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h,
112 Square sq) : pos(p), H(h), cur(moves), end(moves) {
114 assert(d <= DEPTH_ZERO);
119 else if (d > DEPTH_QS_NO_CHECKS)
122 else if (d > DEPTH_QS_RECAPTURES)
126 // Skip TT move if is not a capture or a promotion, this avoids qsearch
127 // tree explosion due to a possible perpetual check or similar rare cases
128 // when TT table is full.
129 if (ttm && !pos.is_capture_or_promotion(ttm))
135 recaptureSquare = sq;
139 ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
140 end += (ttMove != MOVE_NONE);
143 MovePicker::MovePicker(const Position& p, Move ttm, const History& h, PieceType pt)
144 : pos(p), H(h), cur(moves), end(moves) {
146 assert(!pos.checkers());
150 // In ProbCut we generate only captures better than parent's captured piece
151 captureThreshold = PieceValue[MG][pt];
152 ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
154 if (ttMove && (!pos.is_capture(ttMove) || pos.see(ttMove) <= captureThreshold))
157 end += (ttMove != MOVE_NONE);
161 /// MovePicker::score_captures(), MovePicker::score_noncaptures() and
162 /// MovePicker::score_evasions() assign a numerical move ordering score
163 /// to each move in a move list. The moves with highest scores will be
164 /// picked first by next_move().
166 void MovePicker::score_captures() {
167 // Winning and equal captures in the main search are ordered by MVV/LVA.
168 // Suprisingly, this appears to perform slightly better than SEE based
169 // move ordering. The reason is probably that in a position with a winning
170 // capture, capturing a more valuable (but sufficiently defended) piece
171 // first usually doesn't hurt. The opponent will have to recapture, and
172 // the hanging piece will still be hanging (except in the unusual cases
173 // where it is possible to recapture with the hanging piece). Exchanging
174 // big pieces before capturing a hanging piece probably helps to reduce
176 // In main search we want to push captures with negative SEE values to
177 // badCaptures[] array, but instead of doing it now we delay till when
178 // the move has been picked up in pick_move_from_list(), this way we save
179 // some SEE calls in case we get a cutoff (idea from Pablo Vazquez).
182 for (MoveStack* it = moves; it != end; ++it)
185 it->score = PieceValue[MG][pos.piece_on(to_sq(m))]
186 - type_of(pos.piece_moved(m));
188 if (type_of(m) == PROMOTION)
189 it->score += PieceValue[MG][promotion_type(m)];
193 void MovePicker::score_noncaptures() {
197 for (MoveStack* it = moves; it != end; ++it)
200 it->score = H[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.
214 for (MoveStack* it = moves; it != end; ++it)
217 if ((seeScore = pos.see_sign(m)) < 0)
218 it->score = seeScore - History::Max; // Be sure we are at the bottom
219 else if (pos.is_capture(m))
220 it->score = PieceValue[MG][pos.piece_on(to_sq(m))]
221 - type_of(pos.piece_moved(m)) + History::Max;
223 it->score = H[pos.piece_moved(m)][to_sq(m)];
228 /// MovePicker::generate_next() generates, scores and sorts the next bunch of moves,
229 /// when there are no more moves to try for the current phase.
231 void MovePicker::generate_next() {
237 case CAPTURES_S1: case CAPTURES_S3: case CAPTURES_S4: case CAPTURES_S5: case CAPTURES_S6:
238 end = generate<CAPTURES>(pos, moves);
248 endQuiets = end = generate<QUIETS>(pos, moves);
250 end = std::partition(cur, end, has_positive_score);
251 sort<MoveStack>(cur, end);
257 if (depth >= 3 * ONE_PLY)
258 sort<MoveStack>(cur, end);
261 case BAD_CAPTURES_S1:
262 // Just pick them in reverse order to get MVV/LVA ordering
263 cur = moves + MAX_MOVES - 1;
264 end = endBadCaptures;
268 end = generate<EVASIONS>(pos, moves);
272 case QUIET_CHECKS_S3:
273 end = generate<QUIET_CHECKS>(pos, moves);
276 case EVASION: case QSEARCH_0: case QSEARCH_1: case PROBCUT: case RECAPTURE:
279 end = cur + 1; // Avoid another next_phase() call
288 /// MovePicker::next_move() is the most important method of the MovePicker class.
289 /// It returns a new pseudo legal move every time it is called, until there
290 /// are no more moves left. It picks the move with the biggest score from a list
291 /// of generated moves taking care not to return the tt move if has already been
292 /// searched previously.
294 Move MovePicker::next_move<false>() {
305 case MAIN_SEARCH: case EVASION: case QSEARCH_0: case QSEARCH_1: case PROBCUT:
310 move = pick_best(cur++, end)->move;
313 assert(captureThreshold <= 0); // Otherwise we cannot use see_sign()
315 if (pos.see_sign(move) >= captureThreshold)
318 // Losing capture, move it to the tail of the array
319 (endBadCaptures--)->move = move;
324 move = (cur++)->move;
325 if ( move != MOVE_NONE
326 && pos.is_pseudo_legal(move)
328 && !pos.is_capture(move))
332 case QUIETS_1_S1: case QUIETS_2_S1:
333 move = (cur++)->move;
335 && move != killers[0].move
336 && move != killers[1].move)
340 case BAD_CAPTURES_S1:
341 return (cur--)->move;
343 case EVASIONS_S2: case CAPTURES_S3: case CAPTURES_S4:
344 move = pick_best(cur++, end)->move;
350 move = pick_best(cur++, end)->move;
351 if (move != ttMove && pos.see(move) > captureThreshold)
356 move = pick_best(cur++, end)->move;
357 if (to_sq(move) == recaptureSquare)
361 case QUIET_CHECKS_S3:
362 move = (cur++)->move;
377 /// Version of next_move() to use at split point nodes where the move is grabbed
378 /// from the split point's shared MovePicker object. This function is not thread
379 /// safe so should be lock protected by the caller.
381 Move MovePicker::next_move<true>() { return ss->sp->mp->next_move<false>(); }