Skip to content

Commit

Permalink
Bench: 32418864
Browse files Browse the repository at this point in the history
  • Loading branch information
TerjeKir committed Nov 20, 2023
1 parent c000a56 commit 3524a97
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 37 deletions.
12 changes: 6 additions & 6 deletions src/evaluate.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const int PieceValue[2][PIECE_NB] = {
};

// Bonus for being the side to move
const int Tempo = 19;
const int Tempo = 18;

// Misc bonuses and maluses
const int PawnDoubled = S(-11,-48);
Expand Down Expand Up @@ -137,9 +137,9 @@ const int Mobility[4][28] = {
};

// KingSafety [pt-2]
const int AttackPower[4] = { 36, 19, 22, 72 };
const int CheckPower[4] = { 71, 39, 80, 74 };
const int CountModifier[8] = { 0, 0, 63, 126, 96, 124, 124, 128 };
const int AttackPower[4] = { 32, 20, 22, 69 };
const int CheckPower[4] = { 70, 42, 91, 68 };
const int CountModifier[8] = { 0, 0, 63, 120, 95, 124, 124, 128 };


// Evaluates pawns
Expand Down Expand Up @@ -335,7 +335,7 @@ INLINE int EvalKings(const Position *pos, EvalInfo *ei, const Color color) {
}

// King safety
ei->attackPower[!color] += (count - 3) * 8;
ei->attackPower[!color] += (count - 3) * 9;

