namespace {
+ namespace Tracing {
+
+ enum Term { // First 8 entries are for PieceType
+ MATERIAL = 8, IMBALANCE, MOBILITY, THREAT, PASSED, SPACE, TOTAL, TERM_NB
+ };
+
+ Score scores[COLOR_NB][TERM_NB];
+
+ std::ostream& operator<<(std::ostream& os, Term idx);
+
+ double to_cp(Value v);
+ void write(int idx, Color c, Score s);
+ void write(int idx, Score w, Score b = SCORE_ZERO);
+ std::string do_trace(const Position& pos);
+ }
+
+
// Struct EvalInfo contains various information computed and collected
// by the evaluation functions.
struct EvalInfo {
// KingAttackWeights array.
int kingAttackersWeight[COLOR_NB];
- // kingAdjacentZoneAttacksCount[color] is the number of attacks to squares
- // directly adjacent to the king of the given color. Pieces which attack
- // more than one square are counted multiple times. For instance, if black's
- // king is on g8 and there's a white knight on g5, this knight adds
- // 2 to kingAdjacentZoneAttacksCount[BLACK].
+ // kingAdjacentZoneAttacksCount[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 kingAdjacentZoneAttacksCount[WHITE].
int kingAdjacentZoneAttacksCount[COLOR_NB];
Bitboard pinnedPieces[COLOR_NB];
};
- namespace Tracing {
-
- enum Term { // First 8 entries are for PieceType
- MATERIAL = 8, IMBALANCE, MOBILITY, THREAT, PASSED, SPACE, TOTAL, TERMS_NB
- };
-
- Score scores[COLOR_NB][TERMS_NB];
-
- std::ostream& operator<<(std::ostream& os, Term idx);
-
- double to_cp(Value v);
- void write(int idx, Color c, Score s);
- void write(int idx, Score w, Score b = SCORE_ZERO);
- std::string do_trace(const Position& pos);
- }
- // Evaluation weights, indexed by evaluation term
+ // Evaluation weights, indexed by the corresponding evaluation term
enum { Mobility, PawnStructure, PassedPawns, Space, KingSafety };
+
const struct Weight { int mg, eg; } Weights[] = {
{289, 344}, {233, 201}, {221, 273}, {46, 0}, {322, 0}
};
+ Score operator*(Score s, const Weight& w) {
+ return make_score(mg_value(s) * w.mg / 256, eg_value(s) * w.eg / 256);
+ }
+
+
#define V(v) Value(v)
#define S(mg, eg) make_score(mg, eg)
// friendly pieces.
const Score MobilityBonus[][32] = {
{}, {},
- { S(-65,-50), S(-42,-30), S(-9,-10), S( 3, 0), S(15, 10), S(27, 20), // Knights
- S( 37, 28), S( 42, 31), S(44, 33) },
- { S(-52,-47), S(-28,-23), S( 6, 1), S(20, 15), S(34, 29), S(48, 43), // Bishops
- S( 60, 55), S( 68, 63), S(74, 68), S(77, 72), S(80, 75), S(82, 77),
- S( 84, 79), S( 86, 81) },
- { S(-47,-53), S(-31,-26), S(-5, 0), S( 1, 16), S( 7, 32), S(13, 48), // Rooks
- S( 18, 64), S( 22, 80), S(26, 96), S(29,109), S(31,115), S(33,119),
- S( 35,122), S( 36,123), S(37,124) },
- { S(-42,-40), S(-28,-23), S(-5, -7), S( 0, 0), S( 6, 10), S(11, 19), // Queens
- S( 13, 29), S( 18, 38), S(20, 40), S(21, 41), S(22, 41), S(22, 41),
- S( 22, 41), S( 23, 41), S(24, 41), S(25, 41), S(25, 41), S(25, 41),
- S( 25, 41), S( 25, 41), S(25, 41), S(25, 41), S(25, 41), S(25, 41),
- S( 25, 41), S( 25, 41), S(25, 41), S(25, 41) }
+ { S(-67,-48), S(-41,-27), S(-4, -7), S( 7, 4), S(14, 16), S(21, 29), // Knights
+ S( 27, 39), S( 31, 43), S(34, 44) },
+ { S(-48,-48), S(-21,-21), S(16, -2), S(30, 13), S(44, 25), S(57, 34), // Bishops
+ S( 69, 47), S( 78, 53), S(78, 56), S(83, 58), S(88, 64), S(90, 68),
+ S( 90, 74), S( 91, 76) },
+ { S(-47,-56), S(-28,-27), S(-9, 1), S( 0, 22), S( 2, 33), S( 7, 47), // Rooks
+ S( 9, 62), S( 14, 78), S(21, 89), S(23,105), S(25,113), S(26,114),
+ S( 27,115), S( 29,118), S(32,122) },
+ { S(-46,-37), S(-33,-18), S(-9,-10), S(-3, -7), S( 7, 1), S( 9, 5), // Queens
+ S( 12, 10), S( 20, 19), S(23, 22), S(24, 29), S(30, 30), S(32, 34),
+ S( 37, 35), S( 47, 38), S(47, 41), S(47, 42), S(52, 42), S(55, 42),
+ S( 58, 43), S( 60, 49), S(60, 52), S(61, 56), S(65, 57), S(68, 57),
+ S( 68, 59), S( 71, 61), S(72, 63), S(76, 63) }
};
// Outpost[Bishop/Knight][Square] contains bonuses for knights and bishops
{// A B C D E F G H
V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), // Knights
V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0),
- V(0), V(0), V(4), V(8), V(8), V(4), V(0), V(0),
- V(0), V(4),V(17),V(26),V(26),V(17), V(4), V(0),
- V(0), V(8),V(26),V(35),V(35),V(26), V(8), V(0),
- V(0), V(4),V(17),V(17),V(17),V(17), V(4), V(0) },
+ V(0), V(0), V(3), V(9), V(9), V(3), V(0), V(0),
+ V(0), V(4),V(18),V(25),V(25),V(18), V(4), V(0),
+ V(4), V(9),V(29),V(38),V(38),V(29), V(9), V(4),
+ V(2), V(9),V(19),V(15),V(15),V(19), V(9), V(2) },
{
V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), // Bishops
V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0),
- V(0), V(0), V(5), V(5), V(5), V(5), V(0), V(0),
- V(0), V(5),V(10),V(10),V(10),V(10), V(5), V(0),
- V(0),V(10),V(21),V(21),V(21),V(21),V(10), V(0),
- V(0), V(5), V(8), V(8), V(8), V(8), V(5), V(0) }
+ V(2), V(4), V(3), V(8), V(8), V(3), V(4), V(2),
+ V(1), V(9), V(9),V(13),V(13), V(9), V(9), V(1),
+ V(2), V(8),V(21),V(24),V(24),V(21), V(8), V(2),
+ V(0), V(4), V(6), V(6), V(6), V(6), V(4), V(0) }
};
// Threat[defended/weak][minor/major attacking][attacked PieceType] contains
// ThreatenedByPawn[PieceType] contains a penalty according to which piece
// type is attacked by an enemy pawn.
const Score ThreatenedByPawn[PIECE_TYPE_NB] = {
- S(0, 0), S(0, 0), S(87, 118), S(84, 122), S(114, 203), S(121, 217)
+ S(0, 0), S(0, 0), S(107, 138), S(84, 122), S(114, 203), S(121, 217)
};
// Assorted bonuses and penalties used by evaluation
const Score Unstoppable = S( 0, 20);
const Score Hanging = S(31, 26);
const Score PawnAttackThreat = S(20, 20);
+ const Score PawnSafePush = S( 5, 5);
// Penalty for a bishop on a1/h1 (a8/h8 for black) which is trapped by
// a friendly pawn on b2/g2 (b7/g7 for black). This can obviously only
// in KingDanger[]. Various little "meta-bonuses" measuring the strength
// of the enemy attack are added up into an integer, which is used as an
// index to KingDanger[].
- //
+ Score KingDanger[512];
+
// KingAttackWeights[PieceType] contains king attack weights by piece type
const int KingAttackWeights[PIECE_TYPE_NB] = { 0, 0, 7, 5, 4, 1 };
- // Bonuses for enemy's safe checks
+ // Penalties for enemy's safe checks
const int QueenContactCheck = 89;
const int RookContactCheck = 71;
const int QueenCheck = 50;
const int BishopCheck = 6;
const int KnightCheck = 14;
- // KingDanger[attackUnits] contains the actual king danger weighted
- // scores, indexed by a calculated integer number.
- Score KingDanger[512];
-
- // apply_weight() weighs score 's' by weight 'w' trying to prevent overflow
- Score apply_weight(Score s, const Weight& w) {
- return make_score(mg_value(s) * w.mg / 256, eg_value(s) * w.eg / 256);
- }
-
// init_eval_info() initializes king bitboards for given color adding
// pawn attacks. To be done at the beginning of the evaluation.
| ei.attackedBy[Them][BISHOP]
| ei.attackedBy[Them][ROOK]);
- int mob = Pt != QUEEN ? popcount<Max15>(b & mobilityArea[Us])
- : popcount<Full >(b & mobilityArea[Us]);
+ int mob = popcount<Pt == QUEEN ? Full : Max15>(b & mobilityArea[Us]);
mobility[Us] += MobilityBonus[Pt][mob];
// attacked and undefended squares around our king and the quality of
// the pawn shelter (current 'score' value).
attackUnits = std::min(74, ei.kingAttackersCount[Them] * ei.kingAttackersWeight[Them])
- + 8 * ei.kingAdjacentZoneAttacksCount[Them]
+ + 8 * ei.kingAdjacentZoneAttacksCount[Them]
+ 25 * popcount<Max15>(undefended)
- + 11 * (ei.pinnedPieces[Us] != 0)
- - mg_value(score) * 31 / 256
+ + 11 * (ei.pinnedPieces[Us] != 0)
+ - mg_value(score) / 8
- !pos.count<QUEEN>(Them) * 60;
// Analyse the enemy's safe queen contact checks. Firstly, find the
Score score = SCORE_ZERO;
// Non-pawn enemies defended by a pawn
- defended = (pos.pieces(Them) ^ pos.pieces(Them, PAWN))
- & ei.attackedBy[Them][PAWN];
+ defended = (pos.pieces(Them) ^ pos.pieces(Them, PAWN)) & ei.attackedBy[Them][PAWN];
// Add a bonus according to the kind of attacking pieces
if (defended)
score += more_than_one(b) ? KingOnMany : KingOnOne;
}
- // Add bonus for safe pawn pushes which attacks an enemy piece
+ // Add a small bonus for safe pawn pushes
b = pos.pieces(Us, PAWN) & ~TRank7BB;
b = shift_bb<Up>(b | (shift_bb<Up>(b & TRank2BB) & ~pos.pieces()));
b &= ~pos.pieces()
& ~ei.attackedBy[Them][PAWN]
- & (ei.attackedBy[Us][PAWN] | ~ei.attackedBy[Them][ALL_PIECES]);
+ & (ei.attackedBy[Us][ALL_PIECES] | ~ei.attackedBy[Them][ALL_PIECES]);
+
+ if (b)
+ score += popcount<Full>(b) * PawnSafePush;
+ // Add another bonus if the pawn push attacks an enemy piece
b = (shift_bb<Left>(b) | shift_bb<Right>(b))
& pos.pieces(Them)
& ~ei.attackedBy[Us][PAWN];
}
if (Trace)
- Tracing::write(Tracing::PASSED, Us, apply_weight(score, Weights[PassedPawns]));
+ Tracing::write(Tracing::PASSED, Us, score * Weights[PassedPawns]);
// Add the scores to the middlegame and endgame eval
- return apply_weight(score, Weights[PassedPawns]);
+ return score * Weights[PassedPawns];
}
// Probe the pawn hash table
ei.pi = Pawns::probe(pos);
- score += apply_weight(ei.pi->pawns_score(), Weights[PawnStructure]);
+ score += ei.pi->pawns_score() * Weights[PawnStructure];
// Initialize attack and king safety bitboards
init_eval_info<WHITE>(pos, ei);
// Evaluate pieces and mobility
score += evaluate_pieces<KNIGHT, WHITE, Trace>(pos, ei, mobility, mobilityArea);
- score += apply_weight(mobility[WHITE] - mobility[BLACK], Weights[Mobility]);
+ score += (mobility[WHITE] - mobility[BLACK]) * Weights[Mobility];
// Evaluate kings after all other pieces because we need complete attack
// information when computing the king safety evaluation.
}
// Evaluate space for both sides, only during opening
- if (pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) >= 2 * QueenValueMg + 4 * RookValueMg + 2 * KnightValueMg)
- {
- Score s = evaluate_space<WHITE>(pos, ei) - evaluate_space<BLACK>(pos, ei);
- score += apply_weight(s, Weights[Space]);
- }
+ if (pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) >= 11756)
+ score += (evaluate_space<WHITE>(pos, ei) - evaluate_space<BLACK>(pos, ei)) * Weights[Space];
// Scale winning side if position is more drawish than it appears
Color strongSide = eg_value(score) > VALUE_DRAW ? WHITE : BLACK;
Tracing::write(Tracing::MATERIAL, pos.psq_score());
Tracing::write(Tracing::IMBALANCE, ei.mi->imbalance());
Tracing::write(PAWN, ei.pi->pawns_score());
- Tracing::write(Tracing::MOBILITY, apply_weight(mobility[WHITE], Weights[Mobility])
- , apply_weight(mobility[BLACK], Weights[Mobility]));
- Tracing::write(Tracing::SPACE, apply_weight(evaluate_space<WHITE>(pos, ei), Weights[Space])
- , apply_weight(evaluate_space<BLACK>(pos, ei), Weights[Space]));
+ Tracing::write(Tracing::MOBILITY, mobility[WHITE] * Weights[Mobility]
+ , mobility[BLACK] * Weights[Mobility]);
+ Tracing::write(Tracing::SPACE, evaluate_space<WHITE>(pos, ei) * Weights[Space]
+ , evaluate_space<BLACK>(pos, ei) * Weights[Space]);
Tracing::write(Tracing::TOTAL, score);
}
- return (pos.side_to_move() == WHITE ? v : -v) + Eval::Tempo;
+ return (pos.side_to_move() == WHITE ? v : -v) + Eval::Tempo; // Side to move point of view
}
void init() {
- const double MaxSlope = 8.7;
- const double Peak = 1280;
- double t = 0.0;
+ const int MaxSlope = 8700;
+ const int Peak = 1280000;
+ int t = 0;
- for (int i = 1; i < 400; ++i)
+ for (int i = 0; i < 400; ++i)
{
- t = std::min(Peak, std::min(0.027 * i * i, t + MaxSlope));
- KingDanger[i] = apply_weight(make_score(int(t), 0), Weights[KingSafety]);
+ t = std::min(Peak, std::min(i * i * 27, t + MaxSlope));
+ KingDanger[i] = make_score(t / 1000, 0) * Weights[KingSafety];
}
}