Skip to content

Commit

Permalink
Search tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
PikaCat-OuO committed Jul 22, 2024
1 parent 5df62a6 commit 54c6960
Showing 1 changed file with 55 additions and 55 deletions.
110 changes: 55 additions & 55 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace {

// Futility margin
Value futility_margin(Depth d, bool noTtCutNode, bool improving, bool oppWorsening) {
Value futilityMult = 174 - 28 * noTtCutNode;
Value futilityMult = 176 - 28 * noTtCutNode;
Value improvingDeduction = improving * futilityMult * 2;
Value worseningDeduction = oppWorsening * futilityMult / 3;

Expand All @@ -73,10 +73,10 @@ Value to_corrected_static_eval(Value v, const Worker& w, const Position& pos) {
}

// History and stats update bonus, based on depth
int stat_bonus(Depth d) { return std::min(226 * d - 101, 1675); }
int stat_bonus(Depth d) { return std::min(184 * d - 112, 1816); }

// History and stats update malus, based on depth
int stat_malus(Depth d) { return std::min(748 * d - 255, 1435); }
int stat_malus(Depth d) { return std::min(751 * d - 240, 1609); }

// 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); }
Expand Down Expand Up @@ -262,12 +262,12 @@ void Search::Worker::iterative_deepening() {

// Reset aspiration window starting size
Value avg = rootMoves[pvIdx].averageScore;
delta = 12 + avg * avg / 30272;
delta = 12 + avg * avg / 31464;
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] = 95 * avg / (std::abs(avg) + 95);
optimism[us] = 98 * avg / (std::abs(avg) + 93);
optimism[~us] = -optimism[us];

// Start with a small aspiration window and, in the case of a fail
Expand Down Expand Up @@ -390,23 +390,23 @@ void Search::Worker::iterative_deepening() {
{
int nodesEffort = rootMoves[0].effort * 111 / std::max(size_t(1), size_t(nodes));

double fallingEval = (73 + 18 * (mainThread->bestPreviousAverageScore - bestValue)
double fallingEval = (80 + 17 * (mainThread->bestPreviousAverageScore - bestValue)
+ 4 * (mainThread->iterValue[iterIdx] - bestValue))
/ 742.98;
fallingEval = std::clamp(fallingEval, 0.46, 1.60);
/ 713.14;
fallingEval = std::clamp(fallingEval, 0.50, 1.51);

// If the bestMove is stable over several iterations, reduce time accordingly
timeReduction = lastBestMoveDepth + 10 < completedDepth ? 1.85 : 0.64;
double reduction = (1.94 + mainThread->previousTimeReduction) / (2.43 * timeReduction);
double bestMoveInstability = 1.03 + 1.83 * totBestMoveChanges / threads.size();
double recapture = limits.capSq == rootMoves[0].pv[0].to_sq() ? 1.026 : 0.931;
timeReduction = lastBestMoveDepth + 10 < completedDepth ? 2.03 : 0.68;
double reduction = (1.87 + mainThread->previousTimeReduction) / (2.52 * timeReduction);
double bestMoveInstability = 1.05 + 1.79 * totBestMoveChanges / threads.size();
double recapture = limits.capSq == rootMoves[0].pv[0].to_sq() ? 0.965 : 0.948;

double totalTime =
mainThread->tm.optimum() * fallingEval * reduction * bestMoveInstability * recapture;

auto elapsedTime = elapsed();

if (completedDepth >= 10 && nodesEffort >= 89 && elapsedTime > totalTime * 0.80
if (completedDepth >= 9 && nodesEffort >= 100 && elapsedTime > totalTime * 0.69
&& !mainThread->ponder)
threads.stop = true;

Expand All @@ -421,7 +421,7 @@ void Search::Worker::iterative_deepening() {
threads.stop = true;
}
else
threads.increaseDepth = mainThread->ponder || elapsedTime <= totalTime * 0.343;
threads.increaseDepth = mainThread->ponder || elapsedTime <= totalTime * 0.335;
}

mainThread->iterValue[iterIdx] = bestValue;
Expand All @@ -437,8 +437,8 @@ void Search::Worker::iterative_deepening() {
// Reset histories, usually before a new game
void Search::Worker::clear() {
mainHistory.fill(0);
captureHistory.fill(-700);
pawnHistory.fill(-1328);
captureHistory.fill(-723);
pawnHistory.fill(-1372);
correctionHistory.fill(0);

for (bool inCheck : {false, true})
Expand All @@ -448,7 +448,7 @@ void Search::Worker::clear() {
h->fill(-72);

for (size_t i = 1; i < reductions.size(); ++i)
reductions[i] = int((20.55 + std::log(size_t(options["Threads"])) / 2) * std::log(i));
reductions[i] = int((19.14 + std::log(size_t(options["Threads"])) / 2) * std::log(i));

refreshTable.clear(network[numaAccessToken]);
}
Expand Down Expand Up @@ -639,7 +639,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(-12 * int((ss - 1)->staticEval + ss->staticEval), -1003, 1820) + 800;
int bonus = std::clamp(-14 * int((ss - 1)->staticEval + ss->staticEval), -1099, 1860) + 633;
thisThread->mainHistory[~us][((ss - 1)->currentMove).from_to()] << bonus;
if (type_of(pos.piece_on(prevSq)) != PAWN)
thisThread->pawnHistory[pawn_structure_index(pos)][pos.piece_on(prevSq)][prevSq]
Expand All @@ -657,7 +657,7 @@ Value Search::Worker::search(
// Step 6. Razoring (~1 Elo)
// If eval is really low, check with qsearch if we can exceed alpha. If the
// search suggests we cannot exceed alpha, return a speculative fail low.
if (eval < alpha - 1095 - 337 * depth * depth)
if (eval < alpha - 1092 - 327 * depth * depth)
{
value = qsearch<NonPV>(pos, ss, alpha - 1, alpha);
if (value < alpha && std::abs(value) < VALUE_MATE_IN_MAX_PLY)
Expand All @@ -666,24 +666,24 @@ Value Search::Worker::search(

// Step 7. Futility pruning: child node (~40 Elo)
// The depth condition is important for mate finding.
if (!ss->ttPv && depth < 9
if (!ss->ttPv && depth < 10
&& eval - futility_margin(depth, cutNode && !ss->ttHit, improving, opponentWorsening)
- (ss - 1)->statScore / 183
- (ss - 1)->statScore / 188
>= beta
&& eval >= beta && (!ttData.move || ttCapture) && beta > VALUE_MATED_IN_MAX_PLY
&& eval < VALUE_MATE_IN_MAX_PLY)
return beta + (eval - beta) / 3;

// Step 8. Null move search with verification search (~35 Elo)
if (cutNode && (ss - 1)->currentMove != Move::null() && (ss - 1)->statScore < 12853
&& eval >= beta && ss->staticEval >= beta - 11 * depth + 129 && !excludedMove
if (cutNode && (ss - 1)->currentMove != Move::null() && (ss - 1)->statScore < 13085
&& eval >= beta && ss->staticEval >= beta - 11 * depth + 138 && !excludedMove
&& pos.major_material(us) && ss->ply >= thisThread->nmpMinPly
&& beta > VALUE_MATED_IN_MAX_PLY)
{
assert(eval - beta >= 0);

// Null move dynamic reduction based on depth and eval
Depth R = std::min(int(eval - beta) / 143, 5) + depth / 3 + 5;
Depth R = std::min(int(eval - beta) / 158, 5) + depth / 3 + 5;

ss->currentMove = Move::null();
ss->continuationHistory = &thisThread->continuationHistory[0][0][NO_PIECE][0];
Expand Down Expand Up @@ -726,13 +726,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 >= 9 && (!ttData.move || ttData.bound == BOUND_UPPER))
if (cutNode && depth >= 8 && (!ttData.move || ttData.bound == BOUND_UPPER))
depth -= 1 + !ttData.move;

// Step 10. ProbCut (~10 Elo)
// 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.
probCutBeta = beta + 146 - 67 * improving;
probCutBeta = beta + 152 - 71 * improving;
if (!PvNode && depth > 4
&& std::abs(beta) < VALUE_MATE_IN_MAX_PLY
// If value from transposition table is lower than probCutBeta, don't attempt
Expand Down Expand Up @@ -800,7 +800,7 @@ Value Search::Worker::search(
moves_loop: // When in check, search starts here

// Step 11. A small Probcut idea (~4 Elo)
probCutBeta = beta + 425;
probCutBeta = beta + 478;
if ((ttData.bound & BOUND_LOWER) && ttData.depth >= depth - 3 && ttData.value >= probCutBeta
&& std::abs(beta) < VALUE_MATE_IN_MAX_PLY && std::abs(probCutBeta) < VALUE_MATE_IN_MAX_PLY)
return probCutBeta;
Expand Down Expand Up @@ -881,17 +881,17 @@ Value Search::Worker::search(
thisThread->captureHistory[movedPiece][move.to_sq()][type_of(capturedPiece)];

// Futility pruning for captures (~2 Elo)
if (!givesCheck && lmrDepth < 16 && !ss->inCheck)
if (!givesCheck && lmrDepth < 17 && !ss->inCheck)
{
Value futilityValue = ss->staticEval + 375 + 322 * lmrDepth
Value futilityValue = ss->staticEval + 404 + 294 * lmrDepth
+ PieceValue[capturedPiece] + captHist / 5;
if (futilityValue <= alpha)
continue;
}

// SEE based pruning for captures and checks (~11 Elo)
int seeHist = std::clamp(captHist / 35, -229 * depth, 163 * depth);
if (!pos.see_ge(move, -241 * depth - seeHist))
int seeHist = std::clamp(captHist / 34, -211 * depth, 166 * depth);
if (!pos.see_ge(move, -270 * depth - seeHist))
continue;
}
else
Expand All @@ -902,15 +902,15 @@ Value Search::Worker::search(
+ thisThread->pawnHistory[pawn_structure_index(pos)][movedPiece][move.to_sq()];

// Continuation history based pruning (~2 Elo)
if (history < -3653 * depth)
if (history < -3120 * depth)
continue;

history += 2 * thisThread->mainHistory[us][move.from_to()];

lmrDepth += history / 4727;
lmrDepth += history / 4188;

Value futilityValue =
ss->staticEval + (bestValue < ss->staticEval - 58 ? 136 : 119) + 138 * lmrDepth;
ss->staticEval + (bestValue < ss->staticEval - 57 ? 144 : 121) + 135 * lmrDepth;

// Futility pruning: parent node (~13 Elo)
if (!ss->inCheck && lmrDepth < 9 && futilityValue <= alpha)
Expand All @@ -924,7 +924,7 @@ Value Search::Worker::search(
lmrDepth = std::max(lmrDepth, 0);

// Prune moves with negative SEE (~4 Elo)
if (!pos.see_ge(move, -28 * lmrDepth * lmrDepth))
if (!pos.see_ge(move, -27 * lmrDepth * lmrDepth))
continue;
}
}
Expand All @@ -951,7 +951,7 @@ Value Search::Worker::search(
&& std::abs(ttData.value) < VALUE_MATE_IN_MAX_PLY && (ttData.bound & BOUND_LOWER)
&& ttData.depth >= depth - 3)
{
Value singularBeta = ttData.value - (46 + 56 * (ss->ttPv && !PvNode)) * depth / 81;
Value singularBeta = ttData.value - (43 + 57 * (ss->ttPv && !PvNode)) * depth / 71;
Depth singularDepth = newDepth / 2;

ss->excludedMove = move;
Expand All @@ -961,13 +961,13 @@ Value Search::Worker::search(

if (value < singularBeta)
{
int doubleMargin = 313 * PvNode - 183 * !ttCapture;
int tripleMargin = 128 + 227 * PvNode - 293 * !ttCapture + 106 * ss->ttPv;
int doubleMargin = 272 * PvNode - 168 * !ttCapture;
int tripleMargin = 132 + 238 * PvNode - 303 * !ttCapture + 110 * ss->ttPv;

extension = 1 + (value < singularBeta - doubleMargin)
+ (value < singularBeta - tripleMargin);

depth += ((!PvNode) && (depth < 16));
depth += ((!PvNode) && (depth < 17));
}

// Multi-cut pruning
Expand Down Expand Up @@ -1000,7 +1000,7 @@ Value Search::Worker::search(
else if (PvNode && move.to_sq() == prevSq
&& thisThread->captureHistory[movedPiece][move.to_sq()]
[type_of(pos.piece_on(move.to_sq()))]
> 4306)
> 4528)
extension = 1;
}

Expand Down Expand Up @@ -1056,10 +1056,10 @@ Value Search::Worker::search(

ss->statScore = 2 * thisThread->mainHistory[us][move.from_to()]
+ (*contHist[0])[movedPiece][move.to_sq()]
+ (*contHist[1])[movedPiece][move.to_sq()] - 5294;
+ (*contHist[1])[movedPiece][move.to_sq()] - 4769;

// Decrease/increase reduction for moves with a good/bad history (~8 Elo)
r -= ss->statScore / 9313;
r -= ss->statScore / 9242;

// Step 16. Late moves reduction / extension (LMR, ~117 Elo)
if (depth >= 2 && moveCount > 1 + (rootNode && depth < 10))
Expand All @@ -1078,7 +1078,7 @@ Value Search::Worker::search(
{
// Adjust full-depth search based on LMR results - if the result was
// good enough search deeper, if it was bad enough search shallower.
const bool doDeeperSearch = value > (bestValue + 65 + 2 * newDepth); // (~1 Elo)
const bool doDeeperSearch = value > (bestValue + 67 + 2 * newDepth); // (~1 Elo)
const bool doShallowerSearch = value < bestValue + newDepth; // (~2 Elo)

newDepth += doDeeperSearch - doShallowerSearch;
Expand Down Expand Up @@ -1202,7 +1202,7 @@ Value Search::Worker::search(
else
{
// Reduce other moves if we have found at least one score improvement (~2 Elo)
if (depth > 2 && depth < 10 && std::abs(value) < VALUE_MATE_IN_MAX_PLY)
if (depth > 2 && depth < 11 && std::abs(value) < VALUE_MATE_IN_MAX_PLY)
depth -= 2;

assert(depth > 0);
Expand Down Expand Up @@ -1246,23 +1246,23 @@ Value Search::Worker::search(
else if (!priorCapture && prevSq != SQ_NONE)
{
int bonus =
(121 * (depth > 5) + 102 * (PvNode || cutNode) + 121 * ((ss - 1)->moveCount > 13)
+ 61 * (!ss->inCheck && bestValue <= ss->staticEval - 138)
+ 143 * (!(ss - 1)->inCheck && bestValue <= -(ss - 1)->staticEval - 81));
(108 * (depth > 5) + 105 * (PvNode || cutNode) + 123 * ((ss - 1)->moveCount > 14)
+ 64 * (!ss->inCheck && bestValue <= ss->staticEval - 133)
+ 145 * (!(ss - 1)->inCheck && bestValue <= -(ss - 1)->staticEval - 75));

// proportional to "how much damage we have to undo"
bonus += std::clamp(-(ss - 1)->statScore / 100, -50, 250);
bonus += std::clamp(-(ss - 1)->statScore / 101, -56, 236);

bonus = std::max(bonus, 0);

update_continuation_histories(ss - 1, pos.piece_on(prevSq), prevSq,
stat_bonus(depth) * bonus / 106);
stat_bonus(depth) * bonus / 115);
thisThread->mainHistory[~us][((ss - 1)->currentMove).from_to()]
<< stat_bonus(depth) * bonus / 188;
<< stat_bonus(depth) * bonus / 178;

if (type_of(pos.piece_on(prevSq)) != PAWN)
thisThread->pawnHistory[pawn_structure_index(pos)][pos.piece_on(prevSq)][prevSq]
<< stat_bonus(depth) * bonus / 23;
<< stat_bonus(depth) * bonus / 22;
}

if (PvNode)
Expand Down Expand Up @@ -1431,7 +1431,7 @@ Value Search::Worker::qsearch(Position& pos, Stack* ss, Value alpha, Value beta,
if (bestValue > alpha)
alpha = bestValue;

futilityBase = ss->staticEval + 143;
futilityBase = ss->staticEval + 153;
}

const PieceToHistory* contHist[] = {(ss - 1)->continuationHistory,
Expand Down Expand Up @@ -1501,11 +1501,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()]
<= 3733)
<= 3682)
continue;

// Do not search moves with bad enough SEE values (~5 Elo)
if (!pos.see_ge(move, -105))
if (!pos.see_ge(move, -110))
continue;
}

Expand Down Expand Up @@ -1571,7 +1571,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 + 2061 - delta * 1399 / rootDelta) / 1235 + (!i && reductionScale > 773);
return (reductionScale + 1863 - delta * 1516 / rootDelta) / 1312 + (!i && reductionScale > 701);
}

// elapsed() returns the time elapsed since the search started. If the
Expand Down

0 comments on commit 54c6960

Please sign in to comment.