Rev 154 | Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 96 | pmbaty | 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-2016 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, GOOD_CAPTURES, KILLERS, GOOD_QUIETS, BAD_QUIETS, BAD_CAPTURES, |
||
| 30 | EVASION, ALL_EVASIONS, |
||
| 31 | QSEARCH_WITH_CHECKS, QCAPTURES_1, CHECKS, |
||
| 32 | QSEARCH_WITHOUT_CHECKS, QCAPTURES_2, |
||
| 33 | PROBCUT, PROBCUT_CAPTURES, |
||
| 34 | RECAPTURE, RECAPTURES, |
||
| 35 | STOP |
||
| 36 | }; |
||
| 37 | |||
| 38 | // Our insertion sort, which is guaranteed to be stable, as it should be |
||
| 39 | void insertion_sort(ExtMove* begin, ExtMove* end) |
||
| 40 | { |
||
| 41 | ExtMove tmp, *p, *q; |
||
| 42 | |||
| 43 | for (p = begin + 1; p < end; ++p) |
||
| 44 | { |
||
| 45 | tmp = *p; |
||
| 46 | for (q = p; q != begin && *(q-1) < tmp; --q) |
||
| 47 | *q = *(q-1); |
||
| 48 | *q = tmp; |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | // pick_best() finds the best move in the range (begin, end) and moves it to |
||
| 53 | // the front. It's faster than sorting all the moves in advance when there |
||
| 54 | // are few moves, e.g., the possible captures. |
||
| 55 | Move pick_best(ExtMove* begin, ExtMove* end) |
||
| 56 | { |
||
| 57 | std::swap(*begin, *std::max_element(begin, end)); |
||
| 58 | return *begin; |
||
| 59 | } |
||
| 60 | |||
| 61 | } // namespace |
||
| 62 | |||
| 63 | |||
| 64 | /// Constructors of the MovePicker class. As arguments we pass information |
||
| 65 | /// to help it to return the (presumably) good moves first, to decide which |
||
| 66 | /// moves to return (in the quiescence search, for instance, we only want to |
||
| 67 | /// search captures, promotions, and some checks) and how important good move |
||
| 68 | /// ordering is at the current node. |
||
| 69 | |||
| 70 | MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h, |
||
| 71 | const CounterMoveStats& cmh, Move cm, Search::Stack* s) |
||
| 72 | : pos(p), history(h), counterMoveHistory(&cmh), ss(s), countermove(cm), depth(d) { |
||
| 73 | |||
| 74 | assert(d > DEPTH_ZERO); |
||
| 75 | |||
| 76 | stage = pos.checkers() ? EVASION : MAIN_SEARCH; |
||
| 77 | ttMove = ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE; |
||
| 78 | endMoves += (ttMove != MOVE_NONE); |
||
| 79 | } |
||
| 80 | |||
| 81 | MovePicker::MovePicker(const Position& p, Move ttm, Depth d, |
||
| 82 | const HistoryStats& h, Square s) |
||
| 83 | : pos(p), history(h), counterMoveHistory(nullptr) { |
||
| 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_WITHOUT_CHECKS; |
||
| 95 | |||
| 96 | else |
||
| 97 | { |
||
| 98 | stage = RECAPTURE; |
||
| 99 | recaptureSquare = s; |
||
| 100 | ttm = MOVE_NONE; |
||
| 101 | } |
||
| 102 | |||
| 103 | ttMove = ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE; |
||
| 104 | endMoves += (ttMove != MOVE_NONE); |
||
| 105 | } |
||
| 106 | |||
| 107 | MovePicker::MovePicker(const Position& p, Move ttm, const HistoryStats& h, Value th) |
||
| 108 | : pos(p), history(h), counterMoveHistory(nullptr), threshold(th) { |
||
| 109 | |||
| 110 | assert(!pos.checkers()); |
||
| 111 | |||
| 112 | stage = PROBCUT; |
||
| 113 | |||
| 114 | // In ProbCut we generate captures with SEE higher than the given threshold |
||
| 115 | ttMove = ttm |
||
| 116 | && pos.pseudo_legal(ttm) |
||
| 117 | && pos.capture(ttm) |
||
| 118 | && pos.see(ttm) > threshold ? ttm : MOVE_NONE; |
||
| 119 | |||
| 120 | endMoves += (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 | for (auto& m : *this) |
||
| 144 | m.value = history[pos.moved_piece(m)][to_sq(m)] |
||
| 145 | + (*counterMoveHistory)[pos.moved_piece(m)][to_sq(m)]; |
||
| 146 | } |
||
| 147 | |||
| 148 | template<> |
||
| 149 | void MovePicker::score<EVASIONS>() { |
||
| 150 | // Try winning and equal captures ordered by MVV/LVA, then non-captures ordered |
||
| 151 | // by history value, then bad captures and quiet moves with a negative SEE ordered |
||
| 152 | // by SEE value. |
||
| 153 | Value see; |
||
| 154 | |||
| 155 | for (auto& m : *this) |
||
| 156 | if ((see = pos.see_sign(m)) < VALUE_ZERO) |
||
| 157 | m.value = see - HistoryStats::Max; // At the bottom |
||
| 158 | |||
| 159 | else if (pos.capture(m)) |
||
| 160 | m.value = PieceValue[MG][pos.piece_on(to_sq(m))] |
||
| 161 | - Value(type_of(pos.moved_piece(m))) + HistoryStats::Max; |
||
| 162 | else |
||
| 163 | m.value = history[pos.moved_piece(m)][to_sq(m)]; |
||
| 164 | } |
||
| 165 | |||
| 166 | |||
| 167 | /// generate_next_stage() generates, scores, and sorts the next bunch of moves |
||
| 168 | /// when there are no more moves to try for the current stage. |
||
| 169 | |||
| 170 | void MovePicker::generate_next_stage() { |
||
| 171 | |||
| 172 | assert(stage != STOP); |
||
| 173 | |||
| 174 | cur = moves; |
||
| 175 | |||
| 176 | switch (++stage) { |
||
| 177 | |||
| 178 | case GOOD_CAPTURES: case QCAPTURES_1: case QCAPTURES_2: |
||
| 179 | case PROBCUT_CAPTURES: case RECAPTURES: |
||
| 180 | endMoves = generate<CAPTURES>(pos, moves); |
||
| 181 | score<CAPTURES>(); |
||
| 182 | break; |
||
| 183 | |||
| 184 | case KILLERS: |
||
| 185 | killers[0] = ss->killers[0]; |
||
| 186 | killers[1] = ss->killers[1]; |
||
| 187 | killers[2] = countermove; |
||
| 188 | cur = killers; |
||
| 189 | endMoves = cur + 2 + (countermove != killers[0] && countermove != killers[1]); |
||
| 190 | break; |
||
| 191 | |||
| 192 | case GOOD_QUIETS: |
||
| 193 | endQuiets = endMoves = generate<QUIETS>(pos, moves); |
||
| 194 | score<QUIETS>(); |
||
| 195 | endMoves = std::partition(cur, endMoves, [](const ExtMove& m) { return m.value > VALUE_ZERO; }); |
||
| 196 | insertion_sort(cur, endMoves); |
||
| 197 | break; |
||
| 198 | |||
| 199 | case BAD_QUIETS: |
||
| 200 | cur = endMoves; |
||
| 201 | endMoves = endQuiets; |
||
| 202 | if (depth >= 3 * ONE_PLY) |
||
| 203 | insertion_sort(cur, endMoves); |
||
| 204 | break; |
||
| 205 | |||
| 206 | case BAD_CAPTURES: |
||
| 207 | // Just pick them in reverse order to get correct ordering |
||
| 208 | cur = moves + MAX_MOVES - 1; |
||
| 209 | endMoves = endBadCaptures; |
||
| 210 | break; |
||
| 211 | |||
| 212 | case ALL_EVASIONS: |
||
| 213 | endMoves = generate<EVASIONS>(pos, moves); |
||
| 214 | if (endMoves - moves > 1) |
||
| 215 | score<EVASIONS>(); |
||
| 216 | break; |
||
| 217 | |||
| 218 | case CHECKS: |
||
| 219 | endMoves = generate<QUIET_CHECKS>(pos, moves); |
||
| 220 | break; |
||
| 221 | |||
| 222 | case EVASION: case QSEARCH_WITH_CHECKS: case QSEARCH_WITHOUT_CHECKS: |
||
| 223 | case PROBCUT: case RECAPTURE: case STOP: |
||
| 224 | stage = STOP; |
||
| 225 | break; |
||
| 226 | |||
| 227 | default: |
||
| 228 | assert(false); |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | |||
| 233 | /// next_move() is the most important method of the MovePicker class. It returns |
||
| 234 | /// a new pseudo legal move every time it is called, until there are no more moves |
||
| 235 | /// left. It picks the move with the biggest value from a list of generated moves |
||
| 236 | /// taking care not to return the ttMove if it has already been searched. |
||
| 237 | |||
| 238 | Move MovePicker::next_move() { |
||
| 239 | |||
| 240 | Move move; |
||
| 241 | |||
| 242 | while (true) |
||
| 243 | { |
||
| 244 | while (cur == endMoves && stage != STOP) |
||
| 245 | generate_next_stage(); |
||
| 246 | |||
| 247 | switch (stage) { |
||
| 248 | |||
| 249 | case MAIN_SEARCH: case EVASION: case QSEARCH_WITH_CHECKS: |
||
| 250 | case QSEARCH_WITHOUT_CHECKS: case PROBCUT: |
||
| 251 | ++cur; |
||
| 252 | return ttMove; |
||
| 253 | |||
| 254 | case GOOD_CAPTURES: |
||
| 255 | move = pick_best(cur++, endMoves); |
||
| 256 | if (move != ttMove) |
||
| 257 | { |
||
| 258 | if (pos.see_sign(move) >= VALUE_ZERO) |
||
| 259 | return move; |
||
| 260 | |||
| 261 | // Losing capture, move it to the tail of the array |
||
| 262 | *endBadCaptures-- = move; |
||
| 263 | } |
||
| 264 | break; |
||
| 265 | |||
| 266 | case KILLERS: |
||
| 267 | move = *cur++; |
||
| 268 | if ( move != MOVE_NONE |
||
| 269 | && move != ttMove |
||
| 270 | && pos.pseudo_legal(move) |
||
| 271 | && !pos.capture(move)) |
||
| 272 | return move; |
||
| 273 | break; |
||
| 274 | |||
| 275 | case GOOD_QUIETS: case BAD_QUIETS: |
||
| 276 | move = *cur++; |
||
| 277 | if ( move != ttMove |
||
| 278 | && move != killers[0] |
||
| 279 | && move != killers[1] |
||
| 280 | && move != killers[2]) |
||
| 281 | return move; |
||
| 282 | break; |
||
| 283 | |||
| 284 | case BAD_CAPTURES: |
||
| 285 | return *cur--; |
||
| 286 | |||
| 287 | case ALL_EVASIONS: case QCAPTURES_1: case QCAPTURES_2: |
||
| 288 | move = pick_best(cur++, endMoves); |
||
| 289 | if (move != ttMove) |
||
| 290 | return move; |
||
| 291 | break; |
||
| 292 | |||
| 293 | case PROBCUT_CAPTURES: |
||
| 294 | move = pick_best(cur++, endMoves); |
||
| 295 | if (move != ttMove && pos.see(move) > threshold) |
||
| 296 | return move; |
||
| 297 | break; |
||
| 298 | |||
| 299 | case RECAPTURES: |
||
| 300 | move = pick_best(cur++, endMoves); |
||
| 301 | if (to_sq(move) == recaptureSquare) |
||
| 302 | return move; |
||
| 303 | break; |
||
| 304 | |||
| 305 | case CHECKS: |
||
| 306 | move = *cur++; |
||
| 307 | if (move != ttMove) |
||
| 308 | return move; |
||
| 309 | break; |
||
| 310 | |||
| 311 | case STOP: |
||
| 312 | return MOVE_NONE; |
||
| 313 | |||
| 314 | default: |
||
| 315 | assert(false); |
||
| 316 | } |
||
| 317 | } |
||
| 318 | } |