+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]);
+
+ int KingDanger[] = { KingDangerWeights[0], KingDangerWeights[1] };
+
+ // If running in analysis mode, make sure we use symmetrical king safety.
+ // We do so by replacing both KingDanger weights by their average.
+ if (Options["UCI_AnalyseMode"])
+ KingDanger[0] = KingDanger[1] = (KingDanger[0] + KingDanger[1]) / 2;
+
+ 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[0][i] = apply_weight(make_score(t, 0), make_score(KingDanger[0], 0));
+ KingDangerTable[1][i] = apply_weight(make_score(t, 0), make_score(KingDanger[1], 0));
+ }
+ }
+
+
+ /// 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