Skip to content

Commit

Permalink
Bench: 19885655
Browse files Browse the repository at this point in the history
  • Loading branch information
TerjeKir committed Nov 15, 2023
1 parent 95562bb commit 83238f2
Show file tree
Hide file tree
Showing 8 changed files with 394 additions and 41 deletions.
47 changes: 42 additions & 5 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;
extern int Tempo;

// Misc bonuses and maluses
const int PawnDoubled = S(-11,-48);
Expand Down Expand Up @@ -136,11 +136,48 @@ const int Mobility[4][28] = {
S(122,221), S(135,193), S(146,166), S(125,162) }
};

// 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 };
extern int NPower;
extern int BPower;
extern int RPower;
extern int QPower;
extern int NCPower;
extern int BCPower;
extern int RCPower;
extern int QCPower;
extern int Modifier1;
extern int Modifier2;
extern int Modifier3;
extern int Modifier4;
extern int Modifier5;
extern int Modifier6;
extern int Modifier7;
extern int Modifier8;

// KingSafety [pt-2]
int AttackPower[4] = { 0 };
int CheckPower[4] = { 0 };
int CountModifier[8] = { 0 };

void InitSafety() {
AttackPower[KNIGHT-2] = NPower;
AttackPower[BISHOP-2] = BPower;
AttackPower[ROOK -2] = RPower;
AttackPower[QUEEN -2] = QPower;

CheckPower[KNIGHT-2] = NCPower;
CheckPower[BISHOP-2] = BCPower;
CheckPower[ROOK -2] = RCPower;
CheckPower[QUEEN -2] = QCPower;

CountModifier[0] = Modifier1;
CountModifier[1] = Modifier2;
CountModifier[2] = Modifier3;
CountModifier[3] = Modifier4;
CountModifier[4] = Modifier5;
CountModifier[5] = Modifier6;
CountModifier[6] = Modifier7;
CountModifier[7] = Modifier8;
}

