Skip to content

Commit

Permalink
Post merge search tune
Browse files Browse the repository at this point in the history
Book: 3mvs 151-350_1097123
TC: 30+0.3
Total/Win/Draw/Lose: 16084 / 3443 / 9401 / 3240
PTNML: 11 / 1475 / 4871 / 1670 / 15
WinRate: 50.63%
ELO: 4.16[1.70, 6.61]
LOS: 99.95
LLR: 2.95[-2.94, 2.94]
  • Loading branch information
PikaCat-OuO committed Jan 4, 2024
1 parent f29c039 commit e9c386c
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ enum NodeType {

// Futility margin
Value futility_margin(Depth d, bool noTtCutNode, bool improving) {
return Value((128 - 30 * noTtCutNode) * (d - improving));
return Value((140 - 30 * noTtCutNode) * (d - improving));
}

// Reductions lookup table initialized at startup
int Reductions[MAX_MOVES]; // [depth or moveNumber]

Depth reduction(bool i, Depth d, int mn, Value delta, Value rootDelta) {
int reductionScale = Reductions[d] * Reductions[mn];
return (reductionScale + 1776 - int(delta) * 1485 / int(rootDelta)) / 1386
+ (!i && reductionScale > 858);
return (reductionScale + 1816 - int(delta) * 1517 / int(rootDelta)) / 1225
+ (!i && reductionScale > 909);
}

constexpr int futility_move_count(bool improving, Depth depth) {
Expand All @@ -86,10 +86,10 @@ constexpr Value to_static_eval(const Value v) {
}

// History and stats update bonus, based on depth
int stat_bonus(Depth d) { return std::min(291 * d - 410, 1735); }
int stat_bonus(Depth d) { return std::min(295 * d - 407, 1752); }

// History and stats update malus, based on depth
int stat_malus(Depth d) { return std::min(455 * d - 248, 1537); }
int stat_malus(Depth d) { return std::min(516 * d - 227, 1527); }

// Add a small random component to draw evaluations to avoid 3-fold blindness
Value value_draw(const Thread* thisThread) {
Expand Down Expand Up @@ -154,7 +154,7 @@ uint64_t perft(Position& pos, Depth depth) {
void Search::init() {

for (int i = 1; i < MAX_MOVES; ++i)
Reductions[i] = int((19.71 + std::log(Threads.size()) / 2) * std::log(i));
Reductions[i] = int((18.00 + std::log(Threads.size()) / 2) * std::log(i));
}


Expand Down Expand Up @@ -317,12 +317,12 @@ void Thread::search() {

// Reset aspiration window starting size
Value avg = rootMoves[pvIdx].averageScore;
delta = Value(18) + int(avg) * avg / 30646;
delta = Value(17) + int(avg) * avg / 30122;
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] = 187 * avg / (std::abs(avg) + 116);
optimism[us] = 195 * avg / (std::abs(avg) + 114);
optimism[~us] = -optimism[us];

// Start with a small aspiration window and, in the case of a fail
Expand Down Expand Up @@ -649,7 +649,7 @@ Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, boo
// Use static evaluation difference to improve quiet move ordering (~9 Elo)
if (is_ok((ss - 1)->currentMove) && !(ss - 1)->inCheck && !priorCapture)
{
int bonus = std::clamp(-13 * int((ss - 1)->staticEval + ss->staticEval), -1064, 1900);
int bonus = std::clamp(-12 * int((ss - 1)->staticEval + ss->staticEval), -1120, 2080);
bonus = bonus > 0 ? 2 * bonus : bonus / 2;
thisThread->mainHistory[~us][from_to((ss - 1)->currentMove)] << bonus;
if (type_of(pos.piece_on(prevSq)) != PAWN)
Expand All @@ -669,7 +669,7 @@ Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, boo
// Step 6. 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 - 660 - (338 - 172 * ((ss + 1)->cutoffCnt > 3)) * depth * depth)
if (eval < alpha - 686 - (335 - 173 * ((ss + 1)->cutoffCnt > 3)) * depth * depth)
{
value = qsearch<NonPV>(pos, ss, alpha - 1, alpha);
if (value < alpha)
Expand All @@ -680,22 +680,22 @@ Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, boo
// The depth condition is important for mate finding.
if (!ss->ttPv && depth < 8
&& eval - futility_margin(depth, cutNode && !ss->ttHit, improving)
- (ss - 1)->statScore / 217
- (ss - 1)->statScore / 242
>= beta
&& eval >= beta && eval < 24296 // smaller than wins.
&& eval >= beta && eval < 24354 // smaller than wins.
&& (!ttMove || ttCapture))
return beta > VALUE_MATED_IN_MAX_PLY ? (eval + beta) / 2 : eval;

// Step 8. Null move search with verification search (~35 Elo)
if (!PvNode && (ss - 1)->currentMove != MOVE_NULL && (ss - 1)->statScore < 12401 && eval >= beta
&& eval >= ss->staticEval && ss->staticEval >= beta - 12 * depth + 129 && !excludedMove
if (!PvNode && (ss - 1)->currentMove != MOVE_NULL && (ss - 1)->statScore < 11654 && eval >= beta
&& eval >= ss->staticEval && ss->staticEval >= beta - 12 * depth + 120 && !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) / 149, 5) + depth / 3 + 4;
Depth R = std::min(int(eval - beta) / 130, 5) + depth / 3 + 4;

ss->currentMove = MOVE_NULL;
ss->continuationHistory = &thisThread->continuationHistory[0][0][NO_PIECE][0];
Expand Down Expand Up @@ -739,10 +739,10 @@ Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, boo
return qsearch<PV>(pos, ss, alpha, beta);

// For cutNodes without a ttMove, we decrease depth by 2 if depth is high enough.
if (cutNode && depth >= 8 && !ttMove)
if (cutNode && depth >= 9 && !ttMove)
depth -= 2;

probCutBeta = beta + 165 - 51 * improving;
probCutBeta = beta + 169 - 55 * improving;

// Step 10. ProbCut (~4 Elo)
// If we have a good enough capture and a reduced search returns a value
Expand Down Expand Up @@ -801,7 +801,7 @@ Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, boo
moves_loop: // When in check, search starts here

// Step 11. A small Probcut idea, when we are in check (~0 Elo)
probCutBeta = beta + 468;
probCutBeta = beta + 444;
if (ss->inCheck && !PvNode && ttCapture && (tte->bound() & BOUND_LOWER)
&& tte->depth() >= depth - 3 && ttValue >= probCutBeta
&& std::abs(ttValue) < VALUE_MATE_IN_MAX_PLY && std::abs(beta) < VALUE_MATE_IN_MAX_PLY)
Expand Down Expand Up @@ -883,14 +883,14 @@ Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, boo
{
Piece capturedPiece = pos.piece_on(to_sq(move));
int futilityEval =
ss->staticEval + 291 + 238 * lmrDepth + PieceValue[capturedPiece]
ss->staticEval + 278 + 243 * lmrDepth + PieceValue[capturedPiece]
+ captureHistory[movedPiece][to_sq(move)][type_of(capturedPiece)] / 5;
if (futilityEval < alpha)
continue;
}

// SEE based pruning for captures and checks (~11 Elo)
if (!pos.see_ge(move, Value(-248) * depth))
if (!pos.see_ge(move, Value(-228) * depth))
continue;
}
else
Expand All @@ -901,25 +901,25 @@ Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, boo
+ thisThread->pawnHistory[pawn_structure_index(pos)][movedPiece][to_sq(move)];

// Continuation history based pruning (~2 Elo)
if (lmrDepth < 6 && history < -3547 * depth)
if (lmrDepth < 6 && history < -3503 * depth)
continue;

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

lmrDepth += history / 6062;
lmrDepth += history / 5704;
lmrDepth = std::max(lmrDepth, -2);

// Futility pruning: parent node (~9 Elo)
if (!ss->inCheck && lmrDepth < 9
&& ss->staticEval + (bestValue < ss->staticEval - 58 ? 138 : 119)
+ 136 * lmrDepth
&& ss->staticEval + (bestValue < ss->staticEval - 58 ? 139 : 121)
+ 128 * lmrDepth
<= alpha)
continue;

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

// Prune moves with negative SEE (~4 Elo)
if (!pos.see_ge(move, Value(-39 * lmrDepth * lmrDepth)))
if (!pos.see_ge(move, Value(-38 * lmrDepth * lmrDepth)))
continue;
}
}
Expand All @@ -939,11 +939,11 @@ Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, boo
// so changing them requires tests at these types of time controls.
// Recursive singular search is avoided.
if (!rootNode && move == ttMove && !excludedMove
&& depth >= 4 - (thisThread->completedDepth > 31) + 2 * (PvNode && tte->is_pv())
&& depth >= 4 - (thisThread->completedDepth > 34) + 2 * (PvNode && tte->is_pv())
&& std::abs(ttValue) < VALUE_MATE_IN_MAX_PLY && (tte->bound() & BOUND_LOWER)
&& tte->depth() >= depth - 3)
{
Value singularBeta = ttValue - (54 + 71 * (ss->ttPv && !PvNode)) * depth / 69;
Value singularBeta = ttValue - (50 + 69 * (ss->ttPv && !PvNode)) * depth / 64;
Depth singularDepth = newDepth / 2;

ss->excludedMove = move;
Expand All @@ -957,10 +957,10 @@ Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, boo
singularQuietLMR = !ttCapture;

// Avoid search explosion by limiting the number of double extensions
if (!PvNode && value < singularBeta - 19 && ss->doubleExtensions <= 8)
if (!PvNode && value < singularBeta - 18 && ss->doubleExtensions <= 8)
{
extension = 2;
depth += depth < 13;
depth += depth < 14;
}
}

Expand Down Expand Up @@ -997,13 +997,13 @@ Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, boo

// Quiet ttMove extensions (~1 Elo)
else if (PvNode && move == ttMove && move == ss->killers[0]
&& (*contHist[0])[movedPiece][to_sq(move)] >= 6598)
&& (*contHist[0])[movedPiece][to_sq(move)] >= 7061)
extension = 1;

// Recapture extensions (~1 Elo)
else if (PvNode && move == ttMove && to_sq(move) == prevSq
&& captureHistory[movedPiece][to_sq(move)][type_of(pos.piece_on(to_sq(move)))]
> 3479)
> 3639)
extension = 1;
}

