Skip to content

Commit 102e9eb

Browse files
committed
Refactored evaluation, adding an additional tapering dimension.
1 parent ebdef74 commit 102e9eb

File tree

13 files changed

+495
-603
lines changed

13 files changed

+495
-603
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ find_package(Threads REQUIRED)
4747
target_link_libraries(Topple Threads::Threads)
4848
target_link_libraries(ToppleTest Threads::Threads)
4949
target_link_libraries(ToppleTune Threads::Threads)
50+
target_link_libraries(ToppleTexelTune Threads::Threads)
5051

5152
# Set -march for the Topple target
5253
target_compile_options(ToppleTest PUBLIC -march=native -O3)

eval.cpp

Lines changed: 76 additions & 88 deletions
Large diffs are not rendered by default.

eval.h

Lines changed: 182 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
#include "board.h"
1212
#include "pawns.h"
1313

14-
enum GamePhase {
15-
MG, EG
16-
};
17-
1814
// qlc: 0.0582706 lqa: 0.0647049
1915
struct eval_params_t {
2016
/// Material
@@ -31,131 +27,182 @@ struct eval_params_t {
3127
// A2, B2, C2, D2
3228
// A1, B1, C1, D1
3329
// Horizontal, Vertical and diagonal symmetry
34-
int n_pst_mg[16] = {
35-
404, 421, 429, 432,
36-
387, 413, 422, 427,
37-
387, 385, 396, 415,
38-
275, 372, 393, 387,
39-
};
40-
int n_pst_eg[16] = {
41-
375, 378, 383, 388,
42-
372, 381, 386, 390,
43-
362, 380, 383, 381,
44-
312, 364, 365, 379,
45-
};
46-
int q_pst_mg[16] = {
47-
1318, 1321, 1316, 1310,
48-
1333, 1329, 1324, 1315,
49-
1326, 1315, 1327, 1331,
50-
1327, 1318, 1314, 1328,
30+
v4si_t n_pst[16] = {
31+
{404, 404, 375, 375},
32+
{421, 421, 378, 378},
33+
{429, 429, 383, 383},
34+
{432, 432, 388, 388},
35+
{387, 387, 372, 372},
36+
{413, 413, 381, 381},
37+
{422, 422, 386, 386},
38+
{427, 427, 390, 390},
39+
{387, 387, 362, 362},
40+
{385, 385, 380, 380},
41+
{396, 396, 383, 383},
42+
{415, 415, 381, 381},
43+
{275, 275, 312, 312},
44+
{372, 372, 364, 364},
45+
{393, 393, 365, 365},
46+
{387, 387, 379, 379},
5147
};
52-
int q_pst_eg[16] = {
53-
1255, 1271, 1266, 1298,
54-
1219, 1253, 1275, 1274,
55-
1228, 1246, 1243, 1249,
56-
1201, 1223, 1239, 1227,
48+
v4si_t q_pst[16] = {
49+
{1318, 1318, 1255, 1255},
50+
{1321, 1321, 1271, 1271},
51+
{1316, 1316, 1266, 1266},
52+
{1310, 1310, 1298, 1298},
53+
{1333, 1333, 1219, 1219},
54+
{1329, 1329, 1253, 1253},
55+
{1324, 1324, 1275, 1275},
56+
{1315, 1315, 1274, 1274},
57+
{1326, 1326, 1228, 1228},
58+
{1315, 1315, 1246, 1246},
59+
{1327, 1327, 1243, 1243},
60+
{1331, 1331, 1249, 1249},
61+
{1327, 1327, 1201, 1201},
62+
{1318, 1318, 1223, 1223},
63+
{1314, 1314, 1239, 1239},
64+
{1328, 1328, 1227, 1227},
5765
};
58-
int b_pst_mg[16] = {
59-
412, 406, 420, 426,
60-
427, 438, 435, 430,
61-
425, 438, 435, 429,
62-
408, 432, 412, 413,
66+
v4si_t b_pst[16] = {
67+
{412, 412, 382, 382},
68+
{406, 406, 388, 388},
69+
{420, 420, 383, 383},
70+
{426, 426, 377, 377},
71+
{427, 427, 378, 378},
72+
{438, 438, 385, 385},
73+
{435, 435, 387, 387},
74+
{430, 430, 389, 389},
75+
{425, 425, 364, 364},
76+
{438, 438, 378, 378},
77+
{435, 435, 375, 375},
78+
{429, 429, 386, 386},
79+
{408, 408, 376, 376},
80+
{432, 432, 376, 376},
81+
{412, 412, 383, 383},
82+
{413, 413, 390, 390},
6383
};
64-
int b_pst_eg[16] = {
65-
382, 388, 383, 377,
66-
378, 385, 387, 389,
67-
364, 378, 375, 386,
68-
376, 376, 383, 390,
84+
v4si_t r_pst[16] = {
85+
{572, 572, 679, 679},
86+
{578, 578, 673, 673},
87+
{570, 570, 678, 678},
88+
{576, 576, 673, 673},
89+
{579, 579, 672, 672},
90+
{591, 591, 671, 671},
91+
{577, 577, 677, 677},
92+
{580, 580, 671, 671},
93+
{573, 573, 684, 684},
94+
{577, 577, 689, 689},
95+
{576, 576, 688, 688},
96+
{583, 583, 686, 686},
97+
{580, 580, 688, 688},
98+
{585, 585, 680, 680},
99+
{590, 590, 682, 682},
100+
{591, 591, 676, 676},
69101
};
70-
int r_pst_mg[16] = {
71-
572, 578, 570, 576,
72-
579, 591, 577, 580,
73-
573, 577, 576, 583,
74-
580, 585, 590, 591,
75-
};
76-
int r_pst_eg[16] = {
77-
679, 673, 678, 673,
78-
672, 671, 677, 671,
79-
684, 689, 688, 686,
80-
688, 680, 682, 676,
81-
};
82-
// Pawns have first and eighth rank excluded
83-
int p_pst_mg[24] = {
84-
205, 158, 149, 121,
85-
132, 145, 133, 135,
86-
110, 108, 99, 99,
87-
106, 100, 100, 111,
88-
94, 83, 96, 88,
89-
95, 94, 101, 86
90-
};
91-
int p_pst_eg[24] = {
92-
184, 204, 206, 183,
93-
122, 138, 133, 111,
94-
109, 113, 114, 104,
95-
103, 105, 104, 95,
96-
99, 100, 94, 92,
97-
98, 98, 92, 86,
102+
103+
// PST for pawns: mirrored horizontally with first + eighth excluded
104+
v4si_t p_pst[24] = {
105+
{205, 205, 184, 184},
106+
{158, 158, 204, 204},
107+
{149, 149, 206, 206},
108+
{121, 121, 183, 183},
109+
{132, 132, 122, 122},
110+
{145, 145, 138, 138},
111+
{133, 133, 133, 133},
112+
{135, 135, 111, 111},
113+
{110, 110, 109, 109},
114+
{108, 108, 113, 113},
115+
{99, 99, 114, 114},
116+
{99, 99, 104, 104},
117+
{106, 106, 103, 103},
118+
{100, 100, 105, 105},
119+
{100, 100, 104, 104},
120+
{111, 111, 95, 95},
121+
{94, 94, 99, 99},
122+
{83, 83, 100, 100},
123+
{96, 96, 94, 94},
124+
{88, 88, 92, 92},
125+
{95, 95, 98, 98},
126+
{94, 94, 98, 98},
127+
{101, 101, 92, 92},
128+
{86, 86, 86, 86},
98129
};
99130

131+
100132
// PST for king
101133
// Horizontal, Vertical and diagonal symmetry, like N and Q
102-
int k_pst_mg[16] = {
103-
-55, -80, -51, -73,
104-
26, -10, -23, -46,
105-
29, -15, -58, -80,
106-
54, 37, 26, 0,
107-
};
108-
int k_pst_eg[16] = {
109-
8, 30, 24, 24,
110-
6, 20, 17, 18,
111-
-13, 4, 17, 21,
112-
-42, -19, -2, 3,
134+
v4si_t k_pst[16] = {
135+
{-55, -55, 8, 8},
136+
{-80, -80, 30, 30},
137+
{-51, -51, 24, 24},
138+
{-73, -73, 24, 24},
139+
{26, 26, 6, 6},
140+
{-10, -10, 20, 20},
141+
{-23, -23, 17, 17},
142+
{-46, -46, 18, 18},
143+
{29, 29, -13, -13},
144+
{-15, -15, 4, 4},
145+
{-58, -58, 17, 17},
146+
{-80, -80, 21, 21},
147+
{54, 54, -42, -42},
148+
{37, 37, -19, -19},
149+
{26, 26, -2, -2},
150+
{0, 0, 3, 3},
113151
};
114152

115153
/// Pawn structure
116154

117155
// Pawn structure: doubled, isolated, backwards, chain, protected, etc.
118-
int isolated_mg[2] = {-4, -17}; // [OPEN FILE]
119-
int isolated_eg[2] = {-2, 0}; // [OPEN FILE]
120-
int backwards_mg[2] = {1, -17}; // [OPEN FILE]
121-
int backwards_eg[2] = {-4, -14}; // [OPEN FILE]
122-
int semi_backwards_mg[2] = {5, -17}; // [OPEN FILE]
123-
int semi_backwards_eg[2] = {3, -2}; // [OPEN FILE]
124-
int paired_mg[2] = {10, 0}; // [OPEN FILE]
125-
int paired_eg[2] = {3, 4}; // [OPEN FILE]
126-
int detached_mg[2] = {-5, -3}; // [OPEN FILE]
127-
int detached_eg[2] = {-6, -7}; // [OPEN FILE]
128-
int doubled_mg[2] = {-16, 5}; // [OPEN FILE]
129-
int doubled_eg[2] = {-21, -32}; // [OPEN FILE]
130-
int chain_mg[5] = {16, 8, 15, 30, 209}; // [RANK - 2]
131-
int chain_eg[5] = {17, 12, 16, 33, -3}; // [RANK - 2]
132-
int passed_mg[6] = {-23, -31, -20, 10, 14, 12}; // [RANK - 1]
133-
int passed_eg[6] = {-40, -17, 21, 59, 116, 124}; // [RANK - 1]
134-
int candidate_mg[4] = {-14, -6, 10, 56}; // [RANK - 1]
135-
int candidate_eg[4] = {-11, 1, 15, 48}; // [RANK - 1]
136-
int king_tropism_eg[2] = {-1, 5}; // [OWN, OTHER]
137-
int passer_tropism_eg[2] = {-8, 15}; // [OWN, OTHER]
156+
157+
// Indexed by [OPEN FILE]
158+
v4si_t isolated[2] = {{-4, -4, -2, -2}, {-17, -17, 0, 0}};
159+
v4si_t backwards[2] = {{1, 1, -4, -4}, {-17, -17, -14, -14}};
160+
v4si_t semi_backwards[2] = {{5, 5, 3, 3}, {-17, -17, -2, -2}};
161+
v4si_t paired[2] = {{10, 10, 3, 3}, {0, 0, 4, 4}};
162+
v4si_t detached[2] = {{-5, -5, -6, -6}, {-3, -3, -7, -7}};
163+
v4si_t doubled[2] = {{-16, -16, -21, -21}, {5, 5, -32, -32}};
164+
165+
// Indexed by [RANK - 2]
166+
v4si_t chain[5] = {
167+
{16, 16, 17, 17},
168+
{8, 8, 12, 12},
169+
{15, 15, 16, 16},
170+
{30, 30, 33, 33},
171+
{209, 209, -3, -3},
172+
};
173+
174+
// Indexed by [RANK - 1]
175+
v4si_t passed[6] = {
176+
{-23, -23, -40, -40},
177+
{-31, -31, -17, -17},
178+
{-20, -20, 21, 21},
179+
{10, 10, 59, 59},
180+
{14, 14, 116, 116},
181+
{12, 12, 124, 124},
182+
};
183+
v4si_t candidate[4] = {
184+
{-14, -14, -11, -11},
185+
{-6, -6, 1, 1},
186+
{10, 10, 15, 15},
187+
{56, 56, 48, 48},
188+
};
189+
v4si_t king_tropism[2] = {{0, 0, -1, -1}, {0, 0, 5, 5}};
190+
v4si_t passer_tropism[2] = {{0, 0, -8, -8}, {0, 0, 15, 15}};
138191

139192
// Evaluation of pawn structure relative to other pieces
140-
int blocked_mg[2] = {-7, -5}; // [OWN, OTHER]
141-
int blocked_eg[2] = {-5, -33}; // [OWN, OTHER]
142-
int pos_r_open_file_mg = 41;
143-
int pos_r_open_file_eg = 13;
144-
int pos_r_own_half_open_file_mg = 24; // "own" refers to side with missing pawn
145-
int pos_r_own_half_open_file_eg = -5;
146-
int pos_r_other_half_open_file_mg = 16;
147-
int pos_r_other_half_open_file_eg = 4;
193+
v4si_t blocked[2] = {{-7, -7, -5, -5}, {-5, -5, -33, -33}};
194+
v4si_t pos_r_open_file = {41, 41, 13, 13};
195+
v4si_t pos_r_own_half_open_file = {24, 24, -5, -5}; // "own" refers to side with missing pawn
196+
v4si_t pos_r_other_half_open_file = {16, 16, 4, 4};
148197

149198
// [KNIGHT, BISHOP]
150-
int outpost_mg[2] = {5, -2};
151-
int outpost_eg[2] = {7, 9};
152-
int outpost_hole_mg[2] = {33, 37};
153-
int outpost_hole_eg[2] = {17, -5};
154-
int outpost_half_mg[2] = {-3, 4};
155-
int outpost_half_eg[2] = {-6, 1};
199+
v4si_t outpost[2] = {{5, 5, 7, 7}, {-2, -2, 9, 9}};
200+
v4si_t outpost_hole[2] = {{33, 33, 17, 17}, {37, 37, -5, -5}};
201+
v4si_t outpost_half[2] = {{-3, -3, -6, -6}, {4, 4, 1, 1}};
156202

157203
/// King safety
158-
int ks_pawn_shield[4] = {-18, -3, 14, 34}; // 0, 1, 2, and 3 pawns close to the king
204+
// 0, 1, 2, and 3 pawns close to the king
205+
v4si_t ks_pawn_shield[4] = {{-18, -18, 0, 0}, {-3, -3, 0, 0}, {14, 14, 0, 0}, {34, 34, 0, 0}};
159206

160207
int kat_zero = 8;
161208
int kat_open_file = 24;
@@ -166,44 +213,41 @@ struct eval_params_t {
166213

167214
int kat_table_scale = 45;
168215
int kat_table_translate = 77;
169-
int kat_table_max = 430;
216+
v4si_t kat_table_max = {430, 430, 0, 0};
170217

171218
/// Threats
172-
int undefended_mg[5] = {-9, -17, -13, -3, -12}; // [¬KING]
173-
int undefended_eg[5] = {6, -1, -5, -7, -6}; // [¬KING]
174-
int threat_matrix_mg[4][5] = {
175-
{20, 50, 46, 49, 40},
176-
{-9, 24, 24, 61, 35},
177-
{1, 28, -11, 19, 26},
178-
{-3, 12, 27, 42, 68},
219+
v4si_t undefended[5] = {
220+
{-9, -9, 6, 6},
221+
{-17, -17, -1, -1},
222+
{-13, -13, -5, -5},
223+
{-3, -3, -7, -7},
224+
{-12, -12, -6, -6},
179225
};
180-
int threat_matrix_eg[4][5] = {
181-
{-2, -10, 32, -43, -57},
182-
{6, 159, 24, -11, -45},
183-
{13, 26, -37, -1, 107},
184-
{17, 17, 14, 3, -1},
226+
v4si_t threat_matrix[4][5] = {
227+
{{20, 20, -2, -2}, {50, 50, -10, -10}, {46, 46, 32, 32}, {49, 49, -43, -43}, {40, 40, -57, -57}},
228+
{{-9, -9, 6, 6}, {24, 24, 159, 159}, {24, 24, 24, 24}, {61, 61, -11, -11}, {35, 35, -45, -45}},
229+
{{1, 1, 13, 13}, {28, 28, 26, 26}, {-11, -11, -37, -37}, {19, 19, -1, -1}, {26, 26, 107, 107}},
230+
{{-3, -3, 17, 17}, {12, 12, 17, 17}, {27, 27, 14, 14}, {42, 42, 3, 3}, {68, 68, -1, -1}}
185231
};
186232

187233
/// Other positional
188-
int pos_bishop_pair_mg = 25;
189-
int pos_bishop_pair_eg = 67;
190-
int mat_opp_bishop[3] = {53, 64, 69}; // [PAWN ADVANTAGE]
234+
v4si_t pos_bishop_pair = {25, 25, 67, 67};
235+
v4si_t mat_opp_bishop[3] = {{0, 0, 53, 53}, {0, 0, 64, 64}, {0, 0, 69, 69}}; // [PAWN ADVANTAGE]
191236

192-
int pos_r_trapped_mg = -68;
193-
int pos_r_behind_own_passer_eg = -4;
194-
int pos_r_behind_enemy_passer_eg = 59;
237+
v4si_t pos_r_trapped = {-68, -68, 0, 0};
238+
v4si_t pos_r_behind_own_passer = {0, 0, -4, -4};
239+
v4si_t pos_r_behind_enemy_passer = {0, 0, 59, 59};
195240

196-
int pos_mob_mg[4] = {7, 7, 3, 3}; // [PIECE, ¬KING, ¬PAWN]
197-
int pos_mob_eg[4] = {5, 6, 4, 6};
241+
v4si_t pos_mob[4] = {{7, 7, 5, 5}, {7, 7, 6, 6}, {3, 3, 4, 4}, {3, 3, 6, 6}}; // [PIECE, ¬KING, ¬PAWN]
198242

199243
// TODO: Evaluate better
200244
};
201245

202246
struct processed_params_t : public eval_params_t {
203247
explicit processed_params_t(const eval_params_t &params);
204248

205-
int pst[2][6][64][2] = {}; // [TEAM][PIECE][SQUARE][MG/EG]
206-
int kat_table[128] = {};
249+
v4si_t pst[2][6][64] = {}; // [TEAM][PIECE][SQUARE][MG/EG]
250+
v4si_t kat_table[128] = {};
207251
};
208252

209253
class alignas(64) evaluator_t {
@@ -222,7 +266,7 @@ class alignas(64) evaluator_t {
222266
/// Initialise generic evaluation tables
223267
static void eval_init();
224268

225-
double game_phase(const board_t &board) const; // returns tapering factor 0-1
269+
[[nodiscard]] float game_phase(const board_t &board) const; // returns tapering factor 0-1
226270
private:
227271
struct eval_data_t {
228272
int king_pos[2];
@@ -240,10 +284,10 @@ class alignas(64) evaluator_t {
240284
}
241285
};
242286

243-
void eval_pawns(const board_t &board, int &mg, int &eg, eval_data_t &data);
244-
void eval_pieces(const board_t &board, int &mg, int &eg, eval_data_t &data);
245-
void eval_threats(const board_t &board, int &mg, int &eg, eval_data_t &data);
246-
void eval_positional(const board_t &board, int &mg, int &eg, eval_data_t &data);
287+
v4si_t eval_pawns(const board_t &board, eval_data_t &data, float &taper);
288+
v4si_t eval_pieces(const board_t &board, eval_data_t &data);
289+
v4si_t eval_threats(const board_t &board, eval_data_t &data);
290+
v4si_t eval_positional(const board_t &board, eval_data_t &data);
247291
};
248292

249293
#endif //TOPPLE_EVAL_H

0 commit comments

Comments
 (0)