#include "misc.h"
#include "movegen.h"
#include "movepick.h"
+#include "position.h"
#include "search.h"
#include "timeman.h"
#include "thread.h"
EasyMoveManager EasyMove;
Value DrawValue[COLOR_NB];
- CounterMoveHistoryStats CounterMoveHistory;
template <NodeType NT>
Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, bool cutNode);
void Search::clear() {
TT.clear();
- CounterMoveHistory.clear();
for (Thread* th : Threads)
{
th->history.clear();
th->counterMoves.clear();
th->fromTo.clear();
+ th->counterMoveHistory.clear();
}
Threads.main()->previousScore = VALUE_INFINITE;
if ( rootMoves.size() == 1
|| Time.elapsed() > Time.optimum() * unstablePvFactor * improvingFactor / 628
- || (mainThread->easyMovePlayed = doEasyMove))
+ || (mainThread->easyMovePlayed = doEasyMove, doEasyMove))
{
// If we are allowed to ponder do not stop the search now but
// keep pondering until the GUI sends "ponderhit" or "stop".
TTEntry* tte;
Key posKey;
Move ttMove, move, excludedMove, bestMove;
- Depth extension, newDepth, predictedDepth;
+ Depth extension, newDepth;
Value bestValue, value, ttValue, eval, nullValue;
bool ttHit, inCheck, givesCheck, singularExtensionNode, improving;
bool captureOrPromotion, doFullDepthSearch, moveCountPruning;
// search to overwrite a previous full search TT value, so we use a different
// position key in case of an excluded move.
excludedMove = ss->excludedMove;
- posKey = excludedMove ? pos.exclusion_key() : pos.key();
+ posKey = pos.key() ^ Key(excludedMove);
tte = TT.probe(posKey, ttHit);
ttValue = ttHit ? value_from_tt(tte->value(), ss->ply) : VALUE_NONE;
ttMove = rootNode ? thisThread->rootMoves[thisThread->PVIdx].pv[0]
}
// Extra penalty for a quiet TT move in previous ply when it gets refuted
- if ((ss-1)->moveCount == 1 && !pos.captured_piece_type())
+ if ((ss-1)->moveCount == 1 && !pos.captured_piece())
{
Value penalty = Value(d * d + 4 * d + 1);
Square prevSq = to_sq((ss-1)->currentMove);
// Step 6. Razoring (skipped when in check)
if ( !PvNode
&& depth < 4 * ONE_PLY
- && eval + razor_margin[depth / ONE_PLY] <= alpha
- && ttMove == MOVE_NONE)
+ && ttMove == MOVE_NONE
+ && eval + razor_margin[depth / ONE_PLY] <= alpha)
{
- if ( depth <= ONE_PLY
- && eval + razor_margin[3 * ONE_PLY] <= alpha)
+ if (depth <= ONE_PLY)
return qsearch<NonPV, false>(pos, ss, alpha, beta, DEPTH_ZERO);
Value ralpha = alpha - razor_margin[depth / ONE_PLY];
}
// Step 9. ProbCut (skipped when in check)
- // If we have a very good capture (i.e. SEE > seeValues[captured_piece_type])
- // and a reduced search returns a value much above beta, we can (almost)
- // safely prune the previous move.
+ // If we have a good enough capture and a reduced search returns a value
+ // much above beta, we can (almost) safely prune the previous move.
if ( !PvNode
&& depth >= 5 * ONE_PLY
&& abs(beta) < VALUE_MATE_IN_MAX_PLY)
assert((ss-1)->currentMove != MOVE_NONE);
assert((ss-1)->currentMove != MOVE_NULL);
- MovePicker mp(pos, ttMove, PieceValue[MG][pos.captured_piece_type()]);
+ MovePicker mp(pos, ttMove, rbeta - ss->staticEval);
while ((move = mp.next_move()) != MOVE_NONE)
if (pos.legal(move))
{
ss->currentMove = move;
- ss->counterMoves = &CounterMoveHistory[pos.moved_piece(move)][to_sq(move)];
+ ss->counterMoves = &thisThread->counterMoveHistory[pos.moved_piece(move)][to_sq(move)];
pos.do_move(move, st, pos.gives_check(move));
value = -search<NonPV>(pos, ss+1, -rbeta, -rbeta+1, rdepth, !cutNode);
pos.undo_move(move);
singularExtensionNode = !rootNode
&& depth >= 8 * ONE_PLY
&& ttMove != MOVE_NONE
- /* && ttValue != VALUE_NONE Already implicit in the next condition */
- && abs(ttValue) < VALUE_KNOWN_WIN
+ && ttValue != VALUE_NONE
&& !excludedMove // Recursive singular search is not allowed
&& (tte->bound() & BOUND_LOWER)
&& tte->depth() >= depth - 3 * ONE_PLY;
moved_piece = pos.moved_piece(move);
givesCheck = type_of(move) == NORMAL && !pos.discovered_check_candidates()
- ? pos.check_info().checkSquares[type_of(pos.piece_on(from_sq(move)))] & to_sq(move)
+ ? pos.check_squares(type_of(pos.piece_on(from_sq(move)))) & to_sq(move)
: pos.gives_check(move);
moveCountPruning = depth < 16 * ONE_PLY
// Step 12. Extend checks
if ( givesCheck
&& !moveCountPruning
- && pos.see_sign(move) >= VALUE_ZERO)
+ && pos.see_ge(move, VALUE_ZERO))
extension = ONE_PLY;
// Singular extension search. If all moves but one fail low on a search of
&& !extension
&& pos.legal(move))
{
- Value rBeta = ttValue - 2 * depth / ONE_PLY;
+ Value rBeta = std::max(ttValue - 2 * depth / ONE_PLY, -VALUE_MATE);
Depth d = (depth / (2 * ONE_PLY)) * ONE_PLY;
ss->excludedMove = move;
ss->skipEarlyPruning = true;
newDepth = depth - ONE_PLY + extension;
// Step 13. Pruning at shallow depth
- if ( !rootNode
- && !captureOrPromotion
- && !inCheck
- && !givesCheck
- && !pos.advanced_pawn_push(move)
+ if ( !rootNode
&& bestValue > VALUE_MATED_IN_MAX_PLY)
{
- // Move count based pruning
- if (moveCountPruning)
- continue;
-
- predictedDepth = std::max(newDepth - reduction<PvNode>(improving, depth, moveCount), DEPTH_ZERO);
+ if ( !captureOrPromotion
+ && !givesCheck
+ && !pos.advanced_pawn_push(move))
+ {
+ // Move count based pruning
+ if (moveCountPruning)
+ continue;
- // Countermoves based pruning
- if ( predictedDepth < 3 * ONE_PLY
- && move != ss->killers[0]
- && (!cmh || (*cmh )[moved_piece][to_sq(move)] < VALUE_ZERO)
- && (!fmh || (*fmh )[moved_piece][to_sq(move)] < VALUE_ZERO)
- && (!fmh2 || (*fmh2)[moved_piece][to_sq(move)] < VALUE_ZERO || (cmh && fmh)))
- continue;
+ // Reduced depth of the next LMR search
+ int lmrDepth = std::max(newDepth - reduction<PvNode>(improving, depth, moveCount), DEPTH_ZERO) / ONE_PLY;
- // Futility pruning: parent node
- if ( predictedDepth < 7 * ONE_PLY
- && ss->staticEval + 256 + 200 * predictedDepth / ONE_PLY <= alpha)
- continue;
+ // Countermoves based pruning
+ if ( lmrDepth < 3
+ && (!cmh || (*cmh )[moved_piece][to_sq(move)] < VALUE_ZERO)
+ && (!fmh || (*fmh )[moved_piece][to_sq(move)] < VALUE_ZERO)
+ && (!fmh2 || (*fmh2)[moved_piece][to_sq(move)] < VALUE_ZERO || (cmh && fmh)))
+ continue;
- // Prune moves with negative SEE at low depths and below a decreasing
- // threshold at higher depths.
- if (predictedDepth < 8 * ONE_PLY)
- {
- Value see_v = predictedDepth < 4 * ONE_PLY ? VALUE_ZERO
- : -PawnValueMg * 2 * int(predictedDepth - 3 * ONE_PLY) / ONE_PLY;
+ // Futility pruning: parent node
+ if ( lmrDepth < 7
+ && ss->staticEval + 256 + 200 * lmrDepth <= alpha)
+ continue;
- if (pos.see_sign(move) < see_v)
+ // Prune moves with negative SEE
+ if ( lmrDepth < 8
+ && !pos.see_ge(move, Value(-35 * lmrDepth * lmrDepth)))
continue;
}
+ else if ( depth < 7 * ONE_PLY
+ && !pos.see_ge(move, Value(-35 * depth / ONE_PLY * depth / ONE_PLY)))
+ continue;
}
// Speculative prefetch as early as possible
}
ss->currentMove = move;
- ss->counterMoves = &CounterMoveHistory[moved_piece][to_sq(move)];
+ ss->counterMoves = &thisThread->counterMoveHistory[moved_piece][to_sq(move)];
// Step 14. Make the move
pos.do_move(move, st, givesCheck);
// because the destination square is empty.
else if ( type_of(move) == NORMAL
&& type_of(pos.piece_on(to_sq(move))) != PAWN
- && pos.see(make_move(to_sq(move), from_sq(move))) < VALUE_ZERO)
+ && !pos.see_ge(make_move(to_sq(move), from_sq(move)), VALUE_ZERO))
r -= 2 * ONE_PLY;
// Decrease/increase reduction for moves with a good/bad history
// All legal moves have been searched and if there are no legal moves, it
// must be a mate or a stalemate. If we are in a singular extension search then
// return a fail low score.
+
+ assert(moveCount || !inCheck || excludedMove || !MoveList<LEGAL>(pos).size());
+
if (!moveCount)
bestValue = excludedMove ? alpha
: inCheck ? mated_in(ss->ply) : DrawValue[pos.side_to_move()];
}
// Extra penalty for a quiet TT move in previous ply when it gets refuted
- if ((ss-1)->moveCount == 1 && !pos.captured_piece_type())
+ if ((ss-1)->moveCount == 1 && !pos.captured_piece())
{
Value penalty = Value(d * d + 4 * d + 1);
Square prevSq = to_sq((ss-1)->currentMove);
}
// Bonus for prior countermove that caused the fail low
else if ( depth >= 3 * ONE_PLY
- && !pos.captured_piece_type()
+ && !pos.captured_piece()
&& is_ok((ss-1)->currentMove))
{
int d = depth / ONE_PLY;
assert(is_ok(move));
givesCheck = type_of(move) == NORMAL && !pos.discovered_check_candidates()
- ? pos.check_info().checkSquares[type_of(pos.piece_on(from_sq(move)))] & to_sq(move)
+ ? pos.check_squares(type_of(pos.piece_on(from_sq(move)))) & to_sq(move)
: pos.gives_check(move);
// Futility pruning
continue;
}
- if (futilityBase <= alpha && pos.see(move) <= VALUE_ZERO)
+ if (futilityBase <= alpha && !pos.see_ge(move, VALUE_ZERO + 1))
{
bestValue = std::max(bestValue, futilityBase);
continue;
// Don't search moves with negative SEE values
if ( (!InCheck || evasionPrunable)
&& type_of(move) != PROMOTION
- && pos.see_sign(move) < VALUE_ZERO)
+ && !pos.see_ge(move, VALUE_ZERO))
continue;
// Speculative prefetch as early as possible
/// fail high at root. We try hard to have a ponder move to return to the GUI,
/// otherwise in case of 'ponder on' we have nothing to think on.
-bool RootMove::extract_ponder_from_tt(Position& pos)
-{
+bool RootMove::extract_ponder_from_tt(Position& pos) {
+
StateInfo st;
bool ttHit;
assert(pv.size() == 1);
+ if (!pv[0])
+ return false;
+
pos.do_move(pv[0], st, pos.gives_check(pv[0]));
TTEntry* tte = TT.probe(pos.key(), ttHit);
RootInTB = root_probe_wdl(pos, rootMoves, TB::Score);
// Only probe during search if winning
- if (TB::Score <= VALUE_DRAW)
+ if (RootInTB && TB::Score <= VALUE_DRAW)
Cardinality = 0;
}