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/>.
32 MAIN_SEARCH, TT_MOVE_S1, GOOD_CAPTURES_S1, KILLERS_S1, NONCAPTURES_1_S1,
33 NONCAPTURES_2_S1, BAD_CAPTURES_S1, STOP_S1,
34 EVASIONS, TT_MOVE_S2, EVASIONS_S2, STOP_S2,
35 CAPTURES_AND_CHECKS, TT_MOVE_S3, QCAPTURES_S3, QCHECKS_S3, STOP_S3,
36 CAPTURES, TT_MOVE_S4, QCAPTURES_S4, STOP_S4,
37 RECAPTURES, TT_MOVE_S5, RECAPTURES_S5, STOP_S5,
38 PROBCUT, TT_MOVE_S6, GOOD_CAPTURES_S6, STOP_S6
41 // Unary predicate used by std::partition to split positive scores from remaining
42 // ones so to sort separately the two sets, and with the second sort delayed.
43 inline bool has_positive_score(const MoveStack& move) { return move.score > 0; }
45 // Picks and pushes to the front the best move in range [firstMove, lastMove),
46 // it is faster than sorting all the moves in advance when moves are few, as
47 // normally are the possible captures.
48 inline MoveStack* pick_best(MoveStack* firstMove, MoveStack* lastMove)
50 std::swap(*firstMove, *std::max_element(firstMove, lastMove));
55 /// Constructors for the MovePicker class. As arguments we pass information
56 /// to help it to return the presumably good moves first, to decide which
57 /// moves to return (in the quiescence search, for instance, we only want to
58 /// search captures, promotions and some checks) and about how important good
59 /// move ordering is at the current node.
61 MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h,
62 Search::Stack* ss, Value beta) : pos(p), H(h), depth(d) {
64 badCaptures = moves + MAX_MOVES;
66 assert(d > DEPTH_ZERO);
70 killers[0].move = killers[1].move = MOVE_NONE;
75 killers[0].move = ss->killers[0];
76 killers[1].move = ss->killers[1];
78 // Consider sligtly negative captures as good if at low depth and far from beta
79 if (ss && ss->eval < beta - PawnValueMidgame && d < 3 * ONE_PLY)
80 captureThreshold = -PawnValueMidgame;
82 // Consider negative captures as good if still enough to reach beta
83 else if (ss && ss->eval > beta)
84 captureThreshold = beta - ss->eval;
89 ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
90 phase += int(ttMove == MOVE_NONE);
94 MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h, Square recaptureSq)
97 assert(d <= DEPTH_ZERO);
102 else if (d >= DEPTH_QS_CHECKS)
103 phase = CAPTURES_AND_CHECKS;
105 else if (d >= DEPTH_QS_RECAPTURES)
109 // Skip TT move if is not a capture or a promotion, this avoids
110 // qsearch tree explosion due to a possible perpetual check or
111 // similar rare cases when TT table is full.
112 if (ttm != MOVE_NONE && !pos.is_capture_or_promotion(ttm))
118 recaptureSquare = recaptureSq;
122 ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
123 phase += int(ttMove == MOVE_NONE);
127 MovePicker::MovePicker(const Position& p, Move ttm, const History& h, PieceType parentCapture)
130 assert (!pos.in_check());
132 // In ProbCut we consider only captures better than parent's move
133 captureThreshold = PieceValueMidgame[Piece(parentCapture)];
136 if ( ttm != MOVE_NONE
137 && (!pos.is_capture(ttm) || pos.see(ttm) <= captureThreshold))
140 ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
141 phase += int(ttMove == MOVE_NONE);
146 /// MovePicker::go_next_phase() generates, scores and sorts the next bunch
147 /// of moves when there are no more moves to try for the current phase.
149 void MovePicker::go_next_phase() {
155 case TT_MOVE_S1: case TT_MOVE_S2: case TT_MOVE_S3:
156 case TT_MOVE_S4: case TT_MOVE_S5: case TT_MOVE_S6:
157 lastMove = curMove + 1;
160 case GOOD_CAPTURES_S1:
161 case GOOD_CAPTURES_S6:
162 lastMove = generate<MV_CAPTURE>(pos, moves);
168 lastMove = curMove + 2;
171 case NONCAPTURES_1_S1:
172 lastNonCapture = lastMove = generate<MV_NON_CAPTURE>(pos, moves);
174 lastMove = std::partition(curMove, lastMove, has_positive_score);
175 sort<MoveStack>(curMove, lastMove);
178 case NONCAPTURES_2_S1:
180 lastMove = lastNonCapture;
181 if (depth >= 3 * ONE_PLY)
182 sort<MoveStack>(curMove, lastMove);
185 case BAD_CAPTURES_S1:
186 // Bad captures SEE value is already calculated so just pick
187 // them in order to get SEE move ordering.
188 curMove = badCaptures;
189 lastMove = moves + MAX_MOVES;
193 assert(pos.in_check());
194 lastMove = generate<MV_EVASION>(pos, moves);
200 lastMove = generate<MV_CAPTURE>(pos, moves);
205 lastMove = generate<MV_CAPTURE>(pos, moves);
209 lastMove = generate<MV_NON_CAPTURE_CHECK>(pos, moves);
212 case STOP_S1: case STOP_S2: case STOP_S3:
213 case STOP_S4: case STOP_S5: case STOP_S6:
214 lastMove = curMove + 1; // Avoid another go_next_phase() call
223 /// MovePicker::score_captures(), MovePicker::score_noncaptures() and
224 /// MovePicker::score_evasions() assign a numerical move ordering score
225 /// to each move in a move list. The moves with highest scores will be
226 /// picked first by next_move().
228 void MovePicker::score_captures() {
229 // Winning and equal captures in the main search are ordered by MVV/LVA.
230 // Suprisingly, this appears to perform slightly better than SEE based
231 // move ordering. The reason is probably that in a position with a winning
232 // capture, capturing a more valuable (but sufficiently defended) piece
233 // first usually doesn't hurt. The opponent will have to recapture, and
234 // the hanging piece will still be hanging (except in the unusual cases
235 // where it is possible to recapture with the hanging piece). Exchanging
236 // big pieces before capturing a hanging piece probably helps to reduce
238 // In main search we want to push captures with negative SEE values to
239 // badCaptures[] array, but instead of doing it now we delay till when
240 // the move has been picked up in pick_move_from_list(), this way we save
241 // some SEE calls in case we get a cutoff (idea from Pablo Vazquez).
244 // Use MVV/LVA ordering
245 for (MoveStack* cur = moves; cur != lastMove; cur++)
248 cur->score = PieceValueMidgame[pos.piece_on(to_sq(m))]
249 - type_of(pos.piece_moved(m));
252 cur->score += PieceValueMidgame[Piece(promotion_piece_type(m))];
256 void MovePicker::score_noncaptures() {
261 for (MoveStack* cur = moves; cur != lastMove; cur++)
265 cur->score = H.value(pos.piece_on(from), to_sq(m));
269 void MovePicker::score_evasions() {
270 // Try good captures ordered by MVV/LVA, then non-captures if
271 // destination square is not under attack, ordered by history
272 // value, and at the end bad-captures and non-captures with a
273 // negative SEE. This last group is ordered by the SEE score.
277 // Skip if we don't have at least two moves to order
278 if (lastMove < moves + 2)
281 for (MoveStack* cur = moves; cur != lastMove; cur++)
284 if ((seeScore = pos.see_sign(m)) < 0)
285 cur->score = seeScore - History::MaxValue; // Be sure we are at the bottom
286 else if (pos.is_capture(m))
287 cur->score = PieceValueMidgame[pos.piece_on(to_sq(m))]
288 - type_of(pos.piece_moved(m)) + History::MaxValue;
290 cur->score = H.value(pos.piece_moved(m), to_sq(m));
294 /// MovePicker::next_move() is the most important method of the MovePicker class.
295 /// It returns a new pseudo legal move every time it is called, until there
296 /// are no more moves left. It picks the move with the biggest score from a list
297 /// of generated moves taking care not to return the tt move if has already been
298 /// searched previously. Note that this function is not thread safe so should be
299 /// lock protected by caller when accessed through a shared MovePicker object.
301 Move MovePicker::next_move() {
307 while (curMove == lastMove)
312 case TT_MOVE_S1: case TT_MOVE_S2: case TT_MOVE_S3:
313 case TT_MOVE_S4: case TT_MOVE_S5: case TT_MOVE_S6:
318 case GOOD_CAPTURES_S1:
319 move = pick_best(curMove++, lastMove)->move;
322 assert(captureThreshold <= 0); // Otherwise we must use see instead of see_sign
324 // Check for a non negative SEE now
325 int seeValue = pos.see_sign(move);
326 if (seeValue >= captureThreshold)
329 // Losing capture, move it to the tail of the array
330 (--badCaptures)->move = move;
331 badCaptures->score = seeValue;
335 case GOOD_CAPTURES_S6:
336 move = pick_best(curMove++, lastMove)->move;
338 && pos.see(move) > captureThreshold)
343 move = (curMove++)->move;
344 if ( move != MOVE_NONE
345 && pos.is_pseudo_legal(move)
347 && !pos.is_capture(move))
351 case NONCAPTURES_1_S1:
352 case NONCAPTURES_2_S1:
353 move = (curMove++)->move;
355 && move != killers[0].move
356 && move != killers[1].move)
360 case BAD_CAPTURES_S1:
361 move = pick_best(curMove++, lastMove)->move;
367 move = pick_best(curMove++, lastMove)->move;
373 move = (curMove++)->move;
374 if (to_sq(move) == recaptureSquare)
379 move = (curMove++)->move;
384 case STOP_S1: case STOP_S2: case STOP_S3:
385 case STOP_S4: case STOP_S5: case STOP_S6: