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/>.
32 //// Local definitions
37 const Value BishopPairMidgameBonus = Value(100);
38 const Value BishopPairEndgameBonus = Value(100);
40 Key KRPKRMaterialKey, KRKRPMaterialKey;
41 Key KNNKMaterialKey, KKNNMaterialKey;
42 Key KBPKBMaterialKey, KBKBPMaterialKey;
43 Key KBPKNMaterialKey, KNKBPMaterialKey;
44 Key KNPKMaterialKey, KKNPMaterialKey;
46 Key KRPPKRPMaterialKey, KRPKRPPMaterialKey;
48 std::map<Key, EndgameEvaluationFunction*> EEFmap;
50 void EEFAdd(Key k, EndgameEvaluationFunction* f) {
52 EEFmap.insert(std::pair<Key, EndgameEvaluationFunction*>(k, f));
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.
65 void MaterialInfo::init() {
67 typedef Key ZM[2][8][16];
68 const ZM& z = Position::zobMaterial;
70 static const Color W = WHITE;
71 static const Color B = BLACK;
73 EEFAdd(z[W][PAWN][1], &EvaluateKPK);
74 EEFAdd(z[B][PAWN][1], &EvaluateKKP);
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);
87 KRPKRMaterialKey = z[W][ROOK][1]
91 KRKRPMaterialKey = z[W][ROOK][1]
143 /// Constructor for the MaterialInfoTable class.
145 MaterialInfoTable::MaterialInfoTable(unsigned numOfEntries) {
148 entries = new MaterialInfo[size];
151 std::cerr << "Failed to allocate " << (numOfEntries * sizeof(MaterialInfo))
152 << " bytes for material hash table." << std::endl;
159 /// Destructor for the MaterialInfoTable class.
161 MaterialInfoTable::~MaterialInfoTable() {
167 /// MaterialInfoTable::clear() clears a material hash table by setting
168 /// all entries to 0.
170 void MaterialInfoTable::clear() {
172 memset(entries, 0, size * sizeof(MaterialInfo));
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.
182 MaterialInfo *MaterialInfoTable::get_material_info(const Position &pos) {
184 Key key = pos.get_material_key();
185 int index = key & (size - 1);
186 MaterialInfo *mi = entries + index;
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:
194 // Clear the MaterialInfo object, and set its key:
198 // A special case before looking for a specialized evaluation function:
199 // KNN vs K is a draw:
200 if (key == KNNKMaterialKey || key == KKNNMaterialKey)
202 mi->factor[WHITE] = mi->factor[BLACK] = 0;
206 // Let's look if we have a specialized evaluation function for this
207 // particular material configuration
208 if (EEFmap.find(key) != EEFmap.end())
210 mi->evaluationFunction = EEFmap[key];
213 else if ( pos.non_pawn_material(BLACK) == Value(0)
214 && pos.piece_count(BLACK, PAWN) == 0
215 && pos.non_pawn_material(WHITE) >= RookValueEndgame)
217 mi->evaluationFunction = &EvaluateKXK;
220 else if ( pos.non_pawn_material(WHITE) == Value(0)
221 && pos.piece_count(WHITE, PAWN) == 0
222 && pos.non_pawn_material(BLACK) >= RookValueEndgame)
224 mi->evaluationFunction = &EvaluateKKX;
228 // OK, we didn't find any special evaluation function for the current
229 // material configuration. Is there a suitable scaling function?
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.
236 if(key == KRPKRMaterialKey) {
237 mi->scalingFunction[WHITE] = &ScaleKRPKR;
240 if(key == KRKRPMaterialKey) {
241 mi->scalingFunction[BLACK] = &ScaleKRKRP;
244 if(key == KRPPKRPMaterialKey) {
245 mi->scalingFunction[WHITE] = &ScaleKRPPKRP;
248 else if(key == KRPKRPPMaterialKey) {
249 mi->scalingFunction[BLACK] = &ScaleKRPKRPP;
252 if(key == KBPKBMaterialKey) {
253 mi->scalingFunction[WHITE] = &ScaleKBPKB;
256 if(key == KBKBPMaterialKey) {
257 mi->scalingFunction[BLACK] = &ScaleKBKBP;
260 if(key == KBPKNMaterialKey) {
261 mi->scalingFunction[WHITE] = &ScaleKBPKN;
264 if(key == KNKBPMaterialKey) {
265 mi->scalingFunction[BLACK] = &ScaleKNKBP;
268 if(key == KNPKMaterialKey) {
269 mi->scalingFunction[WHITE] = &ScaleKNPK;
272 if(key == KKNPMaterialKey) {
273 mi->scalingFunction[BLACK] = &ScaleKKNP;
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;
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;
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;
300 else if(pos.piece_count(WHITE, PAWN) == 0) {
301 assert(pos.piece_count(BLACK, PAWN) >= 2);
302 mi->scalingFunction[BLACK] = &ScaleKKPs;
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;
310 // Evaluate the material balance.
314 Value egValue = Value(0), mgValue = Value(0);
316 for(c = WHITE, sign = 1; c <= BLACK; c++, sign = -sign) {
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)))
324 else if(pos.non_pawn_material(c) < RookValueMidgame)
327 switch(pos.piece_count(c, BISHOP)) {
329 mi->factor[c] = 32; break;
331 mi->factor[c] = 12; break;
333 mi->factor[c] = 6; break;
339 if(pos.piece_count(c, BISHOP) >= 2) {
340 mgValue += sign * BishopPairMidgameBonus;
341 egValue += sign * BishopPairEndgameBonus;
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);
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);
360 mi->mgValue = int16_t(mgValue);
361 mi->egValue = int16_t(egValue);