Skip to content

Commit

Permalink
Bench: 30658368
Browse files Browse the repository at this point in the history
  • Loading branch information
TerjeKir committed Nov 15, 2023
1 parent 95562bb commit f7da48b
Show file tree
Hide file tree
Showing 8 changed files with 471 additions and 51 deletions.
69 changes: 59 additions & 10 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,49 @@ 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 BasePower;
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 Expand Up @@ -313,6 +351,9 @@ INLINE int EvalPiece(const Position *pos, EvalInfo *ei, const Color color, const
return eval;
}

extern int KSCountOffset;
extern int KSCountScale;

// Evaluates kings
INLINE int EvalKings(const Position *pos, EvalInfo *ei, const Color color) {

Expand All @@ -335,7 +376,7 @@ INLINE int EvalKings(const Position *pos, EvalInfo *ei, const Color color) {
}

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

int danger = ei->attackPower[!color]
* CountModifier[MIN(7, ei->attackCount[!color])];
Expand Down Expand Up @@ -485,7 +526,7 @@ INLINE void InitEvalInfo(const Position *pos, EvalInfo *ei, const Color color) {
// King Safety
ei->kingZone[color] = AttackBB(KING, kingSq(color), 0);

ei->attackPower[color] = -30;
ei->attackPower[color] = BasePower;
ei->attackCount[color] = 0;

// Clear passed pawns, filled in during pawn eval
Expand All @@ -496,6 +537,12 @@ INLINE void InitEvalInfo(const Position *pos, EvalInfo *ei, const Color color) {
ei->attackedBy[color][ALL] = ei->attackedBy[color][KING] | ei->attackedBy[color][PAWN];
}

extern int PawnScaleBase;
extern int PawnScaleX;
extern int PawnScaleBothSides;
extern int OCBSolo;
extern int OCBDuo;

// Calculate scale factor to lower overall eval based on various features
static int ScaleFactor(const Position *pos, const int eval) {

Expand All @@ -505,11 +552,13 @@ static int ScaleFactor(const Position *pos, const int eval) {

int strongPawnCount = PopCount(strongPawns);
int x = 8 - strongPawnCount;
int pawnScale = 128 - x * x;
int pawnScale = PawnScaleBase - PawnScaleX * x - x * x;

// Scale down when there aren't pawns on both sides of the board
if (!(strongPawns & QueenSideBB) || !(strongPawns & KingSideBB))
pawnScale -= 20;
pawnScale -= PawnScaleBothSides;

pawnScale = MIN(pawnScale, 128);

// Opposite-colored bishop
if ( pos->nonPawnCount[WHITE] <= 2
Expand All @@ -518,7 +567,7 @@ static int ScaleFactor(const Position *pos, const int eval) {
&& Single(colorPieceBB(WHITE, BISHOP))
&& Single(colorPieceBB(BLACK, BISHOP))
&& Single(pieceBB(BISHOP) & BlackSquaresBB))
return MIN((pos->nonPawnCount[WHITE] == 1 ? 64 : 96), pawnScale);
return MIN((pos->nonPawnCount[WHITE] == 1 ? OCBSolo : OCBDuo), pawnScale);

return pawnScale;
}
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();
37 changes: 28 additions & 9 deletions src/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,32 @@
#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;
extern int HistMalusMax;
extern int HistMalusBase;
extern int HistMalusDepth;


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 int Malus(Depth depth) {
return -MIN(HistMalusMax, HistMalusDepth * depth - HistMalusBase);
}

INLINE void UpdateContHistories(Stack *ss, Move move, int bonus) {
Expand All @@ -50,7 +65,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 +84,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
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
Loading

0 comments on commit f7da48b

Please sign in to comment.