2 Glaurung, a UCI chess playing engine.
3 Copyright (C) 2004-2008 Tord Romstad
5 Glaurung is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Glaurung is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
34 //// Local definitions
41 MovePicker::MovegenPhase PhaseTable[32];
42 int MainSearchPhaseIndex;
43 int EvasionsPhaseIndex;
44 int QsearchWithChecksPhaseIndex;
45 int QsearchWithoutChecksPhaseIndex;
56 /// Constructor for the MovePicker class. Apart from the position for which
57 /// it is asked to pick legal moves, MovePicker also wants some information
58 /// to help it to return the presumably good moves first, to decide which
59 /// moves to return (in the quiescence search, for instance, we only want to
60 /// search captures, promotions and some checks) and about how important good
61 /// move ordering is at the current node.
63 MovePicker::MovePicker(const Position& p, bool pvnode, Move ttm, Move mk,
64 Move k1, Move k2, Depth d) : pos(p) {
67 mateKiller = (mk == ttm)? MOVE_NONE : mk;
74 dc = p.discovered_check_candidates(p.side_to_move());
77 phaseIndex = EvasionsPhaseIndex;
78 else if (depth > Depth(0))
79 phaseIndex = MainSearchPhaseIndex;
80 else if (depth == Depth(0))
81 phaseIndex = QsearchWithChecksPhaseIndex;
83 phaseIndex = QsearchWithoutChecksPhaseIndex;
85 pinned = p.pinned_pieces(p.side_to_move());
91 /// MovePicker::get_next_move() is the most important method of the MovePicker
92 /// class. It returns a new legal move every time it is called, until there
93 /// are no more moves left of the types we are interested in.
95 Move MovePicker::get_next_move() {
101 // If we already have a list of generated moves, pick the best move from
102 // the list, and return it.
103 move = pick_move_from_list();
104 if (move != MOVE_NONE)
106 assert(move_is_ok(move));
112 switch (PhaseTable[phaseIndex]) {
115 if (ttMove != MOVE_NONE)
117 assert(move_is_ok(ttMove));
118 if (generate_move_if_legal(pos, ttMove, pinned) != MOVE_NONE)
124 if (mateKiller != MOVE_NONE)
126 assert(move_is_ok(mateKiller));
127 if (generate_move_if_legal(pos, mateKiller, pinned) != MOVE_NONE)
132 case PH_GOOD_CAPTURES:
133 numOfMoves = generate_captures(pos, moves);
138 case PH_BAD_CAPTURES:
139 badCapturesPicked = 0;
143 numOfMoves = generate_noncaptures(pos, moves);
149 assert(pos.is_check());
150 numOfMoves = generate_evasions(pos, moves);
156 numOfMoves = generate_captures(pos, moves);
162 numOfMoves = generate_checks(pos, moves, dc);
180 /// A variant of get_next_move() which takes a lock as a parameter, used to
181 /// prevent multiple threads from picking the same move at a split point.
183 Move MovePicker::get_next_move(Lock &lock) {
191 Move m = get_next_move();
199 /// MovePicker::score_captures(), MovePicker::score_noncaptures(),
200 /// MovePicker::score_evasions() and MovePicker::score_qcaptures() assign a
201 /// numerical move ordering score to each move in a move list. The moves
202 /// with highest scores will be picked first by pick_move_from_list().
204 void MovePicker::score_captures() {
205 // Winning and equal captures in the main search are ordered by MVV.
206 // Suprisingly, this appears to perform slightly better than SEE based
207 // move ordering. The reason is probably that in a position with a winning
208 // capture, capturing a more valuable (but sufficiently defended) piece
209 // first usually doesn't hurt. The opponent will have to recapture, and
210 // the hanging piece will still be hanging (except in the unusual cases
211 // where it is possible to recapture with the hanging piece). Exchanging
212 // big pieces before capturing a hanging piece probably helps to reduce
214 for (int i = 0; i < numOfMoves; i++)
216 Move m = moves[i].move;
217 moves[i].score = pos.see(m);
218 if (moves[i].score >= 0)
219 moves[i].score = move_promotion(m) ? QueenValueMidgame
220 : pos.midgame_value_of_piece_on(move_to(m));
224 void MovePicker::score_noncaptures() {
226 for (int i = 0; i < numOfMoves; i++)
228 Move m = moves[i].move;
230 moves[i].score = HistoryMax + 2;
231 else if (m == killer2)
232 moves[i].score = HistoryMax + 1;
234 moves[i].score = H.move_ordering_score(pos.piece_on(move_from(m)), m);
236 // Ensure moves in history are always sorted as first
237 if (moves[i].score > 0)
238 moves[i].score += 1000;
240 moves[i].score += pos.mg_pst_delta(moves[i].move);
244 void MovePicker::score_evasions() {
246 for (int i = 0; i < numOfMoves; i++)
248 Move m = moves[i].move;
250 moves[i].score = 2*HistoryMax;
251 else if (!pos.square_is_empty(move_to(m)))
253 moves[i].score = pos.see(m);
254 if (moves[i].score >= 0)
255 moves[i].score += HistoryMax;
258 moves[i].score = H.move_ordering_score(pos.piece_on(move_from(m)), m);
260 // FIXME try psqt also here
263 void MovePicker::score_qcaptures() {
266 for (int i = 0; i < numOfMoves; i++)
268 Move m = moves[i].move;
269 moves[i].score = move_promotion(m) ? QueenValueMidgame
270 : pos.midgame_value_of_piece_on(move_to(m));
275 /// find_best_index() loops across the moves and returns index of
276 /// the highest scored one.
278 int MovePicker::find_best_index() {
280 int bestScore = -10000000, bestIndex = -1;
282 for (int i = movesPicked; i < numOfMoves; i++)
283 if (moves[i].score > bestScore)
286 bestScore = moves[i].score;
292 /// MovePicker::pick_move_from_list() picks the move with the biggest score
293 /// from a list of generated moves (moves[] or badCaptures[], depending on
294 /// the current move generation phase). It takes care not to return the
295 /// transposition table move if that has already been serched previously.
296 /// While picking captures in the PH_GOOD_CAPTURES phase (i.e. while picking
297 /// non-losing captures in the main search), it moves all captures with
298 /// negative SEE values to the badCaptures[] array.
300 Move MovePicker::pick_move_from_list() {
305 switch (PhaseTable[phaseIndex]) {
307 case PH_GOOD_CAPTURES:
308 assert(!pos.is_check());
309 assert(movesPicked >= 0);
310 while (movesPicked < numOfMoves)
312 int bestScore = -10000000;
314 for (int i = movesPicked; i < numOfMoves; i++)
316 if (moves[i].score < 0)
318 // Losing capture, move it to the badCaptures[] array
319 assert(numOfBadCaptures < 63);
320 badCaptures[numOfBadCaptures++] = moves[i];
321 moves[i--] = moves[--numOfMoves];
323 else if (moves[i].score > bestScore)
326 bestScore = moves[i].score;
329 if (bestIndex != -1) // Found a good capture
331 move = moves[bestIndex].move;
\r
332 moves[bestIndex] = moves[movesPicked++];
334 && move != mateKiller
335 && pos.move_is_legal(move, pinned))
342 assert(!pos.is_check());
343 assert(movesPicked >= 0);
344 while (movesPicked < numOfMoves)
346 // If this is a PV node or we have only picked a few moves, scan
347 // the entire move list for the best move. If many moves have already
348 // been searched and it is not a PV node, we are probably failing low
349 // anyway, so we just pick the first move from the list.
350 bestIndex = (movesPicked < 12 || pvNode ? find_best_index() : movesPicked);
354 move = moves[bestIndex].move;
\r
355 moves[bestIndex] = moves[movesPicked++];
357 && move != mateKiller
358 && pos.move_is_legal(move, pinned))
365 assert(pos.is_check());
366 assert(movesPicked >= 0);
367 while (movesPicked < numOfMoves)
369 bestIndex = find_best_index();
373 move = moves[bestIndex].move;
\r
374 moves[bestIndex] = moves[movesPicked++];
380 case PH_BAD_CAPTURES:
381 assert(!pos.is_check());
382 assert(badCapturesPicked >= 0);
383 // It's probably a good idea to use SEE move ordering here, instead
384 // of just picking the first move. FIXME
385 while (badCapturesPicked < numOfBadCaptures)
387 move = badCaptures[badCapturesPicked++].move;
389 && move != mateKiller
390 && pos.move_is_legal(move, pinned))
396 assert(!pos.is_check());
397 assert(movesPicked >= 0);
398 while (movesPicked < numOfMoves)
400 // FIXME makes sens to score qcaps?
401 bestIndex = (movesPicked < 4 ? find_best_index() : movesPicked);
405 move = moves[bestIndex].move;
\r
406 moves[bestIndex] = moves[movesPicked++];
407 if (move != ttMove && pos.move_is_legal(move, pinned))
414 assert(!pos.is_check());
415 assert(movesPicked >= 0);
416 // Perhaps we should do something better than just picking the first
418 while (movesPicked < numOfMoves)
420 move = moves[movesPicked++].move;
421 if (move != ttMove && pos.move_is_legal(move, pinned))
433 /// MovePicker::current_move_type() returns the type of the just
434 /// picked next move. It can be used in search to further differentiate
435 /// according to the current move type: capture, non capture, escape, etc.
436 MovePicker::MovegenPhase MovePicker::current_move_type() const {
438 return PhaseTable[phaseIndex];
442 /// MovePicker::init_phase_table() initializes the PhaseTable[],
443 /// MainSearchPhaseIndex, EvasionPhaseIndex, QsearchWithChecksPhaseIndex
444 /// and QsearchWithoutChecksPhaseIndex variables. It is only called once
445 /// during program startup, and never again while the program is running.
447 void MovePicker::init_phase_table() {
452 MainSearchPhaseIndex = i - 1;
453 PhaseTable[i++] = PH_TT_MOVE;
454 PhaseTable[i++] = PH_MATE_KILLER;
455 PhaseTable[i++] = PH_GOOD_CAPTURES;
456 // PH_KILLER_1 and PH_KILLER_2 are not yet used.
457 // PhaseTable[i++] = PH_KILLER_1;
458 // PhaseTable[i++] = PH_KILLER_2;
459 PhaseTable[i++] = PH_NONCAPTURES;
460 PhaseTable[i++] = PH_BAD_CAPTURES;
461 PhaseTable[i++] = PH_STOP;
464 EvasionsPhaseIndex = i - 1;
465 PhaseTable[i++] = PH_EVASIONS;
466 PhaseTable[i++] = PH_STOP;
468 // Quiescence search with checks
469 QsearchWithChecksPhaseIndex = i - 1;
470 PhaseTable[i++] = PH_QCAPTURES;
471 PhaseTable[i++] = PH_QCHECKS;
472 PhaseTable[i++] = PH_STOP;
474 // Quiescence search without checks
475 QsearchWithoutChecksPhaseIndex = i - 1;
476 PhaseTable[i++] = PH_QCAPTURES;
477 PhaseTable[i++] = PH_STOP;