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/>.
33 //// Local definitions
38 const Value BishopPairMidgameBonus = Value(100);
39 const Value BishopPairEndgameBonus = Value(100);
41 Key KNNKMaterialKey, KKNNMaterialKey;
50 /// See header for a class description. It is declared here to avoid
51 /// to include <map> in the header file.
53 class EndgameFunctions {
57 EndgameEvaluationFunction* getEEF(Key key) const;
58 ScalingFunction* getESF(Key key, Color* c) const;
61 void add(Key k, EndgameEvaluationFunction* f);
62 void add(Key k, Color c, ScalingFunction* f);
70 std::map<Key, EndgameEvaluationFunction*> EEFmap;
71 std::map<Key, ScalingInfo> ESFmap;
80 /// Constructor for the MaterialInfoTable class
82 MaterialInfoTable::MaterialInfoTable(unsigned int numOfEntries) {
85 entries = new MaterialInfo[size];
86 funcs = new EndgameFunctions();
87 if (!entries || !funcs)
89 std::cerr << "Failed to allocate " << (numOfEntries * sizeof(MaterialInfo))
90 << " bytes for material hash table." << std::endl;
97 /// Destructor for the MaterialInfoTable class
99 MaterialInfoTable::~MaterialInfoTable() {
106 /// MaterialInfoTable::clear() clears a material hash table by setting
107 /// all entries to 0.
109 void MaterialInfoTable::clear() {
111 memset(entries, 0, size * sizeof(MaterialInfo));
115 /// MaterialInfoTable::get_material_info() takes a position object as input,
116 /// computes or looks up a MaterialInfo object, and returns a pointer to it.
117 /// If the material configuration is not already present in the table, it
118 /// is stored there, so we don't have to recompute everything when the
119 /// same material configuration occurs again.
121 MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
123 Key key = pos.get_material_key();
124 int index = key & (size - 1);
125 MaterialInfo* mi = entries + index;
127 // If mi->key matches the position's material hash key, it means that we
128 // have analysed this material configuration before, and we can simply
129 // return the information we found the last time instead of recomputing it.
133 // Clear the MaterialInfo object, and set its key
137 // A special case before looking for a specialized evaluation function
138 // KNN vs K is a draw.
139 if (key == KNNKMaterialKey || key == KKNNMaterialKey)
141 mi->factor[WHITE] = mi->factor[BLACK] = 0;
145 // Let's look if we have a specialized evaluation function for this
146 // particular material configuration.
147 if ((mi->evaluationFunction = funcs->getEEF(key)) != NULL)
150 else if ( pos.non_pawn_material(BLACK) == Value(0)
151 && pos.piece_count(BLACK, PAWN) == 0
152 && pos.non_pawn_material(WHITE) >= RookValueEndgame)
154 mi->evaluationFunction = &EvaluateKXK;
157 else if ( pos.non_pawn_material(WHITE) == Value(0)
158 && pos.piece_count(WHITE, PAWN) == 0
159 && pos.non_pawn_material(BLACK) >= RookValueEndgame)
161 mi->evaluationFunction = &EvaluateKKX;
165 // OK, we didn't find any special evaluation function for the current
166 // material configuration. Is there a suitable scaling function?
168 // The code below is rather messy, and it could easily get worse later,
169 // if we decide to add more special cases. We face problems when there
170 // are several conflicting applicable scaling functions and we need to
171 // decide which one to use.
175 if ((sf = funcs->getESF(key, &c)) != NULL)
177 mi->scalingFunction[c] = sf;
181 if ( pos.non_pawn_material(WHITE) == BishopValueMidgame
182 && pos.piece_count(WHITE, BISHOP) == 1
183 && pos.piece_count(WHITE, PAWN) >= 1)
184 mi->scalingFunction[WHITE] = &ScaleKBPK;
186 if ( pos.non_pawn_material(BLACK) == BishopValueMidgame
187 && pos.piece_count(BLACK, BISHOP) == 1
188 && pos.piece_count(BLACK, PAWN) >= 1)
189 mi->scalingFunction[BLACK] = &ScaleKKBP;
191 if ( pos.piece_count(WHITE, PAWN) == 0
192 && pos.non_pawn_material(WHITE) == QueenValueMidgame
193 && pos.piece_count(WHITE, QUEEN) == 1
194 && pos.piece_count(BLACK, ROOK) == 1
195 && pos.piece_count(BLACK, PAWN) >= 1)
196 mi->scalingFunction[WHITE] = &ScaleKQKRP;
198 else if ( pos.piece_count(BLACK, PAWN) == 0
199 && pos.non_pawn_material(BLACK) == QueenValueMidgame
200 && pos.piece_count(BLACK, QUEEN) == 1
201 && pos.piece_count(WHITE, ROOK) == 1
202 && pos.piece_count(WHITE, PAWN) >= 1)
203 mi->scalingFunction[BLACK] = &ScaleKRPKQ;
205 if (pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) == Value(0))
207 if (pos.piece_count(BLACK, PAWN) == 0)
209 assert(pos.piece_count(WHITE, PAWN) >= 2);
210 mi->scalingFunction[WHITE] = &ScaleKPsK;
212 else if (pos.piece_count(WHITE, PAWN) == 0)
214 assert(pos.piece_count(BLACK, PAWN) >= 2);
215 mi->scalingFunction[BLACK] = &ScaleKKPs;
217 else if (pos.piece_count(WHITE, PAWN) == 1 && pos.piece_count(BLACK, PAWN) == 1)
219 mi->scalingFunction[WHITE] = &ScaleKPKPw;
220 mi->scalingFunction[BLACK] = &ScaleKPKPb;
224 // Evaluate the material balance
227 Value egValue = Value(0);
228 Value mgValue = Value(0);
230 for (c = WHITE, sign = 1; c <= BLACK; c++, sign = -sign)
232 // No pawns makes it difficult to win, even with a material advantage
233 if ( pos.piece_count(c, PAWN) == 0
234 && pos.non_pawn_material(c) - pos.non_pawn_material(opposite_color(c)) <= BishopValueMidgame)
236 if ( pos.non_pawn_material(c) == pos.non_pawn_material(opposite_color(c))
237 || pos.non_pawn_material(c) < RookValueMidgame)
241 switch (pos.piece_count(c, BISHOP)) {
256 if (pos.piece_count(c, BISHOP) >= 2)
258 mgValue += sign * BishopPairMidgameBonus;
259 egValue += sign * BishopPairEndgameBonus;
262 // Knights are stronger when there are many pawns on the board. The
263 // formula is taken from Larry Kaufman's paper "The Evaluation of Material
264 // Imbalances in Chess":
265 // http://mywebpages.comcast.net/danheisman/Articles/evaluation_of_material_imbalance.htm
266 mgValue += sign * Value(pos.piece_count(c, KNIGHT)*(pos.piece_count(c, PAWN)-5)*16);
267 egValue += sign * Value(pos.piece_count(c, KNIGHT)*(pos.piece_count(c, PAWN)-5)*16);
269 // Redundancy of major pieces, again based on Kaufman's paper:
270 if (pos.piece_count(c, ROOK) >= 1)
272 Value v = Value((pos.piece_count(c, ROOK) - 1) * 32 + pos.piece_count(c, QUEEN) * 16);
277 mi->mgValue = int16_t(mgValue);
278 mi->egValue = int16_t(egValue);
283 /// EndgameFunctions member definitions. This class is used to store the maps
284 /// of end game and scaling functions that MaterialInfoTable will query for
285 /// each key. The maps are constant and are populated only at construction,
286 /// but are per-thread instead of globals to avoid expensive locks.
288 EndgameFunctions::EndgameFunctions() {
290 typedef Key ZM[2][8][16];
291 const ZM& z = Position::zobMaterial;
293 static const Color W = WHITE;
294 static const Color B = BLACK;
296 KNNKMaterialKey = z[W][KNIGHT][1] ^ z[W][KNIGHT][2];
297 KKNNMaterialKey = z[B][KNIGHT][1] ^ z[B][KNIGHT][2];
299 add(z[W][PAWN][1], &EvaluateKPK);
300 add(z[B][PAWN][1], &EvaluateKKP);
302 add(z[W][BISHOP][1] ^ z[W][KNIGHT][1], &EvaluateKBNK);
303 add(z[B][BISHOP][1] ^ z[B][KNIGHT][1], &EvaluateKKBN);
304 add(z[W][ROOK][1] ^ z[B][PAWN][1], &EvaluateKRKP);
305 add(z[W][PAWN][1] ^ z[B][ROOK][1], &EvaluateKPKR);
306 add(z[W][ROOK][1] ^ z[B][BISHOP][1], &EvaluateKRKB);
307 add(z[W][BISHOP][1] ^ z[B][ROOK][1], &EvaluateKBKR);
308 add(z[W][ROOK][1] ^ z[B][KNIGHT][1], &EvaluateKRKN);
309 add(z[W][KNIGHT][1] ^ z[B][ROOK][1], &EvaluateKNKR);
310 add(z[W][QUEEN][1] ^ z[B][ROOK][1], &EvaluateKQKR);
311 add(z[W][ROOK][1] ^ z[B][QUEEN][1], &EvaluateKRKQ);
313 add(z[W][KNIGHT][1] ^ z[W][PAWN][1], W, &ScaleKNPK);
314 add(z[B][KNIGHT][1] ^ z[B][PAWN][1], B, &ScaleKKNP);
316 add(z[W][ROOK][1] ^ z[W][PAWN][1] ^ z[B][ROOK][1] , W, &ScaleKRPKR);
317 add(z[W][ROOK][1] ^ z[B][ROOK][1] ^ z[B][PAWN][1] , B, &ScaleKRKRP);
318 add(z[W][BISHOP][1] ^ z[W][PAWN][1] ^ z[B][BISHOP][1], W, &ScaleKBPKB);
319 add(z[W][BISHOP][1] ^ z[B][BISHOP][1] ^ z[B][PAWN][1] , B, &ScaleKBKBP);
320 add(z[W][BISHOP][1] ^ z[W][PAWN][1] ^ z[B][KNIGHT][1], W, &ScaleKBPKN);
321 add(z[W][KNIGHT][1] ^ z[B][BISHOP][1] ^ z[B][PAWN][1] , B, &ScaleKNKBP);
323 add(z[W][ROOK][1] ^ z[W][PAWN][1] ^ z[W][PAWN][2] ^ z[B][ROOK][1] ^ z[B][PAWN][1], W, &ScaleKRPPKRP);
324 add(z[W][ROOK][1] ^ z[W][PAWN][1] ^ z[B][ROOK][1] ^ z[B][PAWN][1] ^ z[B][PAWN][2], B, &ScaleKRPKRPP);
327 void EndgameFunctions::add(Key k, EndgameEvaluationFunction* f) {
329 EEFmap.insert(std::pair<Key, EndgameEvaluationFunction*>(k, f));
332 void EndgameFunctions::add(Key k, Color c, ScalingFunction* f) {
334 ScalingInfo s = {c, f};
335 ESFmap.insert(std::pair<Key, ScalingInfo>(k, s));
338 EndgameEvaluationFunction* EndgameFunctions::getEEF(Key key) const {
340 std::map<Key, EndgameEvaluationFunction*>::const_iterator it(EEFmap.find(key));
341 return (it != EEFmap.end() ? it->second : NULL);
344 ScalingFunction* EndgameFunctions::getESF(Key key, Color* c) const {
346 std::map<Key, ScalingInfo>::const_iterator it(ESFmap.find(key));
347 if (it == ESFmap.end())
351 return it->second.fun;