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-2009 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/>.
38 //// Local definitions
45 MovePicker::MovegenPhase PhaseTable[32];
46 int MainSearchPhaseIndex;
47 int EvasionsPhaseIndex;
48 int QsearchWithChecksPhaseIndex;
49 int QsearchWithoutChecksPhaseIndex;
59 /// Constructor for the MovePicker class. Apart from the position for which
60 /// it is asked to pick legal moves, MovePicker also wants some information
61 /// to help it to return the presumably good moves first, to decide which
62 /// moves to return (in the quiescence search, for instance, we only want to
63 /// search captures, promotions and some checks) and about how important good
64 /// move ordering is at the current node.
66 MovePicker::MovePicker(const Position& p, bool pv, Move ttm,
67 const SearchStack& ss, Depth d) : pos(p) {
70 mateKiller = (ss.mateKiller == ttm)? MOVE_NONE : ss.mateKiller;
71 killer1 = ss.killers[0];
72 killer2 = ss.killers[1];
77 checkKillers = checkLegal = false;
80 phaseIndex = EvasionsPhaseIndex;
81 else if (depth > Depth(0))
82 phaseIndex = MainSearchPhaseIndex;
83 else if (depth == Depth(0))
84 phaseIndex = QsearchWithChecksPhaseIndex;
86 phaseIndex = QsearchWithoutChecksPhaseIndex;
88 Color us = pos.side_to_move();
90 dc = p.discovered_check_candidates(us);
91 pinned = p.pinned_pieces(us);
97 /// MovePicker::get_next_move() is the most important method of the MovePicker
98 /// class. It returns a new legal move every time it is called, until there
99 /// are no more moves left of the types we are interested in.
101 Move MovePicker::get_next_move() {
107 // If we already have a list of generated moves, pick the best move from
108 // the list, and return it.
109 move = pick_move_from_list();
110 if (move != MOVE_NONE)
112 assert(move_is_ok(move));
118 switch (PhaseTable[phaseIndex]) {
121 if (ttMove != MOVE_NONE)
123 assert(move_is_ok(ttMove));
124 if (move_is_legal(pos, ttMove, pinned))
130 if (mateKiller != MOVE_NONE)
132 assert(move_is_ok(mateKiller));
133 if (move_is_legal(pos, mateKiller, pinned))
138 case PH_GOOD_CAPTURES:
139 numOfMoves = generate_captures(pos, moves);
141 std::sort(moves, moves + numOfMoves);
147 movesPicked = numOfMoves = 0;
149 if (killer1 != MOVE_NONE && move_is_legal(pos, killer1, pinned) && !pos.move_is_capture(killer1))
150 moves[numOfMoves++].move = killer1;
151 if (killer2 != MOVE_NONE && move_is_legal(pos, killer2, pinned) && !pos.move_is_capture(killer2) )
152 moves[numOfMoves++].move = killer2;
156 checkKillers = (numOfMoves != 0); // previous phase is PH_KILLERS
157 numOfMoves = generate_noncaptures(pos, moves);
159 std::sort(moves, moves + numOfMoves);
164 case PH_BAD_CAPTURES:
165 // It's probably a good idea to use SEE move ordering here. FIXME
170 assert(pos.is_check());
171 numOfMoves = generate_evasions(pos, moves, pinned);
173 std::sort(moves, moves + numOfMoves);
178 numOfMoves = generate_captures(pos, moves);
180 std::sort(moves, moves + numOfMoves);
185 // Perhaps we should order moves move here? FIXME
186 numOfMoves = generate_non_capture_checks(pos, moves, dc);
201 /// A variant of get_next_move() which takes a lock as a parameter, used to
202 /// prevent multiple threads from picking the same move at a split point.
204 Move MovePicker::get_next_move(Lock &lock) {
212 Move m = get_next_move();
221 /// MovePicker::score_captures(), MovePicker::score_noncaptures(),
222 /// MovePicker::score_evasions() and MovePicker::score_qcaptures() assign a
223 /// numerical move ordering score to each move in a move list. The moves
224 /// with highest scores will be picked first by pick_move_from_list().
226 void MovePicker::score_captures() {
227 // Winning and equal captures in the main search are ordered by MVV/LVA.
228 // Suprisingly, this appears to perform slightly better than SEE based
229 // move ordering. The reason is probably that in a position with a winning
230 // capture, capturing a more valuable (but sufficiently defended) piece
231 // first usually doesn't hurt. The opponent will have to recapture, and
232 // the hanging piece will still be hanging (except in the unusual cases
233 // where it is possible to recapture with the hanging piece). Exchanging
234 // big pieces before capturing a hanging piece probably helps to reduce
236 // While scoring captures it moves all captures with negative SEE values
237 // to the badCaptures[] array.
241 for (int i = 0; i < numOfMoves; i++)
244 seeValue = pos.see(m);
247 if (move_promotion(m))
248 moves[i].score = QueenValueMidgame;
250 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
251 -int(pos.type_of_piece_on(move_from(m)));
255 // Losing capture, move it to the badCaptures[] array
256 assert(numOfBadCaptures < 63);
257 moves[i].score = seeValue;
258 badCaptures[numOfBadCaptures++] = moves[i];
259 moves[i--] = moves[--numOfMoves];
264 void MovePicker::score_noncaptures() {
265 // First score by history, when no history is available then use
266 // piece/square tables values. This seems to be better then a
267 // random choice when we don't have an history for any move.
271 for (int i = 0; i < numOfMoves; i++)
274 hs = H.move_ordering_score(pos.piece_on(move_from(m)), move_to(m));
276 // Ensure history is always preferred to pst
281 moves[i].score = hs + pos.mg_pst_delta(m);
285 void MovePicker::score_evasions() {
287 for (int i = 0; i < numOfMoves; i++)
289 Move m = moves[i].move;
291 moves[i].score = 2*HistoryMax;
292 else if (!pos.square_is_empty(move_to(m)))
294 int seeScore = pos.see(m);
295 moves[i].score = (seeScore >= 0)? seeScore + HistoryMax : seeScore;
297 moves[i].score = H.move_ordering_score(pos.piece_on(move_from(m)), move_to(m));
301 void MovePicker::score_qcaptures() {
303 // Use MVV/LVA ordering
304 for (int i = 0; i < numOfMoves; i++)
306 Move m = moves[i].move;
307 if (move_promotion(m))
308 moves[i].score = QueenValueMidgame;
310 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
311 -int(pos.type_of_piece_on(move_from(m)));
316 /// MovePicker::pick_move_from_list() picks the move with the biggest score
317 /// from a list of generated moves (moves[] or badCaptures[], depending on
318 /// the current move generation phase). It takes care not to return the
319 /// transposition table move if that has already been serched previously.
321 Move MovePicker::pick_move_from_list() {
323 assert(movesPicked >= 0);
324 assert(!pos.is_check() || PhaseTable[phaseIndex] == PH_EVASIONS || PhaseTable[phaseIndex] == PH_STOP);
325 assert( pos.is_check() || PhaseTable[phaseIndex] != PH_EVASIONS);
327 switch (PhaseTable[phaseIndex]) {
329 case PH_GOOD_CAPTURES:
332 while (movesPicked < numOfMoves)
334 Move move = moves[movesPicked++].move;
336 && move != mateKiller
337 && (!checkKillers || (move != killer1 && move != killer2))
338 && (!checkLegal || pos.pl_move_is_legal(move, pinned)))
344 if (movesPicked < numOfMoves)
345 return moves[movesPicked++].move;
349 case PH_BAD_CAPTURES:
350 while (movesPicked < numOfBadCaptures)
352 Move move = badCaptures[movesPicked++].move;
354 && move != mateKiller
355 && pos.pl_move_is_legal(move, pinned))
362 while (movesPicked < numOfMoves)
364 Move move = moves[movesPicked++].move;
365 // Maybe postpone the legality check until after futility pruning?
367 && pos.pl_move_is_legal(move, pinned))
379 /// MovePicker::init_phase_table() initializes the PhaseTable[],
380 /// MainSearchPhaseIndex, EvasionPhaseIndex, QsearchWithChecksPhaseIndex
381 /// and QsearchWithoutChecksPhaseIndex. It is only called once during
382 /// program startup, and never again while the program is running.
384 void MovePicker::init_phase_table() {
389 MainSearchPhaseIndex = i - 1;
390 PhaseTable[i++] = PH_TT_MOVE;
391 PhaseTable[i++] = PH_MATE_KILLER;
392 PhaseTable[i++] = PH_GOOD_CAPTURES;
393 PhaseTable[i++] = PH_KILLERS;
394 PhaseTable[i++] = PH_NONCAPTURES;
395 PhaseTable[i++] = PH_BAD_CAPTURES;
396 PhaseTable[i++] = PH_STOP;
399 EvasionsPhaseIndex = i - 1;
400 PhaseTable[i++] = PH_EVASIONS;
401 PhaseTable[i++] = PH_STOP;
403 // Quiescence search with checks
404 QsearchWithChecksPhaseIndex = i - 1;
405 PhaseTable[i++] = PH_TT_MOVE;
406 PhaseTable[i++] = PH_QCAPTURES;
407 PhaseTable[i++] = PH_QCHECKS;
408 PhaseTable[i++] = PH_STOP;
410 // Quiescence search without checks
411 QsearchWithoutChecksPhaseIndex = i - 1;
412 PhaseTable[i++] = PH_TT_MOVE;
413 PhaseTable[i++] = PH_QCAPTURES;
414 PhaseTable[i++] = PH_STOP;