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, Move ttm, Depth d,
67 const History& h, SearchStack* ss) : pos(p), H(h) {
71 mateKiller = (ss->mateKiller == ttm)? MOVE_NONE : ss->mateKiller;
72 killer1 = ss->killers[0];
73 killer2 = ss->killers[1];
75 mateKiller = killer1 = killer2 = MOVE_NONE;
77 movesPicked = numOfMoves = numOfBadCaptures = 0;
78 checkKillers = checkLegal = finished = false;
81 phaseIndex = EvasionsPhaseIndex;
82 else if (d > Depth(0))
83 phaseIndex = MainSearchPhaseIndex;
84 else if (d == Depth(0))
85 phaseIndex = QsearchWithChecksPhaseIndex;
87 phaseIndex = QsearchWithoutChecksPhaseIndex;
89 Color us = pos.side_to_move();
91 dc = p.discovered_check_candidates(us);
92 pinned = p.pinned_pieces(us);
98 /// MovePicker::get_next_move() is the most important method of the MovePicker
99 /// class. It returns a new legal move every time it is called, until there
100 /// are no more moves left of the types we are interested in.
102 Move MovePicker::get_next_move() {
108 // If we already have a list of generated moves, pick the best move from
109 // the list, and return it.
110 move = pick_move_from_list();
111 if (move != MOVE_NONE)
113 assert(move_is_ok(move));
119 switch (PhaseTable[phaseIndex]) {
122 if (ttMove != MOVE_NONE)
124 assert(move_is_ok(ttMove));
125 if (move_is_legal(pos, ttMove, pinned))
131 if (mateKiller != MOVE_NONE)
133 assert(move_is_ok(mateKiller));
134 if (move_is_legal(pos, mateKiller, pinned))
139 case PH_GOOD_CAPTURES:
140 numOfMoves = generate_captures(pos, moves);
142 std::sort(moves, moves + numOfMoves);
148 movesPicked = numOfMoves = 0;
150 if (killer1 != MOVE_NONE && move_is_legal(pos, killer1, pinned) && !pos.move_is_capture(killer1))
151 moves[numOfMoves++].move = killer1;
152 if (killer2 != MOVE_NONE && move_is_legal(pos, killer2, pinned) && !pos.move_is_capture(killer2) )
153 moves[numOfMoves++].move = killer2;
157 checkKillers = (numOfMoves != 0); // previous phase is PH_KILLERS
158 numOfMoves = generate_noncaptures(pos, moves);
160 std::sort(moves, moves + numOfMoves);
165 case PH_BAD_CAPTURES:
166 // Bad captures SEE value is already calculated by score_captures()
167 // so just sort them to get SEE move ordering.
168 std::sort(badCaptures, badCaptures + numOfBadCaptures);
173 assert(pos.is_check());
174 numOfMoves = generate_evasions(pos, moves, pinned);
176 std::sort(moves, moves + numOfMoves);
181 numOfMoves = generate_captures(pos, moves);
183 std::sort(moves, moves + numOfMoves);
188 // Perhaps we should order moves move here? FIXME
189 numOfMoves = generate_non_capture_checks(pos, moves, dc);
204 /// A variant of get_next_move() which takes a lock as a parameter, used to
205 /// prevent multiple threads from picking the same move at a split point.
207 Move MovePicker::get_next_move(Lock &lock) {
215 Move m = get_next_move();
224 /// MovePicker::score_captures(), MovePicker::score_noncaptures(),
225 /// MovePicker::score_evasions() and MovePicker::score_qcaptures() assign a
226 /// numerical move ordering score to each move in a move list. The moves
227 /// with highest scores will be picked first by pick_move_from_list().
229 void MovePicker::score_captures() {
230 // Winning and equal captures in the main search are ordered by MVV/LVA.
231 // Suprisingly, this appears to perform slightly better than SEE based
232 // move ordering. The reason is probably that in a position with a winning
233 // capture, capturing a more valuable (but sufficiently defended) piece
234 // first usually doesn't hurt. The opponent will have to recapture, and
235 // the hanging piece will still be hanging (except in the unusual cases
236 // where it is possible to recapture with the hanging piece). Exchanging
237 // big pieces before capturing a hanging piece probably helps to reduce
239 // While scoring captures it moves all captures with negative SEE values
240 // to the badCaptures[] array.
244 for (int i = 0; i < numOfMoves; i++)
247 seeValue = pos.see(m);
250 if (move_promotion(m))
251 moves[i].score = QueenValueMidgame;
253 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
254 -int(pos.type_of_piece_on(move_from(m)));
258 // Losing capture, move it to the badCaptures[] array
259 assert(numOfBadCaptures < 63);
260 moves[i].score = seeValue;
261 badCaptures[numOfBadCaptures++] = moves[i];
262 moves[i--] = moves[--numOfMoves];
267 void MovePicker::score_noncaptures() {
268 // First score by history, when no history is available then use
269 // piece/square tables values. This seems to be better then a
270 // random choice when we don't have an history for any move.
274 for (int i = 0; i < numOfMoves; i++)
277 hs = H.move_ordering_score(pos.piece_on(move_from(m)), move_to(m));
279 // Ensure history is always preferred to pst
284 moves[i].score = hs + pos.mg_pst_delta(m);
288 void MovePicker::score_evasions() {
290 for (int i = 0; i < numOfMoves; i++)
292 Move m = moves[i].move;
294 moves[i].score = 2*HistoryMax;
295 else if (!pos.square_is_empty(move_to(m)))
297 int seeScore = pos.see(m);
298 moves[i].score = (seeScore >= 0)? seeScore + HistoryMax : seeScore;
300 moves[i].score = H.move_ordering_score(pos.piece_on(move_from(m)), move_to(m));
304 void MovePicker::score_qcaptures() {
306 // Use MVV/LVA ordering
307 for (int i = 0; i < numOfMoves; i++)
309 Move m = moves[i].move;
310 if (move_promotion(m))
311 moves[i].score = QueenValueMidgame;
313 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
314 -int(pos.type_of_piece_on(move_from(m)));
319 /// MovePicker::pick_move_from_list() picks the move with the biggest score
320 /// from a list of generated moves (moves[] or badCaptures[], depending on
321 /// the current move generation phase). It takes care not to return the
322 /// transposition table move if that has already been serched previously.
324 Move MovePicker::pick_move_from_list() {
326 assert(movesPicked >= 0);
327 assert(!pos.is_check() || PhaseTable[phaseIndex] == PH_EVASIONS || PhaseTable[phaseIndex] == PH_STOP);
328 assert( pos.is_check() || PhaseTable[phaseIndex] != PH_EVASIONS);
330 switch (PhaseTable[phaseIndex]) {
332 case PH_GOOD_CAPTURES:
335 while (movesPicked < numOfMoves)
337 Move move = moves[movesPicked++].move;
339 && move != mateKiller
340 && (!checkKillers || (move != killer1 && move != killer2))
341 && (!checkLegal || pos.pl_move_is_legal(move, pinned)))
347 if (movesPicked < numOfMoves)
348 return moves[movesPicked++].move;
352 case PH_BAD_CAPTURES:
353 while (movesPicked < numOfBadCaptures)
355 Move move = badCaptures[movesPicked++].move;
357 && move != mateKiller
358 && pos.pl_move_is_legal(move, pinned))
365 while (movesPicked < numOfMoves)
367 Move move = moves[movesPicked++].move;
368 // Maybe postpone the legality check until after futility pruning?
370 && pos.pl_move_is_legal(move, pinned))
382 /// MovePicker::init_phase_table() initializes the PhaseTable[],
383 /// MainSearchPhaseIndex, EvasionPhaseIndex, QsearchWithChecksPhaseIndex
384 /// and QsearchWithoutChecksPhaseIndex. It is only called once during
385 /// program startup, and never again while the program is running.
387 void MovePicker::init_phase_table() {
392 MainSearchPhaseIndex = i - 1;
393 PhaseTable[i++] = PH_TT_MOVE;
394 PhaseTable[i++] = PH_MATE_KILLER;
395 PhaseTable[i++] = PH_GOOD_CAPTURES;
396 PhaseTable[i++] = PH_KILLERS;
397 PhaseTable[i++] = PH_NONCAPTURES;
398 PhaseTable[i++] = PH_BAD_CAPTURES;
399 PhaseTable[i++] = PH_STOP;
402 EvasionsPhaseIndex = i - 1;
403 PhaseTable[i++] = PH_EVASIONS;
404 PhaseTable[i++] = PH_STOP;
406 // Quiescence search with checks
407 QsearchWithChecksPhaseIndex = i - 1;
408 PhaseTable[i++] = PH_TT_MOVE;
409 PhaseTable[i++] = PH_QCAPTURES;
410 PhaseTable[i++] = PH_QCHECKS;
411 PhaseTable[i++] = PH_STOP;
413 // Quiescence search without checks
414 QsearchWithoutChecksPhaseIndex = i - 1;
415 PhaseTable[i++] = PH_TT_MOVE;
416 PhaseTable[i++] = PH_QCAPTURES;
417 PhaseTable[i++] = PH_STOP;