Rev 154 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
96 | pmbaty | 1 | /* |
2 | Stockfish, a UCI chess playing engine derived from Glaurung 2.1 |
||
3 | Copyright (C) 2004-2008 Tord Romstad (Glaurung author) |
||
4 | Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad |
||
5 | Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad |
||
6 | |||
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 |
||
9 | the Free Software Foundation, either version 3 of the License, or |
||
10 | (at your option) any later version. |
||
11 | |||
12 | Stockfish is distributed in the hope that it will be useful, |
||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
15 | GNU General Public License for more details. |
||
16 | |||
17 | You should have received a copy of the GNU General Public License |
||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
19 | */ |
||
20 | |||
21 | #include <algorithm> // For std::min |
||
22 | #include <cassert> |
||
23 | #include <cstring> // For std::memset |
||
24 | |||
25 | #include "material.h" |
||
26 | #include "thread.h" |
||
27 | |||
28 | using namespace std; |
||
29 | |||
30 | namespace { |
||
31 | |||
32 | // Polynomial material imbalance parameters |
||
33 | |||
34 | // pair pawn knight bishop rook queen |
||
35 | const int Linear[6] = { 1667, -168, -1027, -166, 238, -138 }; |
||
36 | |||
37 | const int QuadraticOurs[][PIECE_TYPE_NB] = { |
||
38 | // OUR PIECES |
||
39 | // pair pawn knight bishop rook queen |
||
40 | { 0 }, // Bishop pair |
||
41 | { 40, 2 }, // Pawn |
||
42 | { 32, 255, -3 }, // Knight OUR PIECES |
||
43 | { 0, 104, 4, 0 }, // Bishop |
||
44 | { -26, -2, 47, 105, -149 }, // Rook |
||
45 | {-185, 24, 122, 137, -134, 0 } // Queen |
||
46 | }; |
||
47 | |||
48 | const int QuadraticTheirs[][PIECE_TYPE_NB] = { |
||
49 | // THEIR PIECES |
||
50 | // pair pawn knight bishop rook queen |
||
51 | { 0 }, // Bishop pair |
||
52 | { 36, 0 }, // Pawn |
||
53 | { 9, 63, 0 }, // Knight OUR PIECES |
||
54 | { 59, 65, 42, 0 }, // Bishop |
||
55 | { 46, 39, 24, -24, 0 }, // Rook |
||
56 | { 101, 100, -37, 141, 268, 0 } // Queen |
||
57 | }; |
||
58 | |||
59 | // Endgame evaluation and scaling functions are accessed directly and not through |
||
60 | // the function maps because they correspond to more than one material hash key. |
||
61 | Endgame<KXK> EvaluateKXK[] = { Endgame<KXK>(WHITE), Endgame<KXK>(BLACK) }; |
||
62 | |||
63 | Endgame<KBPsK> ScaleKBPsK[] = { Endgame<KBPsK>(WHITE), Endgame<KBPsK>(BLACK) }; |
||
64 | Endgame<KQKRPs> ScaleKQKRPs[] = { Endgame<KQKRPs>(WHITE), Endgame<KQKRPs>(BLACK) }; |
||
65 | Endgame<KPsK> ScaleKPsK[] = { Endgame<KPsK>(WHITE), Endgame<KPsK>(BLACK) }; |
||
66 | Endgame<KPKP> ScaleKPKP[] = { Endgame<KPKP>(WHITE), Endgame<KPKP>(BLACK) }; |
||
67 | |||
68 | // Helper used to detect a given material distribution |
||
69 | bool is_KXK(const Position& pos, Color us) { |
||
70 | return !more_than_one(pos.pieces(~us)) |
||
71 | && pos.non_pawn_material(us) >= RookValueMg; |
||
72 | } |
||
73 | |||
74 | bool is_KBPsKs(const Position& pos, Color us) { |
||
75 | return pos.non_pawn_material(us) == BishopValueMg |
||
76 | && pos.count<BISHOP>(us) == 1 |
||
77 | && pos.count<PAWN >(us) >= 1; |
||
78 | } |
||
79 | |||
80 | bool is_KQKRPs(const Position& pos, Color us) { |
||
81 | return !pos.count<PAWN>(us) |
||
82 | && pos.non_pawn_material(us) == QueenValueMg |
||
83 | && pos.count<QUEEN>(us) == 1 |
||
84 | && pos.count<ROOK>(~us) == 1 |
||
85 | && pos.count<PAWN>(~us) >= 1; |
||
86 | } |
||
87 | |||
88 | /// imbalance() calculates the imbalance by comparing the piece count of each |
||
89 | /// piece type for both colors. |
||
90 | template<Color Us> |
||
91 | int imbalance(const int pieceCount[][PIECE_TYPE_NB]) { |
||
92 | |||
93 | const Color Them = (Us == WHITE ? BLACK : WHITE); |
||
94 | |||
95 | int bonus = 0; |
||
96 | |||
97 | // Second-degree polynomial material imbalance by Tord Romstad |
||
98 | for (int pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; ++pt1) |
||
99 | { |
||
100 | if (!pieceCount[Us][pt1]) |
||
101 | continue; |
||
102 | |||
103 | int v = Linear[pt1]; |
||
104 | |||
105 | for (int pt2 = NO_PIECE_TYPE; pt2 <= pt1; ++pt2) |
||
106 | v += QuadraticOurs[pt1][pt2] * pieceCount[Us][pt2] |
||
107 | + QuadraticTheirs[pt1][pt2] * pieceCount[Them][pt2]; |
||
108 | |||
109 | bonus += pieceCount[Us][pt1] * v; |
||
110 | } |
||
111 | |||
112 | return bonus; |
||
113 | } |
||
114 | |||
115 | } // namespace |
||
116 | |||
117 | namespace Material { |
||
118 | |||
119 | /// Material::probe() looks up the current position's material configuration in |
||
120 | /// the material hash table. It returns a pointer to the Entry if the position |
||
121 | /// is found. Otherwise a new Entry is computed and stored there, so we don't |
||
122 | /// have to recompute all when the same material configuration occurs again. |
||
123 | |||
124 | Entry* probe(const Position& pos) { |
||
125 | |||
126 | Key key = pos.material_key(); |
||
127 | Entry* e = pos.this_thread()->materialTable[key]; |
||
128 | |||
129 | if (e->key == key) |
||
130 | return e; |
||
131 | |||
132 | std::memset(e, 0, sizeof(Entry)); |
||
133 | e->key = key; |
||
134 | e->factor[WHITE] = e->factor[BLACK] = (uint8_t)SCALE_FACTOR_NORMAL; |
||
135 | e->gamePhase = pos.game_phase(); |
||
136 | |||
137 | // Let's look if we have a specialized evaluation function for this particular |
||
138 | // material configuration. Firstly we look for a fixed configuration one, then |
||
139 | // for a generic one if the previous search failed. |
||
140 | if ((e->evaluationFunction = pos.this_thread()->endgames.probe<Value>(key)) != nullptr) |
||
141 | return e; |
||
142 | |||
143 | for (Color c = WHITE; c <= BLACK; ++c) |
||
144 | if (is_KXK(pos, c)) |
||
145 | { |
||
146 | e->evaluationFunction = &EvaluateKXK[c]; |
||
147 | return e; |
||
148 | } |
||
149 | |||
150 | // OK, we didn't find any special evaluation function for the current material |
||
151 | // configuration. Is there a suitable specialized scaling function? |
||
152 | EndgameBase<ScaleFactor>* sf; |
||
153 | |||
154 | if ((sf = pos.this_thread()->endgames.probe<ScaleFactor>(key)) != nullptr) |
||
155 | { |
||
156 | e->scalingFunction[sf->strong_side()] = sf; // Only strong color assigned |
||
157 | return e; |
||
158 | } |
||
159 | |||
160 | // We didn't find any specialized scaling function, so fall back on generic |
||
161 | // ones that refer to more than one material distribution. Note that in this |
||
162 | // case we don't return after setting the function. |
||
163 | for (Color c = WHITE; c <= BLACK; ++c) |
||
164 | { |
||
165 | if (is_KBPsKs(pos, c)) |
||
166 | e->scalingFunction[c] = &ScaleKBPsK[c]; |
||
167 | |||
168 | else if (is_KQKRPs(pos, c)) |
||
169 | e->scalingFunction[c] = &ScaleKQKRPs[c]; |
||
170 | } |
||
171 | |||
172 | Value npm_w = pos.non_pawn_material(WHITE); |
||
173 | Value npm_b = pos.non_pawn_material(BLACK); |
||
174 | |||
175 | if (npm_w + npm_b == VALUE_ZERO && pos.pieces(PAWN)) // Only pawns on the board |
||
176 | { |
||
177 | if (!pos.count<PAWN>(BLACK)) |
||
178 | { |
||
179 | assert(pos.count<PAWN>(WHITE) >= 2); |
||
180 | |||
181 | e->scalingFunction[WHITE] = &ScaleKPsK[WHITE]; |
||
182 | } |
||
183 | else if (!pos.count<PAWN>(WHITE)) |
||
184 | { |
||
185 | assert(pos.count<PAWN>(BLACK) >= 2); |
||
186 | |||
187 | e->scalingFunction[BLACK] = &ScaleKPsK[BLACK]; |
||
188 | } |
||
189 | else if (pos.count<PAWN>(WHITE) == 1 && pos.count<PAWN>(BLACK) == 1) |
||
190 | { |
||
191 | // This is a special case because we set scaling functions |
||
192 | // for both colors instead of only one. |
||
193 | e->scalingFunction[WHITE] = &ScaleKPKP[WHITE]; |
||
194 | e->scalingFunction[BLACK] = &ScaleKPKP[BLACK]; |
||
195 | } |
||
196 | } |
||
197 | |||
198 | // Zero or just one pawn makes it difficult to win, even with a small material |
||
199 | // advantage. This catches some trivial draws like KK, KBK and KNK and gives a |
||
200 | // drawish scale factor for cases such as KRKBP and KmmKm (except for KBBKN). |
||
201 | if (!pos.count<PAWN>(WHITE) && npm_w - npm_b <= BishopValueMg) |
||
202 | e->factor[WHITE] = uint8_t(npm_w < RookValueMg ? SCALE_FACTOR_DRAW : |
||
203 | npm_b <= BishopValueMg ? 4 : 14); |
||
204 | |||
205 | if (!pos.count<PAWN>(BLACK) && npm_b - npm_w <= BishopValueMg) |
||
206 | e->factor[BLACK] = uint8_t(npm_b < RookValueMg ? SCALE_FACTOR_DRAW : |
||
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 | |||
215 | // 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 |
||
217 | // in defining bishop pair bonuses. |
||
218 | const int PieceCount[COLOR_NB][PIECE_TYPE_NB] = { |
||
219 | { 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) }, |
||
221 | { 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) } }; |
||
223 | |||
224 | e->value = int16_t((imbalance<WHITE>(PieceCount) - imbalance<BLACK>(PieceCount)) / 16); |
||
225 | return e; |
||
226 | } |
||
227 | |||
228 | } // namespace Material |