Merge commit '5752529cabb3270e055147709ff0847e4d59ec22' into cluster

This is the last commit before there are more conflicts.
This commit is contained in:
Steinar H. Gunderson
2025-12-25 15:27:42 +01:00
8 changed files with 72 additions and 118 deletions
+62 -95
View File
@@ -66,13 +66,10 @@ using namespace Search;
namespace {
static constexpr double EvalLevel[10] = {0.981, 0.956, 0.895, 0.949, 0.913,
0.942, 0.933, 0.890, 0.984, 0.941};
// Futility margin
Value futility_margin(Depth d, bool noTtCutNode, bool improving, bool oppWorsening) {
Value futilityMult = 109 - 40 * noTtCutNode;
Value improvingDeduction = 59 * improving * futilityMult / 32;
Value futilityMult = 122 - 37 * noTtCutNode;
Value improvingDeduction = improving * futilityMult * 2;
Value worseningDeduction = oppWorsening * futilityMult / 3;
return futilityMult * d - improvingDeduction - worseningDeduction;
@@ -85,15 +82,15 @@ constexpr int futility_move_count(bool improving, Depth depth) {
// Add correctionHistory value to raw staticEval and guarantee evaluation does not hit the tablebase range
Value to_corrected_static_eval(Value v, const Worker& w, const Position& pos) {
auto cv = w.correctionHistory[pos.side_to_move()][pawn_structure_index<Correction>(pos)];
v += cv / 10;
v += cv * std::abs(cv) / 5073;
return std::clamp(v, VALUE_TB_LOSS_IN_MAX_PLY + 1, VALUE_TB_WIN_IN_MAX_PLY - 1);
}
// History and stats update bonus, based on depth
int stat_bonus(Depth d) { return std::clamp(191 * d - 285, 20, 1412); }
int stat_bonus(Depth d) { return std::clamp(190 * d - 298, 20, 1596); }
// History and stats update malus, based on depth
int stat_malus(Depth d) { return (d < 4 ? 727 * d - 260 : 1908); }
int stat_malus(Depth d) { return (d < 4 ? 736 * d - 268 : 2044); }
// Add a small random component to draw evaluations to avoid 3-fold blindness
Value value_draw(size_t nodes) { return VALUE_DRAW - 1 + Value(nodes & 0x2); }
@@ -126,7 +123,7 @@ Value value_to_tt(Value v, int ply);
Value value_from_tt(Value v, int ply, int r50c);
void update_pv(Move* pv, Move move, const Move* childPv);
void update_continuation_histories(Stack* ss, Piece pc, Square to, int bonus);
void update_refutations(const Position& pos, Stack* ss, Search::Worker& workerThread, Move move);
void update_refutations(Stack* ss, Move move);
void update_quiet_histories(
const Position& pos, Stack* ss, Search::Worker& workerThread, Move move, int bonus);
void update_quiet_stats(
@@ -135,8 +132,6 @@ void update_all_stats(const Position& pos,
Stack* ss,
Search::Worker& workerThread,
Move bestMove,
Value bestValue,
Value beta,
Square prevSq,
Move* quietsSearched,
int quietCount,
@@ -366,12 +361,12 @@ void Search::Worker::iterative_deepening() {
// Reset aspiration window starting size
Value avg = rootMoves[pvIdx].averageScore;
delta = 9 + avg * avg / 10182;
delta = 9 + avg * avg / 10424;
alpha = std::max(avg - delta, -VALUE_INFINITE);
beta = std::min(avg + delta, VALUE_INFINITE);
// Adjust optimism based on root move's averageScore (~4 Elo)
optimism[us] = 127 * avg / (std::abs(avg) + 86);
optimism[us] = 125 * avg / (std::abs(avg) + 89);
optimism[~us] = -optimism[us];
// Start with a small aspiration window and, in the case of a fail
@@ -511,11 +506,10 @@ void Search::Worker::iterative_deepening() {
timeReduction = lastBestMoveDepth + 8 < completedDepth ? 1.495 : 0.687;
double reduction = (1.48 + mainThread->previousTimeReduction) / (2.17 * timeReduction);
double bestMoveInstability = 1 + 1.88 * totBestMoveChanges / threads.size();
int el = std::clamp((bestValue + 750) / 150, 0, 9);
double recapture = limits.capSq == rootMoves[0].pv[0].to_sq() ? 0.955 : 1.005;
double totalTime = mainThread->tm.optimum() * fallingEval * reduction
* bestMoveInstability * EvalLevel[el] * recapture;
double totalTime =
mainThread->tm.optimum() * fallingEval * reduction * bestMoveInstability * recapture;
// Cap used time in case of a single legal move for a better viewer experience
if (rootMoves.size() == 1)
@@ -558,20 +552,19 @@ void Search::Worker::iterative_deepening() {
}
void Search::Worker::clear() {
counterMoves.fill(Move::none());
mainHistory.fill(0);
captureHistory.fill(-700);
pawnHistory.fill(-1193);
pawnHistory.fill(-1188);
correctionHistory.fill(0);
for (bool inCheck : {false, true})
for (StatsType c : {NoCaptures, Captures})
for (auto& to : continuationHistory[inCheck][c])
for (auto& h : to)
h->fill(-56);
h->fill(-58);
for (size_t i = 1; i < reductions.size(); ++i)
reductions[i] = int((19.26 + std::log(size_t(options["Threads"])) / 2) * std::log(i));
reductions[i] = int((18.62 + std::log(size_t(options["Threads"])) / 2) * std::log(i));
refreshTable.clear(networks[numaAccessToken]);
}
@@ -612,12 +605,11 @@ Value Search::Worker::search(
Key posKey;
Move move, excludedMove, bestMove;
Depth extension, newDepth;
Value bestValue, value, eval, maxValue, probCutBeta, singularValue;
Value bestValue, value, eval, maxValue, probCutBeta;
bool givesCheck, improving, priorCapture, opponentWorsening;
bool capture, moveCountPruning, ttCapture;
Piece movedPiece;
int moveCount, captureCount, quietCount;
Bound singularBound;
// Step 1. Initialize node
Worker* thisThread = this;
@@ -809,7 +801,7 @@ Value Search::Worker::search(
// Use static evaluation difference to improve quiet move ordering (~9 Elo)
if (((ss - 1)->currentMove).is_ok() && !(ss - 1)->inCheck && !priorCapture)
{
int bonus = std::clamp(-10 * int((ss - 1)->staticEval + ss->staticEval), -1590, 1371) + 800;
int bonus = std::clamp(-10 * int((ss - 1)->staticEval + ss->staticEval), -1664, 1471) + 752;
thisThread->mainHistory[~us][((ss - 1)->currentMove).from_to()] << bonus;
if (type_of(pos.piece_on(prevSq)) != PAWN && ((ss - 1)->currentMove).type_of() != PROMOTION)
thisThread->pawnHistory[pawn_structure_index(pos)][pos.piece_on(prevSq)][prevSq]
@@ -830,7 +822,7 @@ Value Search::Worker::search(
// Step 7. Razoring (~1 Elo)
// If eval is really low check with qsearch if it can exceed alpha, if it can't,
// return a fail low.
if (eval < alpha - 512 - 293 * depth * depth)
if (eval < alpha - 494 - 290 * depth * depth)
{
value = qsearch<NonPV>(pos, ss, alpha - 1, alpha);
if (value < alpha && std::abs(value) < VALUE_TB_WIN_IN_MAX_PLY)
@@ -841,22 +833,22 @@ Value Search::Worker::search(
// The depth condition is important for mate finding.
if (!ss->ttPv && depth < 13
&& eval - futility_margin(depth, cutNode && !ss->ttHit, improving, opponentWorsening)
- (ss - 1)->statScore / 263
- (ss - 1)->statScore / 260
>= beta
&& eval >= beta && (!ttData.move || ttCapture) && beta > VALUE_TB_LOSS_IN_MAX_PLY
&& eval < VALUE_TB_WIN_IN_MAX_PLY)
return beta + (eval - beta) / 3;
// Step 9. Null move search with verification search (~35 Elo)
if (!PvNode && (ss - 1)->currentMove != Move::null() && (ss - 1)->statScore < 14369
&& eval >= beta && ss->staticEval >= beta - 21 * depth + 393 && !excludedMove
if (!PvNode && (ss - 1)->currentMove != Move::null() && (ss - 1)->statScore < 14389
&& eval >= beta && ss->staticEval >= beta - 21 * depth + 390 && !excludedMove
&& pos.non_pawn_material(us) && ss->ply >= thisThread->nmpMinPly
&& beta > VALUE_TB_LOSS_IN_MAX_PLY)
{
assert(eval - beta >= 0);
// Null move dynamic reduction based on depth and eval
Depth R = std::min(int(eval - beta) / 197, 6) + depth / 3 + 5;
Depth R = std::min(int(eval - beta) / 202, 6) + depth / 3 + 5;
ss->currentMove = Move::null();
ss->continuationHistory = &thisThread->continuationHistory[0][0][NO_PIECE][0];
@@ -890,11 +882,8 @@ Value Search::Worker::search(
// Step 10. Internal iterative reductions (~9 Elo)
// For PV nodes without a ttMove, we decrease depth.
// Additionally, if the current position is found in the TT
// and the stored depth in the TT is greater than or equal to
// current search depth, we decrease search depth even further.
if (PvNode && !ttData.move)
depth -= 3 + (ss->ttHit && ttData.depth >= depth);
depth -= 3;
// Use qsearch if depth <= 0.
if (depth <= 0)
@@ -902,13 +891,13 @@ Value Search::Worker::search(
// For cutNodes, if depth is high enough, decrease depth by 2 if there is no ttMove, or
// by 1 if there is a ttMove with an upper bound.
if (cutNode && depth >= 8 && (!ttData.move || ttData.bound == BOUND_UPPER))
if (cutNode && depth >= 7 && (!ttData.move || ttData.bound == BOUND_UPPER))
depth -= 1 + !ttData.move;
// Step 11. ProbCut (~10 Elo)
// If we have a good enough capture (or queen promotion) and a reduced search returns a value
// much above beta, we can (almost) safely prune the previous move.
probCutBeta = beta + 177 - 57 * improving;
probCutBeta = beta + 184 - 53 * improving;
if (
!PvNode && depth > 3
&& std::abs(beta) < VALUE_TB_WIN_IN_MAX_PLY
@@ -987,10 +976,9 @@ Value Search::Worker::search(
moves_loop: // When in check, search starts here
// Step 12. A small Probcut idea, when we are in check (~4 Elo)
probCutBeta = beta + 388;
if (ss->inCheck && (ttData.bound & BOUND_LOWER) && ttData.depth >= depth - 4
&& ttData.value >= probCutBeta && std::abs(ttData.value) < VALUE_TB_WIN_IN_MAX_PLY
// Step 12. A small Probcut idea (~4 Elo)
probCutBeta = beta + 390;
if ((ttData.bound & BOUND_LOWER) && ttData.depth >= depth - 4 && ttData.value >= probCutBeta
&& std::abs(beta) < VALUE_TB_WIN_IN_MAX_PLY)
return probCutBeta;
@@ -1001,16 +989,12 @@ moves_loop: // When in check, search starts here
nullptr,
(ss - 6)->continuationHistory};
Move countermove =
prevSq != SQ_NONE ? thisThread->counterMoves[pos.piece_on(prevSq)][prevSq] : Move::none();
MovePicker mp(pos, ttData.move, depth, &thisThread->mainHistory, &thisThread->captureHistory,
contHist, &thisThread->pawnHistory, countermove, ss->killers);
contHist, &thisThread->pawnHistory, ss->killers);
value = bestValue;
moveCountPruning = false;
singularValue = VALUE_INFINITE;
singularBound = BOUND_NONE;
// Step 13. Loop through all pseudo-legal moves until no moves remain
// or a beta cutoff occurs.
@@ -1060,9 +1044,7 @@ moves_loop: // When in check, search starts here
if (!rootNode && pos.non_pawn_material(us) && bestValue > VALUE_TB_LOSS_IN_MAX_PLY)
{
// Skip quiet moves if movecount exceeds our FutilityMoveCount threshold (~8 Elo)
moveCountPruning =
moveCount >= futility_move_count(improving, depth)
- (singularBound == BOUND_UPPER && singularValue < alpha - 50);
moveCountPruning = moveCount >= futility_move_count(improving, depth);
// Reduced depth of the next LMR search
int lmrDepth = newDepth - r;
@@ -1076,15 +1058,15 @@ moves_loop: // When in check, search starts here
// Futility pruning for captures (~2 Elo)
if (!givesCheck && lmrDepth < 7 && !ss->inCheck)
{
Value futilityValue = ss->staticEval + 294 + 246 * lmrDepth
Value futilityValue = ss->staticEval + 285 + 251 * lmrDepth
+ PieceValue[capturedPiece] + captHist / 7;
if (futilityValue <= alpha)
continue;
}
// SEE based pruning for captures and checks (~11 Elo)
int seeHist = std::clamp(captHist / 32, -180 * depth, 163 * depth);
if (!pos.see_ge(move, -163 * depth - seeHist))
int seeHist = std::clamp(captHist / 32, -182 * depth, 166 * depth);
if (!pos.see_ge(move, -168 * depth - seeHist))
continue;
}
else
@@ -1095,15 +1077,15 @@ moves_loop: // When in check, search starts here
+ thisThread->pawnHistory[pawn_structure_index(pos)][movedPiece][move.to_sq()];
// Continuation history based pruning (~2 Elo)
if (lmrDepth < 6 && history < -3899 * depth)
if (lmrDepth < 6 && history < -4165 * depth)
continue;
history += 2 * thisThread->mainHistory[us][move.from_to()];
lmrDepth += history / 4040;
lmrDepth += history / 3853;
Value futilityValue =
ss->staticEval + (bestValue < ss->staticEval - 51 ? 135 : 56) + 140 * lmrDepth;
ss->staticEval + (bestValue < ss->staticEval - 51 ? 143 : 52) + 135 * lmrDepth;
// Futility pruning: parent node (~13 Elo)
if (!ss->inCheck && lmrDepth < 12 && futilityValue <= alpha)
@@ -1126,8 +1108,8 @@ moves_loop: // When in check, search starts here
// We take care to not overdo to avoid search getting stuck.
if (ss->ply < thisThread->rootDepth * 2)
{
// Singular extension search (~94 Elo). If all moves but one fail low on a
// search of (alpha-s, beta-s), and just one fails high on (alpha, beta),
// Singular extension search (~76 Elo, ~170 nElo). If all moves but one fail
// low on a search of (alpha-s, beta-s), and just one fails high on (alpha, beta),
// then that move is singular and should be extended. To verify this we do
// a reduced search on the position excluding the ttMove and if the result
// is lower than ttValue minus a margin, then we will extend the ttMove.
@@ -1140,28 +1122,27 @@ moves_loop: // When in check, search starts here
// margins scale well.
if (!rootNode && move == ttData.move && !excludedMove
&& depth >= 4 - (thisThread->completedDepth > 35) + ss->ttPv
&& depth >= 4 - (thisThread->completedDepth > 36) + ss->ttPv
&& std::abs(ttData.value) < VALUE_TB_WIN_IN_MAX_PLY && (ttData.bound & BOUND_LOWER)
&& ttData.depth >= depth - 3)
{
Value singularBeta = ttData.value - (52 + 80 * (ss->ttPv && !PvNode)) * depth / 64;
Value singularBeta = ttData.value - (54 + 76 * (ss->ttPv && !PvNode)) * depth / 64;
Depth singularDepth = newDepth / 2;
ss->excludedMove = move;
value = singularValue =
value =
search<NonPV>(pos, ss, singularBeta - 1, singularBeta, singularDepth, cutNode);
singularBound = singularValue >= singularBeta ? BOUND_LOWER : BOUND_UPPER;
ss->excludedMove = Move::none();
if (value < singularBeta)
{
int doubleMargin = 290 * PvNode - 200 * !ttCapture;
int tripleMargin = 107 + 247 * PvNode - 278 * !ttCapture + 99 * ss->ttPv;
int doubleMargin = 293 * PvNode - 195 * !ttCapture;
int tripleMargin = 107 + 259 * PvNode - 260 * !ttCapture + 98 * ss->ttPv;
extension = 1 + (value < singularBeta - doubleMargin)
+ (value < singularBeta - tripleMargin);
depth += ((!PvNode) && (depth < 18));
depth += ((!PvNode) && (depth < 16));
}
// Multi-cut pruning
@@ -1191,7 +1172,7 @@ moves_loop: // When in check, search starts here
else if (PvNode && move.to_sq() == prevSq
&& thisThread->captureHistory[movedPiece][move.to_sq()]
[type_of(pos.piece_on(move.to_sq()))]
> 3922)
> 3994)
extension = 1;
}
@@ -1219,8 +1200,7 @@ moves_loop: // When in check, search starts here
// Decrease reduction if position is or has been on the PV (~7 Elo)
if (ss->ttPv)
r -= 1 + (ttData.value > alpha) + (ttData.depth >= depth)
- (PvNode && ttData.value < alpha && ttData.depth >= depth);
r -= 1 + (ttData.value > alpha) + (ttData.depth >= depth);
// Decrease reduction for PvNodes (~0 Elo on STC, ~2 Elo on LTC)
if (PvNode)
@@ -1248,13 +1228,13 @@ moves_loop: // When in check, search starts here
ss->statScore = 2 * thisThread->mainHistory[us][move.from_to()]
+ (*contHist[0])[movedPiece][move.to_sq()]
+ (*contHist[1])[movedPiece][move.to_sq()] - 4747;
+ (*contHist[1])[movedPiece][move.to_sq()] - 4664;
// Decrease/increase reduction for moves with a good/bad history (~8 Elo)
r -= ss->statScore / 11125;
r -= ss->statScore / 10898;
// Step 17. Late moves reduction / extension (LMR, ~117 Elo)
if (depth >= 2 && moveCount > 1 + rootNode)
if (depth >= 2 && moveCount > 1 + (rootNode && depth < 10))
{
// In general we want to cap the LMR depth search at newDepth, but when
// reduction is negative, we allow this move a limited search extension
@@ -1394,7 +1374,7 @@ moves_loop: // When in check, search starts here
else
{
// Reduce other moves if we have found at least one score improvement (~2 Elo)
if (depth > 2 && depth < 13 && std::abs(value) < VALUE_TB_WIN_IN_MAX_PLY)
if (depth > 2 && depth < 14 && std::abs(value) < VALUE_TB_WIN_IN_MAX_PLY)
depth -= 2;
assert(depth > 0);
@@ -1431,19 +1411,18 @@ moves_loop: // When in check, search starts here
// If there is a move that produces search value greater than alpha we update the stats of searched moves
else if (bestMove)
update_all_stats(pos, ss, *this, bestMove, bestValue, beta, prevSq, quietsSearched,
quietCount, capturesSearched, captureCount, depth);
update_all_stats(pos, ss, *this, bestMove, prevSq, quietsSearched, quietCount,
capturesSearched, captureCount, depth);
// Bonus for prior countermove that caused the fail low
else if (!priorCapture && prevSq != SQ_NONE)
{
int bonus = (113 * (depth > 5) + 118 * (PvNode || cutNode) + 119 * ((ss - 1)->moveCount > 8)
+ 64 * (!ss->inCheck && bestValue <= ss->staticEval - 107)
+ 147 * (!(ss - 1)->inCheck && bestValue <= -(ss - 1)->staticEval - 75));
int bonus = (114 * (depth > 5) + 116 * (PvNode || cutNode) + 123 * ((ss - 1)->moveCount > 8)
+ 64 * (!ss->inCheck && bestValue <= ss->staticEval - 108)
+ 153 * (!(ss - 1)->inCheck && bestValue <= -(ss - 1)->staticEval - 76));
// Proportional to "how much damage we have to undo"
if ((ss - 1)->statScore < -7850)
bonus += std::clamp(-(ss - 1)->statScore / 100, 0, 224);
bonus += std::clamp(-(ss - 1)->statScore / 100, -50, 274);
update_continuation_histories(ss - 1, pos.piece_on(prevSq), prevSq,
stat_bonus(depth) * bonus / 100);
@@ -1618,7 +1597,7 @@ Value Search::Worker::qsearch(Position& pos, Stack* ss, Value alpha, Value beta,
if (bestValue > alpha)
alpha = bestValue;
futilityBase = ss->staticEval + 294;
futilityBase = ss->staticEval + 299;
}
const PieceToHistory* contHist[] = {(ss - 1)->continuationHistory,
@@ -1690,11 +1669,11 @@ Value Search::Worker::qsearch(Position& pos, Stack* ss, Value alpha, Value beta,
+ (*contHist[1])[pos.moved_piece(move)][move.to_sq()]
+ thisThread->pawnHistory[pawn_structure_index(pos)][pos.moved_piece(move)]
[move.to_sq()]
<= 4452)
<= 4643)
continue;
// Do not search moves with bad enough SEE values (~5 Elo)
if (!pos.see_ge(move, -74))
if (!pos.see_ge(move, -83))
continue;
}
@@ -1760,7 +1739,7 @@ Value Search::Worker::qsearch(Position& pos, Stack* ss, Value alpha, Value beta,
Depth Search::Worker::reduction(bool i, Depth d, int mn, int delta) const {
int reductionScale = reductions[d] * reductions[mn];
return (reductionScale + 1236 - delta * 746 / rootDelta) / 1024 + (!i && reductionScale > 1326);
return (reductionScale + 1274 - delta * 746 / rootDelta) / 1024 + (!i && reductionScale > 1293);
}
// elapsed() returns the time elapsed since the search started. If the
@@ -1845,8 +1824,6 @@ void update_all_stats(const Position& pos,
Stack* ss,
Search::Worker& workerThread,
Move bestMove,
Value bestValue,
Value beta,
Square prevSq,
Move* quietsSearched,
int quietCount,
@@ -1863,10 +1840,7 @@ void update_all_stats(const Position& pos,
if (!pos.capture_stage(bestMove))
{
int bestMoveBonus = bestValue > beta + 166 ? quietMoveBonus // larger bonus
: stat_bonus(depth); // smaller bonus
update_quiet_stats(pos, ss, workerThread, bestMove, bestMoveBonus);
update_quiet_stats(pos, ss, workerThread, bestMove, quietMoveBonus);
// Decrease stats for all non-best quiet moves
for (int i = 0; i < quietCount; ++i)
@@ -1901,7 +1875,7 @@ void update_all_stats(const Position& pos,
// by moves at ply -1, -2, -3, -4, and -6 with current move.
void update_continuation_histories(Stack* ss, Piece pc, Square to, int bonus) {
bonus = bonus * 51 / 64;
bonus = bonus * 52 / 64;
for (int i : {1, 2, 3, 4, 6})
{
@@ -1914,7 +1888,7 @@ void update_continuation_histories(Stack* ss, Piece pc, Square to, int bonus) {
}
// Updates move sorting heuristics
void update_refutations(const Position& pos, Stack* ss, Search::Worker& workerThread, Move move) {
void update_refutations(Stack* ss, Move move) {
// Update killers
if (ss->killers[0] != move)
@@ -1922,13 +1896,6 @@ void update_refutations(const Position& pos, Stack* ss, Search::Worker& workerTh
ss->killers[1] = ss->killers[0];
ss->killers[0] = move;
}
// Update countermove history
if (((ss - 1)->currentMove).is_ok())
{
Square prevSq = ((ss - 1)->currentMove).to_sq();
workerThread.counterMoves[pos.piece_on(prevSq)][prevSq] = move;
}
}
void update_quiet_histories(
@@ -1947,7 +1914,7 @@ void update_quiet_histories(
void update_quiet_stats(
const Position& pos, Stack* ss, Search::Worker& workerThread, Move move, int bonus) {
update_refutations(pos, ss, workerThread, move);
update_refutations(ss, move);
update_quiet_histories(pos, ss, workerThread, move, bonus);
}