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 Marco Costalba
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/>.
38 //// Local definitions
45 MovePicker::MovegenPhase PhaseTable[32];
46 int MainSearchPhaseIndex;
47 int EvasionsPhaseIndex;
48 int QsearchWithChecksPhaseIndex;
49 int QsearchWithoutChecksPhaseIndex;
59 /// Constructor for the MovePicker class. Apart from the position for which
60 /// it is asked to pick legal moves, MovePicker also wants some information
61 /// to help it to return the presumably good moves first, to decide which
62 /// moves to return (in the quiescence search, for instance, we only want to
63 /// search captures, promotions and some checks) and about how important good
64 /// move ordering is at the current node.
66 MovePicker::MovePicker(const Position& p, bool pv, Move ttm,
67 const SearchStack& ss, Depth d) : pos(p) {
70 mateKiller = (ss.mateKiller == ttm)? MOVE_NONE : ss.mateKiller;
71 killer1 = ss.killers[0];
72 killer2 = ss.killers[1];
79 phaseIndex = EvasionsPhaseIndex;
80 else if (depth > Depth(0))
81 phaseIndex = MainSearchPhaseIndex;
82 else if (depth == Depth(0))
83 phaseIndex = QsearchWithChecksPhaseIndex;
85 phaseIndex = QsearchWithoutChecksPhaseIndex;
87 Color us = pos.side_to_move();
89 dc = p.discovered_check_candidates(us);
90 pinned = p.pinned_pieces(us);
96 /// MovePicker::get_next_move() is the most important method of the MovePicker
97 /// class. It returns a new legal move every time it is called, until there
98 /// are no more moves left of the types we are interested in.
100 Move MovePicker::get_next_move() {
106 // If we already have a list of generated moves, pick the best move from
107 // the list, and return it.
108 move = pick_move_from_list();
109 if (move != MOVE_NONE)
111 assert(move_is_ok(move));
117 switch (PhaseTable[phaseIndex]) {
120 if (ttMove != MOVE_NONE)
122 assert(move_is_ok(ttMove));
123 if (move_is_legal(pos, ttMove, pinned))
129 if (mateKiller != MOVE_NONE)
131 assert(move_is_ok(mateKiller));
132 if (move_is_legal(pos, mateKiller, pinned))
137 case PH_GOOD_CAPTURES:
138 numOfMoves = generate_captures(pos, moves);
140 std::sort(moves, moves + numOfMoves);
144 case PH_BAD_CAPTURES:
149 numOfMoves = generate_noncaptures(pos, moves);
151 std::sort(moves, moves + numOfMoves);
156 assert(pos.is_check());
157 numOfMoves = generate_evasions(pos, moves, pinned);
159 std::sort(moves, moves + numOfMoves);
164 numOfMoves = generate_captures(pos, moves);
166 std::sort(moves, moves + numOfMoves);
171 numOfMoves = generate_non_capture_checks(pos, moves, dc);
186 /// A variant of get_next_move() which takes a lock as a parameter, used to
187 /// prevent multiple threads from picking the same move at a split point.
189 Move MovePicker::get_next_move(Lock &lock) {
197 Move m = get_next_move();
206 /// MovePicker::score_captures(), MovePicker::score_noncaptures(),
207 /// MovePicker::score_evasions() and MovePicker::score_qcaptures() assign a
208 /// numerical move ordering score to each move in a move list. The moves
209 /// with highest scores will be picked first by pick_move_from_list().
211 void MovePicker::score_captures() {
212 // Winning and equal captures in the main search are ordered by MVV/LVA.
213 // Suprisingly, this appears to perform slightly better than SEE based
214 // move ordering. The reason is probably that in a position with a winning
215 // capture, capturing a more valuable (but sufficiently defended) piece
216 // first usually doesn't hurt. The opponent will have to recapture, and
217 // the hanging piece will still be hanging (except in the unusual cases
218 // where it is possible to recapture with the hanging piece). Exchanging
219 // big pieces before capturing a hanging piece probably helps to reduce
221 // While scoring captures it moves all captures with negative SEE values
222 // to the badCaptures[] array.
226 for (int i = 0; i < numOfMoves; i++)
229 seeValue = pos.see(m);
232 if (move_promotion(m))
233 moves[i].score = QueenValueMidgame;
235 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
236 -int(pos.type_of_piece_on(move_from(m)));
240 // Losing capture, move it to the badCaptures[] array
241 assert(numOfBadCaptures < 63);
242 moves[i].score = seeValue;
243 badCaptures[numOfBadCaptures++] = moves[i];
244 moves[i--] = moves[--numOfMoves];
249 void MovePicker::score_noncaptures() {
250 // First score by history, when no history is available then use
251 // piece/square tables values. This seems to be better then a
252 // random choice when we don't have an history for any move.
256 for (int i = 0; i < numOfMoves; i++)
262 else if (m == killer2)
265 hs = H.move_ordering_score(pos.piece_on(move_from(m)), m);
267 // Ensure history is always preferred to pst
272 moves[i].score = hs + pos.mg_pst_delta(m);
276 void MovePicker::score_evasions() {
278 for (int i = 0; i < numOfMoves; i++)
280 Move m = moves[i].move;
282 moves[i].score = 2*HistoryMax;
283 else if (!pos.square_is_empty(move_to(m)))
285 int seeScore = pos.see(m);
286 moves[i].score = (seeScore >= 0)? seeScore + HistoryMax : seeScore;
288 moves[i].score = H.move_ordering_score(pos.piece_on(move_from(m)), m);
292 void MovePicker::score_qcaptures() {
294 // Use MVV/LVA ordering
295 for (int i = 0; i < numOfMoves; i++)
297 Move m = moves[i].move;
298 if (move_promotion(m))
299 moves[i].score = QueenValueMidgame;
301 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
302 -int(pos.type_of_piece_on(move_from(m)));
307 /// MovePicker::pick_move_from_list() picks the move with the biggest score
308 /// from a list of generated moves (moves[] or badCaptures[], depending on
309 /// the current move generation phase). It takes care not to return the
310 /// transposition table move if that has already been serched previously.
312 Move MovePicker::pick_move_from_list() {
316 switch (PhaseTable[phaseIndex]) {
318 case PH_GOOD_CAPTURES:
319 assert(!pos.is_check());
320 assert(movesPicked >= 0);
322 while (movesPicked < numOfMoves)
324 move = moves[movesPicked++].move;
326 && move != mateKiller
327 && pos.pl_move_is_legal(move, pinned))
333 assert(!pos.is_check());
334 assert(movesPicked >= 0);
336 while (movesPicked < numOfMoves)
338 move = moves[movesPicked++].move;
340 && move != mateKiller
341 && pos.pl_move_is_legal(move, pinned))
347 assert(pos.is_check());
348 assert(movesPicked >= 0);
350 while (movesPicked < numOfMoves)
352 move = moves[movesPicked++].move;
357 case PH_BAD_CAPTURES:
358 assert(!pos.is_check());
359 assert(movesPicked >= 0);
360 // It's probably a good idea to use SEE move ordering here, instead
361 // of just picking the first move. FIXME
362 while (movesPicked < numOfBadCaptures)
364 move = badCaptures[movesPicked++].move;
366 && move != mateKiller
367 && pos.pl_move_is_legal(move, pinned))
373 assert(!pos.is_check());
374 assert(movesPicked >= 0);
376 while (movesPicked < numOfMoves)
378 move = moves[movesPicked++].move;
379 // Maybe postpone the legality check until after futility pruning?
381 && pos.pl_move_is_legal(move, pinned))
387 assert(!pos.is_check());
388 assert(movesPicked >= 0);
389 // Perhaps we should do something better than just picking the first
391 while (movesPicked < numOfMoves)
393 move = moves[movesPicked++].move;
395 && pos.pl_move_is_legal(move, pinned))
407 /// MovePicker::init_phase_table() initializes the PhaseTable[],
408 /// MainSearchPhaseIndex, EvasionPhaseIndex, QsearchWithChecksPhaseIndex
409 /// and QsearchWithoutChecksPhaseIndex. It is only called once during
410 /// program startup, and never again while the program is running.
412 void MovePicker::init_phase_table() {
417 MainSearchPhaseIndex = i - 1;
418 PhaseTable[i++] = PH_TT_MOVE;
419 PhaseTable[i++] = PH_MATE_KILLER;
420 PhaseTable[i++] = PH_GOOD_CAPTURES;
421 // PH_KILLER_1 and PH_KILLER_2 are not yet used.
422 // PhaseTable[i++] = PH_KILLER_1;
423 // PhaseTable[i++] = PH_KILLER_2;
424 PhaseTable[i++] = PH_NONCAPTURES;
425 PhaseTable[i++] = PH_BAD_CAPTURES;
426 PhaseTable[i++] = PH_STOP;
429 EvasionsPhaseIndex = i - 1;
430 PhaseTable[i++] = PH_EVASIONS;
431 PhaseTable[i++] = PH_STOP;
433 // Quiescence search with checks
434 QsearchWithChecksPhaseIndex = i - 1;
435 PhaseTable[i++] = PH_TT_MOVE;
436 PhaseTable[i++] = PH_QCAPTURES;
437 PhaseTable[i++] = PH_QCHECKS;
438 PhaseTable[i++] = PH_STOP;
440 // Quiescence search without checks
441 QsearchWithoutChecksPhaseIndex = i - 1;
442 PhaseTable[i++] = PH_TT_MOVE;
443 PhaseTable[i++] = PH_QCAPTURES;
444 PhaseTable[i++] = PH_STOP;