]> git.sesse.net Git - stockfish/blob - src/material.cpp
Effectively use MVV/LVA in MovePicker::score_captures()
[stockfish] / src / material.cpp
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 Marco Costalba
5
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.
10
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.
15
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/>.
18 */
19
20
21 ////
22 //// Includes
23 ////
24
25 #include <cassert>
26
27 #include "material.h"
28
29
30 ////
31 //// Local definitions
32 ////
33
34 namespace {
35
36   const Value BishopPairMidgameBonus = Value(100);
37   const Value BishopPairEndgameBonus = Value(100);
38
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;
51   Key KPKPMaterialKey;
52
53 }
54
55
56 ////
57 //// Functions
58 ////
59
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.
63
64 void MaterialInfo::init() {
65   KPKMaterialKey = Position::zobMaterial[WHITE][PAWN][1];
66   KKPMaterialKey = Position::zobMaterial[BLACK][PAWN][1];
67   KBNKMaterialKey =
68     Position::zobMaterial[WHITE][BISHOP][1] ^
69     Position::zobMaterial[WHITE][KNIGHT][1];
70   KKBNMaterialKey =
71     Position::zobMaterial[BLACK][BISHOP][1] ^
72     Position::zobMaterial[BLACK][KNIGHT][1];
73   KRKPMaterialKey =
74     Position::zobMaterial[WHITE][ROOK][1] ^
75     Position::zobMaterial[BLACK][PAWN][1];
76   KPKRMaterialKey =
77     Position::zobMaterial[WHITE][PAWN][1] ^
78     Position::zobMaterial[BLACK][ROOK][1];
79   KRKBMaterialKey =
80     Position::zobMaterial[WHITE][ROOK][1] ^
81     Position::zobMaterial[BLACK][BISHOP][1];
82   KBKRMaterialKey =
83     Position::zobMaterial[WHITE][BISHOP][1] ^
84     Position::zobMaterial[BLACK][ROOK][1];
85   KRKNMaterialKey =
86     Position::zobMaterial[WHITE][ROOK][1] ^
87     Position::zobMaterial[BLACK][KNIGHT][1];
88   KNKRMaterialKey =
89     Position::zobMaterial[WHITE][KNIGHT][1] ^
90     Position::zobMaterial[BLACK][ROOK][1];
91   KQKRMaterialKey =
92     Position::zobMaterial[WHITE][QUEEN][1] ^
93     Position::zobMaterial[BLACK][ROOK][1];
94   KRKQMaterialKey =
95     Position::zobMaterial[WHITE][ROOK][1] ^
96     Position::zobMaterial[BLACK][QUEEN][1];
97   KRPKRMaterialKey =
98     Position::zobMaterial[WHITE][ROOK][1] ^
99     Position::zobMaterial[WHITE][PAWN][1] ^
100     Position::zobMaterial[BLACK][ROOK][1];
101   KRKRPMaterialKey =
102     Position::zobMaterial[WHITE][ROOK][1] ^
103     Position::zobMaterial[BLACK][ROOK][1] ^
104     Position::zobMaterial[BLACK][PAWN][1];
105   KRPPKRPMaterialKey =
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];
111   KRPKRPPMaterialKey =
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];
117   KNNKMaterialKey =
118     Position::zobMaterial[WHITE][KNIGHT][1] ^
119     Position::zobMaterial[WHITE][KNIGHT][2];
120   KKNNMaterialKey =
121     Position::zobMaterial[BLACK][KNIGHT][1] ^
122     Position::zobMaterial[BLACK][KNIGHT][2];
123   KBPKBMaterialKey =
124     Position::zobMaterial[WHITE][BISHOP][1] ^
125     Position::zobMaterial[WHITE][PAWN][1] ^
126     Position::zobMaterial[BLACK][BISHOP][1];
127   KBKBPMaterialKey =
128     Position::zobMaterial[WHITE][BISHOP][1] ^
129     Position::zobMaterial[BLACK][BISHOP][1] ^
130     Position::zobMaterial[BLACK][PAWN][1];
131   KBPKNMaterialKey =
132     Position::zobMaterial[WHITE][BISHOP][1] ^
133     Position::zobMaterial[WHITE][PAWN][1] ^
134     Position::zobMaterial[BLACK][KNIGHT][1];
135   KNKBPMaterialKey =
136     Position::zobMaterial[WHITE][KNIGHT][1] ^
137     Position::zobMaterial[BLACK][BISHOP][1] ^
138     Position::zobMaterial[BLACK][PAWN][1];
139   KNPKMaterialKey =
140     Position::zobMaterial[WHITE][KNIGHT][1] ^
141     Position::zobMaterial[WHITE][PAWN][1];
142   KKNPMaterialKey =
143     Position::zobMaterial[BLACK][KNIGHT][1] ^
144     Position::zobMaterial[BLACK][PAWN][1];
145   KPKPMaterialKey =
146     Position::zobMaterial[WHITE][PAWN][1] ^
147     Position::zobMaterial[BLACK][PAWN][1];
148 }
149
150
151 /// Constructor for the MaterialInfoTable class.
152
153 MaterialInfoTable::MaterialInfoTable(unsigned numOfEntries) {
154   size = 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;
159     exit(EXIT_FAILURE);
160   }
161   this->clear();
162 }
163
164
165 /// Destructor for the MaterialInfoTable class.
166
167 MaterialInfoTable::~MaterialInfoTable() {
168   delete [] entries;
169 }
170
171
172 /// MaterialInfoTable::clear() clears a material hash table by setting
173 /// all entries to 0.
174
175 void MaterialInfoTable::clear() {
176   memset(entries, 0, size * sizeof(MaterialInfo));
177 }
178
179
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.
185
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;
190
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:
194   if(mi->key == key)
195     return mi;
196
197   // Clear the MaterialInfo object, and set its key:
198   mi->clear();
199   mi->key = key;
200
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;
205     return mi;
206   }
207
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;
212     return mi;
213   }
214   else if(key == KKPMaterialKey) {
215     mi->evaluationFunction = &EvaluateKKP;
216     return mi;
217   }
218   else if(key == KBNKMaterialKey) {
219     mi->evaluationFunction = &EvaluateKBNK;
220     return mi;
221   }
222   else if(key == KKBNMaterialKey) {
223     mi->evaluationFunction = &EvaluateKKBN;
224     return mi;
225   }
226   else if(key == KRKPMaterialKey) {
227     mi->evaluationFunction = &EvaluateKRKP;
228     return mi;
229   }
230   else if(key == KPKRMaterialKey) {
231     mi->evaluationFunction = &EvaluateKPKR;
232     return mi;
233   }
234   else if(key == KRKBMaterialKey) {
235     mi->evaluationFunction = &EvaluateKRKB;
236     return mi;
237   }
238   else if(key == KBKRMaterialKey) {
239     mi->evaluationFunction = &EvaluateKBKR;
240     return mi;
241   }
242   else if(key == KRKNMaterialKey) {
243     mi->evaluationFunction = &EvaluateKRKN;
244     return mi;
245   }
246   else if(key == KNKRMaterialKey) {
247     mi->evaluationFunction = &EvaluateKNKR;
248     return mi;
249   }
250   else if(key == KQKRMaterialKey) {
251     mi->evaluationFunction = &EvaluateKQKR;
252     return mi;
253   }
254   else if(key == KRKQMaterialKey) {
255     mi->evaluationFunction = &EvaluateKRKQ;
256     return mi;
257   }
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;
262     return mi;
263   }
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;
268     return mi;
269   }
270
271   // OK, we didn't find any special evaluation function for the current
272   // material configuration.  Is there a suitable scaling function?
273   //
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.
278
279   if(key == KRPKRMaterialKey) {
280     mi->scalingFunction[WHITE] = &ScaleKRPKR;
281     return mi;
282   }
283   if(key == KRKRPMaterialKey) {
284     mi->scalingFunction[BLACK] = &ScaleKRKRP;
285     return mi;
286   }
287   if(key == KRPPKRPMaterialKey) {
288     mi->scalingFunction[WHITE] = &ScaleKRPPKRP;
289     return mi;
290   }
291   else if(key == KRPKRPPMaterialKey) {
292     mi->scalingFunction[BLACK] = &ScaleKRPKRPP;
293     return mi;
294   }
295   if(key == KBPKBMaterialKey) {
296     mi->scalingFunction[WHITE] = &ScaleKBPKB;
297     return mi;
298   }
299   if(key == KBKBPMaterialKey) {
300     mi->scalingFunction[BLACK] = &ScaleKBKBP;
301     return mi;
302   }
303   if(key == KBPKNMaterialKey) {
304     mi->scalingFunction[WHITE] = &ScaleKBPKN;
305     return mi;
306   }
307   if(key == KNKBPMaterialKey) {
308     mi->scalingFunction[BLACK] = &ScaleKNKBP;
309     return mi;
310   }
311   if(key == KNPKMaterialKey) {
312     mi->scalingFunction[WHITE] = &ScaleKNPK;
313     return mi;
314   }
315   if(key == KKNPMaterialKey) {
316     mi->scalingFunction[BLACK] = &ScaleKKNP;
317     return mi;
318   }
319
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;
326
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;
337
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;
342     }
343     else if(pos.piece_count(WHITE, PAWN) == 0) {
344       assert(pos.piece_count(BLACK, PAWN) >= 2);
345       mi->scalingFunction[BLACK] = &ScaleKKPs;
346     }
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;
350     }
351   }
352
353   // Evaluate the material balance.
354
355   Color c;
356   int sign;
357   Value egValue = Value(0), mgValue = Value(0);
358
359   for(c = WHITE, sign = 1; c <= BLACK; c++, sign = -sign) {
360
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)))
366         mi->factor[c] = 0;
367       else if(pos.non_pawn_material(c) < RookValueMidgame)
368         mi->factor[c] = 0;
369       else {
370         switch(pos.piece_count(c, BISHOP)) {
371         case 2:
372           mi->factor[c] = 32; break;
373         case 1:
374           mi->factor[c] = 12; break;
375         case 0:
376           mi->factor[c] = 6; break;
377         }
378       }
379     }
380
381     // Bishop pair:
382     if(pos.piece_count(c, BISHOP) >= 2) {
383       mgValue += sign * BishopPairMidgameBonus;
384       egValue += sign * BishopPairEndgameBonus;
385     }
386
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);
393
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);
397       mgValue -= sign * v;
398       egValue -= sign * v;
399     }
400
401   }
402
403   mi->mgValue = int16_t(mgValue);
404   mi->egValue = int16_t(egValue);
405
406   return mi;
407 }