]> git.sesse.net Git - stockfish/blob - src/material.cpp
ce7d7ab591881c2a030ac57efd954822c6ad9425
[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 #include <map>
27
28 #include "material.h"
29
30
31 ////
32 //// Local definitions
33 ////
34
35 namespace {
36
37   const Value BishopPairMidgameBonus = Value(100);
38   const Value BishopPairEndgameBonus = Value(100);
39
40   Key KRPKRMaterialKey, KRKRPMaterialKey;
41   Key KNNKMaterialKey,  KKNNMaterialKey;
42   Key KBPKBMaterialKey, KBKBPMaterialKey;
43   Key KBPKNMaterialKey, KNKBPMaterialKey;
44   Key KNPKMaterialKey,  KKNPMaterialKey;
45   Key KPKPMaterialKey;
46   Key KRPPKRPMaterialKey, KRPKRPPMaterialKey;
47
48   std::map<Key, EndgameEvaluationFunction*> EEFmap;
49
50   void EEFAdd(Key k, EndgameEvaluationFunction* f) {
51
52       EEFmap.insert(std::pair<Key, EndgameEvaluationFunction*>(k, f));
53   }
54 }
55
56
57 ////
58 //// Functions
59 ////
60
61 /// MaterialInfo::init() is called during program initialization. It
62 /// precomputes material hash keys for a few basic endgames, in order
63 /// to make it easy to recognize such endgames when they occur.
64
65 void MaterialInfo::init() {
66
67   typedef Key ZM[2][8][16];
68   const ZM& z = Position::zobMaterial;
69
70   static const Color W = WHITE;
71   static const Color B = BLACK;
72   
73   EEFAdd(z[W][PAWN][1], &EvaluateKPK);
74   EEFAdd(z[B][PAWN][1], &EvaluateKKP);
75
76   EEFAdd(z[W][BISHOP][1] ^ z[W][KNIGHT][1], &EvaluateKBNK);
77   EEFAdd(z[B][BISHOP][1] ^ z[B][KNIGHT][1], &EvaluateKKBN);
78   EEFAdd(z[W][ROOK][1]   ^ z[B][PAWN][1],   &EvaluateKRKP);
79   EEFAdd(z[W][PAWN][1]   ^ z[B][ROOK][1],   &EvaluateKPKR);
80   EEFAdd(z[W][ROOK][1]   ^ z[B][BISHOP][1], &EvaluateKRKB);
81   EEFAdd(z[W][BISHOP][1] ^ z[B][ROOK][1],   &EvaluateKBKR);
82   EEFAdd(z[W][ROOK][1]   ^ z[B][KNIGHT][1], &EvaluateKRKN);
83   EEFAdd(z[W][KNIGHT][1] ^ z[B][ROOK][1],   &EvaluateKNKR);
84   EEFAdd(z[W][QUEEN][1]  ^ z[B][ROOK][1],   &EvaluateKQKR);
85   EEFAdd(z[W][ROOK][1]   ^ z[B][QUEEN][1],  &EvaluateKRKQ);
86
87   KRPKRMaterialKey = z[W][ROOK][1]
88                    ^ z[W][PAWN][1]
89                    ^ z[B][ROOK][1];
90
91   KRKRPMaterialKey = z[W][ROOK][1]
92                    ^ z[B][ROOK][1]
93                    ^ z[B][PAWN][1];
94
95   KRPPKRPMaterialKey =
96     z[W][ROOK][1] ^
97     z[W][PAWN][1] ^
98     z[W][PAWN][2] ^
99     z[B][ROOK][1] ^
100     z[B][PAWN][1];
101   KRPKRPPMaterialKey =
102     z[W][ROOK][1] ^
103     z[W][PAWN][1] ^
104     z[B][ROOK][1] ^
105     z[B][PAWN][1] ^
106     z[B][PAWN][2];
107   KNNKMaterialKey =
108     z[W][KNIGHT][1] ^
109     z[W][KNIGHT][2];
110   KKNNMaterialKey =
111     z[B][KNIGHT][1] ^
112     z[B][KNIGHT][2];
113   KBPKBMaterialKey =
114     z[W][BISHOP][1] ^
115     z[W][PAWN][1] ^
116     z[B][BISHOP][1];
117   KBKBPMaterialKey =
118     z[W][BISHOP][1] ^
119     z[B][BISHOP][1] ^
120     z[B][PAWN][1];
121   KBPKNMaterialKey =
122     z[W][BISHOP][1] ^
123     z[W][PAWN][1] ^
124     z[B][KNIGHT][1];
125   KNKBPMaterialKey =
126     z[W][KNIGHT][1] ^
127     z[B][BISHOP][1] ^
128     z[B][PAWN][1];
129   KNPKMaterialKey =
130     z[W][KNIGHT][1] ^
131     z[W][PAWN][1];
132   KKNPMaterialKey =
133     z[B][KNIGHT][1] ^
134     z[B][PAWN][1];
135   KPKPMaterialKey =
136     z[W][PAWN][1] ^
137     z[B][PAWN][1];
138
139
140 }
141
142
143 /// Constructor for the MaterialInfoTable class.
144
145 MaterialInfoTable::MaterialInfoTable(unsigned numOfEntries) {
146
147   size = numOfEntries;
148   entries = new MaterialInfo[size];
149   if (!entries)
150   {
151       std::cerr << "Failed to allocate " << (numOfEntries * sizeof(MaterialInfo))
152                 << " bytes for material hash table." << std::endl;
153       exit(EXIT_FAILURE);
154   }
155   clear();
156 }
157
158
159 /// Destructor for the MaterialInfoTable class.
160
161 MaterialInfoTable::~MaterialInfoTable() {
162
163   delete [] entries;
164 }
165
166
167 /// MaterialInfoTable::clear() clears a material hash table by setting
168 /// all entries to 0.
169
170 void MaterialInfoTable::clear() {
171
172   memset(entries, 0, size * sizeof(MaterialInfo));
173 }
174
175
176 /// MaterialInfoTable::get_material_info() takes a position object as input,
177 /// computes or looks up a MaterialInfo object, and returns a pointer to it.
178 /// If the material configuration is not already present in the table, it
179 /// is stored there, so we don't have to recompute everything when the
180 /// same material configuration occurs again.
181
182 MaterialInfo *MaterialInfoTable::get_material_info(const Position &pos) {
183
184   Key key = pos.get_material_key();
185   int index = key & (size - 1);
186   MaterialInfo *mi = entries + index;
187
188   // If mi->key matches the position's material hash key, it means that we
189   // have analysed this material configuration before, and we can simply
190   // return the information we found the last time instead of recomputing it:
191   if(mi->key == key)
192     return mi;
193
194   // Clear the MaterialInfo object, and set its key:
195   mi->clear();
196   mi->key = key;
197
198   // A special case before looking for a specialized evaluation function:
199   // KNN vs K is a draw:
200   if (key == KNNKMaterialKey || key == KKNNMaterialKey)
201   {
202     mi->factor[WHITE] = mi->factor[BLACK] = 0;
203     return mi;
204   }
205
206   // Let's look if we have a specialized evaluation function for this
207   // particular material configuration
208   if (EEFmap.find(key) != EEFmap.end())
209   {
210       mi->evaluationFunction = EEFmap[key];
211       return mi;
212   }
213   else if (   pos.non_pawn_material(BLACK) == Value(0)
214            && pos.piece_count(BLACK, PAWN) == 0
215            && pos.non_pawn_material(WHITE) >= RookValueEndgame)
216   {
217       mi->evaluationFunction = &EvaluateKXK;
218       return mi;
219   }
220   else if (   pos.non_pawn_material(WHITE) == Value(0)
221            && pos.piece_count(WHITE, PAWN) == 0
222            && pos.non_pawn_material(BLACK) >= RookValueEndgame)
223   {
224       mi->evaluationFunction = &EvaluateKKX;
225       return mi;
226   }
227
228   // OK, we didn't find any special evaluation function for the current
229   // material configuration. Is there a suitable scaling function?
230   //
231   // The code below is rather messy, and it could easily get worse later,
232   // if we decide to add more special cases.  We face problems when there
233   // are several conflicting applicable scaling functions and we need to
234   // decide which one to use.
235
236   if(key == KRPKRMaterialKey) {
237     mi->scalingFunction[WHITE] = &ScaleKRPKR;
238     return mi;
239   }
240   if(key == KRKRPMaterialKey) {
241     mi->scalingFunction[BLACK] = &ScaleKRKRP;
242     return mi;
243   }
244   if(key == KRPPKRPMaterialKey) {
245     mi->scalingFunction[WHITE] = &ScaleKRPPKRP;
246     return mi;
247   }
248   else if(key == KRPKRPPMaterialKey) {
249     mi->scalingFunction[BLACK] = &ScaleKRPKRPP;
250     return mi;
251   }
252   if(key == KBPKBMaterialKey) {
253     mi->scalingFunction[WHITE] = &ScaleKBPKB;
254     return mi;
255   }
256   if(key == KBKBPMaterialKey) {
257     mi->scalingFunction[BLACK] = &ScaleKBKBP;
258     return mi;
259   }
260   if(key == KBPKNMaterialKey) {
261     mi->scalingFunction[WHITE] = &ScaleKBPKN;
262     return mi;
263   }
264   if(key == KNKBPMaterialKey) {
265     mi->scalingFunction[BLACK] = &ScaleKNKBP;
266     return mi;
267   }
268   if(key == KNPKMaterialKey) {
269     mi->scalingFunction[WHITE] = &ScaleKNPK;
270     return mi;
271   }
272   if(key == KKNPMaterialKey) {
273     mi->scalingFunction[BLACK] = &ScaleKKNP;
274     return mi;
275   }
276
277   if(pos.non_pawn_material(WHITE) == BishopValueMidgame &&
278      pos.piece_count(WHITE, BISHOP) == 1 && pos.piece_count(WHITE, PAWN) >= 1)
279     mi->scalingFunction[WHITE] = &ScaleKBPK;
280   if(pos.non_pawn_material(BLACK) == BishopValueMidgame &&
281      pos.piece_count(BLACK, BISHOP) == 1 && pos.piece_count(BLACK, PAWN) >= 1)
282     mi->scalingFunction[BLACK] = &ScaleKKBP;
283
284   if(pos.piece_count(WHITE, PAWN) == 0 &&
285      pos.non_pawn_material(WHITE) == QueenValueMidgame &&
286      pos.piece_count(WHITE, QUEEN) == 1 &&
287      pos.piece_count(BLACK, ROOK) == 1 && pos.piece_count(BLACK, PAWN) >= 1)
288     mi->scalingFunction[WHITE] = &ScaleKQKRP;
289   else if(pos.piece_count(BLACK, PAWN) == 0 &&
290           pos.non_pawn_material(BLACK) == QueenValueMidgame &&
291           pos.piece_count(BLACK, QUEEN) == 1 &&
292           pos.piece_count(WHITE, ROOK) == 1 && pos.piece_count(WHITE, PAWN) >= 1)
293     mi->scalingFunction[BLACK] = &ScaleKRPKQ;
294
295   if(pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) == Value(0)) {
296     if(pos.piece_count(BLACK, PAWN) == 0) {
297       assert(pos.piece_count(WHITE, PAWN) >= 2);
298       mi->scalingFunction[WHITE] = &ScaleKPsK;
299     }
300     else if(pos.piece_count(WHITE, PAWN) == 0) {
301       assert(pos.piece_count(BLACK, PAWN) >= 2);
302       mi->scalingFunction[BLACK] = &ScaleKKPs;
303     }
304     else if(pos.piece_count(WHITE, PAWN) == 1 && pos.piece_count(BLACK, PAWN) == 1) {
305       mi->scalingFunction[WHITE] = &ScaleKPKPw;
306       mi->scalingFunction[BLACK] = &ScaleKPKPb;
307     }
308   }
309
310   // Evaluate the material balance.
311
312   Color c;
313   int sign;
314   Value egValue = Value(0), mgValue = Value(0);
315
316   for(c = WHITE, sign = 1; c <= BLACK; c++, sign = -sign) {
317
318     // No pawns makes it difficult to win, even with a material advantage:
319     if(pos.piece_count(c, PAWN) == 0 &&
320        pos.non_pawn_material(c) - pos.non_pawn_material(opposite_color(c))
321        <= BishopValueMidgame) {
322       if(pos.non_pawn_material(c) == pos.non_pawn_material(opposite_color(c)))
323         mi->factor[c] = 0;
324       else if(pos.non_pawn_material(c) < RookValueMidgame)
325         mi->factor[c] = 0;
326       else {
327         switch(pos.piece_count(c, BISHOP)) {
328         case 2:
329           mi->factor[c] = 32; break;
330         case 1:
331           mi->factor[c] = 12; break;
332         case 0:
333           mi->factor[c] = 6; break;
334         }
335       }
336     }
337
338     // Bishop pair:
339     if(pos.piece_count(c, BISHOP) >= 2) {
340       mgValue += sign * BishopPairMidgameBonus;
341       egValue += sign * BishopPairEndgameBonus;
342     }
343
344     // Knights are stronger when there are many pawns on the board.  The
345     // formula is taken from Larry Kaufman's paper "The Evaluation of Material
346     // Imbalances in Chess":
347     // http://mywebpages.comcast.net/danheisman/Articles/evaluation_of_material_imbalance.htm
348     mgValue += sign * Value(pos.piece_count(c, KNIGHT)*(pos.piece_count(c, PAWN)-5)*16);
349     egValue += sign * Value(pos.piece_count(c, KNIGHT)*(pos.piece_count(c, PAWN)-5)*16);
350
351     // Redundancy of major pieces, again based on Kaufman's paper:
352     if(pos.piece_count(c, ROOK) >= 1) {
353       Value v = Value((pos.piece_count(c, ROOK) - 1) * 32 + pos.piece_count(c, QUEEN) * 16);
354       mgValue -= sign * v;
355       egValue -= sign * v;
356     }
357
358   }
359
360   mi->mgValue = int16_t(mgValue);
361   mi->egValue = int16_t(egValue);
362
363   return mi;
364 }