-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.c
More file actions
237 lines (204 loc) · 8.41 KB
/
eval.c
File metadata and controls
237 lines (204 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "lib/board.h"
#include "lib/eval.h"
#include "lib/utils.h"
const int PIECE_VALUES[NUM_PIECES] = {100, 320, 330, 500, 900, 20000};
const int END_VALUES[NUM_PIECES] = {120, 310, 340, 500, 900, 20000};
const int TOTAL_PHASE = ((KNIGHT_PHASE * 4) + (BISHOP_PHASE * 4) + (ROOK_PHASE * 4) + (QUEEN_PHASE * 2));
uint64_t wmoves;
uint64_t bmoves;
int tapered(const board *B) {
wmoves = white_moves(B);
bmoves = black_moves(B);
int mg = mid_eval(B);
int eg = end_eval(B);
int p = phase(B);
eg = eg * scale(B, eg) / 64;
int v = (((mg * p + (eg * (128 - p))) / 128) << 0);
return v;
}
int mat_eval(const board *B) {
int score = PIECE_VALUES[PAWN] * (__builtin_popcountll(B->WHITE[PAWN]) - __builtin_popcountll(B->BLACK[PAWN]));
score += PIECE_VALUES[KNIGHT] * (__builtin_popcountll(B->WHITE[KNIGHT]) - __builtin_popcountll(B->BLACK[KNIGHT]));
score += PIECE_VALUES[BISHOP] * (__builtin_popcountll(B->WHITE[BISHOP]) - __builtin_popcountll(B->BLACK[BISHOP]));
score += PIECE_VALUES[ROOK] * (__builtin_popcountll(B->WHITE[ROOK]) - __builtin_popcountll(B->BLACK[ROOK]));
score += PIECE_VALUES[QUEEN] * (__builtin_popcountll(B->WHITE[QUEEN]) - __builtin_popcountll(B->BLACK[QUEEN]));
score += PIECE_VALUES[KING] * (__builtin_popcountll(B->WHITE[KING]) - __builtin_popcountll(B->BLACK[KING]));
return score;
}
int center_control(const board *B) {
const uint64_t CENTER = (FILE_D | FILE_E) & (RANK_4 | RANK_5);
uint64_t wcont = B->whites & CENTER;
uint64_t bcont = B->blacks & CENTER;
return 2 * (__builtin_popcountll(wcont) - __builtin_popcountll(bcont));
}
int king_safe(const board *B) {
uint64_t wking = wk_moves(B->WHITE[KING], B->whites);
uint64_t bking = bk_moves(B->BLACK[KING], B->blacks);
int wsafe = __builtin_popcountll(wking & bmoves);
int bsafe = __builtin_popcountll(bking & wmoves);
return (bsafe - wsafe);
}
int mobility(const board *B) {
int wmobile = __builtin_popcountll(wmoves);
int bmobile = __builtin_popcountll(bmoves);
return (wmobile - bmobile) / 2;
}
int mid_eval(const board *B) {
return mat_eval(B) + center_control(B) + king_safe(B) + mobility(B);
}
int phase(const board *B) {
int phase = 0;
phase += __builtin_popcountll(B->WHITE[KNIGHT]) * KNIGHT_PHASE;
phase += __builtin_popcountll(B->BLACK[KNIGHT]) * KNIGHT_PHASE;
phase += __builtin_popcountll(B->WHITE[BISHOP]) * BISHOP_PHASE;
phase += __builtin_popcountll(B->BLACK[BISHOP]) * BISHOP_PHASE;
phase += __builtin_popcountll(B->WHITE[ROOK]) * ROOK_PHASE;
phase += __builtin_popcountll(B->BLACK[ROOK]) * ROOK_PHASE;
phase += __builtin_popcountll(B->WHITE[QUEEN]) * QUEEN_PHASE;
phase += __builtin_popcountll(B->BLACK[QUEEN]) * QUEEN_PHASE;
if (phase > TOTAL_PHASE)
phase = TOTAL_PHASE;
return (phase * 128) / TOTAL_PHASE;
}
int scale(const board *B, int eg_score) {
int no_wpawns = (__builtin_popcountll(B->WHITE[PAWN]) == 0);
int no_bpawns = (__builtin_popcountll(B->BLACK[PAWN]) == 0);
if (no_wpawns || no_bpawns) return HALF_SCALE;
int wking_mobile = B->WHITE[KING] & ~RANK_1 & ~RANK_2;
int bking_mobile = B->BLACK[KING] & ~RANK_8 & ~RANK_7;
if (wking_mobile && bking_mobile) return MAX_SCALE;
return MOD_SCALE;
}
int end_mat_eval(const board *B) {
int score = END_VALUES[PAWN] * (__builtin_popcountll(B->WHITE[PAWN]) - __builtin_popcountll(B->BLACK[PAWN]));
score += END_VALUES[KNIGHT] * (__builtin_popcountll(B->WHITE[KNIGHT]) - __builtin_popcountll(B->BLACK[KNIGHT]));
score += END_VALUES[BISHOP] * (__builtin_popcountll(B->WHITE[BISHOP]) - __builtin_popcountll(B->BLACK[BISHOP]));
score += END_VALUES[ROOK] * (__builtin_popcountll(B->WHITE[ROOK]) - __builtin_popcountll(B->BLACK[ROOK]));
score += END_VALUES[QUEEN] * (__builtin_popcountll(B->WHITE[QUEEN]) - __builtin_popcountll(B->BLACK[QUEEN]));
score += END_VALUES[KING] * (__builtin_popcountll(B->WHITE[KING]) - __builtin_popcountll(B->BLACK[KING]));
return score;
}
int king_activity(const board *B) {
uint64_t mid_ranks = RANK_3 | RANK_4 | RANK_5 | RANK_6;
int wking = (B->WHITE[KING] & mid_ranks) != 0;
int bking = (B->BLACK[KING] & mid_ranks) != 0;
int w = wking ? (ACTIVE_KING) : 0;
int b = bking ? (ACTIVE_KING) : 0;
return (w - b);
}
int pawn_structure(const board *B) {
const uint64_t WPROMOTE = RANK_7;
const uint64_t BPROMOTE = RANK_2;
uint64_t wp = B->WHITE[PAWN] & WPROMOTE;
uint64_t bp = B->BLACK[PAWN] & BPROMOTE;
return ((__builtin_popcountll(wp) - __builtin_popcountll(bp)) * (PAWN_PROMOTE));
}
int passed_pawns(const board *B) {
uint64_t wp = B->WHITE[PAWN];
uint64_t bp = B->BLACK[PAWN];
uint64_t wpass = sided_passed_pawns(wp, bp, 1);
uint64_t bpass = sided_passed_pawns(bp, wp, 0);
return (PAWN_PASSED * (__builtin_popcountll(wpass) - __builtin_popcountll(bpass)));
}
int development(const board *B) {
int score = 0;
uint64_t wminors = B->WHITE[KNIGHT] | B->WHITE[BISHOP]; // white minor pieces
int w_undeveloped = __builtin_popcountll(wminors & RANK_1);
uint64_t bminors = B->BLACK[KNIGHT] | B->BLACK[BISHOP]; // black minor pieces
int b_undeveloped = __builtin_popcountll(bminors & RANK_8);
score -= DEV_PENALTY * w_undeveloped;
score += DEV_PENALTY * b_undeveloped;
return score;
}
int castle_eval(const board *B) {
int score = 0;
if (B->cc & (WKS | WQS)) // white castled
score += CASTLE_PT;
if (B->cc & (BKS | BQS)) // black castled
score -= CASTLE_PT;
return score;
}
int end_eval(const board *B) {
return end_mat_eval(B) + king_activity(B) + pawn_structure(B) + passed_pawns(B);
}
static inline int pop_lsb(uint64_t *bb) {
int s = __builtin_ctzll(*bb);
*bb &= (*bb - 1);
return s;
}
void init_pesto_tables(void) {
for (int pt = PAWN; pt <= KING; ++pt) {
int wpc = PCODE(pt, CONST_WHITE);
int bpc = PCODE(pt, CONST_BLACK);
for (int sq = 0; sq < 64; ++sq) {
mg_table[wpc][sq] = mg_value[pt] + (pt == PAWN ? mg_pawn_table[sq] : pt == KNIGHT?mg_knight_table[sq] : pt == BISHOP ? mg_bishop_table[sq] : pt == ROOK ? mg_rook_table[sq] : pt == QUEEN ? mg_queen_table[sq] : mg_king_table[sq]);
eg_table[wpc][sq] = eg_value[pt] + (pt==PAWN ? eg_pawn_table[sq] : pt==KNIGHT ? eg_knight_table[sq] : pt==BISHOP ? eg_bishop_table[sq] : pt==ROOK ? eg_rook_table[sq] : pt==QUEEN ? eg_queen_table[sq] : eg_king_table[sq]);
// flip for black
int fsq = FLIP(sq);
mg_table[bpc][sq] = mg_value[pt] + (pt==PAWN ? mg_pawn_table[fsq] : pt==KNIGHT ? mg_knight_table[fsq] : pt==BISHOP ? mg_bishop_table[fsq] : pt==ROOK ? mg_rook_table[fsq] : pt==QUEEN ? mg_queen_table[fsq] : mg_king_table[fsq]);
eg_table[bpc][sq] = eg_value[pt] + (pt==PAWN ? eg_pawn_table[fsq] : pt==KNIGHT ? eg_knight_table[fsq] : pt==BISHOP ? eg_bishop_table[fsq] : pt==ROOK ? eg_rook_table[fsq] : pt==QUEEN ? eg_queen_table[fsq] : eg_king_table[fsq]);
}
}
}
void pesto_terms(const board *B, int *mg, int *eg, int *p24) {
int mgW = 0, mgB = 0;
int egW = 0, egB = 0;
int gp = 0;
for (int pt = PAWN; pt <= KING; ++pt) {
// white
uint64_t bb = B->WHITE[pt];
int wpc = PCODE(pt, CONST_WHITE);
while (bb) {
int sq = pop_lsb(&bb);
mgW += mg_table[wpc][sq];
egW += eg_table[wpc][sq];
gp += gpi[wpc];
}
// black
bb = B->BLACK[pt];
int bpc = PCODE(pt, CONST_BLACK);
while (bb) {
int sq = pop_lsb(&bb);
mgB += mg_table[bpc][sq];
egB += eg_table[bpc][sq];
gp += gpi[bpc];
}
}
if (gp > 24) gp = 24;
*mg = (mgW - mgB);
*eg = (egW - egB);
*p24 = gp;
}
int blended_eval(const board *B) {
wmoves = white_moves(B);
bmoves = black_moves(B);
// PeSTO
int mg_psqt, eg_psqt, phase24;
pesto_terms(B, &mg_psqt, &eg_psqt, &phase24);
int mgPhase = phase24;
int egPhase = 24 - mgPhase;
// heuristics
int mob = mobility(B);
int ctr = center_control(B);
int ks = king_safe(B);
int ka = king_activity(B);
int pstr = pawn_structure(B);
int pps = passed_pawns(B);
int castle = castle_eval(B);
int dev = development(B);
// feature sums
int mg_feats = (MOBILITY_MG * mob) + (CENTER_MG * ctr) + (KING_SAFETY_MG * ks) + castle + (DEV_MG * dev);
int eg_feats = (MOBILITY_EG * mob) + (CENTER_EG * ctr) + (KING_ACTIVITY_EG * ka) + (PSTRUCT_EG * pstr) + (PASSED_EG * pps);
int mg_total = mg_psqt + mg_feats;
int eg_total = eg_psqt + eg_feats;
// scale
int eg_scaled = (eg_total * scale(B, eg_total)) / 64;
int score = (mg_total * mgPhase + eg_scaled * egPhase) / 24;
// tempo
if (B->white) score += TEMPO_BONUS;
else score -= TEMPO_BONUS;
return score;
}