Skip to content

Commit

Permalink
Added delta pruning
Browse files Browse the repository at this point in the history
  • Loading branch information
TampliteSK committed Jan 24, 2024
1 parent 319737c commit 377b5b0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ Evaluation:

## Changelogs: <br>
### 0.x: <br>
0.24: Added punishments for minor pieces in front of pawns. <br>
0.23: Added king safety to evaluation (pawn shield and punish open files near king). Elo gain: ~-20. <br>
0.24: Added punishments for minor pieces in front of pawns. Added delta pruning. Elo gain: ~40. <br>
0.23: Added king safety to evaluation (pawn shield and punish open files near king). <br>
0.22: Improved time management (tested on Lichess). Slight speed boost. <br>
0.21: Slight speed boost. <br>
0.2: Fixed crash. Improved tapered eval. Elo gain: ~270. <br>
Expand All @@ -41,7 +41,9 @@ Evaluation:
## To-do list:
- Pawn / bishop interaction
- Pawn storm
- Add aspiration windows
- Additional VICE features
- Optimise movegen

## Bugs to fix:
- Doesn't castle in some positions where O-O is superior
Expand Down
14 changes: 12 additions & 2 deletions Source/Engine/evaluate.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
// Pos3 (fried liver) [0.0 Na5]: r1bqkb1r/ppp2ppp/2n2n2/3Pp1N1/2B5/8/PPPP1PPP/RNBQK2R b KQkq - 0 5
// Pos4 (king safety test): r1bqkbnr/pp1p1p1p/2n1p3/2p3p1/4P3/3B1N2/PPPP1PPP/RNBQ1RK1 w kq - 0 5

// Pos5 (h6 blunders): 8/8/6R1/5ppP/5k2/3r1P2/8/6K1 w - - 9 57
// Pos6

// Temporary hack. Scales down the eval in case it's too high (assuming the code works fine)
// #define squishFactor 0.35

Expand Down Expand Up @@ -172,6 +175,10 @@ const int KingEgTable[64] = {
-74, -35, -18, -18, -11, 15, 4, -17
};

/********************************
***** Evaluation components *****
********************************/

// Applying gamePhase at startpos
#define openingPhase 64

Expand Down Expand Up @@ -356,6 +363,10 @@ double kingSafetyScore(const S_BOARD *pos, uint8_t sq, uint8_t col, uint16_t mat
// Used for some sort of king eval tapering. Probably not very good, but an interesting approach. Kept for legacy
// #define ENDGAME_MAT (1 * PieceVal[wR] + 2 * PieceVal[wN] + 2 * PieceVal[wP] + PieceVal[wK])

/********************************
*** Main Evaluation Function ****
********************************/

// Evaluation function
inline int EvalPosition(const S_BOARD *pos) {

Expand Down Expand Up @@ -445,7 +456,6 @@ inline int EvalPosition(const S_BOARD *pos) {
ASSERT(SQ64(sq)>=0 && SQ64(sq)<=63);
score += KnightMgTable[SQ64(sq)] * weight + KnightEgTable[SQ64(sq)] * ( 1 - weight );


// Punish knights in front of c-pawn
U64 mask = pos->pawns[WHITE] & FileBBMask[FilesBrd[sq]];
int pawnSq = PopBit(&mask);
Expand Down Expand Up @@ -498,7 +508,7 @@ inline int EvalPosition(const S_BOARD *pos) {
ASSERT(SqOnBoard(sq));
ASSERT(MIRROR64(SQ64(sq))>=0 && MIRROR64(SQ64(sq))<=63);
score -= BishopMgTable[MIRROR64(SQ64(sq))] * weight + BishopEgTable[MIRROR64(SQ64(sq))] * ( 1 - weight );

// Punish bishops in front of e- or d-pawn
U64 mask = pos->pawns[BLACK] & FileBBMask[FilesBrd[sq]];
int pawnSq = PopBit(&mask);
Expand Down
13 changes: 11 additions & 2 deletions Source/Engine/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,27 @@ static int Quiescence(int alpha, int beta, S_BOARD *pos, S_SEARCHINFO *info) {
return EvalPosition(pos);
}

int Score = EvalPosition(pos);
int Score = EvalPosition(pos); // stand-pat score

ASSERT(Score>-INFINITE && Score<INFINITE);

// Beta cutoff
if(Score >= beta) {
return beta;
}

// Delta pruning
uint16_t delta = 1025; // arbitrarily large value (queen mg value)
// We stop searching if it's much lower than alpha
if (Score < alpha - delta) {
return alpha;
}

if(Score > alpha) {
alpha = Score;
}

// Delta pruning fails, and we have to search moves
S_MOVELIST list[1];
GenerateAllCaps(pos,list);

Expand Down Expand Up @@ -328,7 +337,7 @@ void SearchPosition(S_BOARD *pos, S_SEARCHINFO *info) {
}
if(info->GAME_MODE == UCIMODE || info->POST_THINKING == TRUE) {
pvMoves = GetPvLine(currentDepth, pos);
if(!info->GAME_MODE == XBOARDMODE) {
if((!info->GAME_MODE) == XBOARDMODE) {
printf("pv");
}
for(pvNum = 0; pvNum < pvMoves; ++pvNum) {
Expand Down

0 comments on commit 377b5b0

Please sign in to comment.