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/>.
37 //// Local definitions
44 MovePicker::MovegenPhase PhaseTable[32];
45 int MainSearchPhaseIndex;
46 int EvasionsPhaseIndex;
47 int QsearchWithChecksPhaseIndex;
48 int QsearchWithoutChecksPhaseIndex;
58 /// Constructor for the MovePicker class. Apart from the position for which
59 /// it is asked to pick legal moves, MovePicker also wants some information
60 /// to help it to return the presumably good moves first, to decide which
61 /// moves to return (in the quiescence search, for instance, we only want to
62 /// search captures, promotions and some checks) and about how important good
63 /// move ordering is at the current node.
65 MovePicker::MovePicker(const Position& p, bool pv, Move ttm,
66 const SearchStack& ss, Depth d) : pos(p) {
69 mateKiller = (ss.mateKiller == ttm)? MOVE_NONE : ss.mateKiller;
70 killer1 = ss.killers[0];
71 killer2 = ss.killers[1];
78 phaseIndex = EvasionsPhaseIndex;
79 else if (depth > Depth(0))
80 phaseIndex = MainSearchPhaseIndex;
81 else if (depth == Depth(0))
82 phaseIndex = QsearchWithChecksPhaseIndex;
84 phaseIndex = QsearchWithoutChecksPhaseIndex;
86 Color us = pos.side_to_move();
88 dc = p.discovered_check_candidates(us);
89 pinned = p.pinned_pieces(us);
95 /// MovePicker::get_next_move() is the most important method of the MovePicker
96 /// class. It returns a new legal move every time it is called, until there
97 /// are no more moves left of the types we are interested in.
99 Move MovePicker::get_next_move() {
105 // If we already have a list of generated moves, pick the best move from
106 // the list, and return it.
107 move = pick_move_from_list();
108 if (move != MOVE_NONE)
110 assert(move_is_ok(move));
116 switch (PhaseTable[phaseIndex]) {
119 if (ttMove != MOVE_NONE)
121 assert(move_is_ok(ttMove));
122 if (move_is_legal(pos, ttMove, pinned))
128 if (mateKiller != MOVE_NONE)
130 assert(move_is_ok(mateKiller));
131 if (move_is_legal(pos, mateKiller, pinned))
136 case PH_GOOD_CAPTURES:
137 numOfMoves = generate_captures(pos, moves);
142 case PH_BAD_CAPTURES:
147 numOfMoves = generate_noncaptures(pos, moves);
153 assert(pos.is_check());
154 numOfMoves = generate_evasions(pos, moves, pinned);
160 numOfMoves = generate_captures(pos, moves);
166 numOfMoves = generate_non_capture_checks(pos, moves, dc);
181 /// A variant of get_next_move() which takes a lock as a parameter, used to
182 /// prevent multiple threads from picking the same move at a split point.
184 Move MovePicker::get_next_move(Lock &lock) {
192 Move m = get_next_move();
201 /// MovePicker::score_captures(), MovePicker::score_noncaptures(),
202 /// MovePicker::score_evasions() and MovePicker::score_qcaptures() assign a
203 /// numerical move ordering score to each move in a move list. The moves
204 /// with highest scores will be picked first by pick_move_from_list().
206 void MovePicker::score_captures() {
207 // Winning and equal captures in the main search are ordered by MVV/LVA.
208 // Suprisingly, this appears to perform slightly better than SEE based
209 // move ordering. The reason is probably that in a position with a winning
210 // capture, capturing a more valuable (but sufficiently defended) piece
211 // first usually doesn't hurt. The opponent will have to recapture, and
212 // the hanging piece will still be hanging (except in the unusual cases
213 // where it is possible to recapture with the hanging piece). Exchanging
214 // big pieces before capturing a hanging piece probably helps to reduce
216 // While scoring captures it moves all captures with negative SEE values
217 // to the badCaptures[] array.
221 for (int i = 0; i < numOfMoves; i++)
224 seeValue = pos.see(m);
227 if (move_promotion(m))
228 moves[i].score = QueenValueMidgame;
230 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
231 -int(pos.type_of_piece_on(move_from(m)));
235 // Losing capture, move it to the badCaptures[] array
236 assert(numOfBadCaptures < 63);
237 moves[i].score = seeValue;
238 badCaptures[numOfBadCaptures++] = moves[i];
239 moves[i--] = moves[--numOfMoves];
244 void MovePicker::score_noncaptures() {
245 // First score by history, when no history is available then use
246 // piece/square tables values. This seems to be better then a
247 // random choice when we don't have an history for any move.
251 for (int i = 0; i < numOfMoves; i++)
257 else if (m == killer2)
260 hs = H.move_ordering_score(pos.piece_on(move_from(m)), m);
262 // Ensure history is always preferred to pst
267 moves[i].score = hs + pos.mg_pst_delta(m);
271 void MovePicker::score_evasions() {
273 for (int i = 0; i < numOfMoves; i++)
275 Move m = moves[i].move;
277 moves[i].score = 2*HistoryMax;
278 else if (!pos.square_is_empty(move_to(m)))
280 int seeScore = pos.see(m);
281 moves[i].score = (seeScore >= 0)? seeScore + HistoryMax : seeScore;
283 moves[i].score = H.move_ordering_score(pos.piece_on(move_from(m)), m);
287 void MovePicker::score_qcaptures() {
289 // Use MVV/LVA ordering
290 for (int i = 0; i < numOfMoves; i++)
292 Move m = moves[i].move;
293 if (move_promotion(m))
294 moves[i].score = QueenValueMidgame;
296 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
297 -int(pos.type_of_piece_on(move_from(m)));
302 /// find_best_index() loops across the moves and returns index of
303 /// the highest scored one.
305 int MovePicker::find_best_index() const {
307 assert(movesPicked < numOfMoves);
309 int bestIndex = movesPicked;
310 int bestScore = moves[movesPicked].score;
312 for (int i = movesPicked + 1; i < numOfMoves; i++)
313 if (moves[i].score > bestScore)
316 bestScore = moves[i].score;
322 /// MovePicker::pick_move_from_list() picks the move with the biggest score
323 /// from a list of generated moves (moves[] or badCaptures[], depending on
324 /// the current move generation phase). It takes care not to return the
325 /// transposition table move if that has already been serched previously.
327 Move MovePicker::pick_move_from_list() {
332 switch (PhaseTable[phaseIndex]) {
334 case PH_GOOD_CAPTURES:
335 assert(!pos.is_check());
336 assert(movesPicked >= 0);
338 while (movesPicked < numOfMoves)
340 bestIndex = find_best_index();
341 move = moves[bestIndex].move;
342 moves[bestIndex] = moves[movesPicked++];
344 && move != mateKiller
345 && pos.pl_move_is_legal(move, pinned))
351 assert(!pos.is_check());
352 assert(movesPicked >= 0);
354 while (movesPicked < numOfMoves)
356 // If this is a PV node or we have only picked a few moves, scan
357 // the entire move list for the best move. If many moves have already
358 // been searched and it is not a PV node, we are probably failing low
359 // anyway, so we just pick the first move from the list.
360 bestIndex = (pvNode || movesPicked < 12) ? find_best_index() : movesPicked;
361 move = moves[bestIndex].move;
362 moves[bestIndex] = moves[movesPicked++];
364 && move != mateKiller
365 && pos.pl_move_is_legal(move, pinned))
371 assert(pos.is_check());
372 assert(movesPicked >= 0);
374 while (movesPicked < numOfMoves)
376 bestIndex = find_best_index();
377 move = moves[bestIndex].move;
378 moves[bestIndex] = moves[movesPicked++];
383 case PH_BAD_CAPTURES:
384 assert(!pos.is_check());
385 assert(movesPicked >= 0);
386 // It's probably a good idea to use SEE move ordering here, instead
387 // of just picking the first move. FIXME
388 while (movesPicked < numOfBadCaptures)
390 move = badCaptures[movesPicked++].move;
392 && move != mateKiller
393 && pos.pl_move_is_legal(move, pinned))
399 assert(!pos.is_check());
400 assert(movesPicked >= 0);
401 while (movesPicked < numOfMoves)
403 bestIndex = (movesPicked < 4 ? find_best_index() : movesPicked);
404 move = moves[bestIndex].move;
405 moves[bestIndex] = moves[movesPicked++];
406 // Maybe postpone the legality check until after futility pruning?
408 && pos.pl_move_is_legal(move, pinned))
414 assert(!pos.is_check());
415 assert(movesPicked >= 0);
416 // Perhaps we should do something better than just picking the first
418 while (movesPicked < numOfMoves)
420 move = moves[movesPicked++].move;
422 && pos.pl_move_is_legal(move, pinned))
434 /// MovePicker::init_phase_table() initializes the PhaseTable[],
435 /// MainSearchPhaseIndex, EvasionPhaseIndex, QsearchWithChecksPhaseIndex
436 /// and QsearchWithoutChecksPhaseIndex. It is only called once during
437 /// program startup, and never again while the program is running.
439 void MovePicker::init_phase_table() {
444 MainSearchPhaseIndex = i - 1;
445 PhaseTable[i++] = PH_TT_MOVE;
446 PhaseTable[i++] = PH_MATE_KILLER;
447 PhaseTable[i++] = PH_GOOD_CAPTURES;
448 // PH_KILLER_1 and PH_KILLER_2 are not yet used.
449 // PhaseTable[i++] = PH_KILLER_1;
450 // PhaseTable[i++] = PH_KILLER_2;
451 PhaseTable[i++] = PH_NONCAPTURES;
452 PhaseTable[i++] = PH_BAD_CAPTURES;
453 PhaseTable[i++] = PH_STOP;
456 EvasionsPhaseIndex = i - 1;
457 PhaseTable[i++] = PH_EVASIONS;
458 PhaseTable[i++] = PH_STOP;
460 // Quiescence search with checks
461 QsearchWithChecksPhaseIndex = i - 1;
462 PhaseTable[i++] = PH_TT_MOVE;
463 PhaseTable[i++] = PH_QCAPTURES;
464 PhaseTable[i++] = PH_QCHECKS;
465 PhaseTable[i++] = PH_STOP;
467 // Quiescence search without checks
468 QsearchWithoutChecksPhaseIndex = i - 1;
469 PhaseTable[i++] = PH_TT_MOVE;
470 PhaseTable[i++] = PH_QCAPTURES;
471 PhaseTable[i++] = PH_STOP;