int danger = ei->attackPower[!color]
* CountModifier[MIN(7, ei->attackCount[!color])];
Expand Down Expand Up @@ -505,7 +505,7 @@ static int ScaleFactor(const Position *pos, const int eval) {

int strongPawnCount = PopCount(strongPawns);
int x = 8 - strongPawnCount;
int pawnScale = 128 - x * x;
int pawnScale = 133 + 2 * x - x * x;

// Scale down when there aren't pawns on both sides of the board
if (!(strongPawns & QueenSideBB) || !(strongPawns & KingSideBB))
Expand Down
26 changes: 17 additions & 9 deletions src/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,21 @@
#define NoisyEntry(move) (&thread->captureHistory[piece(move)][toSq(move)][PieceTypeOf(capturing(move))])
#define ContEntry(offset, move) (&(*(ss-offset)->continuation)[piece(move)][toSq(move)])

#define QuietHistoryUpdate(move, bonus) (HistoryBonus(QuietEntry(move), bonus, 5885))
#define NoisyHistoryUpdate(move, bonus) (HistoryBonus(NoisyEntry(move), bonus, 14500))
#define ContHistoryUpdate(offset, move, bonus) (HistoryBonus(ContEntry(offset, move), bonus, 23930))
#define QuietHistoryUpdate(move, bonus) (HistoryBonus(QuietEntry(move), bonus, 6500))
#define NoisyHistoryUpdate(move, bonus) (HistoryBonus(NoisyEntry(move), bonus, 15200))
#define ContHistoryUpdate(offset, move, bonus) (HistoryBonus(ContEntry(offset, move), bonus, 26850))


INLINE void HistoryBonus(int16_t *cur, int bonus, int div) {
*cur += bonus - *cur * abs(bonus) / div;
}

INLINE int Bonus(Depth depth) {
return MIN(2300, 315 * depth - 255);
return MIN(2560, 333 * depth - 285);
}

INLINE int Malus(Depth depth) {
return -MIN(1900, 367 * depth - 252);
}

INLINE void UpdateContHistories(Stack *ss, Move move, int bonus) {
Expand All @@ -50,7 +54,10 @@ INLINE void UpdateContHistories(Stack *ss, Move move, int bonus) {
}

// Updates history heuristics when a quiet move is the best move
INLINE void UpdateQuietHistory(Thread *thread, Stack *ss, Move bestMove, int bonus, Depth depth, Move quiets[], int qCount) {
INLINE void UpdateQuietHistory(Thread *thread, Stack *ss, Move bestMove, Depth depth, Move quiets[], int qCount) {

int bonus = Bonus(depth);
int malus = Malus(depth);

// Update killers
if (ss->killers[0] != bestMove) {
Expand All @@ -66,27 +73,28 @@ INLINE void UpdateQuietHistory(Thread *thread, Stack *ss, Move bestMove, int bon

// Penalize quiet moves that failed to produce a cut
for (Move *move = quiets; move < quiets + qCount; ++move) {
QuietHistoryUpdate(*move, -bonus);
UpdateContHistories(ss, *move, -bonus);
QuietHistoryUpdate(*move, malus);
UpdateContHistories(ss, *move, malus);
}
}

// Updates history heuristics
INLINE void UpdateHistory(Thread *thread, Stack *ss, Move bestMove, Depth depth, Move quiets[], int qCount, Move noisys[], int nCount) {

int bonus = Bonus(depth);
int malus = Malus(depth);

// Update quiet history if bestMove is quiet
if (moveIsQuiet(bestMove))
UpdateQuietHistory(thread, ss, bestMove, bonus, depth, quiets, qCount);
UpdateQuietHistory(thread, ss, bestMove, depth, quiets, qCount);

// Bonus to the move that caused the beta cutoff
if (depth > 2 && !moveIsQuiet(bestMove))
NoisyHistoryUpdate(bestMove, bonus);

// Penalize noisy moves that failed to produce a cut
for (Move *move = noisys; move < noisys + nCount; ++move)
NoisyHistoryUpdate(*move, -bonus);
NoisyHistoryUpdate(*move, malus);
}

INLINE int GetQuietHistory(const Thread *thread, Stack *ss, Move move) {
Expand Down
6 changes: 3 additions & 3 deletions src/movepicker.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static void ScoreMoves(MovePicker *mp, const int stage) {
: GetCaptureHistory(thread, move) + PieceValue[MG][capturing(move)];
}

SortMoves(list, -1280 * mp->depth);
SortMoves(list, -1380 * mp->depth);
}

// Returns the next move to try in a position
Expand All @@ -96,8 +96,8 @@ Move NextMove(MovePicker *mp) {
case NOISY_GOOD:
// Save seemingly bad noisy moves for later
while ((move = PickNextMove(mp)))
if ( mp->list.moves[mp->list.next-1].score > 13470
|| (mp->list.moves[mp->list.next-1].score > -8830 && SEE(pos, move, mp->threshold)))
if ( mp->list.moves[mp->list.next-1].score > 13540
|| (mp->list.moves[mp->list.next-1].score > -9135 && SEE(pos, move, mp->threshold)))
return move;
else
mp->list.moves[mp->bads++].move = move;
Expand Down
38 changes: 19 additions & 19 deletions src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ static int Reductions[2][32][32];
CONSTR(1) InitReductions() {
for (int depth = 1; depth < 32; ++depth)
for (int moves = 1; moves < 32; ++moves)
Reductions[0][depth][moves] = 0.20 + log(depth) * log(moves) / 3.35, // capture
Reductions[1][depth][moves] = 1.35 + log(depth) * log(moves) / 2.75; // quiet
Reductions[0][depth][moves] = 0.13 + log(depth) * log(moves) / 3.45, // capture
Reductions[1][depth][moves] = 1.30 + log(depth) * log(moves) / 2.80; // quiet
}

// Checks whether a move was already searched in multi-pv mode
Expand Down Expand Up @@ -121,7 +121,7 @@ static int Quiescence(Thread *thread, Stack *ss, int alpha, const int beta) {
if (eval > alpha)
alpha = eval;

futility = eval + 68;
futility = eval + 75;
bestScore = eval;

moveloop:
Expand Down Expand Up @@ -310,15 +310,15 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth
// Reverse Futility Pruning
if ( depth < 7
&& eval >= beta
&& eval - 80 * (depth - improving) - (ss-1)->histScore / 230 >= beta
&& (!ttMove || GetHistory(thread, ss, ttMove) > 7300))
&& eval - 78 * (depth - improving) - (ss-1)->histScore / 200 >= beta
&& (!ttMove || GetHistory(thread, ss, ttMove) > 6950))
return eval;

// Null Move Pruning
if ( eval >= beta
&& eval >= ss->eval
&& ss->eval >= beta + 135 - 19 * depth
&& (ss-1)->histScore < 25500
&& ss->eval >= beta + 146 - 20 * depth
&& (ss-1)->histScore < 27150
&& pos->nonPawnCount[sideToMove] > (depth > 8)) {

Depth reduction = 3 + depth / 4 + MIN(3, (eval - beta) / 256);
Expand All @@ -335,7 +335,7 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth
return score >= TBWIN_IN_MAX ? beta : score;
}

int probCutBeta = beta + 180;
int probCutBeta = beta + 200;

// ProbCut
if ( depth >= 5
Expand Down Expand Up @@ -363,7 +363,7 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth

// Cut if the reduced depth search beats the threshold
if (score >= probCutBeta)
return score - 138;
return score - 176;
}
}

Expand Down Expand Up @@ -396,19 +396,19 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth
&& thread->doPruning
&& bestScore > -TBWIN_IN_MAX) {

int R = Reductions[quiet][MIN(31, depth)][MIN(31, moveCount)] - ss->histScore / 8550;
int R = Reductions[quiet][MIN(31, depth)][MIN(31, moveCount)] - ss->histScore / 9600;
Depth lmrDepth = depth - 1 - R;

// Quiet late move pruning
if (moveCount > (improving ? 1 + depth * depth : -2 + depth * depth / 2))
if (moveCount > (improving ? depth * depth : -2 + depth * depth / 2))
mp.onlyNoisy = true;

// History pruning
if (lmrDepth < 3 && ss->histScore < -1024 * depth)
continue;

// SEE pruning
if (lmrDepth < 7 && !SEE(pos, move, quiet ? -50 * depth : -85 * depth))
if (lmrDepth < 7 && !SEE(pos, move, quiet ? -47 * depth : -72 * depth))
continue;
}

Expand Down Expand Up @@ -444,7 +444,7 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth
// Singular - extend by 1 or 2 ply
if (score < singularBeta) {
extension = 1;
if (!pvNode && score < singularBeta - 19 && ss->doubleExtensions <= 5)
if (!pvNode && score < singularBeta - 17 && ss->doubleExtensions <= 5)
extension = 2;
// MultiCut - ttMove as well as at least one other move seem good enough to beat beta
} else if (singularBeta >= beta)
Expand Down Expand Up @@ -476,7 +476,7 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth
// Base reduction
int r = Reductions[quiet][MIN(31, depth)][MIN(31, moveCount)];
// Adjust reduction by move history
r -= ss->histScore / 8550;
r -= ss->histScore / 9600;
// Reduce less in pv nodes
r -= pvNode;
// Reduce less when improving
Expand All @@ -495,7 +495,7 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth

// Research with the same window at full depth if the reduced search failed high
if (score > alpha && lmrDepth < newDepth) {
bool deeper = score > bestScore + 55 + 16 * (newDepth - lmrDepth);
bool deeper = score > bestScore + 49 + 16 * (newDepth - lmrDepth);

newDepth += deeper;

Expand Down Expand Up @@ -582,21 +582,21 @@ static void AspirationWindow(Thread *thread, Stack *ss) {

int prevScore = thread->rootMoves[multiPV].score;

int delta = 9 + prevScore * prevScore / 15500;
int delta = 9 + prevScore * prevScore / 16384;

int alpha = MAX(prevScore - delta, -INFINITE);
int beta = MIN(prevScore + delta, INFINITE);

int x = CLAMP(prevScore / 2, -34, 34);
int x = CLAMP(prevScore / 2, -32, 32);
pos->trend = sideToMove == WHITE ? S(x, x/2) : -S(x, x/2);

// Repeatedly search and adjust the window until the score is inside the window
while (true) {

thread->doPruning =
Limits.infinite ? TimeSince(Limits.start) > 1000
: TimeSince(Limits.start) >= Limits.optimalUsage / 58
|| depth > 2 + Limits.optimalUsage / 281;
: TimeSince(Limits.start) >= Limits.optimalUsage / 64
|| depth > 2 + Limits.optimalUsage / 270;

int score = AlphaBeta(thread, ss, alpha, beta, depth, false);

Expand Down

0 comments on commit 3524a97

Please sign in to comment.