- // Compute the space weight
- if (pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) >=
- 2*QueenValueMidgame + 4*RookValueMidgame + 2*KnightValueMidgame)
- {
- int minorPieceCount = pos.piece_count(WHITE, KNIGHT)
- + pos.piece_count(BLACK, KNIGHT)
- + pos.piece_count(WHITE, BISHOP)
- + pos.piece_count(BLACK, BISHOP);
-
- mi->spaceWeight = minorPieceCount * minorPieceCount;
- }
-
- // Evaluate the material balance
-
- int sign;
- Value egValue = Value(0);
- Value mgValue = Value(0);
-
- for (c = WHITE, sign = 1; c <= BLACK; c++, sign = -sign)
- {
- // No pawns makes it difficult to win, even with a material advantage
- 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;
- }
- }
- }
-
- // Bishop pair
- if (pos.piece_count(c, BISHOP) >= 2)
- {
- mgValue += sign * BishopPairMidgameBonus;
- egValue += sign * BishopPairEndgameBonus;
- }
-
- // Knights are stronger when there are many pawns on the board. The
- // formula is taken from Larry Kaufman's paper "The Evaluation of Material
- // Imbalances in Chess":
- // http://mywebpages.comcast.net/danheisman/Articles/evaluation_of_material_imbalance.htm
- 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);
-
- // Redundancy of major pieces, again based on Kaufman's paper:
- if (pos.piece_count(c, ROOK) >= 1)
- {
- Value v = Value((pos.piece_count(c, ROOK) - 1) * 32 + pos.piece_count(c, QUEEN) * 16);
- mgValue -= sign * v;
- egValue -= sign * v;
- }
- }
- mi->mgValue = int16_t(mgValue);
- mi->egValue = int16_t(egValue);
- return mi;
-}
-
-
-/// EndgameFunctions member definitions. This class is used to store the maps
-/// of end game and scaling functions that MaterialInfoTable will query for
-/// each key. The maps are constant and are populated only at construction,
-/// but are per-thread instead of globals to avoid expensive locks.
-
-EndgameFunctions::EndgameFunctions() {
-
- KNNKMaterialKey = buildKey("KNNK");
- KKNNMaterialKey = buildKey("KKNN");
-
- add("KPK", &EvaluateKPK);
- add("KKP", &EvaluateKKP);
- add("KBNK", &EvaluateKBNK);
- add("KKBN", &EvaluateKKBN);
- add("KRKP", &EvaluateKRKP);
- add("KPKR", &EvaluateKPKR);
- add("KRKB", &EvaluateKRKB);
- add("KBKR", &EvaluateKBKR);
- add("KRKN", &EvaluateKRKN);
- add("KNKR", &EvaluateKNKR);
- add("KQKR", &EvaluateKQKR);
- add("KRKQ", &EvaluateKRKQ);
- add("KBBKN", &EvaluateKBBKN);
- add("KNKBB", &EvaluateKNKBB);
-
- add("KNPK", WHITE, &ScaleKNPK);
- add("KKNP", BLACK, &ScaleKKNP);
- add("KRPKR", WHITE, &ScaleKRPKR);
- add("KRKRP", BLACK, &ScaleKRKRP);
- add("KBPKB", WHITE, &ScaleKBPKB);
- add("KBKBP", BLACK, &ScaleKBKBP);
- add("KBPPKB", WHITE, &ScaleKBPPKB);
- add("KBKBPP", BLACK, &ScaleKBKBPP);
- add("KBPKN", WHITE, &ScaleKBPKN);
- add("KNKBP", BLACK, &ScaleKNKBP);
- add("KRPPKRP", WHITE, &ScaleKRPPKRP);
- add("KRPKRPP", BLACK, &ScaleKRPKRPP);
- add("KRPPKRP", WHITE, &ScaleKRPPKRP);
- add("KRPKRPP", BLACK, &ScaleKRPKRPP);
-}
-
-Key EndgameFunctions::buildKey(const std::string& keyCode) {
-
- assert(keyCode.length() > 0 && keyCode[0] == 'K');
- assert(keyCode.length() < 8);
-
- std::stringstream s;
- bool upcase = false;
-
- // Build up a fen substring 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;
-
- s << char(upcase? toupper(keyCode[i]) : tolower(keyCode[i]));
- }
- s << 8 - keyCode.length() << "/8/8/8/8/8/8/8 w -";
- return Position(s.str()).get_material_key();
-}
-
-void EndgameFunctions::add(const std::string& keyCode, EndgameEvaluationFunctionBase* f) {
-
- EEFmap.insert(std::pair<Key, EndgameEvaluationFunctionBase*>(buildKey(keyCode), f));
+ // Zero or just one pawn makes it difficult to win, even with a small material
+ // advantage. This catches some trivial draws like KK, KBK and KNK and gives a
+ // drawish scale factor for cases such as KRKBP and KmmKm (except for KBBKN).
+ if (!pos.count<PAWN>(WHITE) && npm_w - npm_b <= BishopValueMg)
+ e->factor[WHITE] = uint8_t(npm_w < RookValueMg ? SCALE_FACTOR_DRAW :
+ npm_b <= BishopValueMg ? 4 : 14);
+
+ if (!pos.count<PAWN>(BLACK) && npm_b - npm_w <= BishopValueMg)
+ e->factor[BLACK] = uint8_t(npm_b < RookValueMg ? SCALE_FACTOR_DRAW :
+ npm_w <= BishopValueMg ? 4 : 14);
+
+ if (pos.count<PAWN>(WHITE) == 1 && npm_w - npm_b <= BishopValueMg)
+ e->factor[WHITE] = (uint8_t) SCALE_FACTOR_ONEPAWN;
+
+ if (pos.count<PAWN>(BLACK) == 1 && npm_b - npm_w <= BishopValueMg)
+ e->factor[BLACK] = (uint8_t) SCALE_FACTOR_ONEPAWN;
+
+ // Evaluate the material imbalance. We use PIECE_TYPE_NONE as a place holder
+ // for the bishop pair "extended piece", which allows us to be more flexible
+ // in defining bishop pair bonuses.
+ const int PieceCount[COLOR_NB][PIECE_TYPE_NB] = {
+ { pos.count<BISHOP>(WHITE) > 1, pos.count<PAWN>(WHITE), pos.count<KNIGHT>(WHITE),
+ pos.count<BISHOP>(WHITE) , pos.count<ROOK>(WHITE), pos.count<QUEEN >(WHITE) },
+ { pos.count<BISHOP>(BLACK) > 1, pos.count<PAWN>(BLACK), pos.count<KNIGHT>(BLACK),
+ pos.count<BISHOP>(BLACK) , pos.count<ROOK>(BLACK), pos.count<QUEEN >(BLACK) } };
+
+ e->value = int16_t((imbalance<WHITE>(PieceCount) - imbalance<BLACK>(PieceCount)) / 16);
+ return e;