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/>.
36 //// Local definitions
43 MovePicker::MovegenPhase PhaseTable[32];
44 int MainSearchPhaseIndex;
45 int EvasionsPhaseIndex;
46 int QsearchWithChecksPhaseIndex;
47 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 pvnode, Move ttm, Move mk,
66 Move k1, Move k2, Depth d) : pos(p) {
69 mateKiller = (mk == ttm)? MOVE_NONE : mk;
76 dc = p.discovered_check_candidates(p.side_to_move());
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 pinned = p.pinned_pieces(p.side_to_move());
93 /// MovePicker::get_next_move() is the most important method of the MovePicker
94 /// class. It returns a new legal move every time it is called, until there
95 /// are no more moves left of the types we are interested in.
97 Move MovePicker::get_next_move() {
103 // If we already have a list of generated moves, pick the best move from
104 // the list, and return it.
105 move = pick_move_from_list();
106 if (move != MOVE_NONE)
108 assert(move_is_ok(move));
114 switch (PhaseTable[phaseIndex]) {
117 if (ttMove != MOVE_NONE)
119 assert(move_is_ok(ttMove));
120 if (move_is_legal(pos, ttMove, pinned))
126 if (mateKiller != MOVE_NONE)
128 assert(move_is_ok(mateKiller));
129 if (move_is_legal(pos, mateKiller, pinned))
134 case PH_GOOD_CAPTURES:
135 numOfMoves = generate_captures(pos, moves);
140 case PH_BAD_CAPTURES:
141 badCapturesPicked = 0;
145 numOfMoves = generate_noncaptures(pos, moves);
151 assert(pos.is_check());
152 numOfMoves = generate_evasions(pos, moves);
158 numOfMoves = generate_captures(pos, moves);
164 numOfMoves = generate_checks(pos, moves, dc);
179 /// A variant of get_next_move() which takes a lock as a parameter, used to
180 /// prevent multiple threads from picking the same move at a split point.
182 Move MovePicker::get_next_move(Lock &lock) {
190 Move m = get_next_move();
199 /// MovePicker::score_captures(), MovePicker::score_noncaptures(),
200 /// MovePicker::score_evasions() and MovePicker::score_qcaptures() assign a
201 /// numerical move ordering score to each move in a move list. The moves
202 /// with highest scores will be picked first by pick_move_from_list().
204 void MovePicker::score_captures() {
205 // Winning and equal captures in the main search are ordered by MVV/LVA.
206 // Suprisingly, this appears to perform slightly better than SEE based
207 // move ordering. The reason is probably that in a position with a winning
208 // capture, capturing a more valuable (but sufficiently defended) piece
209 // first usually doesn't hurt. The opponent will have to recapture, and
210 // the hanging piece will still be hanging (except in the unusual cases
211 // where it is possible to recapture with the hanging piece). Exchanging
212 // big pieces before capturing a hanging piece probably helps to reduce
213 // the subtree size. Instead of calculating SEE here to filter out
214 // loosing captures, we delay the filtering in pick_move_from_list()
217 for (int i = 0; i < numOfMoves; i++)
220 if (move_promotion(m))
221 moves[i].score = QueenValueMidgame;
223 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
224 -int(pos.type_of_piece_on(move_from(m)));
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++)
241 else if (m == killer2)
244 hs = H.move_ordering_score(pos.piece_on(move_from(m)), m);
246 // Ensure moves in history are always sorted as first
250 moves[i].score = hs + pos.mg_pst_delta(m);
254 void MovePicker::score_evasions() {
\r
259 // Use MVV/LVA ordering
\r
260 for (int i = 0; i < numOfMoves; i++)
\r
266 else if (!pos.square_is_empty(move_to(m)))
\r
267 hs = int(pos.midgame_value_of_piece_on(move_to(m)))
\r
268 -int(pos.type_of_piece_on(move_from(m))) + HistoryMax;
\r
270 hs = H.move_ordering_score(pos.piece_on(move_from(m)), m);
\r
272 moves[i].score = hs;
\r
276 void MovePicker::score_qcaptures() {
278 // Use MVV/LVA ordering
279 for (int i = 0; i < numOfMoves; i++)
281 Move m = moves[i].move;
282 if (move_promotion(m))
283 moves[i].score = QueenValueMidgame;
285 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
286 -int(pos.type_of_piece_on(move_from(m)));
291 /// find_best_index() loops across the moves and returns index of
292 /// the highest scored one.
294 int MovePicker::find_best_index() {
296 int bestScore = -10000000, bestIndex = -1;
298 for (int i = movesPicked; i < numOfMoves; i++)
299 if (moves[i].score > bestScore)
302 bestScore = moves[i].score;
308 /// MovePicker::pick_move_from_list() picks the move with the biggest score
309 /// from a list of generated moves (moves[] or badCaptures[], depending on
310 /// the current move generation phase). It takes care not to return the
311 /// transposition table move if that has already been serched previously.
312 /// While picking captures in the PH_GOOD_CAPTURES phase (i.e. while picking
313 /// non-losing captures in the main search), it moves all captures with
314 /// negative SEE values to the badCaptures[] array.
316 Move MovePicker::pick_move_from_list() {
321 switch (PhaseTable[phaseIndex]) {
322 case PH_GOOD_CAPTURES:
323 assert(!pos.is_check());
324 assert(movesPicked >= 0);
326 while (movesPicked < numOfMoves)
328 bestIndex = find_best_index();
330 if (bestIndex != -1) // Found a possibly good capture
332 move = moves[bestIndex].move;
333 int seeValue = pos.see(move);
336 // Losing capture, move it to the badCaptures[] array
337 assert(numOfBadCaptures < 63);
338 moves[bestIndex].score = seeValue;
339 badCaptures[numOfBadCaptures++] = moves[bestIndex];
340 moves[bestIndex] = moves[--numOfMoves];
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;