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[0];
80 // With EvalInfo we are able to know how many captures are possible before
81 // generating them. So avoid generating in case we know are zero.
82 Color us = pos.side_to_move();
83 Color them = opposite_color(us);
85 && (ei->attackedBy[us][0] & pos.pieces_of_color(them)) == 0
86 && !ei->mi->specialized_eval_exists()
87 && (pos.ep_square() == SQ_NONE)
88 && !pos.has_pawn_on_7th(us);
91 phaseIndex = EvasionsPhaseIndex;
92 else if (depth > Depth(0))
93 phaseIndex = MainSearchPhaseIndex;
94 else if (depth == Depth(0))
95 phaseIndex = (noCaptures ? QsearchNoCapturesPhaseIndex : QsearchWithChecksPhaseIndex);
97 phaseIndex = (noCaptures ? NoMovesPhaseIndex : QsearchWithoutChecksPhaseIndex);
99 dc = p.discovered_check_candidates(us);
100 pinned = p.pinned_pieces(p.side_to_move());
106 /// MovePicker::get_next_move() is the most important method of the MovePicker
107 /// class. It returns a new legal move every time it is called, until there
108 /// are no more moves left of the types we are interested in.
110 Move MovePicker::get_next_move() {
116 // If we already have a list of generated moves, pick the best move from
117 // the list, and return it.
118 move = pick_move_from_list();
119 if (move != MOVE_NONE)
121 assert(move_is_ok(move));
127 switch (PhaseTable[phaseIndex]) {
130 if (ttMove != MOVE_NONE)
132 assert(move_is_ok(ttMove));
133 if (move_is_legal(pos, ttMove, pinned))
139 if (mateKiller != MOVE_NONE)
141 assert(move_is_ok(mateKiller));
142 if (move_is_legal(pos, mateKiller, pinned))
147 case PH_GOOD_CAPTURES:
148 numOfMoves = generate_captures(pos, moves);
153 case PH_BAD_CAPTURES:
154 badCapturesPicked = 0;
158 numOfMoves = generate_noncaptures(pos, moves);
164 assert(pos.is_check());
165 numOfMoves = generate_evasions(pos, moves);
171 numOfMoves = generate_captures(pos, moves);
177 numOfMoves = generate_checks(pos, moves, dc);
192 /// A variant of get_next_move() which takes a lock as a parameter, used to
193 /// prevent multiple threads from picking the same move at a split point.
195 Move MovePicker::get_next_move(Lock &lock) {
203 Move m = get_next_move();
212 /// MovePicker::score_captures(), MovePicker::score_noncaptures(),
213 /// MovePicker::score_evasions() and MovePicker::score_qcaptures() assign a
214 /// numerical move ordering score to each move in a move list. The moves
215 /// with highest scores will be picked first by pick_move_from_list().
217 void MovePicker::score_captures() {
218 // Winning and equal captures in the main search are ordered by MVV/LVA.
219 // Suprisingly, this appears to perform slightly better than SEE based
220 // move ordering. The reason is probably that in a position with a winning
221 // capture, capturing a more valuable (but sufficiently defended) piece
222 // first usually doesn't hurt. The opponent will have to recapture, and
223 // the hanging piece will still be hanging (except in the unusual cases
224 // where it is possible to recapture with the hanging piece). Exchanging
225 // big pieces before capturing a hanging piece probably helps to reduce
227 // While scoring captures it moves all captures with negative SEE values
228 // to the badCaptures[] array.
232 for (int i = 0; i < numOfMoves; i++)
235 seeValue = pos.see(m);
238 if (move_promotion(m))
239 moves[i].score = QueenValueMidgame;
241 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
242 -int(pos.type_of_piece_on(move_from(m)));
246 // Losing capture, move it to the badCaptures[] array
247 assert(numOfBadCaptures < 63);
248 moves[i].score = seeValue;
249 badCaptures[numOfBadCaptures++] = moves[i];
250 moves[i--] = moves[--numOfMoves];
255 void MovePicker::score_noncaptures() {
256 // First score by history, when no history is available then use
257 // piece/square tables values. This seems to be better then a
258 // random choice when we don't have an history for any move.
262 for (int i = 0; i < numOfMoves; i++)
268 else if (m == killer2)
271 hs = H.move_ordering_score(pos.piece_on(move_from(m)), m);
273 // Ensure moves in history are always sorted as first
277 moves[i].score = hs + pos.mg_pst_delta(m);
281 void MovePicker::score_evasions() {
283 for (int i = 0; i < numOfMoves; i++)
285 Move m = moves[i].move;
287 moves[i].score = 2*HistoryMax;
288 else if (!pos.square_is_empty(move_to(m)))
290 int seeScore = pos.see(m);
291 moves[i].score = (seeScore >= 0)? seeScore + HistoryMax : seeScore;
293 moves[i].score = H.move_ordering_score(pos.piece_on(move_from(m)), m);
295 // FIXME try psqt also here
298 void MovePicker::score_qcaptures() {
300 // Use MVV/LVA ordering
301 for (int i = 0; i < numOfMoves; i++)
303 Move m = moves[i].move;
304 if (move_promotion(m))
305 moves[i].score = QueenValueMidgame;
307 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
308 -int(pos.type_of_piece_on(move_from(m)));
313 /// find_best_index() loops across the moves and returns index of
\r
314 /// the highest scored one. There is also a second version that
\r
315 /// lowers the priority of moves that attack the same square,
\r
316 /// so that if the best move that attack a square fails the next
\r
317 /// move picked attacks a different square if any, not the same one.
319 int MovePicker::find_best_index() {
321 int bestScore = -10000000, bestIndex = -1;
323 for (int i = movesPicked; i < numOfMoves; i++)
324 if (moves[i].score > bestScore)
327 bestScore = moves[i].score;
332 int MovePicker::find_best_index(Bitboard* squares, int values[]) {
\r
337 int bestScore = -10000000, bestIndex = -1;
\r
339 for (int i = movesPicked; i < numOfMoves; i++)
\r
344 if (!bit_is_set(*squares, to))
\r
346 // Init at first use
\r
347 set_bit(squares, to);
\r
351 hs = moves[i].score - values[to];
\r
352 if (hs > bestScore)
\r
359 if (bestIndex != -1)
\r
361 // Raise value of the picked square, so next attack
\r
362 // to the same square will get low priority.
\r
363 to = move_to(moves[bestIndex].move);
\r
364 values[to] += 0xB00;
\r
370 /// MovePicker::pick_move_from_list() picks the move with the biggest score
371 /// from a list of generated moves (moves[] or badCaptures[], depending on
372 /// the current move generation phase). It takes care not to return the
373 /// transposition table move if that has already been serched previously.
375 Move MovePicker::pick_move_from_list() {
380 switch (PhaseTable[phaseIndex]) {
381 case PH_GOOD_CAPTURES:
382 assert(!pos.is_check());
383 assert(movesPicked >= 0);
385 while (movesPicked < numOfMoves)
387 bestIndex = find_best_index();
389 if (bestIndex != -1) // Found a good capture
391 move = moves[bestIndex].move;
392 moves[bestIndex] = moves[movesPicked++];
394 && move != mateKiller
395 && pos.pl_move_is_legal(move, pinned))
402 assert(!pos.is_check());
403 assert(movesPicked >= 0);
405 while (movesPicked < numOfMoves)
407 // If this is a PV node or we have only picked a few moves, scan
408 // the entire move list for the best move. If many moves have already
409 // been searched and it is not a PV node, we are probably failing low
410 // anyway, so we just pick the first move from the list.
411 bestIndex = (pvNode || movesPicked < 12) ? find_best_index() : movesPicked;
415 move = moves[bestIndex].move;
416 moves[bestIndex] = moves[movesPicked++];
418 && move != mateKiller
419 && pos.pl_move_is_legal(move, pinned))
426 assert(pos.is_check());
427 assert(movesPicked >= 0);
429 while (movesPicked < numOfMoves)
431 bestIndex = find_best_index();
435 move = moves[bestIndex].move;
436 moves[bestIndex] = moves[movesPicked++];
442 case PH_BAD_CAPTURES:
443 assert(!pos.is_check());
444 assert(badCapturesPicked >= 0);
445 // It's probably a good idea to use SEE move ordering here, instead
446 // of just picking the first move. FIXME
447 while (badCapturesPicked < numOfBadCaptures)
449 move = badCaptures[badCapturesPicked++].move;
451 && move != mateKiller
452 && pos.pl_move_is_legal(move, pinned))
458 assert(!pos.is_check());
459 assert(movesPicked >= 0);
460 while (movesPicked < numOfMoves)
462 bestIndex = (movesPicked < 4 ? find_best_index() : movesPicked);
466 move = moves[bestIndex].move;
467 moves[bestIndex] = moves[movesPicked++];
468 // Remember to change the line below if we decide to hash the qsearch!
469 // Maybe also postpone the legality check until after futility pruning?
470 if (/* move != ttMove && */ pos.pl_move_is_legal(move, pinned))
477 assert(!pos.is_check());
478 assert(movesPicked >= 0);
479 // Perhaps we should do something better than just picking the first
481 while (movesPicked < numOfMoves)
483 move = moves[movesPicked++].move;
484 // Remember to change the line below if we decide to hash the qsearch!
485 if (/* move != ttMove && */ pos.pl_move_is_legal(move, pinned))
497 /// MovePicker::current_move_type() returns the type of the just
498 /// picked next move. It can be used in search to further differentiate
499 /// according to the current move type: capture, non capture, escape, etc.
500 MovePicker::MovegenPhase MovePicker::current_move_type() const {
502 return PhaseTable[phaseIndex];
506 /// MovePicker::init_phase_table() initializes the PhaseTable[],
507 /// MainSearchPhaseIndex, EvasionPhaseIndex, QsearchWithChecksPhaseIndex
508 /// QsearchNoCapturesPhaseIndex, QsearchWithoutChecksPhaseIndex and
509 /// NoMovesPhaseIndex variables. It is only called once during program
510 /// startup, and never again while the program is running.
512 void MovePicker::init_phase_table() {
517 MainSearchPhaseIndex = i - 1;
518 PhaseTable[i++] = PH_TT_MOVE;
519 PhaseTable[i++] = PH_MATE_KILLER;
520 PhaseTable[i++] = PH_GOOD_CAPTURES;
521 // PH_KILLER_1 and PH_KILLER_2 are not yet used.
522 // PhaseTable[i++] = PH_KILLER_1;
523 // PhaseTable[i++] = PH_KILLER_2;
524 PhaseTable[i++] = PH_NONCAPTURES;
525 PhaseTable[i++] = PH_BAD_CAPTURES;
526 PhaseTable[i++] = PH_STOP;
529 EvasionsPhaseIndex = i - 1;
530 PhaseTable[i++] = PH_EVASIONS;
531 PhaseTable[i++] = PH_STOP;
533 // Quiescence search with checks
534 QsearchWithChecksPhaseIndex = i - 1;
535 PhaseTable[i++] = PH_QCAPTURES;
536 PhaseTable[i++] = PH_QCHECKS;
537 PhaseTable[i++] = PH_STOP;
539 // Quiescence search with checks only and no captures
540 QsearchNoCapturesPhaseIndex = i - 1;
541 PhaseTable[i++] = PH_QCHECKS;
542 PhaseTable[i++] = PH_STOP;
544 // Quiescence search without checks
545 QsearchWithoutChecksPhaseIndex = i - 1;
546 PhaseTable[i++] = PH_QCAPTURES;
547 PhaseTable[i++] = PH_STOP;
549 // Do not generate any move
550 NoMovesPhaseIndex = i - 1;
551 PhaseTable[i++] = PH_STOP;