// Razor and futility margins
constexpr int RazorMargin = 510;
Value futility_margin(Depth d, bool improving) {
// Razor and futility margins
constexpr int RazorMargin = 510;
Value futility_margin(Depth d, bool improving) {
Depth reduction(bool i, Depth d, int mn) {
int r = Reductions[d] * Reductions[mn];
Depth reduction(bool i, Depth d, int mn) {
int r = Reductions[d] * Reductions[mn];
beta = std::min(prev + delta, VALUE_INFINITE);
// Adjust contempt based on root move's previousScore (dynamic contempt)
beta = std::min(prev + delta, VALUE_INFINITE);
// Adjust contempt based on root move's previousScore (dynamic contempt)
contempt = (us == WHITE ? make_score(dct, dct / 2)
: -make_score(dct, dct / 2));
contempt = (us == WHITE ? make_score(dct, dct / 2)
: -make_score(dct, dct / 2));
// Start with a small aspiration window and, in the case of a fail
// high/low, re-search with a bigger window until we don't fail
// high/low anymore.
// Start with a small aspiration window and, in the case of a fail
// high/low, re-search with a bigger window until we don't fail
// high/low anymore.
- double totalTime = rootMoves.size() == 1 ? 0 :
- Time.optimum() * fallingEval * reduction * bestMoveInstability;
+ double totalTime = Time.optimum() * fallingEval * reduction * bestMoveInstability;
+
+ // Cap used time in case of a single legal move for a better viewer experience in tournaments
+ // yielding correct scores and sufficiently fast moves.
+ if (rootMoves.size() == 1)
+ totalTime = std::min(500.0, totalTime);
// Check if we have an upcoming move which draws by repetition, or
// if the opponent had an alternative move earlier to this position.
// Check if we have an upcoming move which draws by repetition, or
// if the opponent had an alternative move earlier to this position.
TTEntry* tte;
Key posKey;
Move ttMove, move, excludedMove, bestMove;
TTEntry* tte;
Key posKey;
Move ttMove, move, excludedMove, bestMove;
if (!pos.capture_or_promotion(ttMove))
update_quiet_stats(pos, ss, ttMove, stat_bonus(depth), depth);
if (!pos.capture_or_promotion(ttMove))
update_quiet_stats(pos, ss, ttMove, stat_bonus(depth), depth);
if ((ss-1)->currentMove != MOVE_NULL)
ss->staticEval = eval = evaluate(pos);
else
ss->staticEval = eval = -(ss-1)->staticEval + 2 * Tempo;
if ((ss-1)->currentMove != MOVE_NULL)
ss->staticEval = eval = evaluate(pos);
else
ss->staticEval = eval = -(ss-1)->staticEval + 2 * Tempo;
tte->save(posKey, VALUE_NONE, ss->ttPv, BOUND_NONE, DEPTH_NONE, MOVE_NONE, eval);
}
tte->save(posKey, VALUE_NONE, ss->ttPv, BOUND_NONE, DEPTH_NONE, MOVE_NONE, eval);
}
+ // Use static evaluation difference to improve quiet move ordering
+ if (is_ok((ss-1)->currentMove) && !(ss-1)->inCheck && !priorCapture)
+ {
+ int bonus = std::clamp(-depth * 4 * int((ss-1)->staticEval + ss->staticEval - 2 * Tempo), -1000, 1000);
+ thisThread->mainHistory[~us][from_to((ss-1)->currentMove)] << bonus;
+ }
+
// Step 7. Razoring (~1 Elo)
if ( !rootNode // The required rootNode PV handling is not available in qsearch
&& depth == 1
&& eval <= alpha - RazorMargin)
return qsearch<NT>(pos, ss, alpha, beta);
// Step 7. Razoring (~1 Elo)
if ( !rootNode // The required rootNode PV handling is not available in qsearch
&& depth == 1
&& eval <= alpha - RazorMargin)
return qsearch<NT>(pos, ss, alpha, beta);
+ // Set up improving flag that is used in various pruning heuristics
+ // We define position as improving if static evaluation of position is better
+ // Than the previous static evaluation at our turn
+ // In case of us being in check at our previous move we look at move prior to it
improving = (ss-2)->staticEval == VALUE_NONE
? ss->staticEval > (ss-4)->staticEval || (ss-4)->staticEval == VALUE_NONE
: ss->staticEval > (ss-2)->staticEval;
improving = (ss-2)->staticEval == VALUE_NONE
? ss->staticEval > (ss-4)->staticEval || (ss-4)->staticEval == VALUE_NONE
: ss->staticEval > (ss-2)->staticEval;
ss->currentMove = MOVE_NULL;
ss->continuationHistory = &thisThread->continuationHistory[0][0][NO_PIECE][0];
ss->currentMove = MOVE_NULL;
ss->continuationHistory = &thisThread->continuationHistory[0][0][NO_PIECE][0];
// Step 10. ProbCut (~10 Elo)
// If we have a good enough capture and a reduced search returns a value
// Step 10. ProbCut (~10 Elo)
// If we have a good enough capture and a reduced search returns a value
&& (*contHist[0])[movedPiece][to_sq(move)]
+ (*contHist[1])[movedPiece][to_sq(move)]
+ (*contHist[3])[movedPiece][to_sq(move)]
&& (*contHist[0])[movedPiece][to_sq(move)]
+ (*contHist[1])[movedPiece][to_sq(move)]
+ (*contHist[3])[movedPiece][to_sq(move)]
- // Futility pruning for captures
- if ( !givesCheck
- && lmrDepth < 6
- && !(PvNode && abs(bestValue) < 2)
- && !ss->inCheck
- && ss->staticEval + 169 + 244 * lmrDepth
- + PieceValue[MG][type_of(pos.piece_on(to_sq(move)))] <= alpha)
- continue;
-
- // See based pruning
- if (!pos.see_ge(move, Value(-221) * depth)) // (~25 Elo)
+ // SEE based pruning
+ if (!pos.see_ge(move, Value(-213) * depth)) // (~25 Elo)
{
Depth r = reduction(improving, depth, moveCount);
// Decrease reduction if the ttHit running average is large
{
Depth r = reduction(improving, depth, moveCount);
// Decrease reduction if the ttHit running average is large
{
value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth, !cutNode);
{
value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth, !cutNode);
update_continuation_histories(ss, movedPiece, to_sq(move), bonus);
}
}
update_continuation_histories(ss, movedPiece, to_sq(move), bonus);
}
}
- value = -search<PV>(pos, ss+1, -beta, -alpha, newDepth, false);
+ value = -search<PV>(pos, ss+1, -beta, -alpha,
+ std::min(maxNextDepth, newDepth), false);
else if (bestMove)
update_all_stats(pos, ss, bestMove, bestValue, beta, prevSq,
quietsSearched, quietCount, capturesSearched, captureCount, depth);
else if (bestMove)
update_all_stats(pos, ss, bestMove, bestValue, beta, prevSq,
quietsSearched, quietCount, capturesSearched, captureCount, depth);
if (!excludedMove && !(rootNode && thisThread->pvIdx))
tte->save(posKey, value_to_tt(bestValue, ss->ply), ss->ttPv,
bestValue >= beta ? BOUND_LOWER :
if (!excludedMove && !(rootNode && thisThread->pvIdx))
tte->save(posKey, value_to_tt(bestValue, ss->ply), ss->ttPv,
bestValue >= beta ? BOUND_LOWER :
ss->staticEval = bestValue =
(ss-1)->currentMove != MOVE_NULL ? evaluate(pos)
: -(ss-1)->staticEval + 2 * Tempo;
ss->staticEval = bestValue =
(ss-1)->currentMove != MOVE_NULL ? evaluate(pos)
: -(ss-1)->staticEval + 2 * Tempo;
if (!ss->ttHit)
tte->save(posKey, value_to_tt(bestValue, ss->ply), false, BOUND_LOWER,
DEPTH_NONE, MOVE_NONE, ss->staticEval);
if (!ss->ttHit)
tte->save(posKey, value_to_tt(bestValue, ss->ply), false, BOUND_LOWER,
DEPTH_NONE, MOVE_NONE, ss->staticEval);
&& (*contHist[0])[pos.moved_piece(move)][to_sq(move)] < CounterMovePruneThreshold
&& (*contHist[1])[pos.moved_piece(move)][to_sq(move)] < CounterMovePruneThreshold)
continue;
&& (*contHist[0])[pos.moved_piece(move)][to_sq(move)] < CounterMovePruneThreshold
&& (*contHist[1])[pos.moved_piece(move)][to_sq(move)] < CounterMovePruneThreshold)
continue;
// All legal moves have been searched. A special case: if we're in check
// and no legal moves were found, it is checkmate.
if (ss->inCheck && bestValue == -VALUE_INFINITE)
// All legal moves have been searched. A special case: if we're in check
// and no legal moves were found, it is checkmate.
if (ss->inCheck && bestValue == -VALUE_INFINITE)
tte->save(posKey, value_to_tt(bestValue, ss->ply), pvHit,
bestValue >= beta ? BOUND_LOWER :
PvNode && bestValue > oldAlpha ? BOUND_EXACT : BOUND_UPPER,
tte->save(posKey, value_to_tt(bestValue, ss->ply), pvHit,
bestValue >= beta ? BOUND_LOWER :
PvNode && bestValue > oldAlpha ? BOUND_EXACT : BOUND_UPPER,
update_quiet_stats(pos, ss, bestMove, bonus2, depth);
update_quiet_stats(pos, ss, bestMove, bonus2, depth);
for (int i = 0; i < quietCount; ++i)
{
thisThread->mainHistory[us][from_to(quietsSearched[i])] << -bonus2;
for (int i = 0; i < quietCount; ++i)
{
thisThread->mainHistory[us][from_to(quietsSearched[i])] << -bonus2;
if ( ((ss-1)->moveCount == 1 + (ss-1)->ttHit || ((ss-1)->currentMove == (ss-1)->killers[0]))
&& !pos.captured_piece())
update_continuation_histories(ss-1, pos.piece_on(prevSq), prevSq, -bonus1);
if ( ((ss-1)->moveCount == 1 + (ss-1)->ttHit || ((ss-1)->currentMove == (ss-1)->killers[0]))
&& !pos.captured_piece())
update_continuation_histories(ss-1, pos.piece_on(prevSq), prevSq, -bonus1);
for (int i : {1, 2, 4, 6})
{
for (int i : {1, 2, 4, 6})
{
void update_quiet_stats(const Position& pos, Stack* ss, Move move, int bonus, int depth) {
void update_quiet_stats(const Position& pos, Stack* ss, Move move, int bonus, int depth) {
thisThread->mainHistory[us][from_to(move)] << bonus;
update_continuation_histories(ss, pos.moved_piece(move), to_sq(move), bonus);
thisThread->mainHistory[us][from_to(move)] << bonus;
update_continuation_histories(ss, pos.moved_piece(move), to_sq(move), bonus);
if (type_of(pos.moved_piece(move)) != PAWN)
thisThread->mainHistory[us][from_to(reverse_move(move))] << -bonus;
if (type_of(pos.moved_piece(move)) != PAWN)
thisThread->mainHistory[us][from_to(reverse_move(move))] << -bonus;
if (is_ok((ss-1)->currentMove))
{
Square prevSq = to_sq((ss-1)->currentMove);
thisThread->counterMoves[pos.piece_on(prevSq)][prevSq] = move;
}
if (is_ok((ss-1)->currentMove))
{
Square prevSq = to_sq((ss-1)->currentMove);
thisThread->counterMoves[pos.piece_on(prevSq)][prevSq] = move;
}
if (depth > 11 && ss->ply < MAX_LPH)
thisThread->lowPlyHistory[ss->ply][from_to(move)] << stat_bonus(depth - 7);
}
if (depth > 11 && ss->ply < MAX_LPH)
thisThread->lowPlyHistory[ss->ply][from_to(move)] << stat_bonus(depth - 7);
}