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 Marco Costalba
6 Stockfish is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Stockfish is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 //// Local definitions
36 const Value BishopPairMidgameBonus = Value(100);
37 const Value BishopPairEndgameBonus = Value(100);
39 Key KPKMaterialKey, KKPMaterialKey;
40 Key KBNKMaterialKey, KKBNMaterialKey;
41 Key KRKPMaterialKey, KPKRMaterialKey;
42 Key KRKBMaterialKey, KBKRMaterialKey;
43 Key KRKNMaterialKey, KNKRMaterialKey;
44 Key KQKRMaterialKey, KRKQMaterialKey;
45 Key KRPKRMaterialKey, KRKRPMaterialKey;
46 Key KRPPKRPMaterialKey, KRPKRPPMaterialKey;
47 Key KNNKMaterialKey, KKNNMaterialKey;
48 Key KBPKBMaterialKey, KBKBPMaterialKey;
49 Key KBPKNMaterialKey, KNKBPMaterialKey;
50 Key KNPKMaterialKey, KKNPMaterialKey;
60 /// MaterialInfo::init() is called during program initialization. It
61 /// precomputes material hash keys for a few basic endgames, in order
62 /// to make it easy to recognize such endgames when they occur.
64 void MaterialInfo::init() {
65 KPKMaterialKey = Position::zobMaterial[WHITE][PAWN][1];
66 KKPMaterialKey = Position::zobMaterial[BLACK][PAWN][1];
68 Position::zobMaterial[WHITE][BISHOP][1] ^
69 Position::zobMaterial[WHITE][KNIGHT][1];
71 Position::zobMaterial[BLACK][BISHOP][1] ^
72 Position::zobMaterial[BLACK][KNIGHT][1];
74 Position::zobMaterial[WHITE][ROOK][1] ^
75 Position::zobMaterial[BLACK][PAWN][1];
77 Position::zobMaterial[WHITE][PAWN][1] ^
78 Position::zobMaterial[BLACK][ROOK][1];
80 Position::zobMaterial[WHITE][ROOK][1] ^
81 Position::zobMaterial[BLACK][BISHOP][1];
83 Position::zobMaterial[WHITE][BISHOP][1] ^
84 Position::zobMaterial[BLACK][ROOK][1];
86 Position::zobMaterial[WHITE][ROOK][1] ^
87 Position::zobMaterial[BLACK][KNIGHT][1];
89 Position::zobMaterial[WHITE][KNIGHT][1] ^
90 Position::zobMaterial[BLACK][ROOK][1];
92 Position::zobMaterial[WHITE][QUEEN][1] ^
93 Position::zobMaterial[BLACK][ROOK][1];
95 Position::zobMaterial[WHITE][ROOK][1] ^
96 Position::zobMaterial[BLACK][QUEEN][1];
98 Position::zobMaterial[WHITE][ROOK][1] ^
99 Position::zobMaterial[WHITE][PAWN][1] ^
100 Position::zobMaterial[BLACK][ROOK][1];
102 Position::zobMaterial[WHITE][ROOK][1] ^
103 Position::zobMaterial[BLACK][ROOK][1] ^
104 Position::zobMaterial[BLACK][PAWN][1];
106 Position::zobMaterial[WHITE][ROOK][1] ^
107 Position::zobMaterial[WHITE][PAWN][1] ^
108 Position::zobMaterial[WHITE][PAWN][2] ^
109 Position::zobMaterial[BLACK][ROOK][1] ^
110 Position::zobMaterial[BLACK][PAWN][1];
112 Position::zobMaterial[WHITE][ROOK][1] ^
113 Position::zobMaterial[WHITE][PAWN][1] ^
114 Position::zobMaterial[BLACK][ROOK][1] ^
115 Position::zobMaterial[BLACK][PAWN][1] ^
116 Position::zobMaterial[BLACK][PAWN][2];
118 Position::zobMaterial[WHITE][KNIGHT][1] ^
119 Position::zobMaterial[WHITE][KNIGHT][2];
121 Position::zobMaterial[BLACK][KNIGHT][1] ^
122 Position::zobMaterial[BLACK][KNIGHT][2];
124 Position::zobMaterial[WHITE][BISHOP][1] ^
125 Position::zobMaterial[WHITE][PAWN][1] ^
126 Position::zobMaterial[BLACK][BISHOP][1];
128 Position::zobMaterial[WHITE][BISHOP][1] ^
129 Position::zobMaterial[BLACK][BISHOP][1] ^
130 Position::zobMaterial[BLACK][PAWN][1];
132 Position::zobMaterial[WHITE][BISHOP][1] ^
133 Position::zobMaterial[WHITE][PAWN][1] ^
134 Position::zobMaterial[BLACK][KNIGHT][1];
136 Position::zobMaterial[WHITE][KNIGHT][1] ^
137 Position::zobMaterial[BLACK][BISHOP][1] ^
138 Position::zobMaterial[BLACK][PAWN][1];
140 Position::zobMaterial[WHITE][KNIGHT][1] ^
141 Position::zobMaterial[WHITE][PAWN][1];
143 Position::zobMaterial[BLACK][KNIGHT][1] ^
144 Position::zobMaterial[BLACK][PAWN][1];
146 Position::zobMaterial[WHITE][PAWN][1] ^
147 Position::zobMaterial[BLACK][PAWN][1];
151 /// Constructor for the MaterialInfoTable class.
153 MaterialInfoTable::MaterialInfoTable(unsigned numOfEntries) {
155 entries = new MaterialInfo[size];
156 if(entries == NULL) {
157 std::cerr << "Failed to allocate " << (numOfEntries * sizeof(MaterialInfo))
158 << " bytes for material hash table." << std::endl;
165 /// Destructor for the MaterialInfoTable class.
167 MaterialInfoTable::~MaterialInfoTable() {
172 /// MaterialInfoTable::clear() clears a material hash table by setting
173 /// all entries to 0.
175 void MaterialInfoTable::clear() {
176 memset(entries, 0, size * sizeof(MaterialInfo));
180 /// MaterialInfoTable::get_material_info() takes a position object as input,
181 /// computes or looks up a MaterialInfo object, and returns a pointer to it.
182 /// If the material configuration is not already present in the table, it
183 /// is stored there, so we don't have to recompute everything when the
184 /// same material configuration occurs again.
186 MaterialInfo *MaterialInfoTable::get_material_info(const Position &pos) {
187 Key key = pos.get_material_key();
188 int index = key & (size - 1);
189 MaterialInfo *mi = entries + index;
191 // If mi->key matches the position's material hash key, it means that we
192 // have analysed this material configuration before, and we can simply
193 // return the information we found the last time instead of recomputing it:
197 // Clear the MaterialInfo object, and set its key:
201 // A special case before looking for a specialized evaluation function:
202 // KNN vs K is a draw:
203 if(key == KNNKMaterialKey || key == KKNNMaterialKey) {
204 mi->factor[WHITE] = mi->factor[BLACK] = 0;
208 // Let's look if we have a specialized evaluation function for this
209 // particular material configuration:
210 if(key == KPKMaterialKey) {
211 mi->evaluationFunction = &EvaluateKPK;
214 else if(key == KKPMaterialKey) {
215 mi->evaluationFunction = &EvaluateKKP;
218 else if(key == KBNKMaterialKey) {
219 mi->evaluationFunction = &EvaluateKBNK;
222 else if(key == KKBNMaterialKey) {
223 mi->evaluationFunction = &EvaluateKKBN;
226 else if(key == KRKPMaterialKey) {
227 mi->evaluationFunction = &EvaluateKRKP;
230 else if(key == KPKRMaterialKey) {
231 mi->evaluationFunction = &EvaluateKPKR;
234 else if(key == KRKBMaterialKey) {
235 mi->evaluationFunction = &EvaluateKRKB;
238 else if(key == KBKRMaterialKey) {
239 mi->evaluationFunction = &EvaluateKBKR;
242 else if(key == KRKNMaterialKey) {
243 mi->evaluationFunction = &EvaluateKRKN;
246 else if(key == KNKRMaterialKey) {
247 mi->evaluationFunction = &EvaluateKNKR;
250 else if(key == KQKRMaterialKey) {
251 mi->evaluationFunction = &EvaluateKQKR;
254 else if(key == KRKQMaterialKey) {
255 mi->evaluationFunction = &EvaluateKRKQ;
258 else if(pos.non_pawn_material(BLACK) == Value(0) &&
259 pos.piece_count(BLACK, PAWN) == 0 &&
260 pos.non_pawn_material(WHITE) >= RookValueEndgame) {
261 mi->evaluationFunction = &EvaluateKXK;
264 else if(pos.non_pawn_material(WHITE) == Value(0) &&
265 pos.piece_count(WHITE, PAWN) == 0 &&
266 pos.non_pawn_material(BLACK) >= RookValueEndgame) {
267 mi->evaluationFunction = &EvaluateKKX;
271 // OK, we didn't find any special evaluation function for the current
272 // material configuration. Is there a suitable scaling function?
274 // The code below is rather messy, and it could easily get worse later,
275 // if we decide to add more special cases. We face problems when there
276 // are several conflicting applicable scaling functions and we need to
277 // decide which one to use.
279 if(key == KRPKRMaterialKey) {
280 mi->scalingFunction[WHITE] = &ScaleKRPKR;
283 if(key == KRKRPMaterialKey) {
284 mi->scalingFunction[BLACK] = &ScaleKRKRP;
287 if(key == KRPPKRPMaterialKey) {
288 mi->scalingFunction[WHITE] = &ScaleKRPPKRP;
291 else if(key == KRPKRPPMaterialKey) {
292 mi->scalingFunction[BLACK] = &ScaleKRPKRPP;
295 if(key == KBPKBMaterialKey) {
296 mi->scalingFunction[WHITE] = &ScaleKBPKB;
299 if(key == KBKBPMaterialKey) {
300 mi->scalingFunction[BLACK] = &ScaleKBKBP;
303 if(key == KBPKNMaterialKey) {
304 mi->scalingFunction[WHITE] = &ScaleKBPKN;
307 if(key == KNKBPMaterialKey) {
308 mi->scalingFunction[BLACK] = &ScaleKNKBP;
311 if(key == KNPKMaterialKey) {
312 mi->scalingFunction[WHITE] = &ScaleKNPK;
315 if(key == KKNPMaterialKey) {
316 mi->scalingFunction[BLACK] = &ScaleKKNP;
320 if(pos.non_pawn_material(WHITE) == BishopValueMidgame &&
321 pos.piece_count(WHITE, BISHOP) == 1 && pos.piece_count(WHITE, PAWN) >= 1)
322 mi->scalingFunction[WHITE] = &ScaleKBPK;
323 if(pos.non_pawn_material(BLACK) == BishopValueMidgame &&
324 pos.piece_count(BLACK, BISHOP) == 1 && pos.piece_count(BLACK, PAWN) >= 1)
325 mi->scalingFunction[BLACK] = &ScaleKKBP;
327 if(pos.piece_count(WHITE, PAWN) == 0 &&
328 pos.non_pawn_material(WHITE) == QueenValueMidgame &&
329 pos.piece_count(WHITE, QUEEN) == 1 &&
330 pos.piece_count(BLACK, ROOK) == 1 && pos.piece_count(BLACK, PAWN) >= 1)
331 mi->scalingFunction[WHITE] = &ScaleKQKRP;
332 else if(pos.piece_count(BLACK, PAWN) == 0 &&
333 pos.non_pawn_material(BLACK) == QueenValueMidgame &&
334 pos.piece_count(BLACK, QUEEN) == 1 &&
335 pos.piece_count(WHITE, ROOK) == 1 && pos.piece_count(WHITE, PAWN) >= 1)
336 mi->scalingFunction[BLACK] = &ScaleKRPKQ;
338 if(pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) == Value(0)) {
339 if(pos.piece_count(BLACK, PAWN) == 0) {
340 assert(pos.piece_count(WHITE, PAWN) >= 2);
341 mi->scalingFunction[WHITE] = &ScaleKPsK;
343 else if(pos.piece_count(WHITE, PAWN) == 0) {
344 assert(pos.piece_count(BLACK, PAWN) >= 2);
345 mi->scalingFunction[BLACK] = &ScaleKKPs;
347 else if(pos.piece_count(WHITE, PAWN) == 1 && pos.piece_count(BLACK, PAWN) == 1) {
348 mi->scalingFunction[WHITE] = &ScaleKPKPw;
349 mi->scalingFunction[BLACK] = &ScaleKPKPb;
353 // Evaluate the material balance.
357 Value egValue = Value(0), mgValue = Value(0);
359 for(c = WHITE, sign = 1; c <= BLACK; c++, sign = -sign) {
361 // No pawns makes it difficult to win, even with a material advantage:
362 if(pos.piece_count(c, PAWN) == 0 &&
363 pos.non_pawn_material(c) - pos.non_pawn_material(opposite_color(c))
364 <= BishopValueMidgame) {
365 if(pos.non_pawn_material(c) == pos.non_pawn_material(opposite_color(c)))
367 else if(pos.non_pawn_material(c) < RookValueMidgame)
370 switch(pos.piece_count(c, BISHOP)) {
372 mi->factor[c] = 32; break;
374 mi->factor[c] = 12; break;
376 mi->factor[c] = 6; break;
382 if(pos.piece_count(c, BISHOP) >= 2) {
383 mgValue += sign * BishopPairMidgameBonus;
384 egValue += sign * BishopPairEndgameBonus;
387 // Knights are stronger when there are many pawns on the board. The
388 // formula is taken from Larry Kaufman's paper "The Evaluation of Material
389 // Imbalances in Chess":
390 // http://mywebpages.comcast.net/danheisman/Articles/evaluation_of_material_imbalance.htm
391 mgValue += sign * Value(pos.piece_count(c, KNIGHT)*(pos.piece_count(c, PAWN)-5)*16);
392 egValue += sign * Value(pos.piece_count(c, KNIGHT)*(pos.piece_count(c, PAWN)-5)*16);
394 // Redundancy of major pieces, again based on Kaufman's paper:
395 if(pos.piece_count(c, ROOK) >= 1) {
396 Value v = Value((pos.piece_count(c, ROOK) - 1) * 32 + pos.piece_count(c, QUEEN) * 16);
403 mi->mgValue = int16_t(mgValue);
404 mi->egValue = int16_t(egValue);