Rev 169 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 169 | Rev 185 | ||
---|---|---|---|
Line 4... | Line 4... | ||
4 | Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad |
4 | Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad |
5 | Copyright (C) 2015- |
5 | Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad |
6 | 6 | ||
7 | Stockfish is free software: you can redistribute it and/or modify |
7 | Stockfish is free software: you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by |
8 | it under the terms of the GNU General Public License as published by |
9 | the Free Software Foundation, either version 3 of the License, or |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. |
10 | (at your option) any later version. |
Line 29... | Line 29... | ||
29 | namespace { |
29 | namespace { |
30 | 30 | ||
31 | #define V Value |
31 | #define V Value |
32 | #define S(mg, eg) make_score(mg, eg) |
32 | #define S(mg, eg) make_score(mg, eg) |
33 | 33 | ||
34 | // |
34 | // Pawn penalties |
35 |
|
35 | constexpr Score Backward = S( 9, 24); |
36 | - | ||
37 |
|
36 | constexpr Score Doubled = S(11, 56); |
38 |
|
37 | constexpr Score Isolated = S( 5, 15); |
39 | 38 | ||
40 | // Connected pawn bonus by opposed, phalanx, #support and rank |
39 | // Connected pawn bonus by opposed, phalanx, #support and rank |
41 | Score Connected[2][2][3][RANK_NB]; |
40 | Score Connected[2][2][3][RANK_NB]; |
42 | 41 | ||
43 | // Doubled pawn penalty |
- | |
44 | const Score Doubled = S(18, 38); |
- | |
45 | - | ||
46 | // |
42 | // Strength of pawn shelter for our king by [distance from edge][rank]. |
47 | // RANK_1 = 0 is used for files where we have no |
43 | // RANK_1 = 0 is used for files where we have no pawn, or pawn is behind our king. |
48 |
|
44 | constexpr Value ShelterStrength[int(FILE_NB) / 2][RANK_NB] = { |
49 | { { V( 97), V(17), V( 9), V(44), V( 84), V( 87), V( 99) }, // Not On King file |
- | |
50 | { V(106), V( 6), V(33), V(86), V( 87), V(104), V(112) }, |
- | |
51 |
|
45 | { V( -6), V( 81), V( 93), V( 58), V( 39), V( 18), V( 25) }, |
52 | { V( 73), V( 7), V(54), V(73), V( 84), V( 83), V(111) } }, |
- | |
53 | { { V(104), V(20), V( 6), V(27), V( 86), V( 93), V( 82) }, // On King file |
- | |
54 |
|
46 | { V(-43), V( 61), V( 35), V(-49), V(-29), V(-11), V( -63) }, |
55 |
|
47 | { V(-10), V( 75), V( 23), V( -2), V( 32), V( 3), V( -45) }, |
56 |
|
48 | { V(-39), V(-13), V(-29), V(-52), V(-48), V(-67), V(-166) } |
57 | }; |
49 | }; |
58 | 50 | ||
59 | // Danger of enemy pawns moving toward our king by |
51 | // Danger of enemy pawns moving toward our king by [distance from edge][rank]. |
60 | // |
52 | // RANK_1 = 0 is used for files where the enemy has no pawn, or their pawn |
61 | // |
53 | // is behind our king. |
62 |
|
54 | constexpr Value UnblockedStorm[int(FILE_NB) / 2][RANK_NB] = { |
63 | { { V( 0), V(-290), V(-274), V(57), V(41) }, // BlockedByKing |
- | |
64 | { V( 0), V( 60), V( 144), V(39), V(13) }, |
- | |
65 | { V( 0), V( 65), V( 141), V(41), V(34) }, |
- | |
66 |
|
55 | { V( 89), V(107), V(123), V(93), V(57), V( 45), V( 51) }, |
67 | { |
56 | { V( 44), V(-18), V(123), V(46), V(39), V( -7), V( 23) }, |
68 | { V( 1), V( 64), V( 143), V(26), V(13) }, |
- | |
69 | { V( 1), V( 47), V( 110), V(44), V(24) }, |
- | |
70 |
|
57 | { V( 4), V( 52), V(162), V(37), V( 7), V(-14), V( -2) }, |
71 | { { V( 0), V( 0), V( 79), V(23), V( 1) }, // BlockedByPawn |
- | |
72 | { V( 0), V( 0), V( 148), V(27), V( 2) }, |
- | |
73 | { V( 0), V( 0), V( 161), V(16), V( 1) }, |
- | |
74 | { V( 0), V( 0), V( 171), V(22), V(15) } }, |
- | |
75 | { |
58 | { V(-10), V(-14), V( 90), V(15), V( 2), V( -7), V(-16) } |
76 | { V(31), V( 30), V( 99), V(39), V(19) }, |
- | |
77 | { V(23), V( 29), V( 96), V(41), V(15) }, |
- | |
78 | { V(21), V( 23), V( 116), V(41), V(15) } } |
- | |
79 | }; |
59 | }; |
80 | - | ||
81 | // Max bonus for king safety. Corresponds to start position with all the pawns |
- | |
82 | // in front of the king and no enemy pawn on the horizon. |
- | |
83 | const Value MaxSafetyBonus = V(258); |
- | |
84 | 60 | ||
85 | #undef S |
61 | #undef S |
86 | #undef V |
62 | #undef V |
87 | 63 | ||
88 | template<Color Us> |
64 | template<Color Us> |
89 | Score evaluate(const Position& pos, Pawns::Entry* e) { |
65 | Score evaluate(const Position& pos, Pawns::Entry* e) { |
90 | 66 | ||
91 |
|
67 | constexpr Color Them = (Us == WHITE ? BLACK : WHITE); |
92 |
|
68 | constexpr Direction Up = (Us == WHITE ? NORTH : SOUTH); |
93 | const Direction Right = (Us == WHITE ? NORTH_EAST : SOUTH_WEST); |
- | |
94 | const Direction Left = (Us == WHITE ? NORTH_WEST : SOUTH_EAST); |
- | |
95 | 69 | ||
96 | Bitboard b, neighbours, stoppers, doubled, supported, phalanx; |
70 | Bitboard b, neighbours, stoppers, doubled, supported, phalanx; |
97 | Bitboard lever, leverPush; |
71 | Bitboard lever, leverPush; |
98 | Square s; |
72 | Square s; |
99 | bool opposed, backward; |
73 | bool opposed, backward; |
Line 104... | Line 78... | ||
104 | Bitboard theirPawns = pos.pieces(Them, PAWN); |
78 | Bitboard theirPawns = pos.pieces(Them, PAWN); |
105 | 79 | ||
106 | e->passedPawns[Us] = e->pawnAttacksSpan[Us] = e->weakUnopposed[Us] = 0; |
80 | e->passedPawns[Us] = e->pawnAttacksSpan[Us] = e->weakUnopposed[Us] = 0; |
107 | e->semiopenFiles[Us] = 0xFF; |
81 | e->semiopenFiles[Us] = 0xFF; |
108 | e->kingSquares[Us] = SQ_NONE; |
82 | e->kingSquares[Us] = SQ_NONE; |
109 | e->pawnAttacks[Us] = |
83 | e->pawnAttacks[Us] = pawn_attacks_bb<Us>(ourPawns); |
110 | e->pawnsOnSquares[Us][BLACK] = popcount(ourPawns & DarkSquares); |
84 | e->pawnsOnSquares[Us][BLACK] = popcount(ourPawns & DarkSquares); |
111 | e->pawnsOnSquares[Us][WHITE] = pos.count<PAWN>(Us) - e->pawnsOnSquares[Us][BLACK]; |
85 | e->pawnsOnSquares[Us][WHITE] = pos.count<PAWN>(Us) - e->pawnsOnSquares[Us][BLACK]; |
112 | 86 | ||
113 | // Loop through all pawns of the current color and score each pawn |
87 | // Loop through all pawns of the current color and score each pawn |
114 | while ((s = *pl++) != SQ_NONE) |
88 | while ((s = *pl++) != SQ_NONE) |
Line 128... | Line 102... | ||
128 | doubled = ourPawns & (s - Up); |
102 | doubled = ourPawns & (s - Up); |
129 | neighbours = ourPawns & adjacent_files_bb(f); |
103 | neighbours = ourPawns & adjacent_files_bb(f); |
130 | phalanx = neighbours & rank_bb(s); |
104 | phalanx = neighbours & rank_bb(s); |
131 | supported = neighbours & rank_bb(s - Up); |
105 | supported = neighbours & rank_bb(s - Up); |
132 | 106 | ||
133 | // A pawn is backward when it is behind all pawns of the same color |
107 | // A pawn is backward when it is behind all pawns of the same color |
134 | // adjacent files and cannot be safely advanced. |
108 | // on the adjacent files and cannot be safely advanced. |
135 |
|
109 | backward = !(ourPawns & pawn_attack_span(Them, s + Up)) |
136 | backward = false; |
- | |
137 | else |
- | |
138 | { |
- | |
139 | // Find the backmost rank with neighbours or stoppers |
- | |
140 |
|
110 | && (stoppers & (leverPush | (s + Up))); |
141 | - | ||
142 | // The pawn is backward when it cannot safely progress to that rank: |
- | |
143 | // either there is a stopper in the way on this rank, or there is a |
- | |
144 | // stopper on adjacent file which controls the way to that rank. |
- | |
145 | backward = (b | shift<Up>(b & adjacent_files_bb(f))) & stoppers; |
- | |
146 | - | ||
147 | assert(!(backward && (forward_ranks_bb(Them, s + Up) & neighbours))); |
- | |
148 | } |
- | |
149 | 111 | ||
150 | // Passed pawns will be properly scored in evaluation because we need |
112 | // Passed pawns will be properly scored in evaluation because we need |
151 | // full attack info to evaluate them. Include also not passed pawns |
113 | // full attack info to evaluate them. Include also not passed pawns |
152 | // which could become passed after one or two pawn pushes when are |
114 | // which could become passed after one or two pawn pushes when are |
153 | // not attacked more times than defended. |
115 | // not attacked more times than defended. |
154 | if ( !(stoppers ^ lever ^ leverPush) |
116 | if ( !(stoppers ^ lever ^ leverPush) |
155 | && !(ourPawns & forward_file_bb(Us, s)) |
- | |
156 | && popcount(supported) >= popcount(lever) |
117 | && popcount(supported) >= popcount(lever) - 1 |
157 | && popcount(phalanx) >= popcount(leverPush)) |
118 | && popcount(phalanx) >= popcount(leverPush)) |
158 | e->passedPawns[Us] |= s; |
119 | e->passedPawns[Us] |= s; |
159 | 120 | ||
160 | else if ( stoppers == SquareBB[s + Up] |
121 | else if ( stoppers == SquareBB[s + Up] |
161 | && relative_rank(Us, s) >= RANK_5) |
122 | && relative_rank(Us, s) >= RANK_5) |
Line 191... | Line 152... | ||
191 | /// hard-coded tables, when makes sense, we prefer to calculate them with a formula |
152 | /// hard-coded tables, when makes sense, we prefer to calculate them with a formula |
192 | /// to reduce independent parameters and to allow easier tuning and better insight. |
153 | /// to reduce independent parameters and to allow easier tuning and better insight. |
193 | 154 | ||
194 | void init() { |
155 | void init() { |
195 | 156 | ||
196 | static |
157 | static constexpr int Seed[RANK_NB] = { 0, 13, 24, 18, 65, 100, 175, 330 }; |
197 | 158 | ||
198 | for (int opposed = 0; opposed <= 1; ++opposed) |
159 | for (int opposed = 0; opposed <= 1; ++opposed) |
199 | for (int phalanx = 0; phalanx <= 1; ++phalanx) |
160 | for (int phalanx = 0; phalanx <= 1; ++phalanx) |
200 | for (int support = 0; support <= 2; ++support) |
161 | for (int support = 0; support <= 2; ++support) |
201 | for (Rank r = RANK_2; r < RANK_8; ++r) |
162 | for (Rank r = RANK_2; r < RANK_8; ++r) |
Line 220... | Line 181... | ||
220 | 181 | ||
221 | if (e->key == key) |
182 | if (e->key == key) |
222 | return e; |
183 | return e; |
223 | 184 | ||
224 | e->key = key; |
185 | e->key = key; |
225 | e-> |
186 | e->scores[WHITE] = evaluate<WHITE>(pos, e); |
226 | e-> |
187 | e->scores[BLACK] = evaluate<BLACK>(pos, e); |
227 | e->openFiles = popcount(e->semiopenFiles[WHITE] & e->semiopenFiles[BLACK]); |
188 | e->openFiles = popcount(e->semiopenFiles[WHITE] & e->semiopenFiles[BLACK]); |
- | 189 | e->asymmetry = popcount( (e->passedPawns[WHITE] | e->passedPawns[BLACK]) |
|
- | 190 | | (e->semiopenFiles[WHITE] ^ e->semiopenFiles[BLACK])); |
|
- | 191 | ||
228 | return e; |
192 | return e; |
229 | } |
193 | } |
230 | 194 | ||
231 | 195 | ||
232 | /// Entry:: |
196 | /// Entry::evaluate_shelter() calculates the shelter bonus and the storm |
233 | /// |
197 | /// penalty for a king, looking at the king file and the two closest files. |
234 | 198 | ||
235 | template<Color Us> |
199 | template<Color Us> |
236 | Value Entry:: |
200 | Value Entry::evaluate_shelter(const Position& pos, Square ksq) { |
237 | 201 | ||
238 |
|
202 | constexpr Color Them = (Us == WHITE ? BLACK : WHITE); |
- | 203 | constexpr Direction Down = (Us == WHITE ? SOUTH : NORTH); |
|
- | 204 | constexpr Bitboard BlockRanks = (Us == WHITE ? Rank1BB | Rank2BB : Rank8BB | Rank7BB); |
|
239 | 205 | ||
240 | enum { BlockedByKing, Unopposed, BlockedByPawn, Unblocked }; |
- | |
241 | - | ||
242 | Bitboard b = pos.pieces(PAWN) & ( |
206 | Bitboard b = pos.pieces(PAWN) & ~forward_ranks_bb(Them, ksq); |
243 | Bitboard ourPawns = b & pos.pieces(Us); |
207 | Bitboard ourPawns = b & pos.pieces(Us); |
244 | Bitboard theirPawns = b & pos.pieces(Them); |
208 | Bitboard theirPawns = b & pos.pieces(Them); |
245 | Value safety = MaxSafetyBonus; |
- | |
246 | File center = std::max(FILE_B, std::min(FILE_G, file_of(ksq))); |
- | |
247 | 209 | ||
- | 210 | Value safety = (shift<Down>(theirPawns) & (FileABB | FileHBB) & BlockRanks & ksq) ? |
|
- | 211 | Value(374) : Value(5); |
|
- | 212 | ||
- | 213 | File center = std::max(FILE_B, std::min(FILE_G, file_of(ksq))); |
|
248 | for (File f = File(center - 1); f <= File(center + 1); ++f) |
214 | for (File f = File(center - 1); f <= File(center + 1); ++f) |
249 | { |
215 | { |
250 | b = ourPawns & file_bb(f); |
216 | b = ourPawns & file_bb(f); |
251 |
|
217 | int ourRank = b ? relative_rank(Us, backmost_sq(Us, b)) : 0; |
252 | 218 | ||
253 | b = theirPawns & file_bb(f); |
219 | b = theirPawns & file_bb(f); |
254 |
|
220 | int theirRank = b ? relative_rank(Us, frontmost_sq(Them, b)) : 0; |
255 | 221 | ||
256 | int d = std::min(f, ~f); |
222 | int d = std::min(f, ~f); |
257 | safety |
223 | safety += ShelterStrength[d][ourRank]; |
258 | + StormDanger |
- | |
259 | [f == file_of(ksq) && rkThem == relative_rank(Us, ksq) + 1 ? BlockedByKing : |
- | |
260 |
|
224 | safety -= (ourRank && (ourRank == theirRank - 1)) ? 66 * (theirRank == RANK_3) |
261 |
|
225 | : UnblockedStorm[d][theirRank]; |
262 | [d][rkThem]; |
- | |
263 | } |
226 | } |
264 | 227 | ||
265 | return safety; |
228 | return safety; |
266 | } |
229 | } |
267 | 230 | ||
268 | 231 | ||
269 | /// Entry::do_king_safety() calculates a bonus for king safety. It is called only |
232 | /// Entry::do_king_safety() calculates a bonus for king safety. It is called only |
270 | /// when king square changes, which is about 20% of total king_safety() calls. |
233 | /// when king square changes, which is about 20% of total king_safety() calls. |
271 | 234 | ||
272 | template<Color Us> |
235 | template<Color Us> |
273 | Score Entry::do_king_safety(const Position& |
236 | Score Entry::do_king_safety(const Position& pos) { |
274 | 237 | ||
- | 238 | Square ksq = pos.square<KING>(Us); |
|
275 | kingSquares[Us] = ksq; |
239 | kingSquares[Us] = ksq; |
276 | castlingRights[Us] = pos.can_castle(Us); |
240 | castlingRights[Us] = pos.can_castle(Us); |
277 | int minKingPawnDistance = 0; |
241 | int minKingPawnDistance = 0; |
278 | 242 | ||
279 | Bitboard pawns = pos.pieces(Us, PAWN); |
243 | Bitboard pawns = pos.pieces(Us, PAWN); |
280 | if (pawns) |
244 | if (pawns) |
281 | while (!(DistanceRingBB[ksq][ |
245 | while (!(DistanceRingBB[ksq][++minKingPawnDistance] & pawns)) {} |
282 | 246 | ||
283 | Value bonus = |
247 | Value bonus = evaluate_shelter<Us>(pos, ksq); |
284 | 248 | ||
285 | // If we can castle use the bonus after the castling if it is bigger |
249 | // If we can castle use the bonus after the castling if it is bigger |
286 | if (pos.can_castle( |
250 | if (pos.can_castle(Us | KING_SIDE)) |
287 | bonus = std::max(bonus, |
251 | bonus = std::max(bonus, evaluate_shelter<Us>(pos, relative_square(Us, SQ_G1))); |
288 | 252 | ||
289 | if (pos.can_castle( |
253 | if (pos.can_castle(Us | QUEEN_SIDE)) |
290 | bonus = std::max(bonus, |
254 | bonus = std::max(bonus, evaluate_shelter<Us>(pos, relative_square(Us, SQ_C1))); |
291 | 255 | ||
292 | return make_score(bonus, -16 * minKingPawnDistance); |
256 | return make_score(bonus, -16 * minKingPawnDistance); |
293 | } |
257 | } |
294 | 258 | ||
295 | // Explicit template instantiation |
259 | // Explicit template instantiation |
296 | template Score Entry::do_king_safety<WHITE>(const Position& |
260 | template Score Entry::do_king_safety<WHITE>(const Position& pos); |
297 | template Score Entry::do_king_safety<BLACK>(const Position& |
261 | template Score Entry::do_king_safety<BLACK>(const Position& pos); |
298 | 262 | ||
299 | } // namespace Pawns |
263 | } // namespace Pawns |