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 QsearchNoCapturesPhaseIndex;
49 int QsearchWithoutChecksPhaseIndex;
50 int NoMovesPhaseIndex;
61 /// Constructor for the MovePicker class. Apart from the position for which
62 /// it is asked to pick legal moves, MovePicker also wants some information
63 /// to help it to return the presumably good moves first, to decide which
64 /// moves to return (in the quiescence search, for instance, we only want to
65 /// search captures, promotions and some checks) and about how important good
66 /// move ordering is at the current node.
68 MovePicker::MovePicker(const Position& p, bool pv, Move ttm,
69 const SearchStack& ss, Depth d, EvalInfo* ei) : pos(p) {
72 mateKiller = (ss.mateKiller == ttm)? MOVE_NONE : ss.mateKiller;
73 killer1 = ss.killers[0];
74 killer2 = ss.killers[1];
75 threatMove = ss.threatMove;
81 // With EvalInfo we are able to know how many captures are possible before
82 // generating them. So avoid generating in case we know are zero.
83 Color us = pos.side_to_move();
84 Color them = opposite_color(us);
86 && (ei->attackedBy[us][0] & pos.pieces_of_color(them)) == 0
87 && !ei->mi->specialized_eval_exists()
88 && (pos.ep_square() == SQ_NONE)
89 && !pos.has_pawn_on_7th(us);
92 phaseIndex = EvasionsPhaseIndex;
93 else if (depth > Depth(0))
94 phaseIndex = MainSearchPhaseIndex;
95 else if (depth == Depth(0))
96 phaseIndex = (noCaptures ? QsearchNoCapturesPhaseIndex : QsearchWithChecksPhaseIndex);
98 phaseIndex = (noCaptures ? NoMovesPhaseIndex : QsearchWithoutChecksPhaseIndex);
100 dc = p.discovered_check_candidates(us);
101 pinned = p.pinned_pieces(p.side_to_move());
107 /// MovePicker::get_next_move() is the most important method of the MovePicker
108 /// class. It returns a new legal move every time it is called, until there
109 /// are no more moves left of the types we are interested in.
111 Move MovePicker::get_next_move() {
117 // If we already have a list of generated moves, pick the best move from
118 // the list, and return it.
119 move = pick_move_from_list();
120 if (move != MOVE_NONE)
122 assert(move_is_ok(move));
128 switch (PhaseTable[phaseIndex]) {
131 if (ttMove != MOVE_NONE)
133 assert(move_is_ok(ttMove));
134 if (move_is_legal(pos, ttMove, pinned))
140 if (mateKiller != MOVE_NONE)
142 assert(move_is_ok(mateKiller));
143 if (move_is_legal(pos, mateKiller, pinned))
148 case PH_GOOD_CAPTURES:
149 numOfMoves = generate_captures(pos, moves);
154 case PH_BAD_CAPTURES:
155 badCapturesPicked = 0;
159 numOfMoves = generate_noncaptures(pos, moves);
165 assert(pos.is_check());
166 numOfMoves = generate_evasions(pos, moves);
172 numOfMoves = generate_captures(pos, moves);
178 numOfMoves = generate_checks(pos, moves, dc);
193 /// A variant of get_next_move() which takes a lock as a parameter, used to
194 /// prevent multiple threads from picking the same move at a split point.
196 Move MovePicker::get_next_move(Lock &lock) {
204 Move m = get_next_move();
213 /// MovePicker::score_captures(), MovePicker::score_noncaptures(),
214 /// MovePicker::score_evasions() and MovePicker::score_qcaptures() assign a
215 /// numerical move ordering score to each move in a move list. The moves
216 /// with highest scores will be picked first by pick_move_from_list().
218 void MovePicker::score_captures() {
219 // Winning and equal captures in the main search are ordered by MVV/LVA.
220 // Suprisingly, this appears to perform slightly better than SEE based
221 // move ordering. The reason is probably that in a position with a winning
222 // capture, capturing a more valuable (but sufficiently defended) piece
223 // first usually doesn't hurt. The opponent will have to recapture, and
224 // the hanging piece will still be hanging (except in the unusual cases
225 // where it is possible to recapture with the hanging piece). Exchanging
226 // big pieces before capturing a hanging piece probably helps to reduce
228 // While scoring captures it moves all captures with negative SEE values
229 // to the badCaptures[] array.
233 for (int i = 0; i < numOfMoves; i++)
236 seeValue = pos.see(m);
239 if (move_promotion(m))
240 moves[i].score = QueenValueMidgame;
242 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
243 -int(pos.type_of_piece_on(move_from(m)));
247 // Losing capture, move it to the badCaptures[] array
248 assert(numOfBadCaptures < 63);
249 moves[i].score = seeValue;
250 badCaptures[numOfBadCaptures++] = moves[i];
251 moves[i--] = moves[--numOfMoves];
256 void MovePicker::score_noncaptures() {
257 // First score by history, when no history is available then use
258 // piece/square tables values. This seems to be better then a
259 // random choice when we don't have an history for any move.
263 for (int i = 0; i < numOfMoves; i++)
269 else if (m == killer2)
272 hs = H.move_ordering_score(pos.piece_on(move_from(m)), m);
274 // If the null move was refuted by a capture then give a
275 // bonus if we move away the captured piece.
276 if (threatMove != MOVE_NONE && move_from(m) == move_to(threatMove))
279 // Ensure history is always preferred to pst
284 moves[i].score = hs + pos.mg_pst_delta(m);
288 void MovePicker::score_evasions() {
290 for (int i = 0; i < numOfMoves; i++)
292 Move m = moves[i].move;
294 moves[i].score = 2*HistoryMax;
295 else if (!pos.square_is_empty(move_to(m)))
297 int seeScore = pos.see(m);
298 moves[i].score = (seeScore >= 0)? seeScore + HistoryMax : seeScore;
300 moves[i].score = H.move_ordering_score(pos.piece_on(move_from(m)), m);
304 void MovePicker::score_qcaptures() {
306 // Use MVV/LVA ordering
307 for (int i = 0; i < numOfMoves; i++)
309 Move m = moves[i].move;
310 if (move_promotion(m))
311 moves[i].score = QueenValueMidgame;
313 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
314 -int(pos.type_of_piece_on(move_from(m)));
319 /// find_best_index() loops across the moves and returns index of
\r
320 /// the highest scored one. There is also a second version that
\r
321 /// lowers the priority of moves that attack the same square,
\r
322 /// so that if the best move that attack a square fails the next
\r
323 /// move picked attacks a different square if any, not the same one.
325 int MovePicker::find_best_index() {
327 int bestScore = -10000000, bestIndex = -1;
329 for (int i = movesPicked; i < numOfMoves; i++)
330 if (moves[i].score > bestScore)
333 bestScore = moves[i].score;
338 int MovePicker::find_best_index(Bitboard* squares, int values[]) {
\r
343 int bestScore = -10000000, bestIndex = -1;
\r
345 for (int i = movesPicked; i < numOfMoves; i++)
\r
350 if (!bit_is_set(*squares, to))
\r
352 // Init at first use
\r
353 set_bit(squares, to);
\r
357 hs = moves[i].score - values[to];
\r
358 if (hs > bestScore)
\r
365 if (bestIndex != -1)
\r
367 // Raise value of the picked square, so next attack
\r
368 // to the same square will get low priority.
\r
369 to = move_to(moves[bestIndex].move);
\r
370 values[to] += 0xB00;
\r
376 /// MovePicker::pick_move_from_list() picks the move with the biggest score
377 /// from a list of generated moves (moves[] or badCaptures[], depending on
378 /// the current move generation phase). It takes care not to return the
379 /// transposition table move if that has already been serched previously.
381 Move MovePicker::pick_move_from_list() {
386 switch (PhaseTable[phaseIndex]) {
387 case PH_GOOD_CAPTURES:
388 assert(!pos.is_check());
389 assert(movesPicked >= 0);
391 while (movesPicked < numOfMoves)
393 bestIndex = find_best_index();
395 if (bestIndex != -1) // Found a good capture
397 move = moves[bestIndex].move;
398 moves[bestIndex] = moves[movesPicked++];
400 && move != mateKiller
401 && pos.pl_move_is_legal(move, pinned))
408 assert(!pos.is_check());
409 assert(movesPicked >= 0);
411 while (movesPicked < numOfMoves)
413 // If this is a PV node or we have only picked a few moves, scan
414 // the entire move list for the best move. If many moves have already
415 // been searched and it is not a PV node, we are probably failing low
416 // anyway, so we just pick the first move from the list.
417 bestIndex = (pvNode || movesPicked < 12) ? find_best_index() : movesPicked;
421 move = moves[bestIndex].move;
422 moves[bestIndex] = moves[movesPicked++];
424 && move != mateKiller
425 && pos.pl_move_is_legal(move, pinned))
432 assert(pos.is_check());
433 assert(movesPicked >= 0);
435 while (movesPicked < numOfMoves)
437 bestIndex = find_best_index();
441 move = moves[bestIndex].move;
442 moves[bestIndex] = moves[movesPicked++];
448 case PH_BAD_CAPTURES:
449 assert(!pos.is_check());
450 assert(badCapturesPicked >= 0);
451 // It's probably a good idea to use SEE move ordering here, instead
452 // of just picking the first move. FIXME
453 while (badCapturesPicked < numOfBadCaptures)
455 move = badCaptures[badCapturesPicked++].move;
457 && move != mateKiller
458 && pos.pl_move_is_legal(move, pinned))
464 assert(!pos.is_check());
465 assert(movesPicked >= 0);
466 while (movesPicked < numOfMoves)
468 bestIndex = (movesPicked < 4 ? find_best_index() : movesPicked);
472 move = moves[bestIndex].move;
473 moves[bestIndex] = moves[movesPicked++];
474 // Remember to change the line below if we decide to hash the qsearch!
475 // Maybe also postpone the legality check until after futility pruning?
476 if (/* move != ttMove && */ pos.pl_move_is_legal(move, pinned))
483 assert(!pos.is_check());
484 assert(movesPicked >= 0);
485 // Perhaps we should do something better than just picking the first
487 while (movesPicked < numOfMoves)
489 move = moves[movesPicked++].move;
490 // Remember to change the line below if we decide to hash the qsearch!
491 if (/* move != ttMove && */ pos.pl_move_is_legal(move, pinned))
503 /// MovePicker::current_move_type() returns the type of the just
504 /// picked next move. It can be used in search to further differentiate
505 /// according to the current move type: capture, non capture, escape, etc.
506 MovePicker::MovegenPhase MovePicker::current_move_type() const {
508 return PhaseTable[phaseIndex];
512 /// MovePicker::init_phase_table() initializes the PhaseTable[],
513 /// MainSearchPhaseIndex, EvasionPhaseIndex, QsearchWithChecksPhaseIndex
514 /// QsearchNoCapturesPhaseIndex, QsearchWithoutChecksPhaseIndex and
515 /// NoMovesPhaseIndex variables. It is only called once during program
516 /// startup, and never again while the program is running.
518 void MovePicker::init_phase_table() {
523 MainSearchPhaseIndex = i - 1;
524 PhaseTable[i++] = PH_TT_MOVE;
525 PhaseTable[i++] = PH_MATE_KILLER;
526 PhaseTable[i++] = PH_GOOD_CAPTURES;
527 // PH_KILLER_1 and PH_KILLER_2 are not yet used.
528 // PhaseTable[i++] = PH_KILLER_1;
529 // PhaseTable[i++] = PH_KILLER_2;
530 PhaseTable[i++] = PH_NONCAPTURES;
531 PhaseTable[i++] = PH_BAD_CAPTURES;
532 PhaseTable[i++] = PH_STOP;
535 EvasionsPhaseIndex = i - 1;
536 PhaseTable[i++] = PH_EVASIONS;
537 PhaseTable[i++] = PH_STOP;
539 // Quiescence search with checks
540 QsearchWithChecksPhaseIndex = i - 1;
541 PhaseTable[i++] = PH_QCAPTURES;
542 PhaseTable[i++] = PH_QCHECKS;
543 PhaseTable[i++] = PH_STOP;
545 // Quiescence search with checks only and no captures
546 QsearchNoCapturesPhaseIndex = i - 1;
547 PhaseTable[i++] = PH_QCHECKS;
548 PhaseTable[i++] = PH_STOP;
550 // Quiescence search without checks
551 QsearchWithoutChecksPhaseIndex = i - 1;
552 PhaseTable[i++] = PH_QCAPTURES;
553 PhaseTable[i++] = PH_STOP;
555 // Do not generate any move
556 NoMovesPhaseIndex = i - 1;
557 PhaseTable[i++] = PH_STOP;