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];
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 Color us = pos.side_to_move();
89 dc = p.discovered_check_candidates(us);
90 pinned = p.pinned_pieces(us);
96 /// MovePicker::get_next_move() is the most important method of the MovePicker
97 /// class. It returns a new legal move every time it is called, until there
98 /// are no more moves left of the types we are interested in.
100 Move MovePicker::get_next_move() {
106 // If we already have a list of generated moves, pick the best move from
107 // the list, and return it.
108 move = pick_move_from_list();
109 if (move != MOVE_NONE)
111 assert(move_is_ok(move));
117 switch (PhaseTable[phaseIndex]) {
120 if (ttMove != MOVE_NONE)
122 assert(move_is_ok(ttMove));
123 if (move_is_legal(pos, ttMove, pinned))
129 if (mateKiller != MOVE_NONE)
131 assert(move_is_ok(mateKiller));
132 if (move_is_legal(pos, mateKiller, pinned))
137 case PH_GOOD_CAPTURES:
138 numOfMoves = generate_captures(pos, moves);
140 std::sort(moves, moves + numOfMoves);
144 case PH_BAD_CAPTURES:
145 // It's probably a good idea to use SEE move ordering here. FIXME
150 numOfMoves = generate_noncaptures(pos, moves);
152 std::sort(moves, moves + numOfMoves);
157 assert(pos.is_check());
158 numOfMoves = generate_evasions(pos, moves, pinned);
160 std::sort(moves, moves + numOfMoves);
165 numOfMoves = generate_captures(pos, moves);
167 std::sort(moves, moves + numOfMoves);
172 // Perhaps we should order moves move here? FIXME
173 numOfMoves = generate_non_capture_checks(pos, moves, dc);
188 /// A variant of get_next_move() which takes a lock as a parameter, used to
189 /// prevent multiple threads from picking the same move at a split point.
191 Move MovePicker::get_next_move(Lock &lock) {
199 Move m = get_next_move();
208 /// MovePicker::score_captures(), MovePicker::score_noncaptures(),
209 /// MovePicker::score_evasions() and MovePicker::score_qcaptures() assign a
210 /// numerical move ordering score to each move in a move list. The moves
211 /// with highest scores will be picked first by pick_move_from_list().
213 void MovePicker::score_captures() {
214 // Winning and equal captures in the main search are ordered by MVV/LVA.
215 // Suprisingly, this appears to perform slightly better than SEE based
216 // move ordering. The reason is probably that in a position with a winning
217 // capture, capturing a more valuable (but sufficiently defended) piece
218 // first usually doesn't hurt. The opponent will have to recapture, and
219 // the hanging piece will still be hanging (except in the unusual cases
220 // where it is possible to recapture with the hanging piece). Exchanging
221 // big pieces before capturing a hanging piece probably helps to reduce
223 // While scoring captures it moves all captures with negative SEE values
224 // to the badCaptures[] array.
228 for (int i = 0; i < numOfMoves; i++)
231 seeValue = pos.see(m);
234 if (move_promotion(m))
235 moves[i].score = QueenValueMidgame;
237 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
238 -int(pos.type_of_piece_on(move_from(m)));
242 // Losing capture, move it to the badCaptures[] array
243 assert(numOfBadCaptures < 63);
244 moves[i].score = seeValue;
245 badCaptures[numOfBadCaptures++] = moves[i];
246 moves[i--] = moves[--numOfMoves];
251 void MovePicker::score_noncaptures() {
252 // First score by history, when no history is available then use
253 // piece/square tables values. This seems to be better then a
254 // random choice when we don't have an history for any move.
258 for (int i = 0; i < numOfMoves; i++)
264 else if (m == killer2)
267 hs = H.move_ordering_score(pos.piece_on(move_from(m)), move_to(m));
269 // Ensure history is always preferred to pst
274 moves[i].score = hs + pos.mg_pst_delta(m);
278 void MovePicker::score_evasions() {
280 for (int i = 0; i < numOfMoves; i++)
282 Move m = moves[i].move;
284 moves[i].score = 2*HistoryMax;
285 else if (!pos.square_is_empty(move_to(m)))
287 int seeScore = pos.see(m);
288 moves[i].score = (seeScore >= 0)? seeScore + HistoryMax : seeScore;
290 moves[i].score = H.move_ordering_score(pos.piece_on(move_from(m)), move_to(m));
294 void MovePicker::score_qcaptures() {
296 // Use MVV/LVA ordering
297 for (int i = 0; i < numOfMoves; i++)
299 Move m = moves[i].move;
300 if (move_promotion(m))
301 moves[i].score = QueenValueMidgame;
303 moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
304 -int(pos.type_of_piece_on(move_from(m)));
309 /// MovePicker::pick_move_from_list() picks the move with the biggest score
310 /// from a list of generated moves (moves[] or badCaptures[], depending on
311 /// the current move generation phase). It takes care not to return the
312 /// transposition table move if that has already been serched previously.
314 Move MovePicker::pick_move_from_list() {
316 assert(movesPicked >= 0);
317 assert(!pos.is_check() || PhaseTable[phaseIndex] == PH_EVASIONS || PhaseTable[phaseIndex] == PH_STOP);
318 assert( pos.is_check() || PhaseTable[phaseIndex] != PH_EVASIONS);
320 switch (PhaseTable[phaseIndex]) {
322 case PH_GOOD_CAPTURES:
324 while (movesPicked < numOfMoves)
326 Move move = moves[movesPicked++].move;
328 && move != mateKiller
329 && pos.pl_move_is_legal(move, pinned))
335 if (movesPicked < numOfMoves)
336 return moves[movesPicked++].move;
340 case PH_BAD_CAPTURES:
341 while (movesPicked < numOfBadCaptures)
343 Move move = badCaptures[movesPicked++].move;
345 && move != mateKiller
346 && pos.pl_move_is_legal(move, pinned))
353 while (movesPicked < numOfMoves)
355 Move move = moves[movesPicked++].move;
356 // Maybe postpone the legality check until after futility pruning?
358 && pos.pl_move_is_legal(move, pinned))
370 /// MovePicker::init_phase_table() initializes the PhaseTable[],
371 /// MainSearchPhaseIndex, EvasionPhaseIndex, QsearchWithChecksPhaseIndex
372 /// and QsearchWithoutChecksPhaseIndex. It is only called once during
373 /// program startup, and never again while the program is running.
375 void MovePicker::init_phase_table() {
380 MainSearchPhaseIndex = i - 1;
381 PhaseTable[i++] = PH_TT_MOVE;
382 PhaseTable[i++] = PH_MATE_KILLER;
383 PhaseTable[i++] = PH_GOOD_CAPTURES;
384 // PH_KILLER_1 and PH_KILLER_2 are not yet used.
385 // PhaseTable[i++] = PH_KILLER_1;
386 // PhaseTable[i++] = PH_KILLER_2;
387 PhaseTable[i++] = PH_NONCAPTURES;
388 PhaseTable[i++] = PH_BAD_CAPTURES;
389 PhaseTable[i++] = PH_STOP;
392 EvasionsPhaseIndex = i - 1;
393 PhaseTable[i++] = PH_EVASIONS;
394 PhaseTable[i++] = PH_STOP;
396 // Quiescence search with checks
397 QsearchWithChecksPhaseIndex = i - 1;
398 PhaseTable[i++] = PH_TT_MOVE;
399 PhaseTable[i++] = PH_QCAPTURES;
400 PhaseTable[i++] = PH_QCHECKS;
401 PhaseTable[i++] = PH_STOP;
403 // Quiescence search without checks
404 QsearchWithoutChecksPhaseIndex = i - 1;
405 PhaseTable[i++] = PH_TT_MOVE;
406 PhaseTable[i++] = PH_QCAPTURES;
407 PhaseTable[i++] = PH_STOP;