2008-08-31 23:59:13 -06:00
|
|
|
/*
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
|
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
|
|
|
Copyright (C) 2008 Marco Costalba
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
2008-08-31 23:59:13 -06:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2008-09-23 16:32:53 -06:00
|
|
|
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
2008-08-31 23:59:13 -06:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2008-09-23 16:32:53 -06:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
////
|
|
|
|
//// Includes
|
|
|
|
////
|
|
|
|
|
|
|
|
#include <cassert>
|
2008-10-27 12:07:48 -06:00
|
|
|
#include <map>
|
2008-08-31 23:59:13 -06:00
|
|
|
|
|
|
|
#include "material.h"
|
|
|
|
|
|
|
|
|
|
|
|
////
|
|
|
|
//// Local definitions
|
|
|
|
////
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const Value BishopPairMidgameBonus = Value(100);
|
|
|
|
const Value BishopPairEndgameBonus = Value(100);
|
|
|
|
|
|
|
|
Key KRPKRMaterialKey, KRKRPMaterialKey;
|
2008-10-27 12:07:48 -06:00
|
|
|
Key KNNKMaterialKey, KKNNMaterialKey;
|
2008-08-31 23:59:13 -06:00
|
|
|
Key KBPKBMaterialKey, KBKBPMaterialKey;
|
|
|
|
Key KBPKNMaterialKey, KNKBPMaterialKey;
|
2008-10-27 12:07:48 -06:00
|
|
|
Key KNPKMaterialKey, KKNPMaterialKey;
|
2008-08-31 23:59:13 -06:00
|
|
|
Key KPKPMaterialKey;
|
2008-10-27 12:07:48 -06:00
|
|
|
Key KRPPKRPMaterialKey, KRPKRPPMaterialKey;
|
|
|
|
|
|
|
|
std::map<Key, EndgameEvaluationFunction*> EEFmap;
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2008-10-27 12:07:48 -06:00
|
|
|
void EEFAdd(Key k, EndgameEvaluationFunction* f) {
|
|
|
|
|
|
|
|
EEFmap.insert(std::pair<Key, EndgameEvaluationFunction*>(k, f));
|
|
|
|
}
|
2008-09-23 16:32:53 -06:00
|
|
|
}
|
2008-08-31 23:59:13 -06:00
|
|
|
|
|
|
|
|
|
|
|
////
|
|
|
|
//// Functions
|
|
|
|
////
|
|
|
|
|
2008-10-27 12:07:48 -06:00
|
|
|
/// MaterialInfo::init() is called during program initialization. It
|
2008-08-31 23:59:13 -06:00
|
|
|
/// precomputes material hash keys for a few basic endgames, in order
|
|
|
|
/// to make it easy to recognize such endgames when they occur.
|
|
|
|
|
|
|
|
void MaterialInfo::init() {
|
2008-10-27 12:07:48 -06:00
|
|
|
|
|
|
|
typedef Key ZM[2][8][16];
|
|
|
|
const ZM& z = Position::zobMaterial;
|
|
|
|
|
|
|
|
static const Color W = WHITE;
|
|
|
|
static const Color B = BLACK;
|
|
|
|
|
|
|
|
EEFAdd(z[W][PAWN][1], &EvaluateKPK);
|
|
|
|
EEFAdd(z[B][PAWN][1], &EvaluateKKP);
|
|
|
|
|
|
|
|
EEFAdd(z[W][BISHOP][1] ^ z[W][KNIGHT][1], &EvaluateKBNK);
|
|
|
|
EEFAdd(z[B][BISHOP][1] ^ z[B][KNIGHT][1], &EvaluateKKBN);
|
|
|
|
EEFAdd(z[W][ROOK][1] ^ z[B][PAWN][1], &EvaluateKRKP);
|
|
|
|
EEFAdd(z[W][PAWN][1] ^ z[B][ROOK][1], &EvaluateKPKR);
|
|
|
|
EEFAdd(z[W][ROOK][1] ^ z[B][BISHOP][1], &EvaluateKRKB);
|
|
|
|
EEFAdd(z[W][BISHOP][1] ^ z[B][ROOK][1], &EvaluateKBKR);
|
|
|
|
EEFAdd(z[W][ROOK][1] ^ z[B][KNIGHT][1], &EvaluateKRKN);
|
|
|
|
EEFAdd(z[W][KNIGHT][1] ^ z[B][ROOK][1], &EvaluateKNKR);
|
|
|
|
EEFAdd(z[W][QUEEN][1] ^ z[B][ROOK][1], &EvaluateKQKR);
|
|
|
|
EEFAdd(z[W][ROOK][1] ^ z[B][QUEEN][1], &EvaluateKRKQ);
|
|
|
|
|
|
|
|
KRPKRMaterialKey = z[W][ROOK][1]
|
|
|
|
^ z[W][PAWN][1]
|
|
|
|
^ z[B][ROOK][1];
|
|
|
|
|
|
|
|
KRKRPMaterialKey = z[W][ROOK][1]
|
|
|
|
^ z[B][ROOK][1]
|
|
|
|
^ z[B][PAWN][1];
|
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
KRPPKRPMaterialKey =
|
2008-10-27 12:07:48 -06:00
|
|
|
z[W][ROOK][1] ^
|
|
|
|
z[W][PAWN][1] ^
|
|
|
|
z[W][PAWN][2] ^
|
|
|
|
z[B][ROOK][1] ^
|
|
|
|
z[B][PAWN][1];
|
2008-08-31 23:59:13 -06:00
|
|
|
KRPKRPPMaterialKey =
|
2008-10-27 12:07:48 -06:00
|
|
|
z[W][ROOK][1] ^
|
|
|
|
z[W][PAWN][1] ^
|
|
|
|
z[B][ROOK][1] ^
|
|
|
|
z[B][PAWN][1] ^
|
|
|
|
z[B][PAWN][2];
|
2008-08-31 23:59:13 -06:00
|
|
|
KNNKMaterialKey =
|
2008-10-27 12:07:48 -06:00
|
|
|
z[W][KNIGHT][1] ^
|
|
|
|
z[W][KNIGHT][2];
|
2008-08-31 23:59:13 -06:00
|
|
|
KKNNMaterialKey =
|
2008-10-27 12:07:48 -06:00
|
|
|
z[B][KNIGHT][1] ^
|
|
|
|
z[B][KNIGHT][2];
|
2008-08-31 23:59:13 -06:00
|
|
|
KBPKBMaterialKey =
|
2008-10-27 12:07:48 -06:00
|
|
|
z[W][BISHOP][1] ^
|
|
|
|
z[W][PAWN][1] ^
|
|
|
|
z[B][BISHOP][1];
|
2008-08-31 23:59:13 -06:00
|
|
|
KBKBPMaterialKey =
|
2008-10-27 12:07:48 -06:00
|
|
|
z[W][BISHOP][1] ^
|
|
|
|
z[B][BISHOP][1] ^
|
|
|
|
z[B][PAWN][1];
|
2008-08-31 23:59:13 -06:00
|
|
|
KBPKNMaterialKey =
|
2008-10-27 12:07:48 -06:00
|
|
|
z[W][BISHOP][1] ^
|
|
|
|
z[W][PAWN][1] ^
|
|
|
|
z[B][KNIGHT][1];
|
2008-08-31 23:59:13 -06:00
|
|
|
KNKBPMaterialKey =
|
2008-10-27 12:07:48 -06:00
|
|
|
z[W][KNIGHT][1] ^
|
|
|
|
z[B][BISHOP][1] ^
|
|
|
|
z[B][PAWN][1];
|
2008-08-31 23:59:13 -06:00
|
|
|
KNPKMaterialKey =
|
2008-10-27 12:07:48 -06:00
|
|
|
z[W][KNIGHT][1] ^
|
|
|
|
z[W][PAWN][1];
|
2008-08-31 23:59:13 -06:00
|
|
|
KKNPMaterialKey =
|
2008-10-27 12:07:48 -06:00
|
|
|
z[B][KNIGHT][1] ^
|
|
|
|
z[B][PAWN][1];
|
2008-08-31 23:59:13 -06:00
|
|
|
KPKPMaterialKey =
|
2008-10-27 12:07:48 -06:00
|
|
|
z[W][PAWN][1] ^
|
|
|
|
z[B][PAWN][1];
|
|
|
|
|
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Constructor for the MaterialInfoTable class.
|
|
|
|
|
|
|
|
MaterialInfoTable::MaterialInfoTable(unsigned numOfEntries) {
|
2008-10-27 12:07:48 -06:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
size = numOfEntries;
|
|
|
|
entries = new MaterialInfo[size];
|
2008-10-27 12:07:48 -06:00
|
|
|
if (!entries)
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to allocate " << (numOfEntries * sizeof(MaterialInfo))
|
|
|
|
<< " bytes for material hash table." << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
2008-10-27 12:07:48 -06:00
|
|
|
clear();
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Destructor for the MaterialInfoTable class.
|
|
|
|
|
|
|
|
MaterialInfoTable::~MaterialInfoTable() {
|
2008-10-27 12:07:48 -06:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
delete [] entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// MaterialInfoTable::clear() clears a material hash table by setting
|
|
|
|
/// all entries to 0.
|
|
|
|
|
|
|
|
void MaterialInfoTable::clear() {
|
2008-10-27 12:07:48 -06:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
memset(entries, 0, size * sizeof(MaterialInfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// MaterialInfoTable::get_material_info() takes a position object as input,
|
|
|
|
/// computes or looks up a MaterialInfo object, and returns a pointer to it.
|
|
|
|
/// If the material configuration is not already present in the table, it
|
2008-09-23 16:32:53 -06:00
|
|
|
/// is stored there, so we don't have to recompute everything when the
|
2008-08-31 23:59:13 -06:00
|
|
|
/// same material configuration occurs again.
|
|
|
|
|
|
|
|
MaterialInfo *MaterialInfoTable::get_material_info(const Position &pos) {
|
2008-10-27 12:07:48 -06:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
Key key = pos.get_material_key();
|
|
|
|
int index = key & (size - 1);
|
|
|
|
MaterialInfo *mi = entries + index;
|
|
|
|
|
2008-09-23 16:32:53 -06:00
|
|
|
// If mi->key matches the position's material hash key, it means that we
|
2008-08-31 23:59:13 -06:00
|
|
|
// have analysed this material configuration before, and we can simply
|
|
|
|
// return the information we found the last time instead of recomputing it:
|
|
|
|
if(mi->key == key)
|
|
|
|
return mi;
|
|
|
|
|
|
|
|
// Clear the MaterialInfo object, and set its key:
|
|
|
|
mi->clear();
|
|
|
|
mi->key = key;
|
|
|
|
|
|
|
|
// A special case before looking for a specialized evaluation function:
|
|
|
|
// KNN vs K is a draw:
|
2008-10-27 12:07:48 -06:00
|
|
|
if (key == KNNKMaterialKey || key == KKNNMaterialKey)
|
|
|
|
{
|
2008-08-31 23:59:13 -06:00
|
|
|
mi->factor[WHITE] = mi->factor[BLACK] = 0;
|
|
|
|
return mi;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's look if we have a specialized evaluation function for this
|
2008-10-27 12:07:48 -06:00
|
|
|
// particular material configuration
|
|
|
|
if (EEFmap.find(key) != EEFmap.end())
|
|
|
|
{
|
|
|
|
mi->evaluationFunction = EEFmap[key];
|
|
|
|
return mi;
|
|
|
|
}
|
|
|
|
else if ( pos.non_pawn_material(BLACK) == Value(0)
|
|
|
|
&& pos.piece_count(BLACK, PAWN) == 0
|
|
|
|
&& pos.non_pawn_material(WHITE) >= RookValueEndgame)
|
|
|
|
{
|
|
|
|
mi->evaluationFunction = &EvaluateKXK;
|
|
|
|
return mi;
|
|
|
|
}
|
|
|
|
else if ( pos.non_pawn_material(WHITE) == Value(0)
|
|
|
|
&& pos.piece_count(WHITE, PAWN) == 0
|
|
|
|
&& pos.non_pawn_material(BLACK) >= RookValueEndgame)
|
|
|
|
{
|
|
|
|
mi->evaluationFunction = &EvaluateKKX;
|
|
|
|
return mi;
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// OK, we didn't find any special evaluation function for the current
|
2008-10-27 12:07:48 -06:00
|
|
|
// material configuration. Is there a suitable scaling function?
|
2008-08-31 23:59:13 -06:00
|
|
|
//
|
|
|
|
// The code below is rather messy, and it could easily get worse later,
|
|
|
|
// if we decide to add more special cases. We face problems when there
|
|
|
|
// are several conflicting applicable scaling functions and we need to
|
|
|
|
// decide which one to use.
|
|
|
|
|
|
|
|
if(key == KRPKRMaterialKey) {
|
|
|
|
mi->scalingFunction[WHITE] = &ScaleKRPKR;
|
|
|
|
return mi;
|
|
|
|
}
|
|
|
|
if(key == KRKRPMaterialKey) {
|
|
|
|
mi->scalingFunction[BLACK] = &ScaleKRKRP;
|
|
|
|
return mi;
|
|
|
|
}
|
|
|
|
if(key == KRPPKRPMaterialKey) {
|
|
|
|
mi->scalingFunction[WHITE] = &ScaleKRPPKRP;
|
|
|
|
return mi;
|
|
|
|
}
|
|
|
|
else if(key == KRPKRPPMaterialKey) {
|
|
|
|
mi->scalingFunction[BLACK] = &ScaleKRPKRPP;
|
|
|
|
return mi;
|
|
|
|
}
|
|
|
|
if(key == KBPKBMaterialKey) {
|
|
|
|
mi->scalingFunction[WHITE] = &ScaleKBPKB;
|
|
|
|
return mi;
|
|
|
|
}
|
|
|
|
if(key == KBKBPMaterialKey) {
|
|
|
|
mi->scalingFunction[BLACK] = &ScaleKBKBP;
|
|
|
|
return mi;
|
|
|
|
}
|
|
|
|
if(key == KBPKNMaterialKey) {
|
|
|
|
mi->scalingFunction[WHITE] = &ScaleKBPKN;
|
|
|
|
return mi;
|
|
|
|
}
|
|
|
|
if(key == KNKBPMaterialKey) {
|
|
|
|
mi->scalingFunction[BLACK] = &ScaleKNKBP;
|
|
|
|
return mi;
|
|
|
|
}
|
|
|
|
if(key == KNPKMaterialKey) {
|
|
|
|
mi->scalingFunction[WHITE] = &ScaleKNPK;
|
|
|
|
return mi;
|
|
|
|
}
|
|
|
|
if(key == KKNPMaterialKey) {
|
|
|
|
mi->scalingFunction[BLACK] = &ScaleKKNP;
|
|
|
|
return mi;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pos.non_pawn_material(WHITE) == BishopValueMidgame &&
|
2008-10-19 07:39:29 -06:00
|
|
|
pos.piece_count(WHITE, BISHOP) == 1 && pos.piece_count(WHITE, PAWN) >= 1)
|
2008-08-31 23:59:13 -06:00
|
|
|
mi->scalingFunction[WHITE] = &ScaleKBPK;
|
|
|
|
if(pos.non_pawn_material(BLACK) == BishopValueMidgame &&
|
2008-10-19 07:39:29 -06:00
|
|
|
pos.piece_count(BLACK, BISHOP) == 1 && pos.piece_count(BLACK, PAWN) >= 1)
|
2008-08-31 23:59:13 -06:00
|
|
|
mi->scalingFunction[BLACK] = &ScaleKKBP;
|
|
|
|
|
2008-10-19 07:39:29 -06:00
|
|
|
if(pos.piece_count(WHITE, PAWN) == 0 &&
|
2008-08-31 23:59:13 -06:00
|
|
|
pos.non_pawn_material(WHITE) == QueenValueMidgame &&
|
2008-10-19 07:39:29 -06:00
|
|
|
pos.piece_count(WHITE, QUEEN) == 1 &&
|
|
|
|
pos.piece_count(BLACK, ROOK) == 1 && pos.piece_count(BLACK, PAWN) >= 1)
|
2008-08-31 23:59:13 -06:00
|
|
|
mi->scalingFunction[WHITE] = &ScaleKQKRP;
|
2008-10-19 07:39:29 -06:00
|
|
|
else if(pos.piece_count(BLACK, PAWN) == 0 &&
|
2008-08-31 23:59:13 -06:00
|
|
|
pos.non_pawn_material(BLACK) == QueenValueMidgame &&
|
2008-10-19 07:39:29 -06:00
|
|
|
pos.piece_count(BLACK, QUEEN) == 1 &&
|
|
|
|
pos.piece_count(WHITE, ROOK) == 1 && pos.piece_count(WHITE, PAWN) >= 1)
|
2008-08-31 23:59:13 -06:00
|
|
|
mi->scalingFunction[BLACK] = &ScaleKRPKQ;
|
|
|
|
|
|
|
|
if(pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) == Value(0)) {
|
2008-10-19 07:39:29 -06:00
|
|
|
if(pos.piece_count(BLACK, PAWN) == 0) {
|
|
|
|
assert(pos.piece_count(WHITE, PAWN) >= 2);
|
2008-08-31 23:59:13 -06:00
|
|
|
mi->scalingFunction[WHITE] = &ScaleKPsK;
|
|
|
|
}
|
2008-10-19 07:39:29 -06:00
|
|
|
else if(pos.piece_count(WHITE, PAWN) == 0) {
|
|
|
|
assert(pos.piece_count(BLACK, PAWN) >= 2);
|
2008-08-31 23:59:13 -06:00
|
|
|
mi->scalingFunction[BLACK] = &ScaleKKPs;
|
|
|
|
}
|
2008-10-19 07:39:29 -06:00
|
|
|
else if(pos.piece_count(WHITE, PAWN) == 1 && pos.piece_count(BLACK, PAWN) == 1) {
|
2008-08-31 23:59:13 -06:00
|
|
|
mi->scalingFunction[WHITE] = &ScaleKPKPw;
|
|
|
|
mi->scalingFunction[BLACK] = &ScaleKPKPb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate the material balance.
|
2008-09-23 16:32:53 -06:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
Color c;
|
|
|
|
int sign;
|
|
|
|
Value egValue = Value(0), mgValue = Value(0);
|
2008-09-23 16:32:53 -06:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
for(c = WHITE, sign = 1; c <= BLACK; c++, sign = -sign) {
|
|
|
|
|
|
|
|
// No pawns makes it difficult to win, even with a material advantage:
|
2008-10-19 07:39:29 -06:00
|
|
|
if(pos.piece_count(c, PAWN) == 0 &&
|
2008-08-31 23:59:13 -06:00
|
|
|
pos.non_pawn_material(c) - pos.non_pawn_material(opposite_color(c))
|
|
|
|
<= BishopValueMidgame) {
|
|
|
|
if(pos.non_pawn_material(c) == pos.non_pawn_material(opposite_color(c)))
|
|
|
|
mi->factor[c] = 0;
|
|
|
|
else if(pos.non_pawn_material(c) < RookValueMidgame)
|
|
|
|
mi->factor[c] = 0;
|
|
|
|
else {
|
2008-10-19 07:39:29 -06:00
|
|
|
switch(pos.piece_count(c, BISHOP)) {
|
2008-08-31 23:59:13 -06:00
|
|
|
case 2:
|
|
|
|
mi->factor[c] = 32; break;
|
|
|
|
case 1:
|
|
|
|
mi->factor[c] = 12; break;
|
|
|
|
case 0:
|
|
|
|
mi->factor[c] = 6; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-09-23 16:32:53 -06:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
// Bishop pair:
|
2008-10-19 07:39:29 -06:00
|
|
|
if(pos.piece_count(c, BISHOP) >= 2) {
|
2008-08-31 23:59:13 -06:00
|
|
|
mgValue += sign * BishopPairMidgameBonus;
|
|
|
|
egValue += sign * BishopPairEndgameBonus;
|
|
|
|
}
|
|
|
|
|
2008-09-23 16:32:53 -06:00
|
|
|
// Knights are stronger when there are many pawns on the board. The
|
2008-08-31 23:59:13 -06:00
|
|
|
// formula is taken from Larry Kaufman's paper "The Evaluation of Material
|
2008-09-23 16:32:53 -06:00
|
|
|
// Imbalances in Chess":
|
2008-08-31 23:59:13 -06:00
|
|
|
// http://mywebpages.comcast.net/danheisman/Articles/evaluation_of_material_imbalance.htm
|
2008-10-19 07:39:29 -06:00
|
|
|
mgValue += sign * Value(pos.piece_count(c, KNIGHT)*(pos.piece_count(c, PAWN)-5)*16);
|
|
|
|
egValue += sign * Value(pos.piece_count(c, KNIGHT)*(pos.piece_count(c, PAWN)-5)*16);
|
2008-08-31 23:59:13 -06:00
|
|
|
|
|
|
|
// Redundancy of major pieces, again based on Kaufman's paper:
|
2008-10-19 07:39:29 -06:00
|
|
|
if(pos.piece_count(c, ROOK) >= 1) {
|
|
|
|
Value v = Value((pos.piece_count(c, ROOK) - 1) * 32 + pos.piece_count(c, QUEEN) * 16);
|
2008-08-31 23:59:13 -06:00
|
|
|
mgValue -= sign * v;
|
|
|
|
egValue -= sign * v;
|
|
|
|
}
|
2008-09-23 16:32:53 -06:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
mi->mgValue = int16_t(mgValue);
|
|
|
|
mi->egValue = int16_t(egValue);
|
|
|
|
|
|
|
|
return mi;
|
|
|
|
}
|