Expand Down Expand Up @@ -1058,10 +1058,10 @@ Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, boo
ss->statScore = 2 * thisThread->mainHistory[us][from_to(move)]
+ (*contHist[0])[movedPiece][to_sq(move)]
+ (*contHist[1])[movedPiece][to_sq(move)]
+ (*contHist[3])[movedPiece][to_sq(move)] - 4897;
+ (*contHist[3])[movedPiece][to_sq(move)] - 5019;

// Decrease/increase reduction for moves with a good/bad history (~25 Elo)
r -= ss->statScore / 12482;
r -= ss->statScore / 12254;

// Step 16. Late moves reduction / extension (LMR, ~98 Elo)
// We use various heuristics for the sons of a node after the first son has
Expand All @@ -1084,7 +1084,7 @@ Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, boo
{
// 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 + 61 + 2 * newDepth); // (~1 Elo)
const bool doDeeperSearch = value > (bestValue + 63 + 2 * newDepth); // (~1 Elo)
const bool doShallowerSearch = value < bestValue + newDepth; // (~2 Elo)

newDepth += doDeeperSearch - doShallowerSearch;
Expand Down Expand Up @@ -1200,7 +1200,7 @@ Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, boo
else
{
// Reduce other moves if we have found at least one score improvement (~2 Elo)
if (depth > 2 && depth < 12 && beta < 12606 && value > -13566)
if (depth > 2 && depth < 12 && beta < 12805 && value > -13334)
depth -= 2;

assert(depth > 0);
Expand Down Expand Up @@ -1240,8 +1240,8 @@ Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, boo
else if (!priorCapture && prevSq != SQ_NONE)
{
// Extra bonuses for PV/Cut nodes or bad fail lows
int bonus = (depth > 4) + (PvNode || cutNode) + ((ss - 1)->statScore < -16762)
+ ((ss - 1)->moveCount > 9);
int bonus = (depth > 4) + (PvNode || cutNode) + ((ss - 1)->statScore < -16528)
+ ((ss - 1)->moveCount > 10);
update_continuation_histories(ss - 1, pos.piece_on(prevSq), prevSq,
stat_bonus(depth) * bonus);
thisThread->mainHistory[~us][from_to((ss - 1)->currentMove)]
Expand Down Expand Up @@ -1399,7 +1399,7 @@ Value qsearch(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth) {
if (bestValue > alpha)
alpha = bestValue;

futilityBase = ss->staticEval + 92;
futilityBase = ss->staticEval + 90;
}

const PieceToHistory* contHist[] = {(ss - 1)->continuationHistory,
Expand Down Expand Up @@ -1476,7 +1476,7 @@ Value qsearch(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth) {
continue;

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

Expand Down Expand Up @@ -1611,7 +1611,7 @@ void update_all_stats(const Position& pos,

if (!pos.capture(bestMove))
{
int bestMoveBonus = bestValue > beta + 99 ? quietMoveBonus // larger bonus
int bestMoveBonus = bestValue > beta + 101 ? quietMoveBonus // larger bonus
: stat_bonus(depth); // smaller bonus

// Increase stats for the best move in case it was a quiet move
Expand Down

0 comments on commit e9c386c

Please sign in to comment.