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-2015 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, which is guaranteed to be stable, as it should be
39 void insertion_sort(ExtMove* begin, ExtMove* end)
43 for (p = begin + 1; p < end; ++p)
46 for (q = p; q != begin && *(q-1) < tmp; --q)
52 // pick_best() finds the best move in the range (begin, end) and moves it to
53 // the front. It's faster than sorting all the moves in advance when there
54 // are few moves e.g. the possible captures.
55 Move pick_best(ExtMove* begin, ExtMove* end)
57 std::swap(*begin, *std::max_element(begin, end));
64 /// Constructors of the MovePicker class. As arguments we pass information
65 /// to help it to return the (presumably) good moves first, to decide which
66 /// moves to return (in the quiescence search, for instance, we only want to
67 /// search captures, promotions and some checks) and how important good move
68 /// ordering is at the current node.
70 MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h, const CounterMovesHistoryStats& cmh,
71 Move cm, Search::Stack* s) : pos(p), history(h), counterMovesHistory(cmh), depth(d) {
73 assert(d > DEPTH_ZERO);
75 endBadCaptures = moves + MAX_MOVES - 1;
85 ttMove = (ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE);
86 endMoves += (ttMove != MOVE_NONE);
89 MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h, const CounterMovesHistoryStats& cmh,
90 Square s) : pos(p), history(h), counterMovesHistory(cmh) {
92 assert(d <= DEPTH_ZERO);
97 else if (d > DEPTH_QS_NO_CHECKS)
100 else if (d > DEPTH_QS_RECAPTURES)
110 ttMove = (ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE);
111 endMoves += (ttMove != MOVE_NONE);
114 MovePicker::MovePicker(const Position& p, Move ttm, const HistoryStats& h, const CounterMovesHistoryStats& cmh, PieceType pt)
115 : pos(p), history(h), counterMovesHistory(cmh) {
117 assert(!pos.checkers());
121 // In ProbCut we generate only captures that are better than the parent's
123 captureThreshold = PieceValue[MG][pt];
124 ttMove = (ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE);
126 if (ttMove && (!pos.capture(ttMove) || pos.see(ttMove) <= captureThreshold))
129 endMoves += (ttMove != MOVE_NONE);
133 /// score() assign a numerical value to each move in a move list. The moves with
134 /// highest values will be picked first.
136 void MovePicker::score<CAPTURES>() {
137 // Winning and equal captures in the main search are ordered by MVV.
138 // Suprisingly, this appears to perform slightly better than SEE based
139 // move ordering. The reason is probably that in a position with a winning
140 // capture, capturing a valuable (but sufficiently defended) piece
141 // first usually doesn't hurt. The opponent will have to recapture, and
142 // the hanging piece will still be hanging (except in the unusual cases
143 // where it is possible to recapture with the hanging piece). Exchanging
144 // big pieces before capturing a hanging piece probably helps to reduce
146 // In main search we want to push captures with negative SEE values to the
147 // badCaptures[] array, but instead of doing it now we delay until the move
148 // has been picked up in pick_move_from_list(). This way we save some SEE
149 // calls in case we get a cutoff.
150 for (auto& m : *this)
151 m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
152 - 200 * relative_rank(pos.side_to_move(), to_sq(m));
156 void MovePicker::score<QUIETS>() {
158 Square prevSq = to_sq((ss-1)->currentMove);
159 const HistoryStats& cmh = counterMovesHistory[pos.piece_on(prevSq)][prevSq];
161 for (auto& m : *this)
162 m.value = history[pos.moved_piece(m)][to_sq(m)]
163 + cmh[pos.moved_piece(m)][to_sq(m)] * 3;
167 void MovePicker::score<EVASIONS>() {
168 // Try good captures ordered by MVV/LVA, then non-captures if destination square
169 // is not under attack, ordered by history value, then bad-captures and quiet
170 // moves with a negative SEE. This last group is ordered by the SEE value.
173 for (auto& m : *this)
174 if ((see = pos.see_sign(m)) < VALUE_ZERO)
175 m.value = see - HistoryStats::Max; // At the bottom
177 else if (pos.capture(m))
178 m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
179 - Value(type_of(pos.moved_piece(m))) + HistoryStats::Max;
181 m.value = history[pos.moved_piece(m)][to_sq(m)];
185 /// generate_next_stage() generates, scores and sorts the next bunch of moves,
186 /// when there are no more moves to try for the current stage.
188 void MovePicker::generate_next_stage() {
194 case CAPTURES_S1: case CAPTURES_S3: case CAPTURES_S4: case CAPTURES_S5: case CAPTURES_S6:
195 endMoves = generate<CAPTURES>(pos, moves);
203 killers[0] = ss->killers[0];
204 killers[1] = ss->killers[1];
205 killers[2].move = MOVE_NONE;
207 // Be sure countermoves are different from killers
208 if ( countermove != killers[0]
209 && countermove != killers[1])
210 *endMoves++ = countermove;
214 endQuiets = endMoves = generate<QUIETS>(pos, moves);
216 endMoves = std::partition(cur, endMoves, [](const ExtMove& m) { return m.value > VALUE_ZERO; });
217 insertion_sort(cur, endMoves);
222 endMoves = endQuiets;
223 if (depth >= 3 * ONE_PLY)
224 insertion_sort(cur, endMoves);
227 case BAD_CAPTURES_S1:
228 // Just pick them in reverse order to get MVV/LVA ordering
229 cur = moves + MAX_MOVES - 1;
230 endMoves = endBadCaptures;
234 endMoves = generate<EVASIONS>(pos, moves);
235 if (endMoves - moves > 1)
239 case QUIET_CHECKS_S3:
240 endMoves = generate<QUIET_CHECKS>(pos, moves);
243 case EVASION: case QSEARCH_0: case QSEARCH_1: case PROBCUT: case RECAPTURE:
248 endMoves = cur + 1; // Avoid another generate_next_stage() call
257 /// next_move() is the most important method of the MovePicker class. It returns
258 /// a new pseudo legal move every time it is called, until there are no more moves
259 /// left. It picks the move with the biggest value from a list of generated moves
260 /// taking care not to return the ttMove if it has already been searched.
262 Move MovePicker::next_move<false>() {
268 while (cur == endMoves)
269 generate_next_stage();
273 case MAIN_SEARCH: case EVASION: case QSEARCH_0: case QSEARCH_1: case PROBCUT:
278 move = pick_best(cur++, endMoves);
281 if (pos.see_sign(move) >= VALUE_ZERO)
284 // Losing capture, move it to the tail of the array
285 *endBadCaptures-- = move;
291 if ( move != MOVE_NONE
293 && pos.pseudo_legal(move)
294 && !pos.capture(move))
298 case QUIETS_1_S1: case QUIETS_2_S1:
301 && move != killers[0]
302 && move != killers[1]
303 && move != killers[2])
307 case BAD_CAPTURES_S1:
310 case EVASIONS_S2: case CAPTURES_S3: case CAPTURES_S4:
311 move = pick_best(cur++, endMoves);
317 move = pick_best(cur++, endMoves);
318 if (move != ttMove && pos.see(move) > captureThreshold)
323 move = pick_best(cur++, endMoves);
324 if (to_sq(move) == recaptureSquare)
328 case QUIET_CHECKS_S3:
344 /// Version of next_move() to use at split point nodes where the move is grabbed
345 /// from the split point's shared MovePicker object. This function is not thread
346 /// safe so must be lock protected by the caller.
348 Move MovePicker::next_move<true>() { return ss->splitPoint->movePicker->next_move<false>(); }