Skip to content

Commit

Permalink
Bench: 3680717
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonnold committed Oct 14, 2023
1 parent 61d2433 commit 964fdfc
Show file tree
Hide file tree
Showing 4 changed files with 306 additions and 49 deletions.
136 changes: 103 additions & 33 deletions src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,75 @@
#include "util.h"
#include "zobrist.h"

float a0 = 2.25;
float a1 = 0.25;

float b0 = 1.5;
float b1 = 0.5;

float c0 = 3.0;
float c1 = 1.0;

float d0 = 17.0;

float e0 = 104.0;

float f0 = 1.25;
float f1 = 0.05;

float g0 = 0.1;
float g1 = 0.0275;
float g2 = 0.0275;
float g3 = 0.5;
float g4 = 1.5;

float h0 = 0.5;
float h1 = 2.0;
float h2 = 0.4;

int i0 = 8;
int i1 = 69;
int i2 = 112;
int i3 = 12288;

int v0 = 6;
int v1 = 250;

int k0 = 3;
int k1 = 4;
int k2 = 188;
int k3 = 5;
int k4 = 3;

int l0 = 200;
int l1 = 5;

int m0 = 6;
int m1 = 2500;

int n0 = 10;
int n1 = 88;
int n2 = 47;
int n3 = 13;

int o0 = 6;
int o1 = 5;
int o2 = 18;
int o3 = 6;

int p0 = 9;

int q0 = 75;

int r0 = 10;

int s0 = 86;

int t0 = 55;

int u0 = 10;
int u1 = 16;

// arrays to store these pruning cutoffs at specific depths
int LMR[MAX_SEARCH_PLY][64];
int LMP[2][MAX_SEARCH_PLY];
Expand All @@ -49,19 +118,19 @@ int STATIC_PRUNE[2][MAX_SEARCH_PLY];
void InitPruningAndReductionTables() {
for (int depth = 1; depth < MAX_SEARCH_PLY; depth++)
for (int moves = 1; moves < 64; moves++)
LMR[depth][moves] = log(depth) * log(moves) / 2.25 + 0.25;
LMR[depth][moves] = log(depth) * log(moves) / a0 + a1;

LMR[0][0] = LMR[0][1] = LMR[1][0] = 0;

for (int depth = 0; depth < MAX_SEARCH_PLY; depth++) {
// LMP has both a improving (more strict) and non-improving evalution
// parameter for lmp. If the evaluation is getting better we want to check
// more
LMP[0][depth] = (3 + depth * depth) / 2;
LMP[1][depth] = 3 + depth * depth;
LMP[0][depth] = b0 + b1 * depth * depth;
LMP[1][depth] = c0 + c1 * depth * depth;

STATIC_PRUNE[0][depth] = -SEE_PRUNE_CUTOFF * depth * depth; // quiet move cutoff
STATIC_PRUNE[1][depth] = -SEE_PRUNE_CAPTURE_CUTOFF * depth; // capture cutoff
STATIC_PRUNE[0][depth] = -d0 * depth * depth; // quiet move cutoff
STATIC_PRUNE[1][depth] = -e0 * depth; // capture cutoff
}
}

