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
5 Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
7 Stockfish is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (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/>.
28 MAIN_SEARCH, CAPTURES_INIT, GOOD_CAPTURES, KILLERS, COUNTERMOVE, QUIET_INIT, QUIET, BAD_CAPTURES,
29 EVASION, EVASIONS_INIT, ALL_EVASIONS,
30 PROBCUT, PROBCUT_INIT, PROBCUT_CAPTURES,
31 QSEARCH_WITH_CHECKS, QCAPTURES_1_INIT, QCAPTURES_1, QCHECKS,
32 QSEARCH_NO_CHECKS, QCAPTURES_2_INIT, QCAPTURES_2,
33 QSEARCH_RECAPTURES, QRECAPTURES
36 // partial_insertion_sort() sorts moves in descending order up to and including
37 // a given limit. The order of moves smaller than the limit is left unspecified.
38 void partial_insertion_sort(ExtMove* begin, ExtMove* end, int limit) {
40 for (ExtMove *sortedEnd = begin, *p = begin + 1; p < end; ++p)
41 if (p->value >= limit)
45 for (q = sortedEnd; q != begin && *(q - 1) < tmp; --q)
51 // pick_best() finds the best move in the range (begin, end) and moves it to
52 // the front. It's faster than sorting all the moves in advance when there
53 // are few moves, e.g., the possible captures.
54 Move pick_best(ExtMove* begin, ExtMove* end) {
56 std::swap(*begin, *std::max_element(begin, end));
63 /// Constructors of the MovePicker class. As arguments we pass information
64 /// to help it to return the (presumably) good moves first, to decide which
65 /// moves to return (in the quiescence search, for instance, we only want to
66 /// search captures, promotions, and some checks) and how important good move
67 /// ordering is at the current node.
69 MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh,
70 const PieceToHistory** ch, Move cm, Move* killers_p)
71 : pos(p), mainHistory(mh), contHistory(ch), countermove(cm),
72 killers{killers_p[0], killers_p[1]}, 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 stage += (ttMove == MOVE_NONE);
81 MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh,
82 const PieceToHistory** ch, Square s)
83 : pos(p), mainHistory(mh), contHistory(ch) {
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_NO_CHECKS;
98 stage = QSEARCH_RECAPTURES;
103 ttMove = ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE;
104 stage += (ttMove == MOVE_NONE);
107 MovePicker::MovePicker(const Position& p, Move ttm, Value th)
108 : pos(p), threshold(th) {
110 assert(!pos.checkers());
114 // In ProbCut we generate captures with SEE higher than or equal to the given threshold
116 && pos.pseudo_legal(ttm)
118 && pos.see_ge(ttm, threshold) ? ttm : MOVE_NONE;
120 stage += (ttMove == MOVE_NONE);
124 /// score() assigns a numerical value to each move in a move list. The moves with
125 /// highest values will be picked first.
127 void MovePicker::score<CAPTURES>() {
128 // Winning and equal captures in the main search are ordered by MVV, preferring
129 // captures near our home rank. Surprisingly, this appears to perform slightly
130 // better than SEE-based move ordering: exchanging big pieces before capturing
131 // a hanging piece probably helps to reduce the subtree size.
132 // In the main search we want to push captures with negative SEE values to the
133 // badCaptures[] array, but instead of doing it now we delay until the move
134 // has been picked up, saving some SEE calls in case we get a cutoff.
135 for (auto& m : *this)
136 m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
137 - Value(200 * relative_rank(pos.side_to_move(), to_sq(m)));
141 void MovePicker::score<QUIETS>() {
143 for (auto& m : *this)
144 m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
145 + (*contHistory[0])[pos.moved_piece(m)][to_sq(m)]
146 + (*contHistory[1])[pos.moved_piece(m)][to_sq(m)]
147 + (*contHistory[3])[pos.moved_piece(m)][to_sq(m)];
151 void MovePicker::score<EVASIONS>() {
152 // Try captures ordered by MVV/LVA, then non-captures ordered by stats heuristics
153 for (auto& m : *this)
155 m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
156 - Value(type_of(pos.moved_piece(m))) + (1 << 28);
158 m.value = (*mainHistory)[pos.side_to_move()][from_to(m)];
162 /// next_move() is the most important method of the MovePicker class. It returns
163 /// a new pseudo legal move every time it is called, until there are no more moves
164 /// left. It picks the move with the biggest value from a list of generated moves
165 /// taking care not to return the ttMove if it has already been searched.
167 Move MovePicker::next_move(bool skipQuiets) {
173 case MAIN_SEARCH: case EVASION: case QSEARCH_WITH_CHECKS:
174 case QSEARCH_NO_CHECKS: case PROBCUT:
179 endBadCaptures = cur = moves;
180 endMoves = generate<CAPTURES>(pos, cur);
186 while (cur < endMoves)
188 move = pick_best(cur++, endMoves);
191 if (pos.see_ge(move))
194 // Losing capture, move it to the beginning of the array
195 *endBadCaptures++ = move;
200 move = killers[0]; // First killer move
201 if ( move != MOVE_NONE
203 && pos.pseudo_legal(move)
204 && !pos.capture(move))
210 move = killers[1]; // Second killer move
211 if ( move != MOVE_NONE
213 && pos.pseudo_legal(move)
214 && !pos.capture(move))
221 if ( move != MOVE_NONE
223 && move != killers[0]
224 && move != killers[1]
225 && pos.pseudo_legal(move)
226 && !pos.capture(move))
231 cur = endBadCaptures;
232 endMoves = generate<QUIETS>(pos, cur);
234 partial_insertion_sort(cur, endMoves, -4000 * depth / ONE_PLY);
239 while ( cur < endMoves
240 && (!skipQuiets || cur->value >= VALUE_ZERO))
245 && move != killers[0]
246 && move != killers[1]
247 && move != countermove)
251 cur = moves; // Point to beginning of bad captures
255 if (cur < endBadCaptures)
261 endMoves = generate<EVASIONS>(pos, cur);
267 while (cur < endMoves)
269 move = pick_best(cur++, endMoves);
277 endMoves = generate<CAPTURES>(pos, cur);
282 case PROBCUT_CAPTURES:
283 while (cur < endMoves)
285 move = pick_best(cur++, endMoves);
287 && pos.see_ge(move, threshold))
292 case QCAPTURES_1_INIT: case QCAPTURES_2_INIT:
294 endMoves = generate<CAPTURES>(pos, cur);
299 case QCAPTURES_1: case QCAPTURES_2:
300 while (cur < endMoves)
302 move = pick_best(cur++, endMoves);
306 if (stage == QCAPTURES_2)
309 endMoves = generate<QUIET_CHECKS>(pos, cur);
314 while (cur < endMoves)
322 case QSEARCH_RECAPTURES:
324 endMoves = generate<CAPTURES>(pos, cur);
330 while (cur < endMoves)
332 move = pick_best(cur++, endMoves);
333 if (to_sq(move) == recaptureSquare)