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 KNNKMaterialKey, KKNNMaterialKey;
48 std::map<Key, EndgameEvaluationFunction*> EEFmap;
49 std::map<Key, ScalingInfo> ESFmap;
51 void add(Key k, EndgameEvaluationFunction* f) {
53 EEFmap.insert(std::pair<Key, EndgameEvaluationFunction*>(k, f));
56 void add(Key k, Color c, ScalingFunction* f) {
58 ScalingInfo s = {c, f};
59 ESFmap.insert(std::pair<Key, ScalingInfo>(k, s));
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.
73 void MaterialInfo::init() {
75 typedef Key ZM[2][8][16];
76 const ZM& z = Position::zobMaterial;
78 static const Color W = WHITE;
79 static const Color B = BLACK;
81 KNNKMaterialKey = z[W][KNIGHT][1] ^ z[W][KNIGHT][2];
82 KKNNMaterialKey = z[B][KNIGHT][1] ^ z[B][KNIGHT][2];
84 add(z[W][PAWN][1], &EvaluateKPK);
85 add(z[B][PAWN][1], &EvaluateKKP);
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);
98 add(z[W][KNIGHT][1] ^ z[W][PAWN][1], W, &ScaleKNPK);
99 add(z[B][KNIGHT][1] ^ z[B][PAWN][1], B, &ScaleKKNP);
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);
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);
113 /// Constructor for the MaterialInfoTable class
115 MaterialInfoTable::MaterialInfoTable(unsigned int numOfEntries) {
118 entries = new MaterialInfo[size];
121 std::cerr << "Failed to allocate " << (numOfEntries * sizeof(MaterialInfo))
122 << " bytes for material hash table." << std::endl;
129 /// Destructor for the MaterialInfoTable class
131 MaterialInfoTable::~MaterialInfoTable() {
137 /// MaterialInfoTable::clear() clears a material hash table by setting
138 /// all entries to 0.
140 void MaterialInfoTable::clear() {
142 memset(entries, 0, size * sizeof(MaterialInfo));
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.
152 MaterialInfo *MaterialInfoTable::get_material_info(const Position& pos) {
154 Key key = pos.get_material_key();
155 int index = key & (size - 1);
156 MaterialInfo* mi = entries + index;
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:
164 // Clear the MaterialInfo object, and set its key:
168 // A special case before looking for a specialized evaluation function
169 // KNN vs K is a draw
170 if (key == KNNKMaterialKey || key == KKNNMaterialKey)
172 mi->factor[WHITE] = mi->factor[BLACK] = 0;
176 // Let's look if we have a specialized evaluation function for this
177 // particular material configuration
178 if (EEFmap.find(key) != EEFmap.end())
180 mi->evaluationFunction = EEFmap[key];
183 else if ( pos.non_pawn_material(BLACK) == Value(0)
184 && pos.piece_count(BLACK, PAWN) == 0
185 && pos.non_pawn_material(WHITE) >= RookValueEndgame)
187 mi->evaluationFunction = &EvaluateKXK;
190 else if ( pos.non_pawn_material(WHITE) == Value(0)
191 && pos.piece_count(WHITE, PAWN) == 0
192 && pos.non_pawn_material(BLACK) >= RookValueEndgame)
194 mi->evaluationFunction = &EvaluateKKX;
198 // OK, we didn't find any special evaluation function for the current
199 // material configuration. Is there a suitable scaling function?
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.
206 if (ESFmap.find(key) != ESFmap.end())
208 mi->scalingFunction[ESFmap[key].col] = ESFmap[key].fun;
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;
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;
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;
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;
236 if (pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) == Value(0))
238 if (pos.piece_count(BLACK, PAWN) == 0)
240 assert(pos.piece_count(WHITE, PAWN) >= 2);
241 mi->scalingFunction[WHITE] = &ScaleKPsK;
243 else if (pos.piece_count(WHITE, PAWN) == 0)
245 assert(pos.piece_count(BLACK, PAWN) >= 2);
246 mi->scalingFunction[BLACK] = &ScaleKKPs;
248 else if (pos.piece_count(WHITE, PAWN) == 1 && pos.piece_count(BLACK, PAWN) == 1)
250 mi->scalingFunction[WHITE] = &ScaleKPKPw;
251 mi->scalingFunction[BLACK] = &ScaleKPKPb;
255 // Evaluate the material balance
259 Value egValue = Value(0);
260 Value mgValue = Value(0);
262 for (c = WHITE, sign = 1; c <= BLACK; c++, sign = -sign)
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)
268 if ( pos.non_pawn_material(c) == pos.non_pawn_material(opposite_color(c))
269 || pos.non_pawn_material(c) < RookValueMidgame)
273 switch (pos.piece_count(c, BISHOP)) {
288 if (pos.piece_count(c, BISHOP) >= 2)
290 mgValue += sign * BishopPairMidgameBonus;
291 egValue += sign * BishopPairEndgameBonus;
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);
301 // Redundancy of major pieces, again based on Kaufman's paper:
302 if (pos.piece_count(c, ROOK) >= 1)
304 Value v = Value((pos.piece_count(c, ROOK) - 1) * 32 + pos.piece_count(c, QUEEN) * 16);
310 mi->mgValue = int16_t(mgValue);
311 mi->egValue = int16_t(egValue);