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 | 29 | ||
| 30 | namespace { |
30 | namespace { |
| 31 | 31 | ||
| 32 | // Polynomial material imbalance parameters |
32 | // Polynomial material imbalance parameters |
| 33 | 33 | ||
| 34 |
|
34 | constexpr int QuadraticOurs[][PIECE_TYPE_NB] = { |
| 35 | // OUR PIECES |
35 | // OUR PIECES |
| 36 | // pair pawn knight bishop rook queen |
36 | // pair pawn knight bishop rook queen |
| 37 | { |
37 | {1438 }, // Bishop pair |
| 38 | { 40, |
38 | { 40, 38 }, // Pawn |
| 39 | { 32, 255, |
39 | { 32, 255, -62 }, // Knight OUR PIECES |
| 40 | { 0, 104, 4, 0 }, // Bishop |
40 | { 0, 104, 4, 0 }, // Bishop |
| 41 | { -26, -2, 47, 105, - |
41 | { -26, -2, 47, 105, -208 }, // Rook |
| 42 | {-189, 24, 117, 133, -134, - |
42 | {-189, 24, 117, 133, -134, -6 } // Queen |
| 43 | }; |
43 | }; |
| 44 | 44 | ||
| 45 |
|
45 | constexpr int QuadraticTheirs[][PIECE_TYPE_NB] = { |
| 46 | // THEIR PIECES |
46 | // THEIR PIECES |
| 47 | // pair pawn knight bishop rook queen |
47 | // pair pawn knight bishop rook queen |
| 48 | { 0 }, // Bishop pair |
48 | { 0 }, // Bishop pair |
| 49 | { 36, 0 }, // Pawn |
49 | { 36, 0 }, // Pawn |
| 50 | { 9, 63, 0 }, // Knight OUR PIECES |
50 | { 9, 63, 0 }, // Knight OUR PIECES |
| Line 66... | Line 66... | ||
| 66 | bool is_KXK(const Position& pos, Color us) { |
66 | bool is_KXK(const Position& pos, Color us) { |
| 67 | return !more_than_one(pos.pieces(~us)) |
67 | return !more_than_one(pos.pieces(~us)) |
| 68 | && pos.non_pawn_material(us) >= RookValueMg; |
68 | && pos.non_pawn_material(us) >= RookValueMg; |
| 69 | } |
69 | } |
| 70 | 70 | ||
| 71 | bool |
71 | bool is_KBPsK(const Position& pos, Color us) { |
| 72 | return pos.non_pawn_material(us) == BishopValueMg |
72 | return pos.non_pawn_material(us) == BishopValueMg |
| 73 | && pos.count<BISHOP>(us) == 1 |
73 | && pos.count<BISHOP>(us) == 1 |
| 74 | && pos.count<PAWN >(us) >= 1; |
74 | && pos.count<PAWN >(us) >= 1; |
| 75 | } |
75 | } |
| 76 | 76 | ||
| 77 | bool is_KQKRPs(const Position& pos, Color us) { |
77 | bool is_KQKRPs(const Position& pos, Color us) { |
| 78 | return !pos.count<PAWN>(us) |
78 | return !pos.count<PAWN>(us) |
| 79 | && pos.non_pawn_material(us) == QueenValueMg |
79 | && pos.non_pawn_material(us) == QueenValueMg |
| 80 | && pos.count<QUEEN>(us) |
80 | && pos.count<QUEEN>(us) == 1 |
| 81 | && pos.count<ROOK>(~us) == 1 |
81 | && pos.count<ROOK>(~us) == 1 |
| 82 | && pos.count<PAWN>(~us) >= 1; |
82 | && pos.count<PAWN>(~us) >= 1; |
| 83 | } |
83 | } |
| 84 | 84 | ||
| 85 | /// imbalance() calculates the imbalance by comparing the piece count of each |
85 | /// imbalance() calculates the imbalance by comparing the piece count of each |
| 86 | /// piece type for both colors. |
86 | /// piece type for both colors. |
| 87 | template<Color Us> |
87 | template<Color Us> |
| 88 | int imbalance(const int pieceCount[][PIECE_TYPE_NB]) { |
88 | int imbalance(const int pieceCount[][PIECE_TYPE_NB]) { |
| 89 | 89 | ||
| 90 |
|
90 | constexpr Color Them = (Us == WHITE ? BLACK : WHITE); |
| 91 | 91 | ||
| 92 | int bonus = 0; |
92 | int bonus = 0; |
| 93 | 93 | ||
| 94 | // Second-degree polynomial material imbalance, by Tord Romstad |
94 | // Second-degree polynomial material imbalance, by Tord Romstad |
| 95 | for (int pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; ++pt1) |
95 | for (int pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; ++pt1) |
| Line 150... | Line 150... | ||
| 150 | return e; |
150 | return e; |
| 151 | } |
151 | } |
| 152 | 152 | ||
| 153 | // OK, we didn't find any special evaluation function for the current material |
153 | // OK, we didn't find any special evaluation function for the current material |
| 154 | // configuration. Is there a suitable specialized scaling function? |
154 | // configuration. Is there a suitable specialized scaling function? |
| 155 | EndgameBase<ScaleFactor>* sf; |
155 | const EndgameBase<ScaleFactor>* sf; |
| 156 | 156 | ||
| 157 | if ((sf = pos.this_thread()->endgames.probe<ScaleFactor>(key)) != nullptr) |
157 | if ((sf = pos.this_thread()->endgames.probe<ScaleFactor>(key)) != nullptr) |
| 158 | { |
158 | { |
| 159 | e->scalingFunction[sf->strongSide] = sf; // Only strong color assigned |
159 | e->scalingFunction[sf->strongSide] = sf; // Only strong color assigned |
| 160 | return e; |
160 | return e; |
| Line 163... | Line 163... | ||
| 163 | // We didn't find any specialized scaling function, so fall back on generic |
163 | // We didn't find any specialized scaling function, so fall back on generic |
| 164 | // ones that refer to more than one material distribution. Note that in this |
164 | // ones that refer to more than one material distribution. Note that in this |
| 165 | // case we don't return after setting the function. |
165 | // case we don't return after setting the function. |
| 166 | for (Color c = WHITE; c <= BLACK; ++c) |
166 | for (Color c = WHITE; c <= BLACK; ++c) |
| 167 | { |
167 | { |
| 168 | if ( |
168 | if (is_KBPsK(pos, c)) |
| 169 | e->scalingFunction[c] = &ScaleKBPsK[c]; |
169 | e->scalingFunction[c] = &ScaleKBPsK[c]; |
| 170 | 170 | ||
| 171 | else if (is_KQKRPs(pos, c)) |
171 | else if (is_KQKRPs(pos, c)) |
| 172 | e->scalingFunction[c] = &ScaleKQKRPs[c]; |
172 | e->scalingFunction[c] = &ScaleKQKRPs[c]; |
| 173 | } |
173 | } |
| Line 203... | Line 203... | ||
| 203 | npm_b <= BishopValueMg ? 4 : 14); |
203 | npm_b <= BishopValueMg ? 4 : 14); |
| 204 | 204 | ||
| 205 | if (!pos.count<PAWN>(BLACK) && npm_b - npm_w <= BishopValueMg) |
205 | if (!pos.count<PAWN>(BLACK) && npm_b - npm_w <= BishopValueMg) |
| 206 | e->factor[BLACK] = uint8_t(npm_b < RookValueMg ? SCALE_FACTOR_DRAW : |
206 | e->factor[BLACK] = uint8_t(npm_b < RookValueMg ? SCALE_FACTOR_DRAW : |
| 207 | npm_w <= BishopValueMg ? 4 : 14); |
207 | npm_w <= BishopValueMg ? 4 : 14); |
| 208 | - | ||
| 209 | if (pos.count<PAWN>(WHITE) == 1 && npm_w - npm_b <= BishopValueMg) |
- | |
| 210 | e->factor[WHITE] = (uint8_t) SCALE_FACTOR_ONEPAWN; |
- | |
| 211 | - | ||
| 212 | if (pos.count<PAWN>(BLACK) == 1 && npm_b - npm_w <= BishopValueMg) |
- | |
| 213 | e->factor[BLACK] = (uint8_t) SCALE_FACTOR_ONEPAWN; |
- | |
| 214 | 208 | ||
| 215 | // Evaluate the material imbalance. We use PIECE_TYPE_NONE as a place holder |
209 | // Evaluate the material imbalance. We use PIECE_TYPE_NONE as a place holder |
| 216 | // for the bishop pair "extended piece", which allows us to be more flexible |
210 | // for the bishop pair "extended piece", which allows us to be more flexible |
| 217 | // in defining bishop pair bonuses. |
211 | // in defining bishop pair bonuses. |
| 218 | const int |
212 | const int pieceCount[COLOR_NB][PIECE_TYPE_NB] = { |
| 219 | { pos.count<BISHOP>(WHITE) > 1, pos.count<PAWN>(WHITE), pos.count<KNIGHT>(WHITE), |
213 | { pos.count<BISHOP>(WHITE) > 1, pos.count<PAWN>(WHITE), pos.count<KNIGHT>(WHITE), |
| 220 | pos.count<BISHOP>(WHITE) , pos.count<ROOK>(WHITE), pos.count<QUEEN >(WHITE) }, |
214 | pos.count<BISHOP>(WHITE) , pos.count<ROOK>(WHITE), pos.count<QUEEN >(WHITE) }, |
| 221 | { pos.count<BISHOP>(BLACK) > 1, pos.count<PAWN>(BLACK), pos.count<KNIGHT>(BLACK), |
215 | { pos.count<BISHOP>(BLACK) > 1, pos.count<PAWN>(BLACK), pos.count<KNIGHT>(BLACK), |
| 222 | pos.count<BISHOP>(BLACK) , pos.count<ROOK>(BLACK), pos.count<QUEEN >(BLACK) } }; |
216 | pos.count<BISHOP>(BLACK) , pos.count<ROOK>(BLACK), pos.count<QUEEN >(BLACK) } }; |
| 223 | 217 | ||
| 224 | e->value = int16_t((imbalance<WHITE>( |
218 | e->value = int16_t((imbalance<WHITE>(pieceCount) - imbalance<BLACK>(pieceCount)) / 16); |
| 225 | return e; |
219 | return e; |
| 226 | } |
220 | } |
| 227 | 221 | ||
| 228 | } // namespace Material |
222 | } // namespace Material |