]> git.sesse.net Git - stockfish/blob - src/movepick.cpp
Retire outdated aspiration search code
[stockfish] / src / movepick.cpp
1 /*
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
5
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.
10
11
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.
16
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/>.
19 */
20
21
22 ////
23 //// Includes
24 ////
25
26 #include <cassert>
27
28 #include "history.h"
29 #include "movegen.h"
30 #include "movepick.h"
31 #include "search.h"
32 #include "value.h"
33
34
35 ////
36 //// Local definitions
37 ////
38
39 namespace {
40
41   enum MovegenPhase {
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
50     PH_STOP
51   };
52
53   CACHE_LINE_ALIGNMENT
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_TT_MOVES, 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};
58 }
59
60
61 ////
62 //// Functions
63 ////
64
65
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.
72
73 MovePicker::MovePicker(const Position& p, Move ttm, Depth d,
74                        const History& h, SearchStack* ss) : pos(p), H(h) {
75   int searchTT = ttm;
76   ttMoves[0].move = ttm;
77   lastBadCapture = badCaptures;
78
79   pinned = p.pinned_pieces(pos.side_to_move());
80
81   if (ss && !p.is_check())
82   {
83       ttMoves[1].move = (ss->mateKiller == ttm)? MOVE_NONE : ss->mateKiller;
84       searchTT |= ttMoves[1].move;
85       killers[0].move = ss->killers[0];
86       killers[1].move = ss->killers[1];
87   } else
88       ttMoves[1].move = killers[0].move = killers[1].move = MOVE_NONE;
89
90   if (p.is_check())
91       phasePtr = EvasionsPhaseTable;
92   else if (d > Depth(0))
93       phasePtr = MainSearchPhaseTable;
94   else if (d == Depth(0))
95       phasePtr = QsearchWithChecksPhaseTable;
96   else
97   {
98       phasePtr = QsearchWithoutChecksPhaseTable;
99
100       // Skip TT move if is not a capture or a promotion, this avoids
101       // qsearch tree explosion due to a possible perpetual check or
102       // similar rare cases when TT table is full.
103       if (ttm != MOVE_NONE && !pos.move_is_capture_or_promotion(ttm))
104           searchTT = ttMoves[0].move = MOVE_NONE;
105   }
106
107   phasePtr += !searchTT - 1;
108   go_next_phase();
109 }
110
111
112 /// MovePicker::go_next_phase() generates, scores and sorts the next bunch
113 /// of moves when there are no more moves to try for the current phase.
114
115 void MovePicker::go_next_phase() {
116
117   curMove = moves;
118   phase = *(++phasePtr);
119   switch (phase) {
120
121   case PH_TT_MOVES:
122       curMove = ttMoves;
123       lastMove = curMove + 2;
124       return;
125
126   case PH_GOOD_CAPTURES:
127       lastMove = generate_captures(pos, moves);
128       score_captures();
129       return;
130
131   case PH_KILLERS:
132       curMove = killers;
133       lastMove = curMove + 2;
134       return;
135
136   case PH_NONCAPTURES:
137       lastMove = generate_noncaptures(pos, moves);
138       score_noncaptures();
139       sort_moves(moves, lastMove);
140       return;
141
142   case PH_BAD_CAPTURES:
143       // Bad captures SEE value is already calculated so just sort them
144       // to get SEE move ordering.
145       curMove = badCaptures;
146       lastMove = lastBadCapture;
147       return;
148
149   case PH_EVASIONS:
150       assert(pos.is_check());
151       lastMove = generate_evasions(pos, moves);
152       score_evasions_or_checks();
153       return;
154
155   case PH_QCAPTURES:
156       lastMove = generate_captures(pos, moves);
157       score_captures();
158       return;
159
160   case PH_QCHECKS:
161       lastMove = generate_non_capture_checks(pos, moves);
162       score_evasions_or_checks();
163       return;
164
165   case PH_STOP:
166       lastMove = curMove + 1; // Avoids another go_next_phase() call
167       return;
168
169   default:
170       assert(false);
171       return;
172   }
173 }
174
175
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().
180
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
190   // the subtree size.
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).
195   Move m;
196
197   // Use MVV/LVA ordering
198   for (MoveStack* cur = moves; cur != lastMove; cur++)
199   {
200       m = cur->move;
201       if (move_is_promotion(m))
202           cur->score = QueenValueMidgame;
203       else
204           cur->score =  pos.midgame_value_of_piece_on(move_to(m))
205                       - pos.type_of_piece_on(move_from(m));
206   }
207 }
208
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.
213   Move m;
214   Piece piece;
215   Square from, to;
216   int hs;
217
218   for (MoveStack* cur = moves; cur != lastMove; cur++)
219   {
220       m = cur->move;
221       from = move_from(m);
222       to = move_to(m);
223       piece = pos.piece_on(from);
224       hs = H.move_ordering_score(piece, to);
225
226       // Ensure history is always preferred to pst
227       if (hs > 0)
228           hs += 1000;
229
230       // pst based scoring
231       cur->score = hs + mg_value(pos.pst_delta(piece, from, to));
232   }
233 }
234
235 void MovePicker::score_evasions_or_checks() {
236   // Try good captures ordered by MVV/LVA, then non-captures if
237   // destination square is not under attack, ordered by history
238   // value, and at the end bad-captures and non-captures with a
239   // negative SEE. This last group is ordered by the SEE score.
240   Move m;
241   int seeScore;
242
243   // Skip if we don't have at least two moves to order
244   if (lastMove < moves + 2)
245       return;
246
247   for (MoveStack* cur = moves; cur != lastMove; cur++)
248   {
249       m = cur->move;
250       if ((seeScore = pos.see_sign(m)) < 0)
251           cur->score = seeScore;
252       else if (pos.move_is_capture(m))
253           cur->score =  pos.midgame_value_of_piece_on(move_to(m))
254                       - pos.type_of_piece_on(move_from(m)) + HistoryMax;
255       else
256           cur->score = H.move_ordering_score(pos.piece_on(move_from(m)), move_to(m));
257   }
258 }
259
260 /// MovePicker::get_next_move() is the most important method of the MovePicker
261 /// class. It returns a new legal move every time it is called, until there
262 /// are no more moves left.
263 /// It picks the move with the biggest score from a list of generated moves taking
264 /// care not to return the tt move if has already been searched previously.
265 /// Note that this function is not thread safe so should be lock protected by
266 /// caller when accessed through a shared MovePicker object.
267
268 Move MovePicker::get_next_move() {
269
270   Move move;
271
272   while (true)
273   {
274       while (curMove != lastMove)
275       {
276           switch (phase) {
277
278           case PH_TT_MOVES:
279               move = (curMove++)->move;
280               if (   move != MOVE_NONE
281                   && move_is_legal(pos, move, pinned))
282                   return move;
283               break;
284
285           case PH_GOOD_CAPTURES:
286               move = pick_best(curMove++, lastMove).move;
287               if (   move != ttMoves[0].move
288                   && move != ttMoves[1].move
289                   && pos.pl_move_is_legal(move, pinned))
290               {
291                   // Check for a non negative SEE now
292                   int seeValue = pos.see_sign(move);
293                   if (seeValue >= 0)
294                       return move;
295
296                   // Losing capture, move it to the badCaptures[] array, note
297                   // that move has now been already checked for legality.
298                   assert(int(lastBadCapture - badCaptures) < 63);
299                   lastBadCapture->move = move;
300                   lastBadCapture->score = seeValue;
301                   lastBadCapture++;
302               }
303               break;
304
305           case PH_KILLERS:
306               move = (curMove++)->move;
307               if (   move != MOVE_NONE
308                   && move != ttMoves[0].move
309                   && move != ttMoves[1].move
310                   && move_is_legal(pos, move, pinned)
311                   && !pos.move_is_capture(move))
312                   return move;
313               break;
314
315           case PH_NONCAPTURES:
316               move = (curMove++)->move;
317               if (   move != ttMoves[0].move
318                   && move != ttMoves[1].move
319                   && move != killers[0].move
320                   && move != killers[1].move
321                   && pos.pl_move_is_legal(move, pinned))
322                   return move;
323               break;
324
325           case PH_BAD_CAPTURES:
326               move = pick_best(curMove++, lastMove).move;
327               return move;
328
329           case PH_EVASIONS:
330           case PH_QCAPTURES:
331               move = pick_best(curMove++, lastMove).move;
332               if (   move != ttMoves[0].move
333                   && pos.pl_move_is_legal(move, pinned))
334                   return move;
335               break;
336
337           case PH_QCHECKS:
338               move = (curMove++)->move;
339               if (   move != ttMoves[0].move
340                   && pos.pl_move_is_legal(move, pinned))
341                   return move;
342               break;
343
344           case PH_STOP:
345               return MOVE_NONE;
346
347           default:
348               assert(false);
349               break;
350           }
351       }
352       go_next_phase();
353   }
354 }
355