- // KingDanger[Color][attackUnits] contains the actual king danger weighted
- // scores, indexed by color and by a calculated integer number.
- Score KingDanger[COLOR_NB][128];
-
- // Function prototypes
- template<bool Trace>
- Value do_evaluate(const Position& pos, Value& margin);
-
- template<Color Us>
- void init_eval_info(const Position& pos, EvalInfo& ei);
-
- template<Color Us, bool Trace>
- Score evaluate_pieces_of_color(const Position& pos, EvalInfo& ei, Score& mobility);
-
- template<Color Us, bool Trace>
- Score evaluate_king(const Position& pos, EvalInfo& ei, Value margins[]);
-
- template<Color Us, bool Trace>
- Score evaluate_threats(const Position& pos, EvalInfo& ei);
-
- template<Color Us, bool Trace>
- Score evaluate_passed_pawns(const Position& pos, EvalInfo& ei);
-
- template<Color Us>
- int evaluate_space(const Position& pos, EvalInfo& ei);
-
- Score evaluate_unstoppable_pawns(const Position& pos, EvalInfo& ei);
-
- Value interpolate(const Score& v, Phase ph, ScaleFactor sf);
- Score apply_weight(Score v, Score w);
- Score weight_option(const std::string& mgOpt, const std::string& egOpt, Score internalWeight);
- double to_cp(Value v);
-}
-
-
-namespace Eval {
-
- /// 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 do_evaluate<false>(pos, margin);
- }
-
-
- /// trace() is like evaluate() but instead of a value returns a string suitable
- /// to be print on stdout with the detailed descriptions and values of each
- /// evaluation term. Used mainly for debugging.
- std::string trace(const Position& pos) {
- return Tracing::do_trace(pos);
- }
-
-
- /// init() computes evaluation weights from the corresponding UCI parameters
- /// and setup king tables.
-
- void init() {
-
- Weights[Mobility] = weight_option("Mobility (Midgame)", "Mobility (Endgame)", WeightsInternal[Mobility]);
- Weights[PawnStructure] = weight_option("Pawn Structure (Midgame)", "Pawn Structure (Endgame)", WeightsInternal[PawnStructure]);
- Weights[PassedPawns] = weight_option("Passed Pawns (Midgame)", "Passed Pawns (Endgame)", WeightsInternal[PassedPawns]);
- Weights[Space] = weight_option("Space", "Space", WeightsInternal[Space]);
- Weights[KingDangerUs] = weight_option("Cowardice", "Cowardice", WeightsInternal[KingDangerUs]);
- Weights[KingDangerThem] = weight_option("Aggressiveness", "Aggressiveness", WeightsInternal[KingDangerThem]);
-
- const int MaxSlope = 30;
- const int Peak = 1280;
-
- for (int t = 0, i = 1; i < 100; i++)
- {
- t = std::min(Peak, std::min(int(0.4 * i * i), t + MaxSlope));
-
- KingDanger[1][i] = apply_weight(make_score(t, 0), Weights[KingDangerUs]);
- KingDanger[0][i] = apply_weight(make_score(t, 0), Weights[KingDangerThem]);
- }
- }
-
-} // namespace Eval
-
-
-namespace {
-
-template<bool Trace>
-Value do_evaluate(const Position& pos, Value& margin) {
-
- assert(!pos.checkers());
-
- EvalInfo ei;
- Value margins[COLOR_NB];
- Score score, mobilityWhite, mobilityBlack;
- Thread* th = pos.this_thread();
-
- // 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;
-
- // Initialize score by reading the incrementally updated scores included
- // in the position object (material + piece square tables) and adding
- // Tempo bonus. Score is computed from the point of view of white.
- score = pos.psq_score() + (pos.side_to_move() == WHITE ? Tempo : -Tempo);
-
- // Probe the material hash table
- ei.mi = Material::probe(pos, th->materialTable, th->endgames);
- 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 = Pawns::probe(pos, th->pawnsTable);
- score += apply_weight(ei.pi->pawns_value(), Weights[PawnStructure]);
-
- // Initialize attack and king safety bitboards
- init_eval_info<WHITE>(pos, ei);
- init_eval_info<BLACK>(pos, ei);
-
- // Evaluate pieces and mobility
- score += evaluate_pieces_of_color<WHITE, Trace>(pos, ei, mobilityWhite)
- - evaluate_pieces_of_color<BLACK, 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, Trace>(pos, ei, margins)
- - evaluate_king<BLACK, Trace>(pos, ei, margins);
-
- // Evaluate tactical threats, we need full attack information including king
- score += evaluate_threats<WHITE, Trace>(pos, ei)
- - evaluate_threats<BLACK, Trace>(pos, ei);
-
- // Evaluate passed pawns, we need full attack information including king
- score += evaluate_passed_pawns<WHITE, Trace>(pos, ei)
- - evaluate_passed_pawns<BLACK, Trace>(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(pos, ei);