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.
11 Stockfish is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
35 //// Local definitions
42 MovePicker::MovegenPhase PhaseTable[32];
43 int MainSearchPhaseIndex;
44 int EvasionsPhaseIndex;
45 int QsearchWithChecksPhaseIndex;
46 int QsearchWithoutChecksPhaseIndex;
57 /// Constructor for the MovePicker class. Apart from the position for which
58 /// it is asked to pick legal moves, MovePicker also wants some information
59 /// to help it to return the presumably good moves first, to decide which
60 /// moves to return (in the quiescence search, for instance, we only want to
61 /// search captures, promotions and some checks) and about how important good
62 /// move ordering is at the current node.
64 MovePicker::MovePicker(const Position& p, bool pvnode, Move ttm, Move mk,
65 Move k1, Move k2, Depth d) : pos(p) {
68 mateKiller = (mk == ttm)? MOVE_NONE : mk;
75 dc = p.discovered_check_candidates(p.side_to_move());
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 pinned = p.pinned_pieces(p.side_to_move());
92 /// MovePicker::get_next_move() is the most important method of the MovePicker
93 /// class. It returns a new legal move every time it is called, until there
94 /// are no more moves left of the types we are interested in.
96 Move MovePicker::get_next_move() {
102 // If we already have a list of generated moves, pick the best move from
103 // the list, and return it.
104 move = pick_move_from_list();
105 if (move != MOVE_NONE)
107 assert(move_is_ok(move));
113 switch (PhaseTable[phaseIndex]) {
116 if (ttMove != MOVE_NONE)
118 assert(move_is_ok(ttMove));
119 if (move_is_legal(pos, ttMove, pinned))
125 if (mateKiller != MOVE_NONE)
127 assert(move_is_ok(mateKiller));
128 if (move_is_legal(pos, mateKiller, pinned))
133 case PH_GOOD_CAPTURES:
134 numOfMoves = generate_captures(pos, moves);
139 case PH_BAD_CAPTURES:
140 badCapturesPicked = 0;
144 numOfMoves = generate_noncaptures(pos, moves);
150 assert(pos.is_check());
151 numOfMoves = generate_evasions(pos, moves);
157 numOfMoves = generate_captures(pos, moves);
163 numOfMoves = generate_checks(pos, moves, dc);
178 /// A variant of get_next_move() which takes a lock as a parameter, used to
179 /// prevent multiple threads from picking the same move at a split point.
181 Move MovePicker::get_next_move(Lock &lock) {
189 Move m = get_next_move();
198 /// MovePicker::score_captures(), MovePicker::score_noncaptures(),
199 /// MovePicker::score_evasions() and MovePicker::score_qcaptures() assign a
200 /// numerical move ordering score to each move in a move list. The moves
201 /// with highest scores will be picked first by pick_move_from_list().
203 void MovePicker::score_captures() {
204 // Winning and equal captures in the main search are ordered by MVV/LVA.
205 // Suprisingly, this appears to perform slightly better than SEE based
206 // move ordering. The reason is probably that in a position with a winning
207 // capture, capturing a more valuable (but sufficiently defended) piece
208 // first usually doesn't hurt. The opponent will have to recapture, and
209 // the hanging piece will still be hanging (except in the unusual cases
210 // where it is possible to recapture with the hanging piece). Exchanging
211 // big pieces before capturing a hanging piece probably helps to reduce
213 for (int i = 0; i < numOfMoves; i++)
215 int seeValue = pos.see(moves[i].move);
218 if (move_promotion(moves[i].move))
219 moves[i].score = QueenValueMidgame;
221 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(moves[i].move)))
222 -int(pos.type_of_piece_on(move_from(moves[i].move)));
224 moves[i].score = seeValue;
228 void MovePicker::score_noncaptures() {
229 // First score by history, when no history is available then use
230 // piece/square tables values. This seems to be better then a
231 // random choice when we don't have an history for any move.
235 for (int i = 0; i < numOfMoves; i++)
\r
240 hs = HistoryMax + 2;
\r
241 else if (m == killer2)
\r
242 hs = HistoryMax + 1;
\r
244 hs = H.move_ordering_score(pos.piece_on(move_from(m)), m);
\r
246 // Ensure moves in history are always sorted as first
\r
250 moves[i].score = hs + pos.mg_pst_delta(m);
\r
254 void MovePicker::score_evasions() {
256 for (int i = 0; i < numOfMoves; i++)
258 Move m = moves[i].move;
260 moves[i].score = 2*HistoryMax;
261 else if (!pos.square_is_empty(move_to(m)))
263 int seeScore = pos.see(m);
264 moves[i].score = (seeScore >= 0)? seeScore + HistoryMax : seeScore;
266 moves[i].score = H.move_ordering_score(pos.piece_on(move_from(m)), m);
268 // FIXME try psqt also here
271 void MovePicker::score_qcaptures() {
273 // Use MVV/LVA ordering
274 for (int i = 0; i < numOfMoves; i++)
276 Move m = moves[i].move;
277 if (move_promotion(m))
278 moves[i].score = QueenValueMidgame;
280 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
281 -int(pos.midgame_value_of_piece_on(move_to(m))) / 64;
286 /// find_best_index() loops across the moves and returns index of
287 /// the highest scored one.
289 int MovePicker::find_best_index() {
291 int bestScore = -10000000, bestIndex = -1;
293 for (int i = movesPicked; i < numOfMoves; i++)
294 if (moves[i].score > bestScore)
297 bestScore = moves[i].score;
303 /// MovePicker::pick_move_from_list() picks the move with the biggest score
304 /// from a list of generated moves (moves[] or badCaptures[], depending on
305 /// the current move generation phase). It takes care not to return the
306 /// transposition table move if that has already been serched previously.
307 /// While picking captures in the PH_GOOD_CAPTURES phase (i.e. while picking
308 /// non-losing captures in the main search), it moves all captures with
309 /// negative SEE values to the badCaptures[] array.
311 Move MovePicker::pick_move_from_list() {
316 switch (PhaseTable[phaseIndex]) {
317 case PH_GOOD_CAPTURES:
318 assert(!pos.is_check());
319 assert(movesPicked >= 0);
321 while (movesPicked < numOfMoves)
323 int bestScore = -10000000;
325 for (int i = movesPicked; i < numOfMoves; i++)
327 if (moves[i].score < 0)
329 // Losing capture, move it to the badCaptures[] array
330 assert(numOfBadCaptures < 63);
331 badCaptures[numOfBadCaptures++] = moves[i];
332 moves[i--] = moves[--numOfMoves];
334 else if (moves[i].score > bestScore)
337 bestScore = moves[i].score;
340 if (bestIndex != -1) // Found a good capture
342 move = moves[bestIndex].move;
343 moves[bestIndex] = moves[movesPicked++];
345 && move != mateKiller
346 && pos.pl_move_is_legal(move, pinned))
353 assert(!pos.is_check());
354 assert(movesPicked >= 0);
356 while (movesPicked < numOfMoves)
358 // If this is a PV node or we have only picked a few moves, scan
359 // the entire move list for the best move. If many moves have already
360 // been searched and it is not a PV node, we are probably failing low
361 // anyway, so we just pick the first move from the list.
362 bestIndex = (pvNode || movesPicked < 12) ? find_best_index() : movesPicked;
366 move = moves[bestIndex].move;
367 moves[bestIndex] = moves[movesPicked++];
369 && move != mateKiller
370 && pos.pl_move_is_legal(move, pinned))
377 assert(pos.is_check());
378 assert(movesPicked >= 0);
380 while (movesPicked < numOfMoves)
382 bestIndex = find_best_index();
386 move = moves[bestIndex].move;
387 moves[bestIndex] = moves[movesPicked++];
393 case PH_BAD_CAPTURES:
394 assert(!pos.is_check());
395 assert(badCapturesPicked >= 0);
396 // It's probably a good idea to use SEE move ordering here, instead
397 // of just picking the first move. FIXME
398 while (badCapturesPicked < numOfBadCaptures)
400 move = badCaptures[badCapturesPicked++].move;
402 && move != mateKiller
403 && pos.pl_move_is_legal(move, pinned))
409 assert(!pos.is_check());
410 assert(movesPicked >= 0);
411 while (movesPicked < numOfMoves)
413 bestIndex = (movesPicked < 4 ? find_best_index() : movesPicked);
417 move = moves[bestIndex].move;
418 moves[bestIndex] = moves[movesPicked++];
419 // Remember to change the line below if we decide to hash the qsearch!
420 // Maybe also postpone the legality check until after futility pruning?
421 if (/* move != ttMove && */ pos.pl_move_is_legal(move, pinned))
428 assert(!pos.is_check());
429 assert(movesPicked >= 0);
430 // Perhaps we should do something better than just picking the first
432 while (movesPicked < numOfMoves)
434 move = moves[movesPicked++].move;
435 // Remember to change the line below if we decide to hash the qsearch!
436 if (/* move != ttMove && */ pos.pl_move_is_legal(move, pinned))
448 /// MovePicker::current_move_type() returns the type of the just
449 /// picked next move. It can be used in search to further differentiate
450 /// according to the current move type: capture, non capture, escape, etc.
451 MovePicker::MovegenPhase MovePicker::current_move_type() const {
453 return PhaseTable[phaseIndex];
457 /// MovePicker::init_phase_table() initializes the PhaseTable[],
458 /// MainSearchPhaseIndex, EvasionPhaseIndex, QsearchWithChecksPhaseIndex
459 /// and QsearchWithoutChecksPhaseIndex variables. It is only called once
460 /// during program startup, and never again while the program is running.
462 void MovePicker::init_phase_table() {
467 MainSearchPhaseIndex = i - 1;
468 PhaseTable[i++] = PH_TT_MOVE;
469 PhaseTable[i++] = PH_MATE_KILLER;
470 PhaseTable[i++] = PH_GOOD_CAPTURES;
471 // PH_KILLER_1 and PH_KILLER_2 are not yet used.
472 // PhaseTable[i++] = PH_KILLER_1;
473 // PhaseTable[i++] = PH_KILLER_2;
474 PhaseTable[i++] = PH_NONCAPTURES;
475 PhaseTable[i++] = PH_BAD_CAPTURES;
476 PhaseTable[i++] = PH_STOP;
479 EvasionsPhaseIndex = i - 1;
480 PhaseTable[i++] = PH_EVASIONS;
481 PhaseTable[i++] = PH_STOP;
483 // Quiescence search with checks
484 QsearchWithChecksPhaseIndex = i - 1;
485 PhaseTable[i++] = PH_QCAPTURES;
486 PhaseTable[i++] = PH_QCHECKS;
487 PhaseTable[i++] = PH_STOP;
489 // Quiescence search without checks
490 QsearchWithoutChecksPhaseIndex = i - 1;
491 PhaseTable[i++] = PH_QCAPTURES;
492 PhaseTable[i++] = PH_STOP;