Skip to content

Commit

Permalink
Bench: 19885655
Browse files Browse the repository at this point in the history
  • Loading branch information
TerjeKir committed Oct 26, 2023
1 parent f5e3382 commit a15beb4
Show file tree
Hide file tree
Showing 8 changed files with 340 additions and 32 deletions.
45 changes: 41 additions & 4 deletions src/evaluate.c
Original file line number Diff line number Diff line change
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] = { 35, 20, 40, 80 };
const int CheckPower[4] = { 100, 35, 65, 65 };
const int CountModifier[8] = { 0, 0, 64, 96, 113, 120, 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
1 change: 1 addition & 0 deletions src/evaluate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, 8192))
#define NoisyHistoryUpdate(move, bonus) (HistoryBonus(NoisyEntry(move), bonus, 16384))
#define ContHistoryUpdate(offset, move, bonus) (HistoryBonus(ContEntry(offset, move), bonus, 16384))
#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(2100, 350 * depth - 350);
return MIN(HistBonusMax, HistBonusDepth * depth - HistBonusBase);
}

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


extern int MPGood;
extern int MPBad;


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

Expand Down Expand Up @@ -96,8 +100,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 > 12000
|| (mp->list.moves[mp->list.next-1].score > -8000 && 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
109 changes: 88 additions & 21 deletions src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,79 @@ volatile bool SEARCH_STOPPED = true;
static int Reductions[2][32][32];


float LMRNoisyBase = 0.0;
float LMRNoisyDiv = 3.25;
float LMRQuietBase = 1.50;
float LMRQuietDiv = 1.75;

int IIRDepth = 4;
int IIRCutDepth = 7;
int RFPDepth = 7;
int RFPBase = 100;
int RFPHistScore = 400;
int RFPHistory = 10000;
int NMPFlat = 120;
int NMPDepth = 15;
int NMPHist = 35000;
int NMPRBase = 3;
int NMPRDepth = 5;
int NMPREvalDiv = 256;
int NMPREvalMin = 3;
int ProbCut = 200;
int ProbCutDepth = 5;
int ProbCutReturn = 100;
int HistPruneDepth = 3;
int HistPrune = 1024;
int SEEPruneDepth = 7;
int SEEPruneQ = 50;
int SEEPruneN = 90;
int LMRHist = 8192;
int DeeperBase = 70;
int DeeperDepth = 12;

int QSFutility = 60;

int Aspi = 12;
int Trend = 35;

int HistQDiv = 8192;
int HistCDiv = 16384;
int HistNDiv = 16384;
int HistBonusMax = 2100;
int HistBonusBase = 350;
int HistBonusDepth = 350;

int NPower = 35;
int BPower = 20;
int RPower = 40;
int QPower = 80;
int NCPower = 100;
int BCPower = 35;
int RCPower = 65;
int QCPower = 65;
int Modifier1 = 0;
int Modifier2 = 0;
int Modifier3 = 64;
int Modifier4 = 96;
int Modifier5 = 113;
int Modifier6 = 120;
int Modifier7 = 124;
int Modifier8 = 128;

int MPGood = 12000;
int MPBad = 8000;

// 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.00 + log(depth) * log(moves) / 3.25, // capture
Reductions[1][depth][moves] = 1.50 + log(depth) * log(moves) / 1.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 @@ -125,7 +192,7 @@ static int Quiescence(Thread *thread, Stack *ss, int alpha, const int beta) {
if (eval > alpha)
alpha = eval;

futility = eval + 60;
futility = eval + QSFutility;
bestScore = eval;

moveloop:
Expand Down Expand Up @@ -304,31 +371,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 >= 4 && !ttMove)
if (pvNode && depth >= IIRDepth && !ttMove)
depth--;

if (cutnode && depth >= 7 && !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 - 100 * depth - (ss-1)->histScore / 400 >= beta
&& (!ttMove || GetHistory(thread, ss, ttMove) > 10000))
&& eval - RFPBase * depth - (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 + 120 - 15 * depth
&& (ss-1)->histScore < 35000
&& ss->eval >= beta + NMPFlat - NMPDepth * depth
&& (ss-1)->histScore < NMPHist
&& pos->nonPawnCount[sideToMove] > (depth > 8)) {

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

// Remember who last null-moved
Color nullMoverTemp = thread->nullMover;
Expand All @@ -348,10 +415,10 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth
return score >= TBWIN_IN_MAX ? beta : score;
}

int probCutBeta = beta + 200;
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 @@ -376,7 +443,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 - 100;
return score - ProbCutReturn;
}
}

Expand Down Expand Up @@ -409,19 +476,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 / 8192;
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 ? depth * depth : 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 : -90 * depth))
if (lmrDepth < SEEPruneDepth && !SEE(pos, move, quiet ? -SEEPruneQ * depth : -SEEPruneN * depth))
continue;
}

Expand Down Expand Up @@ -489,7 +556,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 / 8192;
r -= ss->histScore / LMRHist;
// Reduce less in pv nodes
r -= pvNode;
// Reduce less when improving
Expand All @@ -510,7 +577,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 + 70 + 12 * (newDepth - lmrDepth);
bool deeper = score > bestScore + DeeperBase + DeeperDepth * (newDepth - lmrDepth);

newDepth += deeper;

Expand Down Expand Up @@ -597,12 +664,12 @@ static void AspirationWindow(Thread *thread, Stack *ss) {

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

int delta = 12 + prevScore * prevScore / 16384;
int delta = Aspi + prevScore * prevScore / 16384;

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

int x = CLAMP(prevScore / 2, -35, 35);
int x = CLAMP(prevScore / 2, -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
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();
2 changes: 1 addition & 1 deletion src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ typedef struct BenchResult {
} BenchResult;

void Benchmark(int argc, char **argv) {

Reinit();
// Default depth 16, 1 thread, and 32MB hash
Limits.depth = argc > 2 ? atoi(argv[2]) : 16;
int threadCount = argc > 3 ? atoi(argv[3]) : 1;
Expand Down
Loading

0 comments on commit a15beb4

Please sign in to comment.