Skip to content

Commit 9579fc2

Browse files
authored
Merge pull request #24 from konsolas/eval
Separated pawn structure evaluation, along with some bugfixes
2 parents d798144 + c5bf250 commit 9579fc2

File tree

13 files changed

+720
-474
lines changed

13 files changed

+720
-474
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.8)
22
project(Topple)
33

44
set(CMAKE_CXX_STANDARD 17)
5-
set(TOPPLE_VERSION 0.6.1)
5+
set(TOPPLE_VERSION 0.7.0)
66

77
# Source files for different targets
88
set(SOURCE_FILES
@@ -14,6 +14,7 @@ set(SOURCE_FILES
1414
movesort.h movesort.cpp
1515
move.h
1616
eval.h eval.cpp
17+
pawns.cpp pawns.h
1718
search.h search.cpp
1819
pvs.h pvs.cpp
1920
syzygy/tbcore.h

board.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ bool board_t::gives_check(move_t move) const {
552552
bool board_t::is_repetition_draw(int search_ply) const {
553553
int rep = 1;
554554

555-
int max = std::max(record[now].halfmove_clock, search_ply);
555+
int max = record[now].halfmove_clock;
556556

557557
for (int i = 2; i <= max; i += 2) {
558558
if (record[now - i].hash == record[now].hash) rep++;

eval.cpp

Lines changed: 95 additions & 256 deletions
Large diffs are not rendered by default.

eval.h

Lines changed: 126 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,26 @@
55
#ifndef TOPPLE_EVAL_H
66
#define TOPPLE_EVAL_H
77

8+
#include <vector>
9+
810
#include "types.h"
911
#include "board.h"
12+
#include "pawns.h"
1013

1114
enum GamePhase {
1215
MG, EG
1316
};
1417

1518
struct eval_params_t {
1619
/// Material
17-
int mat_mg[5] = {95, 365, 436, 583, 1324,};
18-
int mat_eg[5] = {101, 369, 389, 645, 1237,};
19-
int mat_exch_scale = -7;
20+
int mat_mg[5] = {95, 365, 440, 583, 1324};
21+
int mat_eg[5] = {102, 369, 391, 644, 1239};
22+
int mat_exch_scale = -5;
2023
int mat_exch_pawn = 0;
2124
int mat_exch_minor = 2;
2225
int mat_exch_rook = 4;
2326
int mat_exch_queen = 9;
24-
int mat_opp_bishop[3] = {76, 64, 52,}; // [PAWN ADVANTAGE]
27+
int mat_opp_bishop[3] = {74, 59, 51}; // [PAWN ADVANTAGE]
2528

2629
/// Piece-square tables
2730

@@ -32,186 +35,177 @@ struct eval_params_t {
3235
// A1, B1, C1, D1
3336
// Horizontal, Vertical and diagonal symmetry
3437
int n_pst_mg[16] = {
35-
62, 65, 70, 79,
36-
56, 76, 70, 69,
37-
31, 28, 60, 70,
38-
-59, 52, 19, 49,
38+
53, 60, 70, 71,
39+
47, 75, 81, 78,
40+
24, 27, 64, 78,
41+
-72, 43, 21, 50,
3942
};
4043
int q_pst_mg[16] = {
41-
-24, -49, -38, -54,
42-
-15, -2, -19, -26,
43-
-30, -57, -2, -7,
44-
-23, -16, -14, 6,
44+
-35, -59, -41, -61,
45+
-27, -3, -18, -22,
46+
-41, -59, 0, 2,
47+
-28, -17, -10, 9,
4548
};
4649
int n_pst_eg[16] = {
47-
-21, -2, 13, 17,
48-
-37, -16, -7, 4,
49-
-47, -18, -19, -12,
50-
-65, -59, -26, -28,
50+
-20, -3, 14, 19,
51+
-34, -18, -7, 3,
52+
-45, -19, -21, -14,
53+
-65, -60, -25, -28,
5154
};
5255
int q_pst_eg[16] = {
53-
11, 43, 15, 42,
54-
-7, -30, -4, 7,
55-
-26, 3, -29, -16,
56-
-26, -19, -20, -28,
56+
11, 53, 22, 61,
57+
-3, -28, 8, 16,
58+
-17, 6, -25, -14,
59+
-25, -21, -21, -35,
5760
};
5861

5962
// PST for pawn, rook:
6063
// A8, B8, C8, D8, A7, B7, C7, D7, etc.
6164
// Only vertical symmetry
6265
int b_pst_mg[32] = {
63-
-80, -71, -141, -67,
64-
-63, -26, -46, -94,
65-
-47, -2, -7, -31,
66-
-13, -12, -16, -7,
67-
6, -29, -5, -5,
68-
-7, 19, 4, 2,
69-
-1, 9, 16, -12,
70-
-24, -12, -14, -27,
66+
-91, -67, -137, -74,
67+
-64, -26, -43, -91,
68+
-56, -7, -7, -39,
69+
-15, -16, -20, -12,
70+
9, -27, -5, -3,
71+
-5, 22, 11, 4,
72+
8, 14, 20, -4,
73+
-26, -14, -18, -29,
7174
};
7275
int r_pst_mg[32] = {
73-
9, -3, -50, 6,
74-
-2, 1, 10, 13,
75-
-39, -4, 10, -14,
76-
-29, -21, -9, -15,
77-
-43, -37, -18, -25,
78-
-41, -19, -18, -34,
79-
-37, -15, -32, -49,
80-
-7, -3, -16, 7,
76+
-3, -15, -55, -3,
77+
-9, -9, -2, 3,
78+
-46, -17, 5, -17,
79+
-32, -22, -10, -19,
80+
-42, -37, -16, -25,
81+
-38, -18, -17, -31,
82+
-33, -13, -31, -46,
83+
-5, 4, -13, 8,
8184
};
8285
int b_pst_eg[32] = {
83-
-15, -21, -9, -25,
84-
-11, -19, -15, -21,
85-
-10, -22, -18, -17,
86-
-16, -15, -19, -15,
87-
-26, -13, -17, -13,
88-
-13, -28, -19, -18,
89-
-21, -35, -34, -22,
90-
-26, -31, -33, -20,
86+
-14, -22, -11, -21,
87+
-11, -19, -14, -22,
88+
-9, -21, -20, -15,
89+
-15, -15, -17, -13,
90+
-26, -12, -17, -10,
91+
-12, -25, -17, -20,
92+
-29, -34, -35, -24,
93+
-26, -30, -33, -20,
9194
};
9295
int r_pst_eg[32] = {
93-
33, 30, 46, 29,
94-
21, 23, 21, 19,
95-
22, 17, 8, 14,
96-
19, 17, 17, 13,
97-
23, 21, 19, 15,
98-
17, 10, 9, 10,
99-
16, 7, 14, 24,
100-
14, 14, 18, 1,
96+
30, 30, 44, 28,
97+
18, 22, 20, 17,
98+
24, 19, 8, 13,
99+
19, 17, 17, 14,
100+
24, 21, 21, 15,
101+
18, 14, 11, 11,
102+
17, 10, 17, 25,
103+
14, 12, 18, 1,
101104
};
102105
// Pawns have first and eighth rank excluded
103106
int p_pst_mg[24] = {
104-
116, 187, 79, 75,
105-
45, 59, 37, 22,
106-
15, 10, -4, 14,
107-
13, -2, 6, 16,
108-
9, -4, 11, 5,
109-
9, 22, 15, 9,
107+
118, 190, 68, 74,
108+
53, 59, 46, 28,
109+
17, 10, -2, 16,
110+
15, -2, 6, 17,
111+
8, -4, 11, 5,
112+
10, 21, 14, 12,
110113
};
111114
int p_pst_eg[24] = {
112-
121, 90, 104, 105,
113-
49, 44, 46, 36,
114-
27, 26, 30, 16,
115-
18, 15, 12, 6,
116-
7, 11, 3, -1,
117-
10, 7, 8, -7,
115+
123, 93, 110, 108,
116+
48, 41, 41, 33,
117+
25, 23, 26, 14,
118+
17, 15, 11, 5,
119+
8, 11, 3, -1,
120+
13, 9, 9, -6,
118121
};
119122

120123
// PST for king
121124
// Horizontal, Vertical and diagonal symmetry, like N and Q
122125
int k_pst_mg[16] = {
123-
-92, -32, -40, -70,
124-
-48, -19, -43, -55,
125-
25, 5, -48, -78,
126-
33, 53, -5, -18,
126+
-98, -31, -51, -84,
127+
-49, -17, -42, -62,
128+
23, 5, -49, -78,
129+
35, 55, 3, -21,
127130
};
128131
int k_pst_eg[16] = {
129-
3, 20, 37, 44,
130-
-1, 16, 32, 37,
131-
-29, -1, 25, 37,
132-
-74, -42, -11, -12,
132+
4, 20, 40, 46,
133+
0, 19, 34, 38,
134+
-30, -1, 25, 36,
135+
-75, -43, -14, -10,
133136
};
134137

135138
/// Pawn structure
136139

137140
// Pawn structure: doubled, isolated, backwards, chain, protected, etc.
138-
int isolated_mg[2] = {-12, -30}; // [RANK][OPEN FILE]
139-
int isolated_eg[2] = {1, -8}; // [RANK][OPEN FILE]
140-
int backwards_mg[2] = {-2, -23}; // [OPEN FILE]
141-
int backwards_eg[2] = {-10, -13,}; // [OPEN FILE]
142-
int doubled_mg[2] = {-9, 13,}; // [OPEN FILE]
143-
int doubled_eg[2] = {-27, -32}; // [OPEN FILE]
144-
int blocked_mg = -18; // [OPEN_FILE]
145-
int blocked_eg = 3; // [OPEN_FILE]
146-
int chain_mg[5] = {9, 13, 21, 41, 247,}; // [RANK]
147-
int chain_eg[5] = {17, 6, 1, 16, -79,}; // [RANK]
148-
// Passed pawns
149-
int passed_mg[6] = {-8, -3, -16, 8, 8, -7,}; // [RANK]
150-
int passed_eg[6] = {7, 9, 39, 65, 134, 160,}; // [RANK]
141+
int isolated_mg[2] = {-12, -27}; // [OPEN FILE]
142+
int isolated_eg[2] = {2, -7}; // [OPEN FILE]
143+
int backwards_mg[2] = {-3, -24}; // [OPEN FILE]
144+
int backwards_eg[2] = {-8, -11}; // [OPEN FILE]
145+
int doubled_mg[2] = {-12, 20}; // [OPEN FILE]
146+
int doubled_eg[2] = {-17, -28}; // [OPEN FILE]
147+
int chain_mg[5] = {9, 15, 28, 44, 238}; // [RANK - 2]
148+
int chain_eg[5] = {13, 4, 2, 18, -76}; // [RANK - 2]
149+
int passed_mg[6] = {-9, -2, -14, 15, 12, 5}; // [RANK - 1]
150+
int passed_eg[6] = {8, 10, 40, 67, 136, 161}; // [RANK - 1]
151+
int candidate_mg[5] = {-23, -9, 17, 45, -36}; // [RANK - 1]
152+
int candidate_eg[5] = {4, 9, 11, 44, -10}; // [RANK - 1]
153+
154+
// Evaluation of pawn structure relative to other pieces
155+
int blocked_mg = -13;
156+
int blocked_eg = -14;
157+
int pos_r_open_file_mg = 76;
158+
int pos_r_open_file_eg = 2;
159+
int pos_r_own_half_open_file_mg = 33; // "own" refers to side with missing pawn
160+
int pos_r_own_half_open_file_eg = 3;
161+
int pos_r_other_half_open_file_mg = 18;
162+
int pos_r_other_half_open_file_eg = 13;
151163

152164
/// King safety
153-
int ks_pawn_shield[4] = {-22, 1, 18, 24,}; // 0, 1, 2, and 3 pawns close to the king
165+
int ks_pawn_shield[4] = {-23, 0, 18, 23}; // 0, 1, 2, and 3 pawns close to the king
154166

155-
int kat_open_file = 13;
156-
int kat_own_half_open_file = 10;
157-
int kat_other_half_open_file = 12;
167+
int kat_open_file = 14;
168+
int kat_own_half_open_file = 12;
169+
int kat_other_half_open_file = 5;
158170
int kat_attack_weight[5] = {-2, 10, 4, 3, 7};
159171
int kat_defence_weight[5] = {6, 3, 3, 1, 0};
160172

161-
int kat_table_scale = 18;
162-
int kat_table_translate = 38;
163-
int kat_table_max = 416;
173+
int kat_table_scale = 19;
174+
int kat_table_translate = 39;
175+
int kat_table_max = 418;
164176
int kat_table_offset = 10;
165177

166178
/// Other positional
167179
int pos_bishop_pair_mg = 36;
168-
int pos_bishop_pair_eg = 64;
180+
int pos_bishop_pair_eg = 66;
169181

170-
int pos_r_trapped_mg = -69;
171-
int pos_r_behind_own_passer_eg = 16;
172-
int pos_r_behind_enemy_passer_eg = 3;
173-
int pos_r_xray_pawn_eg = 15;
174-
175-
int pos_r_open_file_mg = 76;
176-
int pos_r_open_file_eg = 3;
177-
int pos_r_own_half_open_file_mg = 17;
178-
int pos_r_own_half_open_file_eg = 9;
179-
int pos_r_other_half_open_file_mg = 34;
180-
int pos_r_other_half_open_file_eg = 8;
182+
int pos_r_trapped_mg = -77;
183+
int pos_r_behind_own_passer_eg = 15;
184+
int pos_r_behind_enemy_passer_eg = 41;
185+
int pos_r_xray_pawn_eg = 16;
181186

182-
int pos_mob_mg[4] = {10, 11, 6, 6}; // [PIECE, ¬KING, ¬PAWN]
183-
int pos_mob_eg[4] = {1, 3, 3, 5};
187+
int pos_mob_mg[4] = {9, 9, 5, 5}; // [PIECE, ¬KING, ¬PAWN]
188+
int pos_mob_eg[4] = {1, 3, 3, 4};
184189

185190
// TODO: Square colour weaknesses, mobility, etc.
186191
};
187192

193+
struct processed_params_t : public eval_params_t {
194+
explicit processed_params_t(const eval_params_t &params);
195+
196+
int pst[2][6][64][2] = {}; // [TEAM][PIECE][SQUARE][MG/EG]
197+
int kat_table[128] = {};
198+
};
199+
188200
class evaluator_t {
189-
struct pawn_entry_t {
190-
U64 hash;
191-
192-
int32_t eval_mg;
193-
int32_t eval_eg;
194-
195-
U64 passers[2];
196-
U64 defended[2];
197-
U64 half_open_files[2]; // [side with the pawn]
198-
U64 open_files;
199-
U64 advance_squares[2];
200-
U64 attackable[2];
201-
};
201+
friend class pawns::structure_t;
202202

203203
size_t pawn_hash_entries;
204-
pawn_entry_t *pawn_hash_table;
205-
206-
int pst[2][6][64][2] = {}; // [TEAM][PIECE][SQUARE][MG/EG]
207-
int kat_table[96] = {};
204+
std::vector<pawns::structure_t> pawn_hash_table;
208205

209-
eval_params_t params;
206+
const processed_params_t &params;
210207
public:
211-
evaluator_t(eval_params_t params, size_t pawn_hash_size);
212-
evaluator_t(const evaluator_t&) = delete;
213-
evaluator_t& operator=(const evaluator_t &) = delete;
214-
~evaluator_t();
208+
evaluator_t(const processed_params_t &params, size_t pawn_hash_size);
215209

216210
int evaluate(const board_t &board);
217211
void prefetch(U64 pawn_hash);
@@ -221,13 +215,10 @@ class evaluator_t {
221215

222216
double eval_material(const board_t &board, int &mg, int &eg); // returns tapering factor 0-1
223217
private:
224-
pawn_entry_t eval_pawns(const board_t &board);
225-
226-
void eval_pst(const board_t &board, int &mg, int &eg, const pawn_entry_t &entry);
227-
228-
void eval_movement(const board_t &board, int &mg, int &eg, const pawn_entry_t &entry);
229-
230-
void eval_positional(const board_t &board, int &mg, int &eg, const pawn_entry_t &entry);
218+
void eval_pawns(const board_t &board, int &mg, int &eg);
219+
void eval_pst(const board_t &board, int &mg, int &eg);
220+
void eval_movement(const board_t &board, int &mg, int &eg);
221+
void eval_positional(const board_t &board, int &mg, int &eg);
231222
};
232223

233224
#endif //TOPPLE_EVAL_H

0 commit comments

Comments
 (0)