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;
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);
143 case PH_BAD_CAPTURES:
148 numOfMoves = generate_noncaptures(pos, moves);
154 assert(pos.is_check());
155 numOfMoves = generate_evasions(pos, moves, pinned);
161 numOfMoves = generate_captures(pos, moves);
167 numOfMoves = generate_checks(pos, moves, dc);
182 /// A variant of get_next_move() which takes a lock as a parameter, used to
183 /// prevent multiple threads from picking the same move at a split point.
185 Move MovePicker::get_next_move(Lock &lock) {
193 Move m = get_next_move();
202 /// MovePicker::score_captures(), MovePicker::score_noncaptures(),
203 /// MovePicker::score_evasions() and MovePicker::score_qcaptures() assign a
204 /// numerical move ordering score to each move in a move list. The moves
205 /// with highest scores will be picked first by pick_move_from_list().
207 void MovePicker::score_captures() {
208 // Winning and equal captures in the main search are ordered by MVV/LVA.
209 // Suprisingly, this appears to perform slightly better than SEE based
210 // move ordering. The reason is probably that in a position with a winning
211 // capture, capturing a more valuable (but sufficiently defended) piece
212 // first usually doesn't hurt. The opponent will have to recapture, and
213 // the hanging piece will still be hanging (except in the unusual cases
214 // where it is possible to recapture with the hanging piece). Exchanging
215 // big pieces before capturing a hanging piece probably helps to reduce
217 // While scoring captures it moves all captures with negative SEE values
218 // to the badCaptures[] array.
222 for (int i = 0; i < numOfMoves; i++)
225 seeValue = pos.see(m);
228 if (move_promotion(m))
229 moves[i].score = QueenValueMidgame;
231 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
232 -int(pos.type_of_piece_on(move_from(m)));
236 // Losing capture, move it to the badCaptures[] array
237 assert(numOfBadCaptures < 63);
238 moves[i].score = seeValue;
239 badCaptures[numOfBadCaptures++] = moves[i];
240 moves[i--] = moves[--numOfMoves];
245 void MovePicker::score_noncaptures() {
246 // First score by history, when no history is available then use
247 // piece/square tables values. This seems to be better then a
248 // random choice when we don't have an history for any move.
252 for (int i = 0; i < numOfMoves; i++)
258 else if (m == killer2)
261 hs = H.move_ordering_score(pos.piece_on(move_from(m)), m);
263 // Ensure history is always preferred to pst
268 moves[i].score = hs + pos.mg_pst_delta(m);
272 void MovePicker::score_evasions() {
274 for (int i = 0; i < numOfMoves; i++)
276 Move m = moves[i].move;
278 moves[i].score = 2*HistoryMax;
279 else if (!pos.square_is_empty(move_to(m)))
281 int seeScore = pos.see(m);
282 moves[i].score = (seeScore >= 0)? seeScore + HistoryMax : seeScore;
284 moves[i].score = H.move_ordering_score(pos.piece_on(move_from(m)), m);
288 void MovePicker::score_qcaptures() {
290 // Use MVV/LVA ordering
291 for (int i = 0; i < numOfMoves; i++)
293 Move m = moves[i].move;
294 if (move_promotion(m))
295 moves[i].score = QueenValueMidgame;
297 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
298 -int(pos.type_of_piece_on(move_from(m)));
303 /// find_best_index() loops across the moves and returns index of
304 /// the highest scored one. There is also a second version that
305 /// lowers the priority of moves that attack the same square,
306 /// so that if the best move that attack a square fails the next
307 /// move picked attacks a different square if any, not the same one.
309 int MovePicker::find_best_index() {
311 assert(movesPicked < numOfMoves);
313 int bestIndex = movesPicked, bestScore = moves[movesPicked].score;
315 for (int i = movesPicked + 1; i < numOfMoves; i++)
316 if (moves[i].score > bestScore)
319 bestScore = moves[i].score;
324 int MovePicker::find_best_index(Bitboard* squares, int values[]) {
326 assert(movesPicked < numOfMoves);
331 int bestScore = -10000000, bestIndex = -1;
333 for (int i = movesPicked; i < numOfMoves; i++)
338 if (!bit_is_set(*squares, to))
341 set_bit(squares, to);
345 hs = moves[i].score - values[to];
355 // Raise value of the picked square, so next attack
356 // to the same square will get low priority.
357 to = move_to(moves[bestIndex].move);
364 /// MovePicker::pick_move_from_list() picks the move with the biggest score
365 /// from a list of generated moves (moves[] or badCaptures[], depending on
366 /// the current move generation phase). It takes care not to return the
367 /// transposition table move if that has already been serched previously.
369 Move MovePicker::pick_move_from_list() {
374 switch (PhaseTable[phaseIndex]) {
376 case PH_GOOD_CAPTURES:
377 assert(!pos.is_check());
378 assert(movesPicked >= 0);
380 while (movesPicked < numOfMoves)
382 bestIndex = find_best_index();
383 move = moves[bestIndex].move;
384 moves[bestIndex] = moves[movesPicked++];
386 && move != mateKiller
387 && pos.pl_move_is_legal(move, pinned))
393 assert(!pos.is_check());
394 assert(movesPicked >= 0);
396 while (movesPicked < numOfMoves)
398 // If this is a PV node or we have only picked a few moves, scan
399 // the entire move list for the best move. If many moves have already
400 // been searched and it is not a PV node, we are probably failing low
401 // anyway, so we just pick the first move from the list.
402 bestIndex = (pvNode || movesPicked < 12) ? find_best_index() : movesPicked;
403 move = moves[bestIndex].move;
404 moves[bestIndex] = moves[movesPicked++];
406 && move != mateKiller
407 && pos.pl_move_is_legal(move, pinned))
413 assert(pos.is_check());
414 assert(movesPicked >= 0);
416 while (movesPicked < numOfMoves)
418 bestIndex = find_best_index();
419 move = moves[bestIndex].move;
420 moves[bestIndex] = moves[movesPicked++];
425 case PH_BAD_CAPTURES:
426 assert(!pos.is_check());
427 assert(movesPicked >= 0);
428 // It's probably a good idea to use SEE move ordering here, instead
429 // of just picking the first move. FIXME
430 while (movesPicked < numOfBadCaptures)
432 move = badCaptures[movesPicked++].move;
434 && move != mateKiller
435 && pos.pl_move_is_legal(move, pinned))
441 assert(!pos.is_check());
442 assert(movesPicked >= 0);
443 while (movesPicked < numOfMoves)
445 bestIndex = (movesPicked < 4 ? find_best_index() : movesPicked);
446 move = moves[bestIndex].move;
447 moves[bestIndex] = moves[movesPicked++];
448 // Remember to change the line below if we decide to hash the qsearch!
449 // Maybe also postpone the legality check until after futility pruning?
450 if (/* move != ttMove && */ pos.pl_move_is_legal(move, pinned))
456 assert(!pos.is_check());
457 assert(movesPicked >= 0);
458 // Perhaps we should do something better than just picking the first
460 while (movesPicked < numOfMoves)
462 move = moves[movesPicked++].move;
463 // Remember to change the line below if we decide to hash the qsearch!
464 if (/* move != ttMove && */ pos.pl_move_is_legal(move, pinned))
476 /// MovePicker::current_move_type() returns the type of the just
477 /// picked next move. It can be used in search to further differentiate
478 /// according to the current move type: capture, non capture, escape, etc.
479 MovePicker::MovegenPhase MovePicker::current_move_type() const {
481 return PhaseTable[phaseIndex];
485 /// MovePicker::init_phase_table() initializes the PhaseTable[],
486 /// MainSearchPhaseIndex, EvasionPhaseIndex, QsearchWithChecksPhaseIndex
487 /// QsearchNoCapturesPhaseIndex, QsearchWithoutChecksPhaseIndex and
488 /// NoMovesPhaseIndex variables. It is only called once during program
489 /// startup, and never again while the program is running.
491 void MovePicker::init_phase_table() {
496 MainSearchPhaseIndex = i - 1;
497 PhaseTable[i++] = PH_TT_MOVE;
498 PhaseTable[i++] = PH_MATE_KILLER;
499 PhaseTable[i++] = PH_GOOD_CAPTURES;
500 // PH_KILLER_1 and PH_KILLER_2 are not yet used.
501 // PhaseTable[i++] = PH_KILLER_1;
502 // PhaseTable[i++] = PH_KILLER_2;
503 PhaseTable[i++] = PH_NONCAPTURES;
504 PhaseTable[i++] = PH_BAD_CAPTURES;
505 PhaseTable[i++] = PH_STOP;
508 EvasionsPhaseIndex = i - 1;
509 PhaseTable[i++] = PH_EVASIONS;
510 PhaseTable[i++] = PH_STOP;
512 // Quiescence search with checks
513 QsearchWithChecksPhaseIndex = i - 1;
514 PhaseTable[i++] = PH_QCAPTURES;
515 PhaseTable[i++] = PH_QCHECKS;
516 PhaseTable[i++] = PH_STOP;
518 // Quiescence search without checks
519 QsearchWithoutChecksPhaseIndex = i - 1;
520 PhaseTable[i++] = PH_QCAPTURES;
521 PhaseTable[i++] = PH_STOP;