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/>.
36 //// Local definitions
42 PH_TT_MOVES, // Transposition table move and mate killer
43 PH_GOOD_CAPTURES, // Queen promotions and captures with SEE values >= 0
44 PH_KILLERS, // Killer moves from the current ply
45 PH_NONCAPTURES, // Non-captures and underpromotions
46 PH_BAD_CAPTURES, // Queen promotions and captures with SEE values < 0
47 PH_EVASIONS, // Check evasions
48 PH_QCAPTURES, // Captures in quiescence search
49 PH_QCHECKS, // Non-capture checks in quiescence search
54 const uint8_t MainSearchPhaseTable[] = { PH_TT_MOVES, PH_GOOD_CAPTURES, PH_KILLERS, PH_NONCAPTURES, PH_BAD_CAPTURES, PH_STOP};
55 const uint8_t EvasionsPhaseTable[] = { PH_EVASIONS, PH_STOP};
56 const uint8_t QsearchWithChecksPhaseTable[] = { PH_TT_MOVES, PH_QCAPTURES, PH_QCHECKS, PH_STOP};
57 const uint8_t QsearchWithoutChecksPhaseTable[] = { PH_TT_MOVES, PH_QCAPTURES, PH_STOP};
66 /// Constructor for the MovePicker class. Apart from the position for which
67 /// it is asked to pick legal moves, MovePicker also wants some information
68 /// to help it to return the presumably good moves first, to decide which
69 /// moves to return (in the quiescence search, for instance, we only want to
70 /// search captures, promotions and some checks) and about how important good
71 /// move ordering is at the current node.
73 MovePicker::MovePicker(const Position& p, Move ttm, Depth d,
74 const History& h, SearchStack* ss) : pos(p), H(h) {
76 ttMoves[0].move = ttm;
78 lastBadCapture = badCaptures;
82 ttMoves[1].move = (ss->mateKiller == ttm)? MOVE_NONE : ss->mateKiller;
83 searchTT |= ttMoves[1].move;
84 killers[0].move = ss->killers[0];
85 killers[1].move = ss->killers[1];
87 ttMoves[1].move = killers[0].move = killers[1].move = MOVE_NONE;
89 Color us = pos.side_to_move();
91 dc = p.discovered_check_candidates(us);
92 pinned = p.pinned_pieces(us);
95 phasePtr = EvasionsPhaseTable;
96 else if (d > Depth(0))
97 phasePtr = MainSearchPhaseTable + !searchTT;
98 else if (d == Depth(0))
99 phasePtr = QsearchWithChecksPhaseTable + !searchTT;
101 phasePtr = QsearchWithoutChecksPhaseTable + !searchTT;
108 /// MovePicker::go_next_phase() generates, scores and sorts the next bunch
109 /// of moves when there are no more moves to try for the current phase.
111 void MovePicker::go_next_phase() {
114 phase = *(++phasePtr);
119 lastMove = curMove + 2;
122 case PH_GOOD_CAPTURES:
123 lastMove = generate_captures(pos, moves);
125 sort_moves(moves, lastMove);
130 lastMove = curMove + 2;
134 lastMove = generate_noncaptures(pos, moves);
136 sort_moves(moves, lastMove);
139 case PH_BAD_CAPTURES:
140 // Bad captures SEE value is already calculated so just sort them
141 // to get SEE move ordering.
142 curMove = badCaptures;
143 lastMove = lastBadCapture;
144 sort_moves(badCaptures, lastMove);
148 assert(pos.is_check());
149 lastMove = generate_evasions(pos, moves, pinned);
151 sort_moves(moves, lastMove);
155 lastMove = generate_captures(pos, moves);
157 sort_moves(moves, lastMove);
161 // Perhaps we should order moves move here? FIXME
162 lastMove = generate_non_capture_checks(pos, moves, dc);
166 lastMove = curMove + 1; // hack to be friendly for get_next_move()
176 /// MovePicker::score_captures(), MovePicker::score_noncaptures() and
177 /// MovePicker::score_evasions() assign a numerical move ordering score
178 /// to each move in a move list. The moves with highest scores will be
179 /// picked first by get_next_move().
181 void MovePicker::score_captures() {
182 // Winning and equal captures in the main search are ordered by MVV/LVA.
183 // Suprisingly, this appears to perform slightly better than SEE based
184 // move ordering. The reason is probably that in a position with a winning
185 // capture, capturing a more valuable (but sufficiently defended) piece
186 // first usually doesn't hurt. The opponent will have to recapture, and
187 // the hanging piece will still be hanging (except in the unusual cases
188 // where it is possible to recapture with the hanging piece). Exchanging
189 // big pieces before capturing a hanging piece probably helps to reduce
191 // In main search we want to push captures with negative SEE values to
192 // badCaptures[] array, but instead of doing it now we delay till when
193 // the move has been picked up in pick_move_from_list(), this way we save
194 // some SEE calls in case we get a cutoff (idea from Pablo Vazquez).
197 // Use MVV/LVA ordering
198 for (MoveStack* cur = moves; cur != lastMove; cur++)
201 if (move_is_promotion(m))
202 cur->score = QueenValueMidgame;
204 cur->score = int(pos.midgame_value_of_piece_on(move_to(m)))
205 -int(pos.type_of_piece_on(move_from(m)));
209 void MovePicker::score_noncaptures() {
210 // First score by history, when no history is available then use
211 // piece/square tables values. This seems to be better then a
212 // random choice when we don't have an history for any move.
218 for (MoveStack* cur = moves; cur != lastMove; cur++)
223 piece = pos.piece_on(from);
224 hs = H.move_ordering_score(piece, to);
226 // Ensure history is always preferred to pst
231 cur->score = hs + pos.pst_delta<Position::MidGame>(piece, from, to);
235 void MovePicker::score_evasions() {
239 for (MoveStack* cur = moves; cur != lastMove; cur++)
242 if (m == ttMoves[0].move)
243 cur->score = 2 * HistoryMax;
244 else if (!pos.square_is_empty(move_to(m)))
246 int seeScore = pos.see(m);
247 cur->score = seeScore + (seeScore >= 0 ? HistoryMax : 0);
249 cur->score = H.move_ordering_score(pos.piece_on(move_from(m)), move_to(m));
253 /// MovePicker::get_next_move() is the most important method of the MovePicker
254 /// class. It returns a new legal move every time it is called, until there
255 /// are no more moves left.
256 /// It picks the move with the biggest score from a list of generated moves taking
257 /// care not to return the tt move if has already been searched previously.
259 Move MovePicker::get_next_move() {
261 assert(!pos.is_check() || *phasePtr == PH_EVASIONS || *phasePtr == PH_STOP);
262 assert( pos.is_check() || *phasePtr != PH_EVASIONS);
268 while (curMove != lastMove)
270 move = (curMove++)->move;
275 if ( move != MOVE_NONE
276 && move_is_legal(pos, move, pinned))
280 case PH_GOOD_CAPTURES:
281 if ( move != ttMoves[0].move
282 && move != ttMoves[1].move
283 && pos.pl_move_is_legal(move, pinned))
285 // Check for a non negative SEE now
286 int seeValue = pos.see_sign(move);
290 // Losing capture, move it to the badCaptures[] array, note
291 // that move has now been already checked for legality.
292 assert(int(lastBadCapture - badCaptures) < 63);
293 lastBadCapture->move = move;
294 lastBadCapture->score = seeValue;
300 if ( move != MOVE_NONE
301 && move != ttMoves[0].move
302 && move != ttMoves[1].move
303 && move_is_legal(pos, move, pinned)
304 && !pos.move_is_capture(move))
309 if ( move != ttMoves[0].move
310 && move != ttMoves[1].move
311 && move != killers[0].move
312 && move != killers[1].move
313 && pos.pl_move_is_legal(move, pinned))
318 case PH_BAD_CAPTURES:
323 // Maybe postpone the legality check until after futility pruning?
324 if ( move != ttMoves[0].move
325 && pos.pl_move_is_legal(move, pinned))
341 /// A variant of get_next_move() which takes a lock as a parameter, used to
342 /// prevent multiple threads from picking the same move at a split point.
344 Move MovePicker::get_next_move(Lock &lock) {
352 Move m = get_next_move();