Skip to content

Commit 786da4f

Browse files
committed
Refactored evaluation and improved eval of safety and threats.
1 parent 675ae36 commit 786da4f

File tree

7 files changed

+351
-428
lines changed

7 files changed

+351
-428
lines changed

eval.cpp

Lines changed: 192 additions & 181 deletions
Large diffs are not rendered by default.

eval.h

Lines changed: 100 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ enum GamePhase {
1919
struct eval_params_t {
2020
/// Material
2121
int mat_exch_knight = 26;
22-
int mat_exch_bishop = 34;
22+
int mat_exch_bishop = 33;
2323
int mat_exch_rook = 48;
24-
int mat_exch_queen = 156;
25-
int mat_opp_bishop[3] = {53, 63, 71}; // [PAWN ADVANTAGE]
24+
int mat_exch_queen = 155;
2625

2726
/// Piece-square tables
2827

@@ -33,174 +32,130 @@ struct eval_params_t {
3332
// A1, B1, C1, D1
3433
// Horizontal, Vertical and diagonal symmetry
3534
int n_pst_mg[16] = {
36-
412, 420, 431, 431,
37-
392, 423, 427, 437,
38-
397, 392, 411, 423,
39-
278, 383, 407, 400,
40-
};
41-
int q_pst_mg[16] = {
42-
1308, 1309, 1307, 1304,
43-
1328, 1328, 1325, 1314,
44-
1317, 1306, 1328, 1333,
45-
1322, 1320, 1320, 1331,
35+
404, 421, 429, 432, 387, 413, 422, 427, 387, 385, 396, 415, 275, 372, 393, 387,
4636
};
4737
int n_pst_eg[16] = {
48-
366, 374, 377, 381,
49-
362, 370, 375, 381,
50-
353, 368, 371, 370,
51-
299, 348, 352, 368,
38+
375, 378, 383, 388, 372, 381, 386, 390, 362, 380, 383, 381, 312, 364, 365, 379,
39+
};
40+
int q_pst_mg[16] = {
41+
1318, 1321, 1316, 1310, 1333, 1329, 1324, 1315, 1326, 1315, 1327, 1331, 1327, 1318, 1314, 1328,
5242
};
5343
int q_pst_eg[16] = {
54-
1254, 1282, 1280, 1300,
55-
1216, 1249, 1267, 1270,
56-
1221, 1250, 1237, 1235,
57-
1194, 1220, 1230, 1226,
44+
1255, 1271, 1266, 1298, 1219, 1253, 1275, 1274, 1228, 1246, 1243, 1249, 1201, 1223, 1239, 1227,
5845
};
59-
60-
// PST for pawn, rook:
61-
// A8, B8, C8, D8, A7, B7, C7, D7, etc.
62-
// Only vertical symmetry
63-
int b_pst_mg[32] = {
64-
352, 362, 364, 337,
65-
381, 392, 391, 382,
66-
417, 436, 427, 441,
67-
433, 428, 425, 425,
68-
439, 421, 430, 441,
69-
438, 449, 448, 444,
70-
443, 443, 456, 428,
71-
426, 424, 426, 427,
46+
int b_pst_mg[16] = {
47+
412, 406, 420, 426, 427, 438, 435, 430, 425, 438, 435, 429, 408, 432, 412, 413,
7248
};
73-
int r_pst_mg[32] = {
74-
594, 585, 608, 608,
75-
573, 566, 587, 615,
76-
583, 593, 592, 596,
77-
568, 579, 585, 587,
78-
563, 552, 578, 577,
79-
567, 576, 590, 578,
80-
575, 573, 577, 574,
81-
581, 586, 585, 590,
49+
int b_pst_eg[16] = {
50+
382, 388, 383, 377, 378, 385, 387, 389, 364, 378, 375, 386, 376, 376, 383, 390,
8251
};
83-
int b_pst_eg[32] = {
84-
389, 388, 384, 390,
85-
374, 379, 378, 377,
86-
369, 368, 367, 357,
87-
366, 372, 364, 376,
88-
372, 373, 377, 370,
89-
386, 385, 378, 376,
90-
372, 368, 370, 374,
91-
379, 376, 363, 370,
52+
int r_pst_mg[16] = {
53+
572, 578, 570, 576, 579, 591, 577, 580, 573, 577, 576, 583, 580, 585, 590, 591,
9254
};
93-
int r_pst_eg[32] = {
94-
681, 690, 683, 687,
95-
672, 685, 682, 666,
96-
669, 669, 675, 661,
97-
673, 671, 673, 668,
98-
678, 680, 668, 671,
99-
673, 669, 667, 668,
100-
676, 672, 677, 673,
101-
679, 678, 678, 669,
55+
int r_pst_eg[16] = {
56+
679, 673, 678, 673, 672, 671, 677, 671, 684, 689, 688, 686, 688, 680, 682, 676,
10257
};
10358
// Pawns have first and eighth rank excluded
10459
int p_pst_mg[24] = {
105-
207, 155, 148, 116,
106-
139, 152, 142, 137,
107-
121, 111, 102, 105,
108-
107, 102, 102, 114,
109-
95, 85, 96, 89,
110-
95, 97, 101, 88,
60+
205, 158, 149, 121, 132, 145, 133, 135, 110, 108, 99, 99, 106, 100, 100, 111, 94, 83, 96, 88, 95, 94, 101, 86
11161
};
11262
int p_pst_eg[24] = {
113-
181, 202, 203, 183,
114-
116, 131, 127, 107,
115-
102, 109, 110, 99,
116-
98, 101, 100, 90,
117-
95, 96, 92, 87,
118-
94, 92, 89, 81,
63+
184, 204, 206, 183, 122, 138, 133, 111, 109, 113, 114, 104, 103, 105, 104, 95, 99, 100, 94, 92, 98, 98, 92, 86,
11964
};
12065

12166
// PST for king
12267
// Horizontal, Vertical and diagonal symmetry, like N and Q
12368
int k_pst_mg[16] = {
124-
-120, -79, -57, -82,
125-
-26, -1, -24, -47,
126-
28, -11, -58, -83,
127-
53, 44, 24, -10,
69+
-55, -80, -51, -73, 26, -10, -23, -46, 29, -15, -58, -80, 54, 37, 26, 0,
12870
};
12971
int k_pst_eg[16] = {
130-
13, 28, 25, 26,
131-
7, 15, 16, 18,
132-
-20, 2, 15, 21,
133-
-48, -24, -2, 4,
72+
8, 30, 24, 24, 6, 20, 17, 18, -13, 4, 17, 21, -42, -19, -2, 3,
13473
};
13574

13675
/// Pawn structure
13776

13877
// Pawn structure: doubled, isolated, backwards, chain, protected, etc.
139-
int isolated_mg[2] = {-4, -19}; // [OPEN FILE]
140-
int isolated_eg[2] = {-1, 1}; // [OPEN FILE]
141-
int backwards_mg[2] = {2, -16}; // [OPEN FILE]
142-
int backwards_eg[2] = {-6, -12}; // [OPEN FILE]
143-
int semi_backwards_mg[2] = {3, -20}; // [OPEN FILE]
144-
int semi_backwards_eg[2] = {2, 2}; // [OPEN FILE]
145-
int paired_mg[2] = {9, -2}; // [OPEN FILE]
146-
int paired_eg[2] = {3, 8}; // [OPEN FILE]
147-
int detached_mg[2] = {-4, -1}; // [OPEN FILE]
148-
int detached_eg[2] = {-7, -8}; // [OPEN FILE]
149-
int doubled_mg[2] = {-17, 4}; // [OPEN FILE]
150-
int doubled_eg[2] = {-18, -29}; // [OPEN FILE]
151-
int chain_mg[5] = {17, 11, 20, 35, 207}; // [RANK - 2]
152-
int chain_eg[5] = {14, 7, 10, 27, -13}; // [RANK - 2]
153-
int passed_mg[6] = {-24, -34, -22, 7, 11, 22}; // [RANK - 1]
154-
int passed_eg[6] = {-39, -14, 23, 60, 117, 120}; // [RANK - 1]
155-
int candidate_mg[4] = {-20, -5, 15, 61}; // [RANK - 1]
156-
int candidate_eg[4] = {-5, 0, 13, 48}; // [RANK - 1]
78+
int isolated_mg[2] = {-4, -17}; // [OPEN FILE]
79+
int isolated_eg[2] = {-2, 0}; // [OPEN FILE]
80+
int backwards_mg[2] = {1, -17}; // [OPEN FILE]
81+
int backwards_eg[2] = {-4, -14}; // [OPEN FILE]
82+
int semi_backwards_mg[2] = {5, -17}; // [OPEN FILE]
83+
int semi_backwards_eg[2] = {3, -2}; // [OPEN FILE]
84+
int paired_mg[2] = {10, 0}; // [OPEN FILE]
85+
int paired_eg[2] = {3, 4}; // [OPEN FILE]
86+
int detached_mg[2] = {-5, -3}; // [OPEN FILE]
87+
int detached_eg[2] = {-6, -7}; // [OPEN FILE]
88+
int doubled_mg[2] = {-16, 5}; // [OPEN FILE]
89+
int doubled_eg[2] = {-21, -32}; // [OPEN FILE]
90+
int chain_mg[5] = {16, 8, 15, 30, 209}; // [RANK - 2]
91+
int chain_eg[5] = {17, 12, 16, 33, -3}; // [RANK - 2]
92+
int passed_mg[6] = {-23, -31, -20, 10, 14, 12}; // [RANK - 1]
93+
int passed_eg[6] = {-40, -17, 21, 59, 116, 124}; // [RANK - 1]
94+
int candidate_mg[4] = {-14, -6, 10, 56}; // [RANK - 1]
95+
int candidate_eg[4] = {-11, 1, 15, 48}; // [RANK - 1]
15796
int king_tropism_eg[2] = {-1, 5}; // [OWN, OTHER]
158-
int passer_tropism_eg[2] = {-7, 15}; // [OWN, OTHER]
97+
int passer_tropism_eg[2] = {-8, 15}; // [OWN, OTHER]
15998

16099
// Evaluation of pawn structure relative to other pieces
161100
int blocked_mg[2] = {-7, -5}; // [OWN, OTHER]
162-
int blocked_eg[2] = {-4, -34}; // [OWN, OTHER]
163-
int pos_r_open_file_mg = 48;
101+
int blocked_eg[2] = {-5, -33}; // [OWN, OTHER]
102+
int pos_r_open_file_mg = 41;
164103
int pos_r_open_file_eg = 13;
165-
int pos_r_own_half_open_file_mg = 26; // "own" refers to side with missing pawn
166-
int pos_r_own_half_open_file_eg = -3;
167-
int pos_r_other_half_open_file_mg = 20;
168-
int pos_r_other_half_open_file_eg = 1;
104+
int pos_r_own_half_open_file_mg = 24; // "own" refers to side with missing pawn
105+
int pos_r_own_half_open_file_eg = -5;
106+
int pos_r_other_half_open_file_mg = 16;
107+
int pos_r_other_half_open_file_eg = 4;
169108

170109
// [KNIGHT, BISHOP]
171-
int outpost_mg[2] = {6, 9};
172-
int outpost_eg[2] = {12, 8};
173-
int outpost_hole_mg[2] = {41, 43};
174-
int outpost_hole_eg[2] = {13, -3};
110+
int outpost_mg[2] = {5, -2};
111+
int outpost_eg[2] = {7, 9};
112+
int outpost_hole_mg[2] = {33, 37};
113+
int outpost_hole_eg[2] = {17, -5};
175114
int outpost_half_mg[2] = {-3, 4};
176-
int outpost_half_eg[2] = {-7, 1};
115+
int outpost_half_eg[2] = {-6, 1};
177116

178117
/// King safety
179-
int ks_pawn_shield[4] = {-20, -5, 14, 35}; // 0, 1, 2, and 3 pawns close to the king
118+
int ks_pawn_shield[4] = {-18, -3, 14, 34}; // 0, 1, 2, and 3 pawns close to the king
180119

181-
int kat_zero = 6;
182-
int kat_open_file = 23;
183-
int kat_own_half_open_file = 19;
120+
int kat_zero = 8;
121+
int kat_open_file = 24;
122+
int kat_own_half_open_file = 17;
184123
int kat_other_half_open_file = 14;
185124
int kat_attack_weight[5] = {8, 15, 11, 9, 11};
186-
int kat_defence_weight[5] = {10, 3, 3, 3, 1};
125+
int kat_defence_weight[5] = {8, 4, 4, 2, 1};
187126

188127
int kat_table_scale = 23;
189128
int kat_table_translate = 77;
190-
int kat_table_max = 441;
129+
int kat_table_max = 430;
191130
int kat_table_offset = 10;
192131

132+
/// Threats
133+
int undefended_mg[5] = {-9, -17, -13, -3, -12}; // [¬KING]
134+
int undefended_eg[5] = {6, -1, -5, -7, -6}; // [¬KING]
135+
int threat_matrix_mg[4][5] = {
136+
{20, 50, 46, 49, 40},
137+
{-9, 24, 24, 61, 35},
138+
{1, 28, -11, 19, 26},
139+
{-3, 12, 27, 42, 68},
140+
};
141+
int threat_matrix_eg[4][5] = {
142+
{-2, -10, 32, -43, -57},
143+
{6, 159, 24, -11, -45},
144+
{13, 26, -37, -1, 107},
145+
{17, 17, 14, 3, -1},
146+
};
147+
193148
/// Other positional
194149
int pos_bishop_pair_mg = 25;
195-
int pos_bishop_pair_eg = 69;
150+
int pos_bishop_pair_eg = 67;
151+
int mat_opp_bishop[3] = {53, 64, 69}; // [PAWN ADVANTAGE]
196152

197-
int pos_r_trapped_mg = -73;
198-
int pos_r_behind_own_passer_eg = -2;
199-
int pos_r_behind_enemy_passer_eg = 58;
200-
int pos_r_xray_pawn_eg = 17;
153+
int pos_r_trapped_mg = -68;
154+
int pos_r_behind_own_passer_eg = -4;
155+
int pos_r_behind_enemy_passer_eg = 59;
201156

202-
int pos_mob_mg[4] = {6, 7, 3, 2}; // [PIECE, ¬KING, ¬PAWN]
203-
int pos_mob_eg[4] = {5, 6, 3, 6};
157+
int pos_mob_mg[4] = {7, 7, 3, 3}; // [PIECE, ¬KING, ¬PAWN]
158+
int pos_mob_eg[4] = {5, 6, 4, 6};
204159

205160
// TODO: Evaluate better
206161
};
@@ -226,12 +181,28 @@ class alignas(64) evaluator_t {
226181
/// Initialise generic evaluation tables
227182
static void eval_init();
228183

229-
double eval_material(const board_t &board, int &mg, int &eg); // returns tapering factor 0-1
184+
double game_phase(const board_t &board) const; // returns tapering factor 0-1
230185
private:
231-
void eval_pawns(const board_t &board, int &mg, int &eg);
232-
void eval_pst(const board_t &board, int &mg, int &eg);
233-
void eval_movement(const board_t &board, int &mg, int &eg);
234-
void eval_positional(const board_t &board, int &mg, int &eg);
186+
struct eval_data_t {
187+
int king_pos[2];
188+
U64 king_circle[2];
189+
int king_danger[2];
190+
191+
U64 team_attacks[2];
192+
U64 attacks[2][6];
193+
U64 double_attacks[2];
194+
195+
void update_attacks(Team team, Piece piece, U64 bb) {
196+
double_attacks[team] = team_attacks[team] & bb;
197+
attacks[team][piece] |= bb;
198+
team_attacks[team] |= bb;
199+
}
200+
};
201+
202+
void eval_pawns(const board_t &board, int &mg, int &eg, eval_data_t &data);
203+
void eval_pieces(const board_t &board, int &mg, int &eg, eval_data_t &data);
204+
void eval_threats(const board_t &board, int &mg, int &eg, eval_data_t &data);
205+
void eval_positional(const board_t &board, int &mg, int &eg, eval_data_t &data);
235206
};
236207

237208
#endif //TOPPLE_EVAL_H

0 commit comments

Comments
 (0)