2 Glaurung, a UCI chess playing engine.
3 Copyright (C) 2004-2008 Tord Romstad
5 Glaurung is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Glaurung is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 //// Local definitions
35 const Value BishopPairMidgameBonus = Value(100);
36 const Value BishopPairEndgameBonus = Value(100);
38 Key KPKMaterialKey, KKPMaterialKey;
39 Key KBNKMaterialKey, KKBNMaterialKey;
40 Key KRKPMaterialKey, KPKRMaterialKey;
41 Key KRKBMaterialKey, KBKRMaterialKey;
42 Key KRKNMaterialKey, KNKRMaterialKey;
43 Key KQKRMaterialKey, KRKQMaterialKey;
44 Key KRPKRMaterialKey, KRKRPMaterialKey;
45 Key KRPPKRPMaterialKey, KRPKRPPMaterialKey;
46 Key KNNKMaterialKey, KKNNMaterialKey;
47 Key KBPKBMaterialKey, KBKBPMaterialKey;
48 Key KBPKNMaterialKey, KNKBPMaterialKey;
49 Key KNPKMaterialKey, KKNPMaterialKey;
59 /// MaterialInfo::init() is called during program initialization. It
60 /// precomputes material hash keys for a few basic endgames, in order
61 /// to make it easy to recognize such endgames when they occur.
63 void MaterialInfo::init() {
64 KPKMaterialKey = Position::zobMaterial[WHITE][PAWN][1];
65 KKPMaterialKey = Position::zobMaterial[BLACK][PAWN][1];
67 Position::zobMaterial[WHITE][BISHOP][1] ^
68 Position::zobMaterial[WHITE][KNIGHT][1];
70 Position::zobMaterial[BLACK][BISHOP][1] ^
71 Position::zobMaterial[BLACK][KNIGHT][1];
73 Position::zobMaterial[WHITE][ROOK][1] ^
74 Position::zobMaterial[BLACK][PAWN][1];
76 Position::zobMaterial[WHITE][PAWN][1] ^
77 Position::zobMaterial[BLACK][ROOK][1];
79 Position::zobMaterial[WHITE][ROOK][1] ^
80 Position::zobMaterial[BLACK][BISHOP][1];
82 Position::zobMaterial[WHITE][BISHOP][1] ^
83 Position::zobMaterial[BLACK][ROOK][1];
85 Position::zobMaterial[WHITE][ROOK][1] ^
86 Position::zobMaterial[BLACK][KNIGHT][1];
88 Position::zobMaterial[WHITE][KNIGHT][1] ^
89 Position::zobMaterial[BLACK][ROOK][1];
91 Position::zobMaterial[WHITE][QUEEN][1] ^
92 Position::zobMaterial[BLACK][ROOK][1];
94 Position::zobMaterial[WHITE][ROOK][1] ^
95 Position::zobMaterial[BLACK][QUEEN][1];
97 Position::zobMaterial[WHITE][ROOK][1] ^
98 Position::zobMaterial[WHITE][PAWN][1] ^
99 Position::zobMaterial[BLACK][ROOK][1];
101 Position::zobMaterial[WHITE][ROOK][1] ^
102 Position::zobMaterial[BLACK][ROOK][1] ^
103 Position::zobMaterial[BLACK][PAWN][1];
105 Position::zobMaterial[WHITE][ROOK][1] ^
106 Position::zobMaterial[WHITE][PAWN][1] ^
107 Position::zobMaterial[WHITE][PAWN][2] ^
108 Position::zobMaterial[BLACK][ROOK][1] ^
109 Position::zobMaterial[BLACK][PAWN][1];
111 Position::zobMaterial[WHITE][ROOK][1] ^
112 Position::zobMaterial[WHITE][PAWN][1] ^
113 Position::zobMaterial[BLACK][ROOK][1] ^
114 Position::zobMaterial[BLACK][PAWN][1] ^
115 Position::zobMaterial[BLACK][PAWN][2];
117 Position::zobMaterial[WHITE][KNIGHT][1] ^
118 Position::zobMaterial[WHITE][KNIGHT][2];
120 Position::zobMaterial[BLACK][KNIGHT][1] ^
121 Position::zobMaterial[BLACK][KNIGHT][2];
123 Position::zobMaterial[WHITE][BISHOP][1] ^
124 Position::zobMaterial[WHITE][PAWN][1] ^
125 Position::zobMaterial[BLACK][BISHOP][1];
127 Position::zobMaterial[WHITE][BISHOP][1] ^
128 Position::zobMaterial[BLACK][BISHOP][1] ^
129 Position::zobMaterial[BLACK][PAWN][1];
131 Position::zobMaterial[WHITE][BISHOP][1] ^
132 Position::zobMaterial[WHITE][PAWN][1] ^
133 Position::zobMaterial[BLACK][KNIGHT][1];
135 Position::zobMaterial[WHITE][KNIGHT][1] ^
136 Position::zobMaterial[BLACK][BISHOP][1] ^
137 Position::zobMaterial[BLACK][PAWN][1];
139 Position::zobMaterial[WHITE][KNIGHT][1] ^
140 Position::zobMaterial[WHITE][PAWN][1];
142 Position::zobMaterial[BLACK][KNIGHT][1] ^
143 Position::zobMaterial[BLACK][PAWN][1];
145 Position::zobMaterial[WHITE][PAWN][1] ^
146 Position::zobMaterial[BLACK][PAWN][1];
150 /// Constructor for the MaterialInfoTable class.
152 MaterialInfoTable::MaterialInfoTable(unsigned numOfEntries) {
154 entries = new MaterialInfo[size];
155 if(entries == NULL) {
156 std::cerr << "Failed to allocate " << (numOfEntries * sizeof(MaterialInfo))
157 << " bytes for material hash table." << std::endl;
164 /// Destructor for the MaterialInfoTable class.
166 MaterialInfoTable::~MaterialInfoTable() {
171 /// MaterialInfoTable::clear() clears a material hash table by setting
172 /// all entries to 0.
174 void MaterialInfoTable::clear() {
175 memset(entries, 0, size * sizeof(MaterialInfo));
179 /// MaterialInfoTable::get_material_info() takes a position object as input,
180 /// computes or looks up a MaterialInfo object, and returns a pointer to it.
181 /// If the material configuration is not already present in the table, it
182 /// is stored there, so we don't have to recompute everything when the
183 /// same material configuration occurs again.
185 MaterialInfo *MaterialInfoTable::get_material_info(const Position &pos) {
186 Key key = pos.get_material_key();
187 int index = key & (size - 1);
188 MaterialInfo *mi = entries + index;
190 // If mi->key matches the position's material hash key, it means that we
191 // have analysed this material configuration before, and we can simply
192 // return the information we found the last time instead of recomputing it:
196 // Clear the MaterialInfo object, and set its key:
200 // A special case before looking for a specialized evaluation function:
201 // KNN vs K is a draw:
202 if(key == KNNKMaterialKey || key == KKNNMaterialKey) {
203 mi->factor[WHITE] = mi->factor[BLACK] = 0;
207 // Let's look if we have a specialized evaluation function for this
208 // particular material configuration:
209 if(key == KPKMaterialKey) {
210 mi->evaluationFunction = &EvaluateKPK;
213 else if(key == KKPMaterialKey) {
214 mi->evaluationFunction = &EvaluateKKP;
217 else if(key == KBNKMaterialKey) {
218 mi->evaluationFunction = &EvaluateKBNK;
221 else if(key == KKBNMaterialKey) {
222 mi->evaluationFunction = &EvaluateKKBN;
225 else if(key == KRKPMaterialKey) {
226 mi->evaluationFunction = &EvaluateKRKP;
229 else if(key == KPKRMaterialKey) {
230 mi->evaluationFunction = &EvaluateKPKR;
233 else if(key == KRKBMaterialKey) {
234 mi->evaluationFunction = &EvaluateKRKB;
237 else if(key == KBKRMaterialKey) {
238 mi->evaluationFunction = &EvaluateKBKR;
241 else if(key == KRKNMaterialKey) {
242 mi->evaluationFunction = &EvaluateKRKN;
245 else if(key == KNKRMaterialKey) {
246 mi->evaluationFunction = &EvaluateKNKR;
249 else if(key == KQKRMaterialKey) {
250 mi->evaluationFunction = &EvaluateKQKR;
253 else if(key == KRKQMaterialKey) {
254 mi->evaluationFunction = &EvaluateKRKQ;
257 else if(pos.non_pawn_material(BLACK) == Value(0) &&
258 pos.pawn_count(BLACK) == 0 &&
259 pos.non_pawn_material(WHITE) >= RookValueEndgame) {
260 mi->evaluationFunction = &EvaluateKXK;
263 else if(pos.non_pawn_material(WHITE) == Value(0) &&
264 pos.pawn_count(WHITE) == 0 &&
265 pos.non_pawn_material(BLACK) >= RookValueEndgame) {
266 mi->evaluationFunction = &EvaluateKKX;
270 // OK, we didn't find any special evaluation function for the current
271 // material configuration. Is there a suitable scaling function?
273 // The code below is rather messy, and it could easily get worse later,
274 // if we decide to add more special cases. We face problems when there
275 // are several conflicting applicable scaling functions and we need to
276 // decide which one to use.
278 if(key == KRPKRMaterialKey) {
279 mi->scalingFunction[WHITE] = &ScaleKRPKR;
282 if(key == KRKRPMaterialKey) {
283 mi->scalingFunction[BLACK] = &ScaleKRKRP;
286 if(key == KRPPKRPMaterialKey) {
287 mi->scalingFunction[WHITE] = &ScaleKRPPKRP;
290 else if(key == KRPKRPPMaterialKey) {
291 mi->scalingFunction[BLACK] = &ScaleKRPKRPP;
294 if(key == KBPKBMaterialKey) {
295 mi->scalingFunction[WHITE] = &ScaleKBPKB;
298 if(key == KBKBPMaterialKey) {
299 mi->scalingFunction[BLACK] = &ScaleKBKBP;
302 if(key == KBPKNMaterialKey) {
303 mi->scalingFunction[WHITE] = &ScaleKBPKN;
306 if(key == KNKBPMaterialKey) {
307 mi->scalingFunction[BLACK] = &ScaleKNKBP;
310 if(key == KNPKMaterialKey) {
311 mi->scalingFunction[WHITE] = &ScaleKNPK;
314 if(key == KKNPMaterialKey) {
315 mi->scalingFunction[BLACK] = &ScaleKKNP;
319 if(pos.non_pawn_material(WHITE) == BishopValueMidgame &&
320 pos.bishop_count(WHITE) == 1 && pos.pawn_count(WHITE) >= 1)
321 mi->scalingFunction[WHITE] = &ScaleKBPK;
322 if(pos.non_pawn_material(BLACK) == BishopValueMidgame &&
323 pos.bishop_count(BLACK) == 1 && pos.pawn_count(BLACK) >= 1)
324 mi->scalingFunction[BLACK] = &ScaleKKBP;
326 if(pos.pawn_count(WHITE) == 0 &&
327 pos.non_pawn_material(WHITE) == QueenValueMidgame &&
328 pos.queen_count(WHITE) == 1 &&
329 pos.rook_count(BLACK) == 1 && pos.pawn_count(BLACK) >= 1)
330 mi->scalingFunction[WHITE] = &ScaleKQKRP;
331 else if(pos.pawn_count(BLACK) == 0 &&
332 pos.non_pawn_material(BLACK) == QueenValueMidgame &&
333 pos.queen_count(BLACK) == 1 &&
334 pos.rook_count(WHITE) == 1 && pos.pawn_count(WHITE) >= 1)
335 mi->scalingFunction[BLACK] = &ScaleKRPKQ;
337 if(pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) == Value(0)) {
338 if(pos.pawn_count(BLACK) == 0) {
339 assert(pos.pawn_count(WHITE) >= 2);
340 mi->scalingFunction[WHITE] = &ScaleKPsK;
342 else if(pos.pawn_count(WHITE) == 0) {
343 assert(pos.pawn_count(BLACK) >= 2);
344 mi->scalingFunction[BLACK] = &ScaleKKPs;
346 else if(pos.pawn_count(WHITE) == 1 && pos.pawn_count(BLACK) == 1) {
347 mi->scalingFunction[WHITE] = &ScaleKPKPw;
348 mi->scalingFunction[BLACK] = &ScaleKPKPb;
352 // Evaluate the material balance.
356 Value egValue = Value(0), mgValue = Value(0);
358 for(c = WHITE, sign = 1; c <= BLACK; c++, sign = -sign) {
360 // No pawns makes it difficult to win, even with a material advantage:
361 if(pos.pawn_count(c) == 0 &&
362 pos.non_pawn_material(c) - pos.non_pawn_material(opposite_color(c))
363 <= BishopValueMidgame) {
364 if(pos.non_pawn_material(c) == pos.non_pawn_material(opposite_color(c)))
366 else if(pos.non_pawn_material(c) < RookValueMidgame)
369 switch(pos.bishop_count(c)) {
371 mi->factor[c] = 32; break;
373 mi->factor[c] = 12; break;
375 mi->factor[c] = 6; break;
381 if(pos.bishop_count(c) >= 2) {
382 mgValue += sign * BishopPairMidgameBonus;
383 egValue += sign * BishopPairEndgameBonus;
386 // Knights are stronger when there are many pawns on the board. The
387 // formula is taken from Larry Kaufman's paper "The Evaluation of Material
388 // Imbalances in Chess":
389 // http://mywebpages.comcast.net/danheisman/Articles/evaluation_of_material_imbalance.htm
390 mgValue += sign * Value(pos.knight_count(c)*(pos.pawn_count(c)-5)*16);
391 egValue += sign * Value(pos.knight_count(c)*(pos.pawn_count(c)-5)*16);
393 // Redundancy of major pieces, again based on Kaufman's paper:
394 if(pos.rook_count(c) >= 1) {
395 Value v = Value((pos.rook_count(c) - 1) * 32 + pos.queen_count(c) * 16);
402 mi->mgValue = int16_t(mgValue);
403 mi->egValue = int16_t(egValue);