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 int MainSearchPhaseIndex;
42 int EvasionsPhaseIndex;
43 int QsearchWithChecksPhaseIndex;
44 int QsearchWithoutChecksPhaseIndex;
48 // Static array definition
49 MovePicker::MovegenPhase MovePicker::PhaseTable[32];
57 /// Constructor for the MovePicker class. Apart from the position for which
58 /// it is asked to pick legal moves, MovePicker also wants some information
59 /// to help it to return the presumably good moves first, to decide which
60 /// moves to return (in the quiescence search, for instance, we only want to
61 /// search captures, promotions and some checks) and about how important good
62 /// move ordering is at the current node.
64 MovePicker::MovePicker(Position &p, bool pvnode, Move ttm, Move mk,
65 Move k1, Move k2, Depth dpth) {
69 mateKiller = (mk == ttm)? MOVE_NONE : mk;
76 dc = p.discovered_check_candidates(p.side_to_move());
79 phaseIndex = EvasionsPhaseIndex;
80 else if(depth > Depth(0))
81 phaseIndex = MainSearchPhaseIndex;
82 else if(depth == Depth(0))
83 phaseIndex = QsearchWithChecksPhaseIndex;
85 phaseIndex = QsearchWithoutChecksPhaseIndex;
87 pinned = p.pinned_pieces(p.side_to_move());
93 /// MovePicker::get_next_move() is the most important method of the MovePicker
94 /// class. It returns a new legal move every time it is called, until there
95 /// are no more moves left of the types we are interested in.
97 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 = this->pick_move_from_list();
104 if(move != MOVE_NONE) {
105 assert(move_is_ok(move));
111 switch(PhaseTable[phaseIndex]) {
114 if(ttMove != MOVE_NONE) {
115 assert(move_is_ok(ttMove));
116 Move m = generate_move_if_legal(*pos, ttMove, pinned);
125 if(mateKiller != MOVE_NONE) {
126 assert(move_is_ok(mateKiller));
127 Move m = generate_move_if_legal(*pos, mateKiller, pinned);
129 assert(m == mateKiller);
135 case PH_GOOD_CAPTURES:
136 // pinned = pos->pinned_pieces(pos->side_to_move());
137 numOfMoves = generate_captures(*pos, moves);
138 this->score_captures();
142 case PH_BAD_CAPTURES:
143 badCapturesPicked = 0;
147 numOfMoves = generate_noncaptures(*pos, moves);
148 this->score_noncaptures();
153 assert(pos->is_check());
154 // pinned = pos->pinned_pieces(pos->side_to_move());
155 numOfMoves = generate_evasions(*pos, moves);
156 this->score_evasions();
161 // pinned = pos->pinned_pieces(pos->side_to_move());
162 numOfMoves = generate_captures(*pos, moves);
163 this->score_qcaptures();
168 numOfMoves = generate_checks(*pos, moves, dc);
187 /// A variant of get_next_move() which takes a lock as a parameter, used to
188 /// prevent multiple threads from picking the same move at a split point.
190 Move MovePicker::get_next_move(Lock &lock) {
198 m = this->get_next_move();
207 /// MovePicker::number_of_moves() simply returns the numOfMoves member
208 /// variable. It is intended to be used in positions where the side to move
209 /// is in check, for detecting checkmates or situations where there is only
210 /// a single reply to check.
212 int MovePicker::number_of_moves() const {
217 /// MovePicker::score_captures(), MovePicker::score_noncaptures(),
218 /// MovePicker::score_evasions() and MovePicker::score_qcaptures() assign a
219 /// numerical move ordering score to each move in a move list. The moves
220 /// with highest scores will be picked first by
221 /// MovePicker::pick_move_from_list().
223 void MovePicker::score_captures() {
224 // Winning and equal captures in the main search are ordered by MVV/LVA.
225 // Suprisingly, this appears to perform slightly better than SEE based
226 // move ordering. The reason is probably that in a position with a winning
227 // capture, capturing a more valuable (but sufficiently defended) piece
228 // first usually doesn't hurt. The opponent will have to recapture, and
229 // the hanging piece will still be hanging (except in the unusual cases
230 // where it is possible to recapture with the hanging piece). Exchanging
231 // big pieces before capturing a hanging piece probably helps to reduce
233 for(int i = 0; i < numOfMoves; i++) {
234 int seeValue = pos->see(moves[i].move);
236 if(move_promotion(moves[i].move))
237 moves[i].score = QueenValueMidgame;
240 int(pos->midgame_value_of_piece_on(move_to(moves[i].move))) -
241 int(pos->type_of_piece_on(move_from(moves[i].move)));
244 moves[i].score = seeValue;
249 void MovePicker::score_noncaptures() {
250 for(int i = 0; i < numOfMoves; i++) {
251 Move m = moves[i].move;
253 moves[i].score = HistoryMax + 2;
254 else if(m == killer2)
255 moves[i].score = HistoryMax + 1;
257 moves[i].score = H.move_ordering_score(pos->piece_on(move_from(m)), m);
261 void MovePicker::score_evasions() {
262 for(int i = 0; i < numOfMoves; i++) {
263 Move m = moves[i].move;
265 moves[i].score = 2*HistoryMax;
266 else if(!pos->square_is_empty(move_to(m))) {
267 int seeScore = pos->see(m);
268 moves[i].score = (seeScore >= 0)? seeScore + HistoryMax : seeScore;
271 moves[i].score = H.move_ordering_score(pos->piece_on(move_from(m)), m);
275 void MovePicker::score_qcaptures() {
276 // Use MVV/LVA ordering.
277 for(int i = 0; i < numOfMoves; i++) {
278 Move m = moves[i].move;
279 if(move_promotion(m))
280 moves[i].score = QueenValueMidgame;
283 int(pos->midgame_value_of_piece_on(move_to(m))) -
284 int(pos->midgame_value_of_piece_on(move_to(m))) / 64;
289 /// MovePicker::pick_move_from_list() picks the move with the biggest score
290 /// from a list of generated moves (moves[] or badCaptures[], depending on
291 /// the current move generation phase). It takes care not to return the
292 /// transposition table move if that has already been serched previously.
293 /// While picking captures in the PH_GOOD_CAPTURES phase (i.e. while picking
294 /// non-losing captures in the main search), it moves all captures with
295 /// negative SEE values to the badCaptures[] array.
297 Move MovePicker::pick_move_from_list() {
298 int bestScore = -10000000;
302 switch(PhaseTable[phaseIndex]) {
304 case PH_GOOD_CAPTURES:
305 assert(!pos->is_check());
306 assert(movesPicked >= 0);
307 while(movesPicked < numOfMoves) {
308 bestScore = -10000000;
310 for(int i = movesPicked; i < numOfMoves; i++) {
311 if(moves[i].score < 0) {
312 // Losing capture, move it to the badCaptures[] array
313 assert(numOfBadCaptures < 63);
314 badCaptures[numOfBadCaptures++] = moves[i];
315 moves[i--] = moves[--numOfMoves];
317 else if(moves[i].score > bestScore) {
319 bestScore = moves[i].score;
322 if(bestIndex != -1) { // Found a good capture
323 move = moves[bestIndex].move;
\r
324 moves[bestIndex] = moves[movesPicked++];
325 if(move != ttMove && move != mateKiller &&
326 pos->move_is_legal(move, pinned))
333 assert(!pos->is_check());
334 assert(movesPicked >= 0);
335 while(movesPicked < numOfMoves) {
336 bestScore = -10000000;
338 // If this is a PV node or we have only picked a few moves, scan
339 // the entire move list for the best move. If many moves have already
340 // been searched and it is not a PV node, we are probably failing low
341 // anyway, so we just pick the first move from the list.
342 if(pvNode || movesPicked < 12) {
344 for(int i = movesPicked; i < numOfMoves; i++)
345 if(moves[i].score > bestScore) {
347 bestScore = moves[i].score;
351 bestIndex = movesPicked;
353 if(bestIndex != -1) {
354 move = moves[bestIndex].move;
\r
355 moves[bestIndex] = moves[movesPicked++];
356 if(move != ttMove && move != mateKiller &&
357 pos->move_is_legal(move, pinned))
364 assert(pos->is_check());
365 assert(movesPicked >= 0);
366 while(movesPicked < numOfMoves) {
367 bestScore = -10000000;
369 for(int i = movesPicked; i < numOfMoves; i++)
370 if(moves[i].score > bestScore) {
372 bestScore = moves[i].score;
375 if(bestIndex != -1) {
376 move = moves[bestIndex].move;
\r
377 moves[bestIndex] = moves[movesPicked++];
383 case PH_BAD_CAPTURES:
384 assert(!pos->is_check());
385 assert(badCapturesPicked >= 0);
386 // It's probably a good idea to use SEE move ordering here, instead
387 // of just picking the first move. FIXME
388 while(badCapturesPicked < numOfBadCaptures) {
389 move = badCaptures[badCapturesPicked++].move;
390 if(move != ttMove && move != mateKiller &&
391 pos->move_is_legal(move, pinned))
397 assert(!pos->is_check());
398 assert(movesPicked >= 0);
399 while(movesPicked < numOfMoves) {
400 bestScore = -10000000;
401 if(movesPicked < 4) {
403 for(int i = movesPicked; i < numOfMoves; i++)
404 if(moves[i].score > bestScore) {
406 bestScore = moves[i].score;
410 bestIndex = movesPicked;
412 if(bestIndex != -1) {
413 move = moves[bestIndex].move;
\r
414 moves[bestIndex] = moves[movesPicked++];
415 // Remember to change the line below if we decide to hash the qsearch!
416 // Maybe also postpone the legality check until after futility pruning?
417 if(/* move != ttMove && */ pos->move_is_legal(move, pinned))
424 assert(!pos->is_check());
425 assert(movesPicked >= 0);
426 // Perhaps we should do something better than just picking the first
428 while(movesPicked < numOfMoves) {
429 move = moves[movesPicked++].move;
430 // Remember to change the line below if we decide to hash the qsearch!
431 if(/* move != ttMove && */ pos->move_is_legal(move, pinned))
444 /// MovePicker::init_phase_table() initializes the PhaseTable[],
445 /// MainSearchPhaseIndex, EvasionPhaseIndex, QsearchWithChecksPhaseIndex
446 /// and QsearchWithoutChecksPhaseIndex variables. It is only called once
447 /// during program startup, and never again while the program is running.
449 void MovePicker::init_phase_table() {
453 MainSearchPhaseIndex = i - 1;
454 PhaseTable[i++] = PH_TT_MOVE;
455 PhaseTable[i++] = PH_MATE_KILLER;
456 PhaseTable[i++] = PH_GOOD_CAPTURES;
457 // PH_KILLER_1 and PH_KILLER_2 are not yet used.
458 // PhaseTable[i++] = PH_KILLER_1;
459 // PhaseTable[i++] = PH_KILLER_2;
460 PhaseTable[i++] = PH_NONCAPTURES;
461 PhaseTable[i++] = PH_BAD_CAPTURES;
462 PhaseTable[i++] = PH_STOP;
465 EvasionsPhaseIndex = i - 1;
466 PhaseTable[i++] = PH_EVASIONS;
467 PhaseTable[i++] = PH_STOP;
469 // Quiescence search with checks
470 QsearchWithChecksPhaseIndex = i - 1;
471 PhaseTable[i++] = PH_QCAPTURES;
472 PhaseTable[i++] = PH_QCHECKS;
473 PhaseTable[i++] = PH_STOP;
475 // Quiescence search without checks
476 QsearchWithoutChecksPhaseIndex = i - 1;
477 PhaseTable[i++] = PH_QCAPTURES;
478 PhaseTable[i++] = PH_STOP;