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, GOOD_CAPTURES, KILLERS, GOOD_QUIETS, BAD_QUIETS, BAD_CAPTURES,
30 EVASION, ALL_EVASIONS,
31 QSEARCH_WITH_CHECKS, QCAPTURES_1, CHECKS,
32 QSEARCH_WITHOUT_CHECKS, QCAPTURES_2,
33 PROBCUT, PROBCUT_CAPTURES,
34 RECAPTURE, RECAPTURES,
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,
71 const CounterMovesHistoryStats& cmh, Move cm, Search::Stack* s)
72 : pos(p), history(h), counterMovesHistory(cmh), ss(s), countermove(cm), depth(d) {
74 assert(d > DEPTH_ZERO);
76 stage = pos.checkers() ? EVASION : MAIN_SEARCH;
77 ttMove = ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE;
78 endMoves += (ttMove != MOVE_NONE);
81 MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h,
82 const CounterMovesHistoryStats& cmh, Square s)
83 : pos(p), history(h), counterMovesHistory(cmh) {
85 assert(d <= DEPTH_ZERO);
90 else if (d > DEPTH_QS_NO_CHECKS)
91 stage = QSEARCH_WITH_CHECKS;
93 else if (d > DEPTH_QS_RECAPTURES)
94 stage = QSEARCH_WITHOUT_CHECKS;
103 ttMove = ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE;
104 endMoves += (ttMove != MOVE_NONE);
107 MovePicker::MovePicker(const Position& p, Move ttm, const HistoryStats& h,
108 const CounterMovesHistoryStats& cmh, Value th)
109 : pos(p), history(h), counterMovesHistory(cmh), threshold(th) {
111 assert(!pos.checkers());
115 // In ProbCut we generate captures with SEE higher than the given threshold
117 && pos.pseudo_legal(ttm)
119 && pos.see(ttm) > threshold ? ttm : MOVE_NONE;
121 endMoves += (ttMove != MOVE_NONE);
125 /// score() assigns a numerical value to each move in a move list. The moves with
126 /// highest values will be picked first.
128 void MovePicker::score<CAPTURES>() {
129 // Winning and equal captures in the main search are ordered by MVV, preferring
130 // captures near our home rank. Suprisingly, this appears to perform slightly
131 // better than SEE based move ordering: exchanging big pieces before capturing
132 // a hanging piece probably helps to reduce the subtree size.
133 // In main search we want to push captures with negative SEE values to the
134 // badCaptures[] array, but instead of doing it now we delay until the move
135 // has been picked up, saving some SEE calls in case we get a cutoff.
136 for (auto& m : *this)
137 m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
138 - Value(200 * relative_rank(pos.side_to_move(), to_sq(m)));
142 void MovePicker::score<QUIETS>() {
144 Square prevSq = to_sq((ss-1)->currentMove);
145 const HistoryStats& cmh = counterMovesHistory[pos.piece_on(prevSq)][prevSq];
147 for (auto& m : *this)
148 m.value = history[pos.moved_piece(m)][to_sq(m)]
149 + cmh[pos.moved_piece(m)][to_sq(m)] * 3;
153 void MovePicker::score<EVASIONS>() {
154 // Try winning and equal captures captures ordered by MVV/LVA, then non-captures
155 // ordered by history value, then bad-captures and quiet moves with a negative
156 // SEE ordered by SEE value.
159 for (auto& m : *this)
160 if ((see = pos.see_sign(m)) < VALUE_ZERO)
161 m.value = see - HistoryStats::Max; // At the bottom
163 else if (pos.capture(m))
164 m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
165 - Value(type_of(pos.moved_piece(m))) + HistoryStats::Max;
167 m.value = history[pos.moved_piece(m)][to_sq(m)];
171 /// generate_next_stage() generates, scores and sorts the next bunch of moves,
172 /// when there are no more moves to try for the current stage.
174 void MovePicker::generate_next_stage() {
176 assert(stage != STOP);
182 case GOOD_CAPTURES: case QCAPTURES_1: case QCAPTURES_2:
183 case PROBCUT_CAPTURES: case RECAPTURES:
184 endMoves = generate<CAPTURES>(pos, moves);
189 killers[0] = ss->killers[0];
190 killers[1] = ss->killers[1];
191 killers[2] = countermove;
193 endMoves = cur + 2 + (countermove != killers[0] && countermove != killers[1]);
197 endQuiets = endMoves = generate<QUIETS>(pos, moves);
199 endMoves = std::partition(cur, endMoves, [](const ExtMove& m) { return m.value > VALUE_ZERO; });
200 insertion_sort(cur, endMoves);
205 endMoves = endQuiets;
206 if (depth >= 3 * ONE_PLY)
207 insertion_sort(cur, endMoves);
211 // Just pick them in reverse order to get correct ordering
212 cur = moves + MAX_MOVES - 1;
213 endMoves = endBadCaptures;
217 endMoves = generate<EVASIONS>(pos, moves);
218 if (endMoves - moves > 1)
223 endMoves = generate<QUIET_CHECKS>(pos, moves);
226 case EVASION: case QSEARCH_WITH_CHECKS: case QSEARCH_WITHOUT_CHECKS:
227 case PROBCUT: case RECAPTURE: case STOP:
237 /// next_move() is the most important method of the MovePicker class. It returns
238 /// a new pseudo legal move every time it is called, until there are no more moves
239 /// left. It picks the move with the biggest value from a list of generated moves
240 /// taking care not to return the ttMove if it has already been searched.
242 Move MovePicker::next_move<false>() {
248 while (cur == endMoves && stage != STOP)
249 generate_next_stage();
253 case MAIN_SEARCH: case EVASION: case QSEARCH_WITH_CHECKS:
254 case QSEARCH_WITHOUT_CHECKS: case PROBCUT:
259 move = pick_best(cur++, endMoves);
262 if (pos.see_sign(move) >= VALUE_ZERO)
265 // Losing capture, move it to the tail of the array
266 *endBadCaptures-- = move;
272 if ( move != MOVE_NONE
274 && pos.pseudo_legal(move)
275 && !pos.capture(move))
279 case GOOD_QUIETS: case BAD_QUIETS:
282 && move != killers[0]
283 && move != killers[1]
284 && move != killers[2])
291 case ALL_EVASIONS: case QCAPTURES_1: case QCAPTURES_2:
292 move = pick_best(cur++, endMoves);
297 case PROBCUT_CAPTURES:
298 move = pick_best(cur++, endMoves);
299 if (move != ttMove && pos.see(move) > threshold)
304 move = pick_best(cur++, endMoves);
305 if (to_sq(move) == recaptureSquare)
325 /// Version of next_move() to use at split point nodes where the move is grabbed
326 /// from the split point's shared MovePicker object. This function is not thread
327 /// safe so must be lock protected by the caller.
329 Move MovePicker::next_move<true>() { return ss->splitPoint->movePicker->next_move<false>(); }