Expand Down Expand Up @@ -200,9 +269,9 @@ void Search(ThreadData* thread) {

// One at depth 5 or later, start search at a reduced window
if (thread->depth >= 5) {
alpha = Max(score - WINDOW, -CHECKMATE);
beta = Min(score + WINDOW, CHECKMATE);
delta = WINDOW;
delta = u0;
alpha = Max(score - delta, -CHECKMATE);
beta = Min(score + delta, CHECKMATE);
}

while (1) {
Expand Down Expand Up @@ -239,7 +308,7 @@ void Search(ThreadData* thread) {
break;

// delta x 1.25
delta += delta / 4;
delta += u1 * delta / 64;
}

SortRootMoves(thread, 0);
Expand Down Expand Up @@ -270,7 +339,7 @@ void Search(ThreadData* thread) {
else if (Limits.timeset && thread->depth >= 5 && !Threads.stopOnPonderHit) {
int sameBestMove = bestMove == previousBestMove; // same move?
searchStability = sameBestMove ? Min(10, searchStability + 1) : 0; // increase how stable our best move is
double stabilityFactor = 1.25 - 0.05 * searchStability;
double stabilityFactor = f0 - f1 * searchStability;

Score searchScoreDiff = scores[thread->depth - 3] - bestScore;
Score prevScoreDiff = thread->previousScore - bestScore;
Expand All @@ -279,14 +348,14 @@ void Search(ThreadData* thread) {
if (thread->previousScore == UNKNOWN)
searchScoreDiff *= 2, prevScoreDiff = 0;

double scoreChangeFactor = 0.1 + //
0.0275 * searchScoreDiff * (searchScoreDiff > 0) + //
0.0275 * prevScoreDiff * (prevScoreDiff > 0);
scoreChangeFactor = Max(0.5, Min(1.5, scoreChangeFactor));
double scoreChangeFactor = g0 + //
g1 * searchScoreDiff * (searchScoreDiff > 0) + //
g2 * prevScoreDiff * (prevScoreDiff > 0);
scoreChangeFactor = Max(g3, Min(g4, scoreChangeFactor));

uint64_t bestMoveNodes = thread->rootMoves[0].nodes;
double pctNodesNotBest = 1.0 - (double) bestMoveNodes / thread->nodes;
double nodeCountFactor = Max(0.5, pctNodesNotBest * 2 + 0.4);
double nodeCountFactor = Max(h0, pctNodesNotBest * h1 + h2);
if (bestScore >= TB_WIN_BOUND)
nodeCountFactor = 0.5;

Expand Down Expand Up @@ -453,13 +522,13 @@ int Negamax(int alpha, int beta, int depth, int cutnode, ThreadData* thread, PV*
if (!isPV && !inCheck) {
// Reverse Futility Pruning
// i.e. the static eval is so far above beta we prune
if (depth <= 8 && !ss->skip && eval < TB_WIN_BOUND && eval >= beta &&
eval - 69 * depth + 112 * (improving && !board->easyCapture) >= beta &&
(!hashMove || GetHistory(ss, thread, hashMove) > 12288))
if (depth <= i0 && !ss->skip && eval < TB_WIN_BOUND && eval >= beta &&
eval - i1 * depth + i2 * (improving && !board->easyCapture) >= beta &&
(!hashMove || GetHistory(ss, thread, hashMove) > i3))
return eval;

// Razoring
if (depth <= 6 && eval + 250 * depth <= alpha) {
if (depth <= v0 && eval + v1 * depth <= alpha) {
score = Quiesce(alpha, beta, 0, thread, ss);
if (score <= alpha)
return score;
Expand All @@ -469,10 +538,11 @@ int Negamax(int alpha, int beta, int depth, int cutnode, ThreadData* thread, PV*
// i.e. Our position is so good we can give our opponnent a free move and
// they still can't catch up (this is usually countered by captures or mate
// threats)
if (depth >= 3 && (ss - 1)->move != NULL_MOVE && !ss->skip && eval >= beta &&
if (depth >= k0 && (ss - 1)->move != NULL_MOVE && !ss->skip && eval >= beta &&
// weiss conditional
HasNonPawn(board) > (depth > 12)) {
int R = 4 + 188 * depth / 1024 + Min(5 * (eval - beta) / 1024, 3) + !board->easyCapture;

int R = k1 + k2 * depth / 1024 + Min(k3 * (eval - beta) / 1024, k4) + !board->easyCapture;
R = Min(depth, R); // don't go too low

TTPrefetch(KeyAfter(board, NULL_MOVE));
Expand All @@ -491,8 +561,8 @@ int Negamax(int alpha, int beta, int depth, int cutnode, ThreadData* thread, PV*
// Prob cut
// If a relatively deep search from our TT doesn't say this node is
// less than beta + margin, then we run a shallow search to look
int probBeta = beta + 200;
if (depth >= 5 && !ss->skip && abs(beta) < TB_WIN_BOUND && !(ttHit && ttDepth >= depth - 3 && ttScore < probBeta)) {
int probBeta = beta + l0;
if (depth >= l1 && !ss->skip && abs(beta) < TB_WIN_BOUND && !(ttHit && ttDepth >= depth - 3 && ttScore < probBeta)) {
InitPCMovePicker(&mp, thread, probBeta > eval);
while ((move = NextMove(&mp, board, 1))) {
if (!IsLegal(move, board))
Expand Down Expand Up @@ -547,12 +617,12 @@ int Negamax(int alpha, int beta, int depth, int cutnode, ThreadData* thread, PV*
if (!IsCap(move) && PromoPT(move) != QUEEN) {
int lmrDepth = Max(1, depth - LMR[Min(depth, 63)][Min(legalMoves, 63)]);

if (!killerOrCounter && lmrDepth < 6 && history < -2500 * (depth - 1)) {
if (!killerOrCounter && lmrDepth < m0 && history < -m1 * (depth - 1)) {
skipQuiets = 1;
continue;
}

if (!inCheck && lmrDepth < 10 && eval + 88 + 47 * lmrDepth + 13 * history / 2048 <= alpha)
if (!inCheck && lmrDepth < n0 && eval + n1 + n2 * lmrDepth + n3 * history / 2048 <= alpha)
skipQuiets = 1;

if (!SEE(board, move, STATIC_PRUNE[0][lmrDepth]))
Expand Down Expand Up @@ -584,9 +654,9 @@ int Negamax(int alpha, int beta, int depth, int cutnode, ThreadData* thread, PV*
// (allows for reductions when doing singular search)
if (!isRoot && ss->ply < thread->depth * 2) {
// ttHit is implied for move == hashMove to ever be true
if (depth >= 6 && move == hashMove && ttDepth >= depth - 3 && (ttBound & BOUND_LOWER) &&
if (depth >= o0 && move == hashMove && ttDepth >= depth - 3 && (ttBound & BOUND_LOWER) &&
abs(ttScore) < TB_WIN_BOUND) {
int sBeta = Max(ttScore - 5 * depth / 8, -CHECKMATE);
int sBeta = Max(ttScore - o1 * depth / 8, -CHECKMATE);
int sDepth = (depth - 1) / 2;

ss->skip = move;
Expand All @@ -595,7 +665,7 @@ int Negamax(int alpha, int beta, int depth, int cutnode, ThreadData* thread, PV*

// no score failed above sBeta, so this is singular
if (score < sBeta) {
if (!isPV && score < sBeta - 18 && ss->de <= 6) {
if (!isPV && score < sBeta - o2 && ss->de <= o3) {
extension = 2;
ss->de = (ss - 1)->de + 1;
} else {
Expand Down Expand Up @@ -650,15 +720,15 @@ int Negamax(int alpha, int beta, int depth, int cutnode, ThreadData* thread, PV*
R += 1 + !IsCap(move);

// adjust reduction based on historical score
R -= 9 * history / 65536;
R -= p0 * history / 65536;

// prevent dropping into QS, extending, or reducing all extensions
R = Min(depth - 1, Max(R, 1));

score = -Negamax(-alpha - 1, -alpha, newDepth - R, 1, thread, &childPv, ss + 1);

if (score > alpha && R > 1) {
newDepth += (score > bestScore + 75);
newDepth += (score > bestScore + q0);

score = -Negamax(-alpha - 1, -alpha, newDepth - 1, !cutnode, thread, &childPv, ss + 1);
}
Expand Down Expand Up @@ -707,12 +777,12 @@ int Negamax(int alpha, int beta, int depth, int cutnode, ThreadData* thread, PV*
alpha = score;

if (alpha < beta && score > -TB_WIN_BOUND)
depth -= (depth >= 2 && depth <= 10);
depth -= (depth >= 2 && depth <= r0);
}

// we're failing high
if (alpha >= beta) {
UpdateHistories(ss, thread, move, depth + (bestScore > beta + 86), quiets, numQuiets, captures, numCaptures);
UpdateHistories(ss, thread, move, depth + (bestScore > beta + s0), quiets, numQuiets, captures, numCaptures);
break;
}
}
Expand Down Expand Up @@ -807,7 +877,7 @@ int Quiesce(int alpha, int beta, int depth, ThreadData* thread, SearchStack* ss)

bestScore = eval;

futility = bestScore + DELTA_CUTOFF;
futility = bestScore + t0;
}

if (!inCheck)
Expand Down
14 changes: 1 addition & 13 deletions src/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,12 @@
#include "types.h"

// search specific score evals
#define UNKNOWN \
32257 // this must be higher than CHECKMATE (some conditional logic relies on
// this)
#define UNKNOWN 32257 // this must be higher than CHECKMATE
#define CHECKMATE 32200
#define MATE_BOUND (32200 - MAX_SEARCH_PLY)
#define TB_WIN_SCORE MATE_BOUND
#define TB_WIN_BOUND (TB_WIN_SCORE - MAX_SEARCH_PLY)

// static evaluation pruning
#define SEE_PRUNE_CAPTURE_CUTOFF 104
#define SEE_PRUNE_CUTOFF 17

// delta pruning in QS
#define DELTA_CUTOFF 55

// base window value
#define WINDOW 10

void InitPruningAndReductionTables();

void StartSearch(Board* board, uint8_t ponder);
Expand Down
Loading

0 comments on commit 964fdfc

Please sign in to comment.