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