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_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. There is also a second version that
304 /// lowers the priority of moves that attack the same square,
305 /// so that if the best move that attack a square fails the next
306 /// move picked attacks a different square if any, not the same one.
308 int MovePicker::find_best_index() {
310 assert(movesPicked < numOfMoves);
312 int bestIndex = movesPicked, bestScore = moves[movesPicked].score;
314 for (int i = movesPicked + 1; i < numOfMoves; i++)
315 if (moves[i].score > bestScore)
318 bestScore = moves[i].score;
323 int MovePicker::find_best_index(Bitboard* squares, int values[]) {
325 assert(movesPicked < numOfMoves);
330 int bestScore = -10000000, bestIndex = -1;
332 for (int i = movesPicked; i < numOfMoves; i++)
337 if (!bit_is_set(*squares, to))
340 set_bit(squares, to);
344 hs = moves[i].score - values[to];
354 // Raise value of the picked square, so next attack
355 // to the same square will get low priority.
356 to = move_to(moves[bestIndex].move);
363 /// MovePicker::pick_move_from_list() picks the move with the biggest score
364 /// from a list of generated moves (moves[] or badCaptures[], depending on
365 /// the current move generation phase). It takes care not to return the
366 /// transposition table move if that has already been serched previously.
368 Move MovePicker::pick_move_from_list() {
373 switch (PhaseTable[phaseIndex]) {
375 case PH_GOOD_CAPTURES:
376 assert(!pos.is_check());
377 assert(movesPicked >= 0);
379 while (movesPicked < numOfMoves)
381 bestIndex = find_best_index();
382 move = moves[bestIndex].move;
383 moves[bestIndex] = moves[movesPicked++];
385 && move != mateKiller
386 && pos.pl_move_is_legal(move, pinned))
392 assert(!pos.is_check());
393 assert(movesPicked >= 0);
395 while (movesPicked < numOfMoves)
397 // If this is a PV node or we have only picked a few moves, scan
398 // the entire move list for the best move. If many moves have already
399 // been searched and it is not a PV node, we are probably failing low
400 // anyway, so we just pick the first move from the list.
401 bestIndex = (pvNode || movesPicked < 12) ? find_best_index() : movesPicked;
402 move = moves[bestIndex].move;
403 moves[bestIndex] = moves[movesPicked++];
405 && move != mateKiller
406 && pos.pl_move_is_legal(move, pinned))
412 assert(pos.is_check());
413 assert(movesPicked >= 0);
415 while (movesPicked < numOfMoves)
417 bestIndex = find_best_index();
418 move = moves[bestIndex].move;
419 moves[bestIndex] = moves[movesPicked++];
424 case PH_BAD_CAPTURES:
425 assert(!pos.is_check());
426 assert(movesPicked >= 0);
427 // It's probably a good idea to use SEE move ordering here, instead
428 // of just picking the first move. FIXME
429 while (movesPicked < numOfBadCaptures)
431 move = badCaptures[movesPicked++].move;
433 && move != mateKiller
434 && pos.pl_move_is_legal(move, pinned))
440 assert(!pos.is_check());
441 assert(movesPicked >= 0);
442 while (movesPicked < numOfMoves)
444 bestIndex = (movesPicked < 4 ? find_best_index() : movesPicked);
445 move = moves[bestIndex].move;
446 moves[bestIndex] = moves[movesPicked++];
447 // Maybe postpone the legality check until after futility pruning?
449 && pos.pl_move_is_legal(move, pinned))
455 assert(!pos.is_check());
456 assert(movesPicked >= 0);
457 // Perhaps we should do something better than just picking the first
459 while (movesPicked < numOfMoves)
461 move = moves[movesPicked++].move;
463 && pos.pl_move_is_legal(move, pinned))
475 /// MovePicker::init_phase_table() initializes the PhaseTable[],
476 /// MainSearchPhaseIndex, EvasionPhaseIndex, QsearchWithChecksPhaseIndex
477 /// and QsearchWithoutChecksPhaseIndex. It is only called once during
478 /// program startup, and never again while the program is running.
480 void MovePicker::init_phase_table() {
485 MainSearchPhaseIndex = i - 1;
486 PhaseTable[i++] = PH_TT_MOVE;
487 PhaseTable[i++] = PH_MATE_KILLER;
488 PhaseTable[i++] = PH_GOOD_CAPTURES;
489 // PH_KILLER_1 and PH_KILLER_2 are not yet used.
490 // PhaseTable[i++] = PH_KILLER_1;
491 // PhaseTable[i++] = PH_KILLER_2;
492 PhaseTable[i++] = PH_NONCAPTURES;
493 PhaseTable[i++] = PH_BAD_CAPTURES;
494 PhaseTable[i++] = PH_STOP;
497 EvasionsPhaseIndex = i - 1;
498 PhaseTable[i++] = PH_EVASIONS;
499 PhaseTable[i++] = PH_STOP;
501 // Quiescence search with checks
502 QsearchWithChecksPhaseIndex = i - 1;
503 PhaseTable[i++] = PH_TT_MOVE;
504 PhaseTable[i++] = PH_QCAPTURES;
505 PhaseTable[i++] = PH_QCHECKS;
506 PhaseTable[i++] = PH_STOP;
508 // Quiescence search without checks
509 QsearchWithoutChecksPhaseIndex = i - 1;
510 PhaseTable[i++] = PH_TT_MOVE;
511 PhaseTable[i++] = PH_QCAPTURES;
512 PhaseTable[i++] = PH_STOP;