- 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[COLOR_NB][128];
-
- // TracedTerms[Color][PieceType || TracedType] contains a breakdown of the
- // evaluation terms, used when tracing.
- Score TracedScores[COLOR_NB][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 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>
- Score evaluate_threats(const Position& pos, EvalInfo& ei);
-
- template<Color Us>
- int evaluate_space(const Position& pos, EvalInfo& ei);
-
- template<Color Us>
- Score evaluate_passed_pawns(const Position& pos, EvalInfo& ei);
-
- Score evaluate_unstoppable_pawns(const Position& pos, EvalInfo& ei);
-
- Value interpolate(const Score& v, Phase ph, ScaleFactor sf);
- Score weight_option(const std::string& mgOpt, const std::string& egOpt, Score internalWeight);
- double to_cp(Value v);
- void trace_add(int idx, Score term_w, Score term_b = SCORE_ZERO);
- void trace_row(const char* name, int idx);
-}
-
-
-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);
- }
-
-
- /// init() computes evaluation weights from the corresponding UCI parameters
- /// and setup king tables.
-
- void init() {
-
- Weights[Mobility] = weight_option("Mobility (Middle Game)", "Mobility (Endgame)", WeightsInternal[Mobility]);
- Weights[PassedPawns] = weight_option("Passed Pawns (Middle Game)", "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));
-
- KingDangerTable[1][i] = apply_weight(make_score(t, 0), Weights[KingDangerUs]);
- KingDangerTable[0][i] = apply_weight(make_score(t, 0), Weights[KingDangerThem]);
- }
- }
-
-
- /// 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) {
-
- Value margin;
- std::string totals;
-
- Search::RootColor = pos.side_to_move();
-
- TraceStream.str("");
- TraceStream << std::showpoint << std::showpos << std::fixed << std::setprecision(2);
- memset(TracedScores, 0, 2 * 16 * sizeof(Score));
-
- do_evaluate<true>(pos, margin);
-
- totals = TraceStream.str();
- TraceStream.str("");
-
- TraceStream << std::setw(21) << "Eval term " << "| White | Black | Total \n"
- << " | MG EG | MG EG | MG EG \n"
- << "---------------------+-------------+-------------+---------------\n";
-
- trace_row("Material, PST, Tempo", PST);
- trace_row("Material imbalance", IMBALANCE);
- trace_row("Pawns", PAWN);
- trace_row("Knights", KNIGHT);
- trace_row("Bishops", BISHOP);
- trace_row("Rooks", ROOK);
- trace_row("Queens", QUEEN);
- trace_row("Mobility", MOBILITY);
- trace_row("King safety", KING);
- trace_row("Threats", THREAT);
- trace_row("Passed pawns", PASSED);
- trace_row("Unstoppable pawns", UNSTOPPABLE);
- trace_row("Space", SPACE);
-
- TraceStream << "---------------------+-------------+-------------+---------------\n";
- trace_row("Total", TOTAL);
- TraceStream << totals;
-
- return TraceStream.str();
- }
-
-} // namespace Eval
-