2008-08-31 23:59:13 -06:00
|
|
|
/*
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
|
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
2016-01-02 02:43:25 -07:00
|
|
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
2018-11-19 03:18:21 -07:00
|
|
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
2008-08-31 23:59:13 -06:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2008-10-25 08:28:24 -06:00
|
|
|
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
2008-08-31 23:59:13 -06:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2008-10-25 08:28:24 -06:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#include "movepick.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2013-07-21 01:54:56 -06:00
|
|
|
enum Stages {
|
2018-03-20 18:48:31 -06:00
|
|
|
MAIN_TT, CAPTURE_INIT, GOOD_CAPTURE, REFUTATION, QUIET_INIT, QUIET, BAD_CAPTURE,
|
2018-03-18 17:56:19 -06:00
|
|
|
EVASION_TT, EVASION_INIT, EVASION,
|
|
|
|
PROBCUT_TT, PROBCUT_INIT, PROBCUT,
|
|
|
|
QSEARCH_TT, QCAPTURE_INIT, QCAPTURE, QCHECK_INIT, QCHECK
|
2009-08-31 06:28:11 -06:00
|
|
|
};
|
|
|
|
|
2017-04-22 01:03:17 -06:00
|
|
|
// partial_insertion_sort() sorts moves in descending order up to and including
|
|
|
|
// a given limit. The order of moves smaller than the limit is left unspecified.
|
|
|
|
void partial_insertion_sort(ExtMove* begin, ExtMove* end, int limit) {
|
|
|
|
|
2017-05-07 21:14:23 -06:00
|
|
|
for (ExtMove *sortedEnd = begin, *p = begin + 1; p < end; ++p)
|
2017-04-20 12:15:48 -06:00
|
|
|
if (p->value >= limit)
|
|
|
|
{
|
|
|
|
ExtMove tmp = *p, *q;
|
2017-05-07 21:14:23 -06:00
|
|
|
*p = *++sortedEnd;
|
|
|
|
for (q = sortedEnd; q != begin && *(q - 1) < tmp; --q)
|
|
|
|
*q = *(q - 1);
|
2017-04-20 12:15:48 -06:00
|
|
|
*q = tmp;
|
|
|
|
}
|
2013-02-10 11:10:01 -07:00
|
|
|
}
|
|
|
|
|
2014-11-01 14:35:10 -06:00
|
|
|
} // namespace
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2012-01-21 15:32:48 -07:00
|
|
|
|
|
|
|
/// Constructors of the MovePicker class. As arguments we pass information
|
2013-12-04 23:18:12 -07:00
|
|
|
/// to help it to return the (presumably) good moves first, to decide which
|
2008-08-31 23:59:13 -06:00
|
|
|
/// moves to return (in the quiescence search, for instance, we only want to
|
2016-01-16 14:34:29 -07:00
|
|
|
/// search captures, promotions, and some checks) and how important good move
|
2013-12-04 23:18:12 -07:00
|
|
|
/// ordering is at the current node.
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2017-08-21 10:59:24 -06:00
|
|
|
/// MovePicker constructor for the main search
|
2017-06-30 09:20:00 -06:00
|
|
|
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh,
|
2018-03-20 18:48:31 -06:00
|
|
|
const CapturePieceToHistory* cph, const PieceToHistory** ch, Move cm, Move* killers)
|
2018-07-19 10:26:05 -06:00
|
|
|
: pos(p), mainHistory(mh), captureHistory(cph), continuationHistory(ch),
|
2018-03-27 08:44:47 -06:00
|
|
|
refutations{{killers[0], 0}, {killers[1], 0}, {cm, 0}}, depth(d) {
|
2009-08-31 06:28:11 -06:00
|
|
|
|
2011-01-23 14:59:05 -07:00
|
|
|
assert(d > DEPTH_ZERO);
|
|
|
|
|
2018-03-18 17:56:19 -06:00
|
|
|
stage = pos.checkers() ? EVASION_TT : MAIN_TT;
|
2018-12-06 07:00:38 -07:00
|
|
|
ttMove = ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE;
|
2016-09-10 13:37:36 -06:00
|
|
|
stage += (ttMove == MOVE_NONE);
|
2011-01-23 14:59:05 -07:00
|
|
|
}
|
|
|
|
|
2017-08-21 10:59:24 -06:00
|
|
|
/// MovePicker constructor for quiescence search
|
2018-03-18 17:56:19 -06:00
|
|
|
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh,
|
2018-07-19 10:26:05 -06:00
|
|
|
const CapturePieceToHistory* cph, const PieceToHistory** ch, Square rs)
|
|
|
|
: pos(p), mainHistory(mh), captureHistory(cph), continuationHistory(ch), recaptureSquare(rs), depth(d) {
|
2011-01-23 14:59:05 -07:00
|
|
|
|
|
|
|
assert(d <= DEPTH_ZERO);
|
|
|
|
|
2018-03-18 17:56:19 -06:00
|
|
|
stage = pos.checkers() ? EVASION_TT : QSEARCH_TT;
|
2019-01-01 06:13:08 -07:00
|
|
|
ttMove = ttm
|
|
|
|
&& (depth > DEPTH_QS_RECAPTURES || to_sq(ttm) == recaptureSquare)
|
|
|
|
&& pos.pseudo_legal(ttm) ? ttm : MOVE_NONE;
|
2016-09-10 13:37:36 -06:00
|
|
|
stage += (ttMove == MOVE_NONE);
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
2018-03-27 08:44:47 -06:00
|
|
|
/// MovePicker constructor for ProbCut: we generate captures with SEE greater
|
2017-08-21 10:59:24 -06:00
|
|
|
/// than or equal to the given threshold.
|
2017-11-03 05:37:11 -06:00
|
|
|
MovePicker::MovePicker(const Position& p, Move ttm, Value th, const CapturePieceToHistory* cph)
|
|
|
|
: pos(p), captureHistory(cph), threshold(th) {
|
2011-05-21 10:17:11 -06:00
|
|
|
|
2012-12-25 09:59:35 -07:00
|
|
|
assert(!pos.checkers());
|
2011-05-21 10:17:11 -06:00
|
|
|
|
2018-03-18 17:56:19 -06:00
|
|
|
stage = PROBCUT_TT;
|
2018-12-06 07:00:38 -07:00
|
|
|
ttMove = ttm
|
2015-07-19 03:25:14 -06:00
|
|
|
&& pos.capture(ttm)
|
2019-01-01 06:13:08 -07:00
|
|
|
&& pos.pseudo_legal(ttm)
|
2017-04-22 01:03:17 -06:00
|
|
|
&& pos.see_ge(ttm, threshold) ? ttm : MOVE_NONE;
|
2016-09-10 13:37:36 -06:00
|
|
|
stage += (ttMove == MOVE_NONE);
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
2018-03-18 17:56:19 -06:00
|
|
|
/// MovePicker::score() assigns a numerical value to each move in a list, used
|
|
|
|
/// for sorting. Captures are ordered by Most Valuable Victim (MVV), preferring
|
|
|
|
/// captures with a good history. Quiets moves are ordered using the histories.
|
2017-08-21 10:59:24 -06:00
|
|
|
template<GenType Type>
|
2017-08-09 06:04:59 -06:00
|
|
|
void MovePicker::score() {
|
2008-10-25 08:28:24 -06:00
|
|
|
|
2017-08-21 10:59:24 -06:00
|
|
|
static_assert(Type == CAPTURES || Type == QUIETS || Type == EVASIONS, "Wrong type");
|
|
|
|
|
2015-05-01 18:36:39 -06:00
|
|
|
for (auto& m : *this)
|
2017-08-21 10:59:24 -06:00
|
|
|
if (Type == CAPTURES)
|
|
|
|
m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
|
2018-08-14 01:24:26 -06:00
|
|
|
+ (*captureHistory)[pos.moved_piece(m)][to_sq(m)][type_of(pos.piece_on(to_sq(m)))] / 8;
|
2017-08-21 10:59:24 -06:00
|
|
|
|
|
|
|
else if (Type == QUIETS)
|
2017-08-09 06:04:59 -06:00
|
|
|
m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
|
2018-07-19 10:26:05 -06:00
|
|
|
+ (*continuationHistory[0])[pos.moved_piece(m)][to_sq(m)]
|
|
|
|
+ (*continuationHistory[1])[pos.moved_piece(m)][to_sq(m)]
|
2019-02-27 13:25:12 -07:00
|
|
|
+ (*continuationHistory[3])[pos.moved_piece(m)][to_sq(m)]
|
|
|
|
+ (*continuationHistory[5])[pos.moved_piece(m)][to_sq(m)] / 2;
|
2017-08-09 06:04:59 -06:00
|
|
|
|
2017-08-21 10:59:24 -06:00
|
|
|
else // Type == EVASIONS
|
|
|
|
{
|
|
|
|
if (pos.capture(m))
|
|
|
|
m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
|
|
|
|
- Value(type_of(pos.moved_piece(m)));
|
|
|
|
else
|
2018-07-19 10:26:05 -06:00
|
|
|
m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
|
|
|
|
+ (*continuationHistory[0])[pos.moved_piece(m)][to_sq(m)]
|
|
|
|
- (1 << 28);
|
2017-08-21 10:59:24 -06:00
|
|
|
}
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
2018-03-27 08:44:47 -06:00
|
|
|
/// MovePicker::select() returns the next move satisfying a predicate function.
|
2018-03-20 18:48:31 -06:00
|
|
|
/// It never returns the TT move.
|
2018-03-27 08:44:47 -06:00
|
|
|
template<MovePicker::PickType T, typename Pred>
|
|
|
|
Move MovePicker::select(Pred filter) {
|
Lazy SMP
Start all threads searching on root position and
use only the shared TT table as synching scheme.
It seems this scheme scales better than YBWC for
high number of threads.
Verified for nor regression at STC 3 threads
LLR: -2.95 (-2.94,2.94) [-3.00,1.00]
Total: 40232 W: 6908 L: 7130 D: 26194
Verified for nor regression at LTC 3 threads
LLR: 2.95 (-2.94,2.94) [-3.00,1.00]
Total: 28186 W: 3908 L: 3798 D: 20480
Verified for nor regression at STC 7 threads
LLR: 2.95 (-2.94,2.94) [-3.00,1.00]
Total: 3607 W: 674 L: 526 D: 2407
Verified for nor regression at LTC 7 threads
LLR: 2.95 (-2.94,2.94) [-3.00,1.00]
Total: 4235 W: 671 L: 528 D: 3036
Tested with fixed games at LTC with 20 threads
ELO: 44.75 +-7.6 (95%) LOS: 100.0%
Total: 2069 W: 407 L: 142 D: 1520
Tested with fixed games at XLTC (120secs) with 20 threads
ELO: 28.01 +-6.7 (95%) LOS: 100.0%
Total: 2275 W: 349 L: 166 D: 1760
Original patch of mbootsector, with additional work
from Ivan Ivec (log formula), Joerg Oster (id loop
simplification) and Marco Costalba (assorted formatting
and rework).
Bench: 8116244
2015-10-06 00:15:17 -06:00
|
|
|
|
2018-03-18 17:56:19 -06:00
|
|
|
while (cur < endMoves)
|
|
|
|
{
|
2018-03-27 08:44:47 -06:00
|
|
|
if (T == Best)
|
2018-03-18 17:56:19 -06:00
|
|
|
std::swap(*cur, *std::max_element(cur, endMoves));
|
2008-10-16 06:25:56 -06:00
|
|
|
|
2019-04-17 13:13:39 -06:00
|
|
|
if (*cur != ttMove && filter())
|
|
|
|
return *cur++;
|
2009-08-30 02:42:55 -06:00
|
|
|
|
2019-04-17 13:13:39 -06:00
|
|
|
cur++;
|
2018-03-18 17:56:19 -06:00
|
|
|
}
|
2019-04-17 13:13:39 -06:00
|
|
|
return MOVE_NONE;
|
2018-03-18 17:56:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// MovePicker::next_move() is the most important method of the MovePicker class. It
|
2018-03-20 18:48:31 -06:00
|
|
|
/// returns a new pseudo legal move every time it is called until there are no more
|
|
|
|
/// moves left, picking the move with the highest score from a list of generated moves.
|
2018-03-18 17:56:19 -06:00
|
|
|
Move MovePicker::next_move(bool skipQuiets) {
|
2018-03-11 19:47:35 -06:00
|
|
|
|
2018-03-18 17:56:19 -06:00
|
|
|
top:
|
2016-09-10 13:37:36 -06:00
|
|
|
switch (stage) {
|
2010-02-05 10:17:52 -07:00
|
|
|
|
2018-03-18 17:56:19 -06:00
|
|
|
case MAIN_TT:
|
|
|
|
case EVASION_TT:
|
|
|
|
case QSEARCH_TT:
|
|
|
|
case PROBCUT_TT:
|
2016-09-10 13:37:36 -06:00
|
|
|
++stage;
|
|
|
|
return ttMove;
|
2010-02-05 10:17:52 -07:00
|
|
|
|
2018-03-18 17:56:19 -06:00
|
|
|
case CAPTURE_INIT:
|
|
|
|
case PROBCUT_INIT:
|
|
|
|
case QCAPTURE_INIT:
|
2018-03-27 08:44:47 -06:00
|
|
|
cur = endBadCaptures = moves;
|
2016-09-10 13:37:36 -06:00
|
|
|
endMoves = generate<CAPTURES>(pos, cur);
|
2018-03-27 08:44:47 -06:00
|
|
|
|
2016-09-10 13:37:36 -06:00
|
|
|
score<CAPTURES>();
|
|
|
|
++stage;
|
2018-03-18 17:56:19 -06:00
|
|
|
goto top;
|
2018-02-25 04:49:03 -07:00
|
|
|
|
2018-03-18 17:56:19 -06:00
|
|
|
case GOOD_CAPTURE:
|
2018-03-27 08:44:47 -06:00
|
|
|
if (select<Best>([&](){
|
2019-04-17 13:13:39 -06:00
|
|
|
return pos.see_ge(*cur, Value(-55 * cur->value / 1024)) ?
|
2018-03-27 08:44:47 -06:00
|
|
|
// Move losing capture to endBadCaptures to be tried later
|
2019-04-17 13:13:39 -06:00
|
|
|
true : (*endBadCaptures++ = *cur, false); }))
|
|
|
|
return *(cur - 1);
|
2018-03-11 19:47:35 -06:00
|
|
|
|
2018-03-20 18:48:31 -06:00
|
|
|
// Prepare the pointers to loop over the refutations array
|
2018-03-27 08:44:47 -06:00
|
|
|
cur = std::begin(refutations);
|
|
|
|
endMoves = std::end(refutations);
|
2018-03-20 18:48:31 -06:00
|
|
|
|
2018-03-11 19:47:35 -06:00
|
|
|
// If the countermove is the same as a killer, skip it
|
2018-03-20 18:48:31 -06:00
|
|
|
if ( refutations[0].move == refutations[2].move
|
|
|
|
|| refutations[1].move == refutations[2].move)
|
|
|
|
--endMoves;
|
|
|
|
|
2018-03-18 17:56:19 -06:00
|
|
|
++stage;
|
2017-04-28 09:30:14 -06:00
|
|
|
/* fallthrough */
|
2016-09-10 13:37:36 -06:00
|
|
|
|
2018-03-20 18:48:31 -06:00
|
|
|
case REFUTATION:
|
2019-04-17 13:13:39 -06:00
|
|
|
if (select<Next>([&](){ return *cur != MOVE_NONE
|
|
|
|
&& !pos.capture(*cur)
|
|
|
|
&& pos.pseudo_legal(*cur); }))
|
|
|
|
return *(cur - 1);
|
2018-03-20 18:48:31 -06:00
|
|
|
++stage;
|
2017-04-28 09:30:14 -06:00
|
|
|
/* fallthrough */
|
2016-09-10 13:37:36 -06:00
|
|
|
|
|
|
|
case QUIET_INIT:
|
2019-06-17 15:35:03 -06:00
|
|
|
if (!skipQuiets)
|
|
|
|
{
|
|
|
|
cur = endBadCaptures;
|
|
|
|
endMoves = generate<QUIETS>(pos, cur);
|
2018-03-27 08:44:47 -06:00
|
|
|
|
2019-06-17 15:35:03 -06:00
|
|
|
score<QUIETS>();
|
|
|
|
partial_insertion_sort(cur, endMoves, -4000 * depth / ONE_PLY);
|
|
|
|
}
|
|
|
|
|
2016-09-10 13:37:36 -06:00
|
|
|
++stage;
|
2017-04-28 09:30:14 -06:00
|
|
|
/* fallthrough */
|
2011-04-16 04:24:30 -06:00
|
|
|
|
2016-09-10 13:37:36 -06:00
|
|
|
case QUIET:
|
2018-03-18 17:56:19 -06:00
|
|
|
if ( !skipQuiets
|
2019-04-17 13:13:39 -06:00
|
|
|
&& select<Next>([&](){return *cur != refutations[0].move
|
|
|
|
&& *cur != refutations[1].move
|
|
|
|
&& *cur != refutations[2].move;}))
|
|
|
|
return *(cur - 1);
|
2018-03-18 17:56:19 -06:00
|
|
|
|
2018-03-20 18:48:31 -06:00
|
|
|
// Prepare the pointers to loop over the bad captures
|
2018-03-27 08:44:47 -06:00
|
|
|
cur = moves;
|
|
|
|
endMoves = endBadCaptures;
|
|
|
|
|
2016-09-10 13:37:36 -06:00
|
|
|
++stage;
|
2017-04-28 09:30:14 -06:00
|
|
|
/* fallthrough */
|
2009-08-30 02:42:55 -06:00
|
|
|
|
2018-03-18 17:56:19 -06:00
|
|
|
case BAD_CAPTURE:
|
2019-01-01 05:36:56 -07:00
|
|
|
return select<Next>([](){ return true; });
|
2016-09-10 13:37:36 -06:00
|
|
|
|
2018-03-18 17:56:19 -06:00
|
|
|
case EVASION_INIT:
|
2016-09-10 13:37:36 -06:00
|
|
|
cur = moves;
|
|
|
|
endMoves = generate<EVASIONS>(pos, cur);
|
2018-03-27 08:44:47 -06:00
|
|
|
|
2016-10-16 07:20:05 -06:00
|
|
|
score<EVASIONS>();
|
2016-09-10 13:37:36 -06:00
|
|
|
++stage;
|
2017-04-28 09:30:14 -06:00
|
|
|
/* fallthrough */
|
2009-10-20 04:38:20 -06:00
|
|
|
|
2018-03-18 17:56:19 -06:00
|
|
|
case EVASION:
|
2019-01-01 05:36:56 -07:00
|
|
|
return select<Best>([](){ return true; });
|
2016-09-15 00:38:20 -06:00
|
|
|
|
2018-03-18 17:56:19 -06:00
|
|
|
case PROBCUT:
|
2019-04-17 13:13:39 -06:00
|
|
|
return select<Best>([&](){ return pos.see_ge(*cur, threshold); });
|
2012-01-21 15:32:48 -07:00
|
|
|
|
2018-03-18 17:56:19 -06:00
|
|
|
case QCAPTURE:
|
2018-03-27 08:44:47 -06:00
|
|
|
if (select<Best>([&](){ return depth > DEPTH_QS_RECAPTURES
|
2019-04-17 13:13:39 -06:00
|
|
|
|| to_sq(*cur) == recaptureSquare; }))
|
|
|
|
return *(cur - 1);
|
2018-03-18 17:56:19 -06:00
|
|
|
|
|
|
|
// If we did not find any move and we do not try checks, we have finished
|
|
|
|
if (depth != DEPTH_QS_CHECKS)
|
|
|
|
return MOVE_NONE;
|
|
|
|
|
|
|
|
++stage;
|
|
|
|
/* fallthrough */
|
|
|
|
|
|
|
|
case QCHECK_INIT:
|
2016-09-15 00:38:20 -06:00
|
|
|
cur = moves;
|
|
|
|
endMoves = generate<QUIET_CHECKS>(pos, cur);
|
2018-03-27 08:44:47 -06:00
|
|
|
|
2016-09-15 00:38:20 -06:00
|
|
|
++stage;
|
2017-04-28 09:30:14 -06:00
|
|
|
/* fallthrough */
|
2016-09-15 00:38:20 -06:00
|
|
|
|
2018-03-18 17:56:19 -06:00
|
|
|
case QCHECK:
|
2019-01-01 05:36:56 -07:00
|
|
|
return select<Next>([](){ return true; });
|
2009-08-27 01:12:51 -06:00
|
|
|
}
|
2016-09-10 13:37:36 -06:00
|
|
|
|
2018-03-18 17:56:19 -06:00
|
|
|
assert(false);
|
|
|
|
return MOVE_NONE; // Silence warning
|
2009-08-27 01:12:51 -06:00
|
|
|
}
|