- const int QueenContactCheckBonus = 6;
- const int RookContactCheckBonus = 4;
- const int QueenCheckBonus = 3;
- const int RookCheckBonus = 2;
- const int BishopCheckBonus = 1;
- const int KnightCheckBonus = 1;
-
- // InitKingDanger[Square] contains penalties based on the position of the
- // defending king, indexed by king's square (from white's point of view).
- const int InitKingDanger[] = {
- 2, 0, 2, 5, 5, 2, 0, 2,
- 2, 2, 4, 8, 8, 4, 2, 2,
- 7, 10, 12, 12, 12, 12, 10, 7,
- 15, 15, 15, 15, 15, 15, 15, 15,
- 15, 15, 15, 15, 15, 15, 15, 15,
- 15, 15, 15, 15, 15, 15, 15, 15,
- 15, 15, 15, 15, 15, 15, 15, 15,
- 15, 15, 15, 15, 15, 15, 15, 15
- };
-
- // KingDangerTable[Color][attackUnits] contains the actual king danger
- // weighted scores, indexed by color and by a calculated integer number.
- Score KingDangerTable[2][128];
-
- // TracedTerms[Color][PieceType || TracedType] contains a breakdown of the
- // evaluation terms, used when tracing.
- Score TracedScores[2][16];
- std::stringstream TraceStream;
-
- enum TracedType {
- PST = 8, IMBALANCE = 9, MOBILITY = 10, THREAT = 11,
- PASSED = 12, UNSTOPPABLE = 13, SPACE = 14, TOTAL = 15
- };
-
- // Function prototypes
- template<bool HasPopCnt, bool Trace>
- Value do_evaluate(const Position& pos, Value& margin);
-
- template<Color Us, bool HasPopCnt>
- void init_eval_info(const Position& pos, EvalInfo& ei);
-
- template<Color Us, bool HasPopCnt, bool Trace>
- Score evaluate_pieces_of_color(const Position& pos, EvalInfo& ei, Score& mobility);
-
- template<Color Us, bool HasPopCnt, bool Trace>
- Score evaluate_king(const Position& pos, EvalInfo& ei, Value margins[]);
-
- template<Color Us>
- Score evaluate_threats(const Position& pos, EvalInfo& ei);
-
- template<Color Us, bool HasPopCnt>
- int evaluate_space(const Position& pos, EvalInfo& ei);
-
- template<Color Us>
- Score evaluate_passed_pawns(const Position& pos, EvalInfo& ei);
-
- template<bool HasPopCnt>
- Score evaluate_unstoppable_pawns(const Position& pos, EvalInfo& ei);
-
- inline Score apply_weight(Score v, Score weight);
- Value scale_by_game_phase(const Score& v, Phase ph, ScaleFactor sf);
- Score weight_option(const std::string& mgOpt, const std::string& egOpt, Score internalWeight);
- void init_safety();
- double to_cp(Value v);
- void trace_add(int idx, Score term_w, Score term_b = SCORE_ZERO);
-}
-
-
-/// evaluate() is the main evaluation function. It always computes two
-/// values, an endgame score and a middle game score, and interpolates
-/// between them based on the remaining material.
-Value evaluate(const Position& pos, Value& margin) {
-
- return CpuHasPOPCNT ? do_evaluate<true, false>(pos, margin)
- : do_evaluate<false, false>(pos, margin);
-}
-
-namespace {
-
-template<bool HasPopCnt, bool Trace>
-Value do_evaluate(const Position& pos, Value& margin) {
-
- EvalInfo ei;
- Value margins[2];
- Score score, mobilityWhite, mobilityBlack;
-
- assert(pos.is_ok());
- assert(pos.thread() >= 0 && pos.thread() < MAX_THREADS);
- assert(!pos.in_check());
-
- // Initialize score by reading the incrementally updated scores included
- // in the position object (material + piece square tables).
- score = pos.value();
-
- // margins[] store the uncertainty estimation of position's evaluation
- // that typically is used by the search for pruning decisions.
- margins[WHITE] = margins[BLACK] = VALUE_ZERO;
-
- // Probe the material hash table
- ei.mi = Threads[pos.thread()].materialTable.get_material_info(pos);
- score += ei.mi->material_value();
-
- // If we have a specialized evaluation function for the current material
- // configuration, call it and return.
- if (ei.mi->specialized_eval_exists())
- {
- margin = VALUE_ZERO;
- return ei.mi->evaluate(pos);
- }
-
- // Probe the pawn hash table
- ei.pi = Threads[pos.thread()].pawnTable.get_pawn_info(pos);
- score += ei.pi->pawns_value();
-
- // Initialize attack and king safety bitboards
- init_eval_info<WHITE, HasPopCnt>(pos, ei);
- init_eval_info<BLACK, HasPopCnt>(pos, ei);
-
- // Evaluate pieces and mobility
- score += evaluate_pieces_of_color<WHITE, HasPopCnt, Trace>(pos, ei, mobilityWhite)
- - evaluate_pieces_of_color<BLACK, HasPopCnt, Trace>(pos, ei, mobilityBlack);
-
- score += apply_weight(mobilityWhite - mobilityBlack, Weights[Mobility]);
-
- // Evaluate kings after all other pieces because we need complete attack
- // information when computing the king safety evaluation.
- score += evaluate_king<WHITE, HasPopCnt, Trace>(pos, ei, margins)
- - evaluate_king<BLACK, HasPopCnt, Trace>(pos, ei, margins);
-
- // Evaluate tactical threats, we need full attack information including king
- score += evaluate_threats<WHITE>(pos, ei)
- - evaluate_threats<BLACK>(pos, ei);
-
- // Evaluate passed pawns, we need full attack information including king
- score += evaluate_passed_pawns<WHITE>(pos, ei)
- - evaluate_passed_pawns<BLACK>(pos, ei);
-
- // If one side has only a king, check whether exists any unstoppable passed pawn
- if (!pos.non_pawn_material(WHITE) || !pos.non_pawn_material(BLACK))
- score += evaluate_unstoppable_pawns<HasPopCnt>(pos, ei);
-
- // Evaluate space for both sides, only in middle-game.
- if (ei.mi->space_weight())
- {
- int s = evaluate_space<WHITE, HasPopCnt>(pos, ei) - evaluate_space<BLACK, HasPopCnt>(pos, ei);
- score += apply_weight(make_score(s * ei.mi->space_weight(), 0), Weights[Space]);
- }
-
- // Scale winning side if position is more drawish that what it appears
- ScaleFactor sf = eg_value(score) > VALUE_DRAW ? ei.mi->scale_factor(pos, WHITE)
- : ei.mi->scale_factor(pos, BLACK);
-
- // If we don't already have an unusual scale factor, check for opposite
- // colored bishop endgames, and use a lower scale for those.
- if ( ei.mi->game_phase() < PHASE_MIDGAME
- && pos.opposite_colored_bishops()
- && sf == SCALE_FACTOR_NORMAL)
- {
- // Only the two bishops ?
- if ( pos.non_pawn_material(WHITE) == BishopValueMidgame
- && pos.non_pawn_material(BLACK) == BishopValueMidgame)
- {
- // Check for KBP vs KB with only a single pawn that is almost
- // certainly a draw or at least two pawns.
- bool one_pawn = (pos.piece_count(WHITE, PAWN) + pos.piece_count(BLACK, PAWN) == 1);
- sf = one_pawn ? ScaleFactor(8) : ScaleFactor(32);
- }
- else
- // Endgame with opposite-colored bishops, but also other pieces. Still
- // a bit drawish, but not as drawish as with only the two bishops.
- sf = ScaleFactor(50);
- }
-
- // Interpolate between the middle game and the endgame score
- margin = margins[pos.side_to_move()];
- Value v = scale_by_game_phase(score, ei.mi->game_phase(), sf);
-
- // In case of tracing add all single evaluation contributions for both white and black
- if (Trace)
- {
- trace_add(PST, pos.value());
- trace_add(IMBALANCE, ei.mi->material_value());
- trace_add(PAWN, ei.pi->pawns_value());
- trace_add(MOBILITY, apply_weight(mobilityWhite, Weights[Mobility]), apply_weight(mobilityBlack, Weights[Mobility]));
- trace_add(THREAT, evaluate_threats<WHITE>(pos, ei), evaluate_threats<BLACK>(pos, ei));
- trace_add(PASSED, evaluate_passed_pawns<WHITE>(pos, ei), evaluate_passed_pawns<BLACK>(pos, ei));
- trace_add(UNSTOPPABLE, evaluate_unstoppable_pawns<false>(pos, ei));
- Score w = make_score(ei.mi->space_weight() * evaluate_space<WHITE, false>(pos, ei), 0);
- Score b = make_score(ei.mi->space_weight() * evaluate_space<BLACK, false>(pos, ei), 0);
- trace_add(SPACE, apply_weight(w, Weights[Space]), apply_weight(b, Weights[Space]));
- trace_add(TOTAL, score);
- TraceStream << "\nUncertainty margin: White: " << to_cp(margins[WHITE])
- << ", Black: " << to_cp(margins[BLACK])
- << "\nScaling: " << std::noshowpos
- << std::setw(6) << 100.0 * ei.mi->game_phase() / 128.0 << "% MG, "
- << std::setw(6) << 100.0 * (1.0 - ei.mi->game_phase() / 128.0) << "% * "
- << std::setw(6) << (100.0 * sf) / SCALE_FACTOR_NORMAL << "% EG.\n"
- << "Total evaluation: " << to_cp(v);