]> git.sesse.net Git - stockfish/blob - src/movepick.cpp
Doubled and supported pawns
[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-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5   Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
6
7   Stockfish is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
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 #include <cassert>
22
23 #include "movepick.h"
24 #include "thread.h"
25
26 namespace {
27
28   enum Stages {
29     MAIN_SEARCH, CAPTURES_INIT, GOOD_CAPTURES, KILLERS, COUNTERMOVE, QUIET_INIT, QUIET, BAD_CAPTURES,
30     EVASION, EVASIONS_INIT, ALL_EVASIONS,
31     PROBCUT, PROBCUT_INIT, PROBCUT_CAPTURES,
32     QSEARCH_WITH_CHECKS, QCAPTURES_1_INIT, QCAPTURES_1, QCHECKS,
33     QSEARCH_NO_CHECKS, QCAPTURES_2_INIT, QCAPTURES_2,
34     QSEARCH_RECAPTURES, QRECAPTURES
35   };
36
37   // Our insertion sort, which is guaranteed to be stable, as it should be
38   void insertion_sort(ExtMove* begin, ExtMove* end)
39   {
40     ExtMove tmp, *p, *q;
41
42     for (p = begin + 1; p < end; ++p)
43     {
44         tmp = *p;
45         for (q = p; q != begin && *(q-1) < tmp; --q)
46             *q = *(q-1);
47         *q = tmp;
48     }
49   }
50
51   // pick_best() finds the best move in the range (begin, end) and moves it to
52   // the front. It's faster than sorting all the moves in advance when there
53   // are few moves, e.g., the possible captures.
54   Move pick_best(ExtMove* begin, ExtMove* end)
55   {
56       std::swap(*begin, *std::max_element(begin, end));
57       return *begin;
58   }
59
60 } // namespace
61
62
63 /// Constructors of the MovePicker class. As arguments we pass information
64 /// to help it to return the (presumably) good moves first, to decide which
65 /// moves to return (in the quiescence search, for instance, we only want to
66 /// search captures, promotions, and some checks) and how important good move
67 /// ordering is at the current node.
68
69 MovePicker::MovePicker(const Position& p, Move ttm, Depth d, Search::Stack* s)
70            : pos(p), ss(s), depth(d) {
71
72   assert(d > DEPTH_ZERO);
73
74   Square prevSq = to_sq((ss-1)->currentMove);
75   countermove = pos.this_thread()->counterMoves[pos.piece_on(prevSq)][prevSq];
76
77   stage = pos.checkers() ? EVASION : MAIN_SEARCH;
78   ttMove = ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE;
79   stage += (ttMove == MOVE_NONE);
80 }
81
82 MovePicker::MovePicker(const Position& p, Move ttm, Depth d, Square s)
83            : pos(p) {
84
85   assert(d <= DEPTH_ZERO);
86
87   if (pos.checkers())
88       stage = EVASION;
89
90   else if (d > DEPTH_QS_NO_CHECKS)
91       stage = QSEARCH_WITH_CHECKS;
92
93   else if (d > DEPTH_QS_RECAPTURES)
94       stage = QSEARCH_NO_CHECKS;
95
96   else
97   {
98       stage = QSEARCH_RECAPTURES;
99       recaptureSquare = s;
100       return;
101   }
102
103   ttMove = ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE;
104   stage += (ttMove == MOVE_NONE);
105 }
106
107 MovePicker::MovePicker(const Position& p, Move ttm, Value th)
108            : pos(p), threshold(th) {
109
110   assert(!pos.checkers());
111
112   stage = PROBCUT;
113
114   // In ProbCut we generate captures with SEE higher than or equal to the given threshold
115   ttMove =   ttm
116           && pos.pseudo_legal(ttm)
117           && pos.capture(ttm)
118           && pos.see_ge(ttm, threshold)? ttm : MOVE_NONE;
119
120   stage += (ttMove == MOVE_NONE);
121 }
122
123
124 /// score() assigns a numerical value to each move in a move list. The moves with
125 /// highest values will be picked first.
126 template<>
127 void MovePicker::score<CAPTURES>() {
128   // Winning and equal captures in the main search are ordered by MVV, preferring
129   // captures near our home rank. Surprisingly, this appears to perform slightly
130   // better than SEE-based move ordering: exchanging big pieces before capturing
131   // a hanging piece probably helps to reduce the subtree size.
132   // In the main search we want to push captures with negative SEE values to the
133   // badCaptures[] array, but instead of doing it now we delay until the move
134   // has been picked up, saving some SEE calls in case we get a cutoff.
135   for (auto& m : *this)
136       m.value =  PieceValue[MG][pos.piece_on(to_sq(m))]
137                - Value(200 * relative_rank(pos.side_to_move(), to_sq(m)));
138 }
139
140 template<>
141 void MovePicker::score<QUIETS>() {
142
143   const HistoryStats& history = pos.this_thread()->history;
144
145   const CounterMoveStats& cmh = *(ss-1)->counterMoves;
146   const CounterMoveStats& fmh = *(ss-2)->counterMoves;
147   const CounterMoveStats& fm2 = *(ss-4)->counterMoves;
148
149   Color c = pos.side_to_move();
150
151   for (auto& m : *this)
152       m.value =  cmh[pos.moved_piece(m)][to_sq(m)]
153                + fmh[pos.moved_piece(m)][to_sq(m)]
154                + fm2[pos.moved_piece(m)][to_sq(m)]
155                + history.get(c, m);
156 }
157
158 template<>
159 void MovePicker::score<EVASIONS>() {
160   // Try captures ordered by MVV/LVA, then non-captures ordered by stats heuristics
161   const HistoryStats& history = pos.this_thread()->history;
162   Color c = pos.side_to_move();
163
164   for (auto& m : *this)
165       if (pos.capture(m))
166           m.value =  PieceValue[MG][pos.piece_on(to_sq(m))]
167                    - Value(type_of(pos.moved_piece(m))) + HistoryStats::Max;
168       else
169           m.value = history.get(c, m);
170 }
171
172
173 /// next_move() is the most important method of the MovePicker class. It returns
174 /// a new pseudo legal move every time it is called, until there are no more moves
175 /// left. It picks the move with the biggest value from a list of generated moves
176 /// taking care not to return the ttMove if it has already been searched.
177
178 Move MovePicker::next_move(bool skipQuiets) {
179
180   Move move;
181
182   switch (stage) {
183
184   case MAIN_SEARCH: case EVASION: case QSEARCH_WITH_CHECKS:
185   case QSEARCH_NO_CHECKS: case PROBCUT:
186       ++stage;
187       return ttMove;
188
189   case CAPTURES_INIT:
190       endBadCaptures = cur = moves;
191       endMoves = generate<CAPTURES>(pos, cur);
192       score<CAPTURES>();
193       ++stage;
194
195   case GOOD_CAPTURES:
196       while (cur < endMoves)
197       {
198           move = pick_best(cur++, endMoves);
199           if (move != ttMove)
200           {
201               if (pos.see_ge(move, VALUE_ZERO))
202                   return move;
203
204               // Losing capture, move it to the beginning of the array
205               *endBadCaptures++ = move;
206           }
207       }
208
209       ++stage;
210       move = ss->killers[0];  // First killer move
211       if (    move != MOVE_NONE
212           &&  move != ttMove
213           &&  pos.pseudo_legal(move)
214           && !pos.capture(move))
215           return move;
216
217   case KILLERS:
218       ++stage;
219       move = ss->killers[1]; // Second killer move
220       if (    move != MOVE_NONE
221           &&  move != ttMove
222           &&  pos.pseudo_legal(move)
223           && !pos.capture(move))
224           return move;
225
226   case COUNTERMOVE:
227       ++stage;
228       move = countermove;
229       if (    move != MOVE_NONE
230           &&  move != ttMove
231           &&  move != ss->killers[0]
232           &&  move != ss->killers[1]
233           &&  pos.pseudo_legal(move)
234           && !pos.capture(move))
235           return move;
236
237   case QUIET_INIT:
238       cur = endBadCaptures;
239       endMoves = generate<QUIETS>(pos, cur);
240       score<QUIETS>();
241       if (depth < 3 * ONE_PLY)
242       {
243           ExtMove* goodQuiet = std::partition(cur, endMoves, [](const ExtMove& m)
244                                              { return m.value > VALUE_ZERO; });
245           insertion_sort(cur, goodQuiet);
246       } else
247           insertion_sort(cur, endMoves);
248       ++stage;
249
250   case QUIET:
251       while (    cur < endMoves
252              && (!skipQuiets || cur->value >= VALUE_ZERO))
253       {
254           move = *cur++;
255
256           if (   move != ttMove
257               && move != ss->killers[0]
258               && move != ss->killers[1]
259               && move != countermove)
260               return move;
261       }
262       ++stage;
263       cur = moves; // Point to beginning of bad captures
264
265   case BAD_CAPTURES:
266       if (cur < endBadCaptures)
267           return *cur++;
268       break;
269
270   case EVASIONS_INIT:
271       cur = moves;
272       endMoves = generate<EVASIONS>(pos, cur);
273       score<EVASIONS>();
274       ++stage;
275
276   case ALL_EVASIONS:
277       while (cur < endMoves)
278       {
279           move = pick_best(cur++, endMoves);
280           if (move != ttMove)
281               return move;
282       }
283       break;
284
285   case PROBCUT_INIT:
286       cur = moves;
287       endMoves = generate<CAPTURES>(pos, cur);
288       score<CAPTURES>();
289       ++stage;
290
291   case PROBCUT_CAPTURES:
292       while (cur < endMoves)
293       {
294           move = pick_best(cur++, endMoves);
295           if (   move != ttMove
296               && pos.see_ge(move, threshold))
297               return move;
298       }
299       break;
300
301   case QCAPTURES_1_INIT: case QCAPTURES_2_INIT:
302       cur = moves;
303       endMoves = generate<CAPTURES>(pos, cur);
304       score<CAPTURES>();
305       ++stage;
306
307   case QCAPTURES_1: case QCAPTURES_2:
308       while (cur < endMoves)
309       {
310           move = pick_best(cur++, endMoves);
311           if (move != ttMove)
312               return move;
313       }
314       if (stage == QCAPTURES_2)
315           break;
316       cur = moves;
317       endMoves = generate<QUIET_CHECKS>(pos, cur);
318       ++stage;
319
320   case QCHECKS:
321       while (cur < endMoves)
322       {
323           move = cur++->move;
324           if (move != ttMove)
325               return move;
326       }
327       break;
328
329   case QSEARCH_RECAPTURES:
330       cur = moves;
331       endMoves = generate<CAPTURES>(pos, cur);
332       score<CAPTURES>();
333       ++stage;
334
335   case QRECAPTURES:
336       while (cur < endMoves)
337       {
338           move = pick_best(cur++, endMoves);
339           if (to_sq(move) == recaptureSquare)
340               return move;
341       }
342       break;
343
344   default:
345       assert(false);
346   }
347
348   return MOVE_NONE;
349 }