// Evaluates pawns
INLINE int EvalPawns(const Position *pos, EvalInfo *ei, const Color color) {
Expand Down
3 changes: 2 additions & 1 deletion src/evaluate.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typedef struct PawnEntry {
typedef PawnEntry PawnCache[PAWN_CACHE_SIZE];


extern const int Tempo;
extern int Tempo;
extern const int PieceValue[COLOR_NB][PIECE_NB];


Expand All @@ -54,3 +54,4 @@ static inline int UpdatePhase(int value) {

// Returns a static evaluation of the position
int EvalPosition(const Position *pos, PawnCache pc);
void InitSafety();
16 changes: 12 additions & 4 deletions src/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,25 @@
#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, HistQDiv))
#define NoisyHistoryUpdate(move, bonus) (HistoryBonus(NoisyEntry(move), bonus, HistNDiv))
#define ContHistoryUpdate(offset, move, bonus) (HistoryBonus(ContEntry(offset, move), bonus, HistCDiv))


extern int HistQDiv;
extern int HistCDiv;
extern int HistNDiv;
extern int HistBonusMax;
extern int HistBonusBase;
extern int HistBonusDepth;


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(HistBonusMax, HistBonusDepth * depth - HistBonusBase);
}

INLINE void UpdateContHistories(Stack *ss, Move move, int bonus) {
Expand Down
11 changes: 8 additions & 3 deletions src/movepicker.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
#include "movepicker.h"


extern int ScoreMovesLimit;
extern int MPGood;
extern int MPBad;


// Return the next best move
static Move PickNextMove(MovePicker *mp) {

Expand Down Expand Up @@ -69,7 +74,7 @@ static void ScoreMoves(MovePicker *mp, const int stage) {
: GetCaptureHistory(thread, move) + PieceValue[MG][capturing(move)];
}

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

// Returns the next move to try in a position
Expand All @@ -96,8 +101,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 > MPGood
|| (mp->list.moves[mp->list.next-1].score > -MPBad && SEE(pos, move, mp->threshold)))
return move;
else
mp->list.moves[mp->bads++].move = move;
Expand Down
132 changes: 105 additions & 27 deletions src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,90 @@ volatile bool SEARCH_STOPPED = true;
static int Reductions[2][32][32];


float LMRNoisyBase = 0.20;
float LMRNoisyDiv = 3.35;
float LMRQuietBase = 1.35;
float LMRQuietDiv = 2.75;

int IIRDepth = 3;
int IIRCutDepth = 8;
int RFPDepth = 7;
int RFPBase = 80;
int RFPHistScore = 230;
int RFPHistory = 7300;
int NMPFlat = 135;
int NMPDepth = 19;
int NMPHist = 25500;
int NMPRBase = 3;
int NMPRDepth = 4;
int NMPREvalDiv = 256;
int NMPREvalMin = 3;
int ProbCut = 180;
int ProbCutDepth = 5;
int ProbCutReturn = 138;
int LMPImp = 1;
int LMPNonImp = -2;
int HistPruneDepth = 3;
int HistPrune = 1024;
int SEEPruneDepth = 7;
int SEEPruneQ = 50;
int SEEPruneN = 85;
int SingExtDepth = 4;
int SingExtTTDepth = 3;
int SingExtDouble = 19;
int LMRHist = 8550;
int DeeperBase = 55;
int DeeperDepth = 16;

int QSFutility = 68;

int Aspi = 9;
int AspiScoreDiv = 15500;
int Trend = 34;
float TrendDiv = 0.50;
int PruneDiv = 58;
int PruneDepthDiv = 281;

int HistQDiv = 5885;
int HistCDiv = 23930;
int HistNDiv = 14500;
int HistBonusMax = 2300;
int HistBonusBase = 255;
int HistBonusDepth = 315;

int Tempo = 19;
int NPower = 36;
int BPower = 19;
int RPower = 22;
int QPower = 72;
int NCPower = 71;
int BCPower = 39;
int RCPower = 80;
int QCPower = 74;
int Modifier1 = 0;
int Modifier2 = 0;
int Modifier3 = 63;
int Modifier4 = 126;
int Modifier5 = 96;
int Modifier6 = 124;
int Modifier7 = 124;
int Modifier8 = 128;

int ScoreMovesLimit = 1280;
int MPGood = 13470;
int MPBad = 8830;

// Initializes the late move reduction array
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] = LMRNoisyBase + log(depth) * log(moves) / LMRNoisyDiv, // capture
Reductions[1][depth][moves] = LMRQuietBase + log(depth) * log(moves) / LMRQuietDiv; // quiet
}

void Reinit() {
InitReductions();
InitSafety();
}

// Checks if the move is in the list of searchmoves if any were given
Expand Down Expand Up @@ -122,7 +200,7 @@ static int Quiescence(Thread *thread, Stack *ss, int alpha, const int beta) {
if (eval > alpha)
alpha = eval;

futility = eval + 68;
futility = eval + QSFutility;
bestScore = eval;

moveloop:
Expand Down Expand Up @@ -298,31 +376,31 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth
bool improving = !inCheck && eval > (ss-2)->eval;

// Internal iterative reduction based on Rebel's idea
if (pvNode && depth >= 3 && !ttMove)
if (pvNode && depth >= IIRDepth && !ttMove)
depth--;

if (cutnode && depth >= 8 && !ttMove)
if (cutnode && depth >= IIRCutDepth && !ttMove)
depth--;

// Skip pruning in check, pv nodes, early iterations, when proving singularity, looking for terminal scores, or after a null move
if (inCheck || pvNode || !thread->doPruning || ss->excluded || abs(beta) >= TBWIN_IN_MAX || history(-1).move == NOMOVE)
goto move_loop;

// Reverse Futility Pruning
if ( depth < 7
if ( depth < RFPDepth
&& eval >= beta
&& eval - 80 * (depth - improving) - (ss-1)->histScore / 230 >= beta
&& (!ttMove || GetHistory(thread, ss, ttMove) > 7300))
&& eval - RFPBase * (depth - improving) - (ss-1)->histScore / RFPHistScore >= beta
&& (!ttMove || GetHistory(thread, ss, ttMove) > RFPHistory))
return eval;

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

Depth reduction = 3 + depth / 4 + MIN(3, (eval - beta) / 256);
Depth reduction = NMPRBase + depth / NMPRDepth + MIN(NMPREvalMin, (eval - beta) / NMPREvalDiv);

ss->continuation = &thread->continuation[0][0][EMPTY][0];

Expand All @@ -336,10 +414,10 @@ 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 + ProbCut;

// ProbCut
if ( depth >= 5
if ( depth >= ProbCutDepth
&& (!ttHit || ttBound == BOUND_LOWER || ttScore >= probCutBeta)) {

InitProbcutMP(&mp, thread, ss, probCutBeta - ss->eval);
Expand All @@ -364,7 +442,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 - ProbCutReturn;
}
}

Expand Down Expand Up @@ -397,19 +475,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 / LMRHist;
Depth lmrDepth = depth - 1 - R;

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

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

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

Expand All @@ -426,10 +504,10 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth
goto skip_extensions;

// Singular extension
if ( depth > 4
if ( depth > SingExtDepth
&& move == ttMove
&& !ss->excluded
&& ttDepth > depth - 3
&& ttDepth > depth - SingExtTTDepth
&& ttBound != BOUND_UPPER
&& abs(ttScore) < TBWIN_IN_MAX / 4) {

Expand All @@ -445,7 +523,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 - SingExtDouble && 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 @@ -477,7 +555,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 / LMRHist;
// Reduce less in pv nodes
r -= pvNode;
// Reduce less when improving
Expand All @@ -496,7 +574,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 + DeeperBase + DeeperDepth * (newDepth - lmrDepth);

newDepth += deeper;

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

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

int delta = 9 + prevScore * prevScore / 15500;
int delta = Aspi + prevScore * prevScore / AspiScoreDiv;

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

int x = CLAMP(prevScore / 2, -34, 34);
int x = CLAMP(prevScore * TrendDiv, -Trend, Trend);
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 / PruneDiv
|| depth > 2 + Limits.optimalUsage / PruneDepthDiv;

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

Expand Down
2 changes: 2 additions & 0 deletions src/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ extern volatile bool SEARCH_STOPPED;


void *SearchPosition(void *pos);

void Reinit();
Loading

0 comments on commit 83238f2

Please sign in to comment.