-
-namespace Trace {
-
- enum Tracing { NO_TRACE, TRACE };
-
- enum Term { // The first 8 entries are reserved for PieceType
- MATERIAL = 8, IMBALANCE, MOBILITY, THREAT, PASSED, SPACE, INITIATIVE, TOTAL, TERM_NB
- };
-
- Score scores[TERM_NB][COLOR_NB];
-
- double to_cp(Value v) { return double(v) / PawnValueEg; }
-
- void add(int idx, Color c, Score s) {
- scores[idx][c] = s;
- }
-
- void add(int idx, Score w, Score b = SCORE_ZERO) {
- scores[idx][WHITE] = w;
- scores[idx][BLACK] = b;
- }
-
- std::ostream& operator<<(std::ostream& os, Score s) {
- os << std::setw(5) << to_cp(mg_value(s)) << " "
- << std::setw(5) << to_cp(eg_value(s));
- return os;
- }
-
- std::ostream& operator<<(std::ostream& os, Term t) {
-
- if (t == MATERIAL || t == IMBALANCE || t == INITIATIVE || t == TOTAL)
- os << " ---- ----" << " | " << " ---- ----";
- else
- os << scores[t][WHITE] << " | " << scores[t][BLACK];
-
- os << " | " << scores[t][WHITE] - scores[t][BLACK] << "\n";
- return os;
- }
-}
-
-using namespace Trace;
-
-namespace {
-
- // Threshold for lazy and space evaluation
- constexpr Value LazyThreshold = Value(1400);
- constexpr Value SpaceThreshold = Value(12222);
-
- // KingAttackWeights[PieceType] contains king attack weights by piece type
- constexpr int KingAttackWeights[PIECE_TYPE_NB] = { 0, 0, 81, 52, 44, 10 };
-
- // Penalties for enemy's safe checks
- constexpr int QueenSafeCheck = 780;
- constexpr int RookSafeCheck = 1080;
- constexpr int BishopSafeCheck = 635;
- constexpr int KnightSafeCheck = 790;
-
-#define S(mg, eg) make_score(mg, eg)
-
- // MobilityBonus[PieceType-2][attacked] contains bonuses for middle and end game,
- // indexed by piece type and number of attacked squares in the mobility area.
- constexpr Score MobilityBonus[][32] = {
- { S(-62,-81), S(-53,-56), S(-12,-30), S( -4,-14), S( 3, 8), S( 13, 15), // Knight
- S( 22, 23), S( 28, 27), S( 33, 33) },
- { S(-48,-59), S(-20,-23), S( 16, -3), S( 26, 13), S( 38, 24), S( 51, 42), // Bishop
- S( 55, 54), S( 63, 57), S( 63, 65), S( 68, 73), S( 81, 78), S( 81, 86),
- S( 91, 88), S( 98, 97) },
- { S(-58,-76), S(-27,-18), S(-15, 28), S(-10, 55), S( -5, 69), S( -2, 82), // Rook
- S( 9,112), S( 16,118), S( 30,132), S( 29,142), S( 32,155), S( 38,165),
- S( 46,166), S( 48,169), S( 58,171) },
- { S(-39,-36), S(-21,-15), S( 3, 8), S( 3, 18), S( 14, 34), S( 22, 54), // Queen
- S( 28, 61), S( 41, 73), S( 43, 79), S( 48, 92), S( 56, 94), S( 60,104),
- S( 60,113), S( 66,120), S( 67,123), S( 70,126), S( 71,133), S( 73,136),
- S( 79,140), S( 88,143), S( 88,148), S( 99,166), S(102,170), S(102,175),
- S(106,184), S(109,191), S(113,206), S(116,212) }
- };
-
- // RookOnFile[semiopen/open] contains bonuses for each rook when there is
- // no (friendly) pawn on the rook file.
- constexpr Score RookOnFile[] = { S(21, 4), S(47, 25) };
-
- // ThreatByMinor/ByRook[attacked PieceType] contains bonuses according to
- // which piece type attacks which one. Attacks on lesser pieces which are
- // pawn-defended are not considered.
- constexpr Score ThreatByMinor[PIECE_TYPE_NB] = {
- S(0, 0), S(5, 32), S(57, 41), S(77, 56), S(88, 119), S(79, 161)
- };
-
- constexpr Score ThreatByRook[PIECE_TYPE_NB] = {
- S(0, 0), S(2, 44), S(36, 71), S(36, 61), S(0, 38), S(51, 38)
- };
-
- // PassedRank[Rank] contains a bonus according to the rank of a passed pawn
- constexpr Score PassedRank[RANK_NB] = {
- S(0, 0), S(10, 28), S(17, 33), S(15, 41), S(62, 72), S(168, 177), S(276, 260)
- };
-
- // Assorted bonuses and penalties
- constexpr Score BishopPawns = S( 3, 7);
- constexpr Score CorneredBishop = S( 50, 50);
- constexpr Score FlankAttacks = S( 8, 0);
- constexpr Score Hanging = S( 69, 36);
- constexpr Score KingProtector = S( 7, 8);
- constexpr Score KnightOnQueen = S( 16, 12);
- constexpr Score LongDiagonalBishop = S( 45, 0);
- constexpr Score MinorBehindPawn = S( 18, 3);
- constexpr Score Outpost = S( 30, 21);
- constexpr Score PassedFile = S( 11, 8);
- constexpr Score PawnlessFlank = S( 17, 95);
- constexpr Score RestrictedPiece = S( 7, 7);
- constexpr Score RookOnQueenFile = S( 7, 6);
- constexpr Score SliderOnQueen = S( 59, 18);
- constexpr Score ThreatByKing = S( 24, 89);
- constexpr Score ThreatByPawnPush = S( 48, 39);
- constexpr Score ThreatBySafePawn = S(173, 94);
- constexpr Score TrappedRook = S( 52, 10);
- constexpr Score WeakQueen = S( 49, 15);
- constexpr Score WeakQueenProtection = S( 14, 0);
-
-#undef S
-
- // Evaluation class computes and stores attacks tables and other working data
- template<Tracing T>
- class Evaluation {
-
- public:
- Evaluation() = delete;
- explicit Evaluation(const Position& p) : pos(p) {}
- Evaluation& operator=(const Evaluation&) = delete;
- Value value();
-
- private:
- template<Color Us> void initialize();
- template<Color Us, PieceType Pt> Score pieces();
- template<Color Us> Score king() const;
- template<Color Us> Score threats() const;
- template<Color Us> Score passed() const;
- template<Color Us> Score space() const;
- ScaleFactor scale_factor(Value eg) const;
- Score initiative(Score score) const;
-
- const Position& pos;
- Material::Entry* me;
- Pawns::Entry* pe;
- Bitboard mobilityArea[COLOR_NB];
- Score mobility[COLOR_NB] = { SCORE_ZERO, SCORE_ZERO };
-
- // attackedBy[color][piece type] is a bitboard representing all squares
- // attacked by a given color and piece type. Special "piece types" which
- // is also calculated is ALL_PIECES.
- Bitboard attackedBy[COLOR_NB][PIECE_TYPE_NB];
-
- // attackedBy2[color] are the squares attacked by at least 2 units of a given
- // color, including x-rays. But diagonal x-rays through pawns are not computed.
- Bitboard attackedBy2[COLOR_NB];
-
- // kingRing[color] are the squares adjacent to the king plus some other
- // very near squares, depending on king position.
- Bitboard kingRing[COLOR_NB];
-
- // kingAttackersCount[color] is the number of pieces of the given color
- // which attack a square in the kingRing of the enemy king.
- int kingAttackersCount[COLOR_NB];
-
- // kingAttackersWeight[color] is the sum of the "weights" of the pieces of
- // the given color which attack a square in the kingRing of the enemy king.
- // The weights of the individual piece types are given by the elements in
- // the KingAttackWeights array.
- int kingAttackersWeight[COLOR_NB];
-
- // kingAttacksCount[color] is the number of attacks by the given color to
- // squares directly adjacent to the enemy king. Pieces which attack more
- // than one square are counted multiple times. For instance, if there is
- // a white knight on g5 and black's king is on g8, this white knight adds 2
- // to kingAttacksCount[WHITE].
- int kingAttacksCount[COLOR_NB];
- };
-
-
- // Evaluation::initialize() computes king and pawn attacks, and the king ring
- // bitboard for a given color. This is done at the beginning of the evaluation.
- template<Tracing T> template<Color Us>
- void Evaluation<T>::initialize() {
-
- constexpr Color Them = ~Us;
- constexpr Direction Up = pawn_push(Us);
- constexpr Direction Down = -Up;
- constexpr Bitboard LowRanks = (Us == WHITE ? Rank2BB | Rank3BB : Rank7BB | Rank6BB);
-
- const Square ksq = pos.square<KING>(Us);
-
- Bitboard dblAttackByPawn = pawn_double_attacks_bb<Us>(pos.pieces(Us, PAWN));
-
- // Find our pawns that are blocked or on the first two ranks
- Bitboard b = pos.pieces(Us, PAWN) & (shift<Down>(pos.pieces()) | LowRanks);
-
- // Squares occupied by those pawns, by our king or queen, by blockers to attacks on our king
- // or controlled by enemy pawns are excluded from the mobility area.
- mobilityArea[Us] = ~(b | pos.pieces(Us, KING, QUEEN) | pos.blockers_for_king(Us) | pe->pawn_attacks(Them));
-
- // Initialize attackedBy[] for king and pawns
- attackedBy[Us][KING] = pos.attacks_from<KING>(ksq);
- attackedBy[Us][PAWN] = pe->pawn_attacks(Us);
- attackedBy[Us][ALL_PIECES] = attackedBy[Us][KING] | attackedBy[Us][PAWN];
- attackedBy2[Us] = dblAttackByPawn | (attackedBy[Us][KING] & attackedBy[Us][PAWN]);
-
- // Init our king safety tables
- Square s = make_square(Utility::clamp(file_of(ksq), FILE_B, FILE_G),
- Utility::clamp(rank_of(ksq), RANK_2, RANK_7));
- kingRing[Us] = PseudoAttacks[KING][s] | s;
-
- kingAttackersCount[Them] = popcount(kingRing[Us] & pe->pawn_attacks(Them));
- kingAttacksCount[Them] = kingAttackersWeight[Them] = 0;
-
- // Remove from kingRing[] the squares defended by two pawns
- kingRing[Us] &= ~dblAttackByPawn;
- }
-
-
- // Evaluation::pieces() scores pieces of a given color and type
- template<Tracing T> template<Color Us, PieceType Pt>
- Score Evaluation<T>::pieces() {
-
- constexpr Color Them = ~Us;
- constexpr Direction Down = -pawn_push(Us);
- constexpr Bitboard OutpostRanks = (Us == WHITE ? Rank4BB | Rank5BB | Rank6BB
- : Rank5BB | Rank4BB | Rank3BB);
- const Square* pl = pos.squares<Pt>(Us);
-
- Bitboard b, bb;
- Score score = SCORE_ZERO;
-
- attackedBy[Us][Pt] = 0;
-
- for (Square s = *pl; s != SQ_NONE; s = *++pl)
- {
- // Find attacked squares, including x-ray attacks for bishops and rooks
- b = Pt == BISHOP ? attacks_bb<BISHOP>(s, pos.pieces() ^ pos.pieces(QUEEN))
- : Pt == ROOK ? attacks_bb< ROOK>(s, pos.pieces() ^ pos.pieces(QUEEN) ^ pos.pieces(Us, ROOK))
- : pos.attacks_from<Pt>(s);
-
- if (pos.blockers_for_king(Us) & s)
- b &= LineBB[pos.square<KING>(Us)][s];
-
- attackedBy2[Us] |= attackedBy[Us][ALL_PIECES] & b;
- attackedBy[Us][Pt] |= b;
- attackedBy[Us][ALL_PIECES] |= b;
-
- if (b & kingRing[Them])
- {
- kingAttackersCount[Us]++;
- kingAttackersWeight[Us] += KingAttackWeights[Pt];
- kingAttacksCount[Us] += popcount(b & attackedBy[Them][KING]);
- }
-
- int mob = popcount(b & mobilityArea[Us]);
-
- mobility[Us] += MobilityBonus[Pt - 2][mob];
-
- if (Pt == BISHOP || Pt == KNIGHT)