Skip to content

Commit d798144

Browse files
authored
Merge pull request #22 from konsolas/eval
Evaluation simplifications + fixes to Syzygy tablebase code.
2 parents ed9f1b6 + 6f6670c commit d798144

File tree

9 files changed

+153
-144
lines changed

9 files changed

+153
-144
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
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.0)
5+
set(TOPPLE_VERSION 0.6.1)
66

77
# Source files for different targets
88
set(SOURCE_FILES
@@ -48,7 +48,7 @@ if (MINGW)
4848
endif ()
4949

5050
# Set -march for the Topple target
51-
target_compile_options(Topple PUBLIC -march=native)
51+
target_compile_options(Topple PUBLIC -march=native -O3)
5252

5353
# Configure the "Release" target
5454
if (CMAKE_BUILD_TYPE STREQUAL "Release")

bb.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@ inline uint8_t file_index(uint8_t sq_index) {
284284
return bb_util::file_index[sq_index];
285285
}
286286

287+
inline uint8_t file_edge_distance(uint8_t file) {
288+
return file < 4 ? file : 7 - file;
289+
}
290+
287291
/**
288292
* Find the distance (in king moves) between two squares
289293
*

eval.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ evaluator_t::evaluator_t(eval_params_t params, size_t pawn_hash_size) : params(p
123123
pst[WHITE][KNIGHT][square_mapping[i][j]][EG] = params.n_pst_eg[i];
124124
pst[WHITE][QUEEN][square_mapping[i][j]][MG] = params.q_pst_mg[i];
125125
pst[WHITE][QUEEN][square_mapping[i][j]][EG] = params.q_pst_eg[i];
126+
pst[WHITE][KING][square_mapping[i][j]][MG] = params.k_pst_mg[i];
126127
pst[WHITE][KING][square_mapping[i][j]][EG] = params.k_pst_eg[i];
127128
}
128129
}
@@ -141,17 +142,6 @@ evaluator_t::evaluator_t(eval_params_t params, size_t pawn_hash_size) : params(p
141142
pst[WHITE][ROOK][sq][EG] = params.r_pst_eg[param_index];
142143
}
143144

144-
// King middlegame pst
145-
for (int i = 0; i < 3; i++) {
146-
for (int j = 0; j < 8; ++j) {
147-
pst[WHITE][KING][(2 - i) * 8 + j][MG] = params.k_pst_mg[i * 8 + j];
148-
}
149-
}
150-
151-
for (int i = 24; i < 64; i++) {
152-
pst[WHITE][KING][i][MG] = params.k_pst_exposed_mg;
153-
}
154-
155145
// Mirror PST for black
156146
for (int piece = 0; piece < 6; piece++) {
157147
for (int square = 0; square < 64; square++) {
@@ -369,8 +359,8 @@ evaluator_t::pawn_entry_t evaluator_t::eval_pawns(const board_t &board) {
369359

370360
U64 not_isolated = BB_ISOLATED[WHITE][sq] & board.bb_pieces[WHITE][PAWN]; // Friendly pawns
371361
if (!not_isolated) {
372-
new_entry.eval_mg += params.isolated_mg[rel_rank(WHITE, rank_index(sq)) - 1][open_file];
373-
new_entry.eval_eg += params.isolated_eg[rel_rank(WHITE, rank_index(sq)) - 1][open_file];
362+
new_entry.eval_mg += params.isolated_mg[open_file];
363+
new_entry.eval_eg += params.isolated_eg[open_file];
374364
}
375365

376366
if(BB_IN_FRONT[WHITE][sq] & board.bb_pieces[WHITE][PAWN]) {
@@ -418,8 +408,8 @@ evaluator_t::pawn_entry_t evaluator_t::eval_pawns(const board_t &board) {
418408

419409
U64 not_isolated = BB_ISOLATED[BLACK][sq] & board.bb_pieces[BLACK][PAWN]; // Friendly pawns
420410
if (!not_isolated) {
421-
new_entry.eval_mg -= params.isolated_mg[rel_rank(BLACK, rank_index(sq)) - 1][open_file];
422-
new_entry.eval_eg -= params.isolated_eg[rel_rank(BLACK, rank_index(sq)) - 1][open_file];
411+
new_entry.eval_mg -= params.isolated_mg[open_file];
412+
new_entry.eval_eg -= params.isolated_eg[open_file];
423413
}
424414

425415
if(BB_IN_FRONT[BLACK][sq] & board.bb_pieces[BLACK][PAWN]) {

eval.h

Lines changed: 87 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ enum GamePhase {
1414

1515
struct eval_params_t {
1616
/// Material
17-
int mat_mg[5] = {95, 365, 436, 583, 1325};
18-
int mat_eg[5] = {101, 369, 389, 645, 1235};
17+
int mat_mg[5] = {95, 365, 436, 583, 1324,};
18+
int mat_eg[5] = {101, 369, 389, 645, 1237,};
1919
int mat_exch_scale = -7;
2020
int mat_exch_pawn = 0;
2121
int mat_exch_minor = 2;
2222
int mat_exch_rook = 4;
2323
int mat_exch_queen = 9;
24-
int mat_opp_bishop[3] = {76, 62, 46}; // [PAWN ADVANTAGE]
24+
int mat_opp_bishop[3] = {76, 64, 52,}; // [PAWN ADVANTAGE]
2525

2626
/// Piece-square tables
2727

@@ -32,127 +32,125 @@ struct eval_params_t {
3232
// A1, B1, C1, D1
3333
// Horizontal, Vertical and diagonal symmetry
3434
int n_pst_mg[16] = {
35-
62, 65, 71, 79,
36-
56, 75, 70, 68,
37-
33, 28, 60, 69,
38-
-56, 52, 21, 49
35+
62, 65, 70, 79,
36+
56, 76, 70, 69,
37+
31, 28, 60, 70,
38+
-59, 52, 19, 49,
3939
};
4040
int q_pst_mg[16] = {
41-
-24, -49, -40, -54,
42-
-16, -2, -19, -26,
43-
-29, -56, -2, -7,
44-
-21, -15, -13, 6
41+
-24, -49, -38, -54,
42+
-15, -2, -19, -26,
43+
-30, -57, -2, -7,
44+
-23, -16, -14, 6,
4545
};
4646
int n_pst_eg[16] = {
47-
-21, -2, 14, 16,
48-
-37, -17, -7, 5,
49-
-49, -18, -20, -12,
50-
-66, -58, -26, -29
47+
-21, -2, 13, 17,
48+
-37, -16, -7, 4,
49+
-47, -18, -19, -12,
50+
-65, -59, -26, -28,
5151
};
5252
int q_pst_eg[16] = {
53-
10, 44, 15, 41,
54-
-7, -31, -3, 9,
55-
-25, 2, -30, -16,
56-
-24, -18, -18, -28
53+
11, 43, 15, 42,
54+
-7, -30, -4, 7,
55+
-26, 3, -29, -16,
56+
-26, -19, -20, -28,
5757
};
5858

5959
// PST for pawn, rook:
6060
// A8, B8, C8, D8, A7, B7, C7, D7, etc.
6161
// Only vertical symmetry
6262
int b_pst_mg[32] = {
63-
-80, -75, -140, -63,
64-
-66, -27, -47, -94,
65-
-49, -4, -8, -30,
66-
-12, -13, -15, -6,
67-
7, -29, -5, -6,
68-
-8, 19, 4, 2,
69-
-2, 9, 16, -12,
70-
-26, -12, -14, -26
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,
7171
};
7272
int r_pst_mg[32] = {
73-
13, -4, -50, 9,
74-
-2, 0, 10, 13,
75-
-39, -2, 10, -14,
76-
-29, -22, -11, -14,
77-
-45, -38, -19, -26,
78-
-40, -20, -19, -33,
79-
-37, -15, -31, -49,
80-
-8, -3, -14, 7
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,
8181
};
8282
int b_pst_eg[32] = {
83-
-16, -21, -10, -26,
84-
-12, -17, -14, -21,
85-
-11, -21, -19, -17,
86-
-17, -14, -19, -15,
87-
-27, -13, -17, -13,
88-
-13, -27, -20, -18,
89-
-21, -35, -35, -22,
90-
-25, -31, -33, -21
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,
9191
};
9292
int r_pst_eg[32] = {
93-
32, 31, 46, 29,
94-
21, 24, 22, 18,
95-
22, 16, 8, 15,
96-
20, 17, 18, 13,
97-
23, 22, 20, 15,
98-
16, 10, 9, 10,
99-
16, 8, 14, 25,
100-
14, 14, 17, 2
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,
101101
};
102102
// Pawns have first and eighth rank excluded
103103
int p_pst_mg[24] = {
104-
127, 196, 78, 77,
105-
43, 59, 38, 22,
106-
15, 10, -4, 14,
104+
116, 187, 79, 75,
105+
45, 59, 37, 22,
106+
15, 10, -4, 14,
107107
13, -2, 6, 16,
108-
9, -3, 11, 5,
109-
9, 22, 15, 9
108+
9, -4, 11, 5,
109+
9, 22, 15, 9,
110110
};
111111
int p_pst_eg[24] = {
112-
111, 86, 102, 103,
113-
54, 46, 49, 38,
114-
30, 29, 32, 19,
115-
18, 16, 12, 6,
116-
6, 10, 2, -3,
117-
10, 7, 8, -8
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,
118118
};
119119

120-
// PST for king:
121-
// A3, B3, C3, D3, E3, F3, G3, H3, A2, B2, C2, etc.
122-
// mg: First 3 ranks only. Default to king_pst_exposed_mg for higher ranks.
123-
// eg: Horizontal, vertical and diagonal symmetry, like N, B, Q
124-
int k_pst_mg[24] = {
125-
-6, -5, -5, -1, 3, -3, 18, -1,
126-
52, 48, 16, -35, -8, 10, 65, 88,
127-
36, 84, 57, 17, 42, 52, 114, 95
120+
// PST for king
121+
// Horizontal, Vertical and diagonal symmetry, like N and Q
122+
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,
128127
};
129-
int k_pst_exposed_mg = 48;
130128
int k_pst_eg[16] = {
131-
22, 54, 68, 70,
132-
37, 59, 71, 72,
133-
6, 36, 61, 71,
134-
-37, -7, 24, 24,
129+
3, 20, 37, 44,
130+
-1, 16, 32, 37,
131+
-29, -1, 25, 37,
132+
-74, -42, -11, -12,
135133
};
136134

137135
/// Pawn structure
138136

139137
// Pawn structure: doubled, isolated, backwards, chain, protected, etc.
140-
int isolated_mg[6][2] = {{-13, -32}, {-19, -37}, {-8, -27}, {1, -14}, {5, 6}, {-509, -33}}; // [RANK][OPEN FILE]
141-
int isolated_eg[6][2] = {{6, -2}, {6, -4}, {-2, -13}, {-15, -18}, {-23, 1}, {607, -1}}; // [RANK][OPEN FILE]
138+
int isolated_mg[2] = {-12, -30}; // [RANK][OPEN FILE]
139+
int isolated_eg[2] = {1, -8}; // [RANK][OPEN FILE]
142140
int backwards_mg[2] = {-2, -23}; // [OPEN FILE]
143-
int backwards_eg[2] = {-9, -13}; // [OPEN FILE]
144-
int doubled_mg[2] = {-8, 13}; // [OPEN FILE]
145-
int doubled_eg[2] = {-28, -32}; // [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]
146144
int blocked_mg = -18; // [OPEN_FILE]
147145
int blocked_eg = 3; // [OPEN_FILE]
148-
int chain_mg[5] = {9, 13, 21, 49, 245}; // [RANK]
149-
int chain_eg[5] = {17, 6, 0, 15, -69}; // [RANK]
146+
int chain_mg[5] = {9, 13, 21, 41, 247,}; // [RANK]
147+
int chain_eg[5] = {17, 6, 1, 16, -79,}; // [RANK]
150148
// Passed pawns
151-
int passed_mg[6] = {-8, -3, -15, 7, -3, -9}; // [RANK]
152-
int passed_eg[6] = {6, 9, 40, 66, 121, 159}; // [RANK]
149+
int passed_mg[6] = {-8, -3, -16, 8, 8, -7,}; // [RANK]
150+
int passed_eg[6] = {7, 9, 39, 65, 134, 160,}; // [RANK]
153151

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

157155
int kat_open_file = 13;
158156
int kat_own_half_open_file = 10;
@@ -166,11 +164,11 @@ struct eval_params_t {
166164
int kat_table_offset = 10;
167165

168166
/// Other positional
169-
int pos_bishop_pair_mg = 37;
170-
int pos_bishop_pair_eg = 65;
167+
int pos_bishop_pair_mg = 36;
168+
int pos_bishop_pair_eg = 64;
171169

172-
int pos_r_trapped_mg = -68;
173-
int pos_r_behind_own_passer_eg = 15;
170+
int pos_r_trapped_mg = -69;
171+
int pos_r_behind_own_passer_eg = 16;
174172
int pos_r_behind_enemy_passer_eg = 3;
175173
int pos_r_xray_pawn_eg = 15;
176174

pvs.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ namespace pvs {
8989
if (n_legal > 1 && !move_is_check && stage == GEN_QUIETS) {
9090
if (depth >= 3) {
9191
// LMR
92-
reduction = depth / 8 + n_legal / 8;
92+
reduction = depth / 8 + n_legal / 8 - 1;
9393
if (reduction >= 1 && board->see(reverse(move)) < 0) reduction -= 2;
9494
}
9595
}
@@ -255,6 +255,9 @@ namespace pvs {
255255
tt->save(bound, board->record[board->now].hash, MAX_PLY - 1, ply, eval, value, EMPTY_MOVE);
256256
return value;
257257
}
258+
259+
if(bound == tt::LOWER) alpha = std::max(alpha, value);
260+
if(bound == tt::UPPER) beta = std::min(beta, value);
258261
}
259262
}
260263

@@ -303,7 +306,7 @@ namespace pvs {
303306
if (n_legal > 1 && !move_is_check && stage == GEN_QUIETS) {
304307
if (depth >= 3) {
305308
// LMR
306-
reduction = depth / 8 + n_legal / 8;
309+
reduction = depth / 8 + n_legal / 8 - 1;
307310
if (reduction >= 1 && board->see(reverse(move)) < 0) reduction -= 2;
308311
}
309312
}

0 commit comments

Comments
 (0)