#include <cassert>
#include <cstring>
-#include <map>
#include "material.h"
const Value MidgameLimit = Value(15581);
const Value EndgameLimit = Value(3998);
+ // Scale factors used when one side has no more pawns
+ const int NoPawnsSF[4] = { 6, 12, 32 };
+
// Polynomial material balance parameters
const Value RedundantQueenPenalty = Value(320);
const Value RedundantRookPenalty = Value(554);
{ 41, 41, 41, 41, 41, 41 }, { 37, 41, 41, 41, 41, 41 }, { 10, 62, 41, 41, 41, 41 },
{ 57, 64, 39, 41, 41, 41 }, { 50, 40, 23, -22, 41, 41 }, { 106, 101, 3, 151, 171, 41 } };
- typedef EndgameEvaluationFunctionBase EF;
- typedef EndgameScalingFunctionBase SF;
- typedef map<Key, EF*> EFMap;
- typedef map<Key, SF*> SFMap;
-
// Endgame evaluation and scaling functions accessed direcly and not through
// the function maps because correspond to more then one material hash key.
- EvaluationFunction<KmmKm> EvaluateKmmKm[] = { EvaluationFunction<KmmKm>(WHITE), EvaluationFunction<KmmKm>(BLACK) };
- EvaluationFunction<KXK> EvaluateKXK[] = { EvaluationFunction<KXK>(WHITE), EvaluationFunction<KXK>(BLACK) };
- ScalingFunction<KBPsK> ScaleKBPsK[] = { ScalingFunction<KBPsK>(WHITE), ScalingFunction<KBPsK>(BLACK) };
- ScalingFunction<KQKRPs> ScaleKQKRPs[] = { ScalingFunction<KQKRPs>(WHITE), ScalingFunction<KQKRPs>(BLACK) };
- ScalingFunction<KPsK> ScaleKPsK[] = { ScalingFunction<KPsK>(WHITE), ScalingFunction<KPsK>(BLACK) };
- ScalingFunction<KPKP> ScaleKPKP[] = { ScalingFunction<KPKP>(WHITE), ScalingFunction<KPKP>(BLACK) };
+ Endgame<Value, KmmKm> EvaluateKmmKm[] = { Endgame<Value, KmmKm>(WHITE), Endgame<Value, KmmKm>(BLACK) };
+ Endgame<Value, KXK> EvaluateKXK[] = { Endgame<Value, KXK>(WHITE), Endgame<Value, KXK>(BLACK) };
+
+ Endgame<ScaleFactor, KBPsK> ScaleKBPsK[] = { Endgame<ScaleFactor, KBPsK>(WHITE), Endgame<ScaleFactor, KBPsK>(BLACK) };
+ Endgame<ScaleFactor, KQKRPs> ScaleKQKRPs[] = { Endgame<ScaleFactor, KQKRPs>(WHITE), Endgame<ScaleFactor, KQKRPs>(BLACK) };
+ Endgame<ScaleFactor, KPsK> ScaleKPsK[] = { Endgame<ScaleFactor, KPsK>(WHITE), Endgame<ScaleFactor, KPsK>(BLACK) };
+ Endgame<ScaleFactor, KPKP> ScaleKPKP[] = { Endgame<ScaleFactor, KPKP>(WHITE), Endgame<ScaleFactor, KPKP>(BLACK) };
// Helper templates used to detect a given material distribution
template<Color Us> bool is_KXK(const Position& pos) {
&& pos.piece_count(Them, ROOK) == 1
&& pos.piece_count(Them, PAWN) >= 1;
}
-}
-
-
-/// EndgameFunctions class stores endgame evaluation and scaling functions
-/// in two std::map. Because STL library is not guaranteed to be thread
-/// safe even for read access, the maps, although with identical content,
-/// are replicated for each thread. This is faster then using locks.
-class EndgameFunctions {
-public:
- EndgameFunctions();
- ~EndgameFunctions();
- template<class T> T* get(Key key) const;
+} // namespace
-private:
- template<class T> void add(const string& keyCode);
- static Key buildKey(const string& keyCode);
- static const string swapColors(const string& keyCode);
+/// MaterialInfoTable c'tor and d'tor allocate and free the space for Endgames
- // Here we store two maps, for evaluate and scaling functions...
- pair<EFMap, SFMap> maps;
-
- // ...and here is the accessing template function
- template<typename T> const map<Key, T*>& get() const;
-};
-
-// Explicit specializations of a member function shall be declared in
-// the namespace of which the class template is a member.
-template<> const EFMap& EndgameFunctions::get<EF>() const { return maps.first; }
-template<> const SFMap& EndgameFunctions::get<SF>() const { return maps.second; }
-
-
-/// MaterialInfoTable c'tor and d'tor allocate and free the space for EndgameFunctions
-
-MaterialInfoTable::MaterialInfoTable() { funcs = new EndgameFunctions(); }
+void MaterialInfoTable::init() { Base::init(); if (!funcs) funcs = new Endgames(); }
MaterialInfoTable::~MaterialInfoTable() { delete funcs; }
MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) const {
Key key = pos.get_material_key();
- MaterialInfo* mi = find(key);
+ MaterialInfo* mi = probe(key);
// If mi->key matches the position's material hash key, it means that we
// have analysed this material configuration before, and we can simply
// Let's look if we have a specialized evaluation function for this
// particular material configuration. First we look for a fixed
// configuration one, then a generic one if previous search failed.
- if ((mi->evaluationFunction = funcs->get<EF>(key)) != NULL)
+ if ((mi->evaluationFunction = funcs->get<EndgameBase<Value> >(key)) != NULL)
return mi;
if (is_KXK<WHITE>(pos))
//
// We face problems when there are several conflicting applicable
// scaling functions and we need to decide which one to use.
- SF* sf;
+ EndgameBase<ScaleFactor>* sf;
- if ((sf = funcs->get<SF>(key)) != NULL)
+ if ((sf = funcs->get<EndgameBase<ScaleFactor> >(key)) != NULL)
{
mi->scalingFunction[sf->color()] = sf;
return mi;
else if (is_KQKRPs<BLACK>(pos))
mi->scalingFunction[BLACK] = &ScaleKQKRPs[BLACK];
- if (pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) == VALUE_ZERO)
+ Value npm_w = pos.non_pawn_material(WHITE);
+ Value npm_b = pos.non_pawn_material(BLACK);
+
+ if (npm_w + npm_b == VALUE_ZERO)
{
if (pos.piece_count(BLACK, PAWN) == 0)
{
}
// No pawns makes it difficult to win, even with a material advantage
- for (Color c = WHITE; c <= BLACK; c++)
- if ( pos.piece_count(c, PAWN) == 0
- && 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))
- || pos.non_pawn_material(c) < RookValueMidgame)
- mi->factor[c] = 0;
- else
- {
- switch (pos.piece_count(c, BISHOP)) {
- case 2:
- mi->factor[c] = 32;
- break;
- case 1:
- mi->factor[c] = 12;
- break;
- case 0:
- mi->factor[c] = 6;
- break;
- }
- }
- }
+ if (pos.piece_count(WHITE, PAWN) == 0 && npm_w - npm_b <= BishopValueMidgame)
+ {
+ mi->factor[WHITE] = uint8_t
+ (npm_w == npm_b || npm_w < RookValueMidgame ? 0 : NoPawnsSF[Min(pos.piece_count(WHITE, BISHOP), 2)]);
+ }
+
+ if (pos.piece_count(BLACK, PAWN) == 0 && npm_b - npm_w <= BishopValueMidgame)
+ {
+ mi->factor[BLACK] = uint8_t
+ (npm_w == npm_b || npm_b < RookValueMidgame ? 0 : NoPawnsSF[Min(pos.piece_count(BLACK, BISHOP), 2)]);
+ }
// Compute the space weight
- if (pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) >=
- 2*QueenValueMidgame + 4*RookValueMidgame + 2*KnightValueMidgame)
+ if (npm_w + npm_b >= 2 * QueenValueMidgame + 4 * RookValueMidgame + 2 * KnightValueMidgame)
{
- int minorPieceCount = pos.piece_count(WHITE, KNIGHT)
- + pos.piece_count(WHITE, BISHOP)
- + pos.piece_count(BLACK, KNIGHT)
- + pos.piece_count(BLACK, BISHOP);
+ int minorPieceCount = pos.piece_count(WHITE, KNIGHT) + pos.piece_count(WHITE, BISHOP)
+ + pos.piece_count(BLACK, KNIGHT) + pos.piece_count(BLACK, BISHOP);
mi->spaceWeight = minorPieceCount * minorPieceCount;
}
// in defining bishop pair bonuses.
const int pieceCount[2][8] = {
{ pos.piece_count(WHITE, BISHOP) > 1, pos.piece_count(WHITE, PAWN), pos.piece_count(WHITE, KNIGHT),
- pos.piece_count(WHITE, BISHOP), pos.piece_count(WHITE, ROOK), pos.piece_count(WHITE, QUEEN) },
+ pos.piece_count(WHITE, BISHOP) , pos.piece_count(WHITE, ROOK), pos.piece_count(WHITE, QUEEN) },
{ pos.piece_count(BLACK, BISHOP) > 1, pos.piece_count(BLACK, PAWN), pos.piece_count(BLACK, KNIGHT),
- pos.piece_count(BLACK, BISHOP), pos.piece_count(BLACK, ROOK), pos.piece_count(BLACK, QUEEN) } };
+ pos.piece_count(BLACK, BISHOP) , pos.piece_count(BLACK, ROOK), pos.piece_count(BLACK, QUEEN) } };
- mi->value = (int16_t)(imbalance<WHITE>(pieceCount) - imbalance<BLACK>(pieceCount)) / 16;
+ mi->value = int16_t((imbalance<WHITE>(pieceCount) - imbalance<BLACK>(pieceCount)) / 16);
return mi;
}
const Color Them = (Us == WHITE ? BLACK : WHITE);
- int pt1, pt2, pc, vv;
+ int pt1, pt2, pc, v;
int value = 0;
// Redundancy of major pieces, formula based on Kaufman's paper
if (!pc)
continue;
- vv = LinearCoefficients[pt1];
+ v = LinearCoefficients[pt1];
for (pt2 = PIECE_TYPE_NONE; pt2 <= pt1; pt2++)
- vv += QuadraticCoefficientsSameColor[pt1][pt2] * pieceCount[Us][pt2]
- + QuadraticCoefficientsOppositeColor[pt1][pt2] * pieceCount[Them][pt2];
+ v += QuadraticCoefficientsSameColor[pt1][pt2] * pieceCount[Us][pt2]
+ + QuadraticCoefficientsOppositeColor[pt1][pt2] * pieceCount[Them][pt2];
- value += pc * vv;
+ value += pc * v;
}
return value;
}
Value npm = pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK);
- if (npm >= MidgameLimit)
- return PHASE_MIDGAME;
-
- if (npm <= EndgameLimit)
- return PHASE_ENDGAME;
-
- return Phase(((npm - EndgameLimit) * 128) / (MidgameLimit - EndgameLimit));
-}
-
-
-/// EndgameFunctions member definitions
-
-EndgameFunctions::EndgameFunctions() {
-
- add<EvaluationFunction<KNNK> >("KNNK");
- add<EvaluationFunction<KPK> >("KPK");
- add<EvaluationFunction<KBNK> >("KBNK");
- add<EvaluationFunction<KRKP> >("KRKP");
- add<EvaluationFunction<KRKB> >("KRKB");
- add<EvaluationFunction<KRKN> >("KRKN");
- add<EvaluationFunction<KQKR> >("KQKR");
- add<EvaluationFunction<KBBKN> >("KBBKN");
-
- add<ScalingFunction<KNPK> >("KNPK");
- add<ScalingFunction<KRPKR> >("KRPKR");
- add<ScalingFunction<KBPKB> >("KBPKB");
- add<ScalingFunction<KBPPKB> >("KBPPKB");
- add<ScalingFunction<KBPKN> >("KBPKN");
- add<ScalingFunction<KRPPKRP> >("KRPPKRP");
-}
-
-EndgameFunctions::~EndgameFunctions() {
-
- for (EFMap::const_iterator it = maps.first.begin(); it != maps.first.end(); ++it)
- delete it->second;
-
- for (SFMap::const_iterator it = maps.second.begin(); it != maps.second.end(); ++it)
- delete it->second;
-}
-
-Key EndgameFunctions::buildKey(const string& keyCode) {
-
- assert(keyCode.length() > 0 && keyCode.length() < 8);
- assert(keyCode[0] == 'K');
-
- string fen;
- bool upcase = false;
-
- // Build up a fen string with the given pieces, note that
- // the fen string could be of an illegal position.
- for (size_t i = 0; i < keyCode.length(); i++)
- {
- if (keyCode[i] == 'K')
- upcase = !upcase;
-
- fen += char(upcase ? toupper(keyCode[i]) : tolower(keyCode[i]));
- }
- fen += char(8 - keyCode.length() + '0');
- fen += "/8/8/8/8/8/8/8 w - -";
- return Position(fen, false, 0).get_material_key();
-}
-
-const string EndgameFunctions::swapColors(const string& keyCode) {
-
- // Build corresponding key for the opposite color: "KBPKN" -> "KNKBP"
- size_t idx = keyCode.find('K', 1);
- return keyCode.substr(idx) + keyCode.substr(0, idx);
-}
-
-template<class T>
-void EndgameFunctions::add(const string& keyCode) {
-
- typedef typename T::Base F;
- typedef map<Key, F*> M;
-
- const_cast<M&>(get<F>()).insert(pair<Key, F*>(buildKey(keyCode), new T(WHITE)));
- const_cast<M&>(get<F>()).insert(pair<Key, F*>(buildKey(swapColors(keyCode)), new T(BLACK)));
-}
-
-template<class T>
-T* EndgameFunctions::get(Key key) const {
-
- typename map<Key, T*>::const_iterator it = get<T>().find(key);
- return it != get<T>().end() ? it->second : NULL;
+ return npm >= MidgameLimit ? PHASE_MIDGAME
+ : npm <= EndgameLimit ? PHASE_ENDGAME
+ : Phase(((npm - EndgameLimit) * 128) / (MidgameLimit - EndgameLimit));
}