////
#include <cassert>
+#include <iostream>
+#include <iomanip>
+#include <sstream>
#include "bitcount.h"
#include "evaluate.h"
// 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 TracedTerms[2][16];
+ std::stringstream TraceStream;
+
+ enum TracedType {
+ PST = 8, IMBALANCE = 9, MOBILITY = 10, THREAT = 11,
+ PASSED = 12, UNSTOPPABLE = 13, SPACE = 14, TOTAL = 15
+ };
+
// Pawn and material hash tables, indexed by the current thread id.
// Note that they will be initialized at 0 being global variables.
MaterialInfoTable* MaterialTable[MAX_THREADS];
PawnInfoTable* PawnTable[MAX_THREADS];
// Function prototypes
- template<bool HasPopCnt>
+ 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>
+ template<Color Us, bool HasPopCnt, bool Trace>
Score evaluate_pieces_of_color(const Position& pos, EvalInfo& ei, Score& mobility);
- template<Color Us, bool HasPopCnt>
+ template<Color Us, bool HasPopCnt, bool Trace>
Score evaluate_king(const Position& pos, EvalInfo& ei, Value margins[]);
template<Color Us>
}
-////
-//// Functions
-////
-
+/// prefetchTables() is called in do_move() to prefetch pawn and material
+/// hash tables data that will be needed shortly after in evaluation.
-/// Prefetches in pawn hash tables
+void prefetchTables(Key pKey, Key mKey, int threadID) {
-void prefetchPawn(Key key, int threadID) {
-
- PawnTable[threadID]->prefetch(key);
+ PawnTable[threadID]->prefetch(pKey);
+ MaterialTable[threadID]->prefetch(mKey);
}
/// between them based on the remaining material.
Value evaluate(const Position& pos, Value& margin) {
- return CpuHasPOPCNT ? do_evaluate<true>(pos, margin)
- : do_evaluate<false>(pos, margin);
+ return CpuHasPOPCNT ? do_evaluate<true, false>(pos, margin)
+ : do_evaluate<false, false>(pos, margin);
}
namespace {
-template<bool HasPopCnt>
+double to_cp(Value v) { return double(v) / double(PawnValueMidgame); }
+
+void trace_add(int idx, Score term_w, Score term_b = Score(0)) {
+
+ TracedTerms[WHITE][idx] = term_w;
+ TracedTerms[BLACK][idx] = term_b;
+}
+
+template<bool HasPopCnt, bool Trace>
Value do_evaluate(const Position& pos, Value& margin) {
EvalInfo ei;
init_eval_info<BLACK, HasPopCnt>(pos, ei);
// Evaluate pieces and mobility
- bonus += evaluate_pieces_of_color<WHITE, HasPopCnt>(pos, ei, mobilityWhite)
- - evaluate_pieces_of_color<BLACK, HasPopCnt>(pos, ei, mobilityBlack);
+ bonus += evaluate_pieces_of_color<WHITE, HasPopCnt, Trace>(pos, ei, mobilityWhite)
+ - evaluate_pieces_of_color<BLACK, HasPopCnt, Trace>(pos, ei, mobilityBlack);
bonus += apply_weight(mobilityWhite - mobilityBlack, Weights[Mobility]);
// Evaluate kings after all other pieces because we need complete attack
// information when computing the king safety evaluation.
- bonus += evaluate_king<WHITE, HasPopCnt>(pos, ei, margins)
- - evaluate_king<BLACK, HasPopCnt>(pos, ei, margins);
+ bonus += 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
bonus += evaluate_threats<WHITE>(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))
+ {
bonus += evaluate_unstoppable_pawns<HasPopCnt>(pos, ei);
+ if (Trace)
+ trace_add(UNSTOPPABLE, evaluate_unstoppable_pawns<HasPopCnt>(pos, ei));
+ }
+
// Evaluate space for both sides, only in middle-game.
if (mi->space_weight())
{
- int s = evaluate_space<WHITE, HasPopCnt>(pos, ei) - evaluate_space<BLACK, HasPopCnt>(pos, ei);
- bonus += apply_weight(make_score(s * mi->space_weight(), 0), Weights[Space]);
+ int s_w = evaluate_space<WHITE, HasPopCnt>(pos, ei);
+ int s_b = evaluate_space<BLACK, HasPopCnt>(pos, ei);
+ bonus += apply_weight(make_score((s_w - s_b) * mi->space_weight(), 0), Weights[Space]);
+
+ if (Trace)
+ trace_add(SPACE, apply_weight(make_score(s_w * mi->space_weight(), make_score(0, 0)), Weights[Space]),
+ apply_weight(make_score(s_b * mi->space_weight(), make_score(0, 0)), Weights[Space]));
}
// Scale winning side if position is more drawish that what it appears
// Interpolate between the middle game and the endgame score
margin = margins[pos.side_to_move()];
Value v = scale_by_game_phase(bonus, phase, sf);
+
+ if (Trace)
+ {
+ trace_add(PST, pos.value());
+ trace_add(IMBALANCE, mi->material_value());
+ trace_add(PAWN, apply_weight(ei.pi->pawns_value(), Weights[PawnStructure]));
+ 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(TOTAL, bonus);
+ TraceStream << "\nUncertainty margin: White: " << to_cp(margins[WHITE])
+ << ", Black: " << to_cp(margins[BLACK])
+ << "\nScaling: " << std::noshowpos
+ << std::setw(6) << 100.0 * phase/128.0 << "% MG, "
+ << std::setw(6) << 100.0 * (1.0 - phase/128.0) << "% * "
+ << std::setw(6) << (100.0 * sf) / SCALE_FACTOR_NORMAL << "% EG.\n"
+ << "Total evaluation: " << to_cp(v);
+ }
+
return pos.side_to_move() == WHITE ? v : -v;
}
// evaluate_pieces<>() assigns bonuses and penalties to the pieces of a given color
- template<PieceType Piece, Color Us, bool HasPopCnt>
+ template<PieceType Piece, Color Us, bool HasPopCnt, bool Trace>
Score evaluate_pieces(const Position& pos, EvalInfo& ei, Score& mobility, Bitboard mobilityArea) {
Bitboard b;
}
}
}
+
+ if (Trace)
+ TracedTerms[Us][Piece] = bonus;
+
return bonus;
}
// evaluate_pieces_of_color<>() assigns bonuses and penalties to all the
// pieces of a given color.
- template<Color Us, bool HasPopCnt>
+ template<Color Us, bool HasPopCnt, bool Trace>
Score evaluate_pieces_of_color(const Position& pos, EvalInfo& ei, Score& mobility) {
const Color Them = (Us == WHITE ? BLACK : WHITE);
// Do not include in mobility squares protected by enemy pawns or occupied by our pieces
const Bitboard mobilityArea = ~(ei.attackedBy[Them][PAWN] | pos.pieces_of_color(Us));
- bonus += evaluate_pieces<KNIGHT, Us, HasPopCnt>(pos, ei, mobility, mobilityArea);
- bonus += evaluate_pieces<BISHOP, Us, HasPopCnt>(pos, ei, mobility, mobilityArea);
- bonus += evaluate_pieces<ROOK, Us, HasPopCnt>(pos, ei, mobility, mobilityArea);
- bonus += evaluate_pieces<QUEEN, Us, HasPopCnt>(pos, ei, mobility, mobilityArea);
+ bonus += evaluate_pieces<KNIGHT, Us, HasPopCnt, Trace>(pos, ei, mobility, mobilityArea);
+ bonus += evaluate_pieces<BISHOP, Us, HasPopCnt, Trace>(pos, ei, mobility, mobilityArea);
+ bonus += evaluate_pieces<ROOK, Us, HasPopCnt, Trace>(pos, ei, mobility, mobilityArea);
+ bonus += evaluate_pieces<QUEEN, Us, HasPopCnt, Trace>(pos, ei, mobility, mobilityArea);
// Sum up all attacked squares
ei.attackedBy[Us][0] = ei.attackedBy[Us][PAWN] | ei.attackedBy[Us][KNIGHT]
// evaluate_king<>() assigns bonuses and penalties to a king of a given color
- template<Color Us, bool HasPopCnt>
+ template<Color Us, bool HasPopCnt, bool Trace>
Score evaluate_king(const Position& pos, EvalInfo& ei, Value margins[]) {
const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64_MAX15 : CNT32_MAX15;
bonus -= KingDangerTable[Us][attackUnits];
margins[Us] += mg_value(KingDangerTable[Us][attackUnits]);
}
+
+ if (Trace)
+ TracedTerms[Us][KING] = bonus;
+
return bonus;
}
assert(eg_value(v) > -VALUE_INFINITE && eg_value(v) < VALUE_INFINITE);
assert(ph >= PHASE_ENDGAME && ph <= PHASE_MIDGAME);
- Value eg = eg_value(v);
- Value ev = Value((eg * int(sf)) / SCALE_FACTOR_NORMAL);
-
+ int ev = (eg_value(v) * int(sf)) / SCALE_FACTOR_NORMAL;
int result = (mg_value(v) * int(ph) + ev * int(128 - ph)) / 128;
return Value((result + GrainSize / 2) & ~(GrainSize - 1));
}
for (int i = 0; i < 100; i++)
KingDangerTable[c][i] = apply_weight(make_score(t[i], 0), Weights[KingDangerUs + c]);
}
+
+
+ // trace_row() is an helper function used by tracing code to register the
+ // values of a single evaluation term.
+
+ void trace_row(const char *name, int idx) {
+
+ Score term_w = TracedTerms[WHITE][idx];
+ Score term_b = TracedTerms[BLACK][idx];
+
+ switch (idx) {
+ case PST: case IMBALANCE: case PAWN: case UNSTOPPABLE: case TOTAL:
+ TraceStream << std::setw(20) << name << " | --- --- | --- --- | "
+ << std::setw(6) << to_cp(mg_value(term_w)) << " "
+ << std::setw(6) << to_cp(eg_value(term_w)) << " \n";
+ break;
+ default:
+ TraceStream << std::setw(20) << name << " | " << std::noshowpos
+ << std::setw(5) << to_cp(mg_value(term_w)) << " "
+ << std::setw(5) << to_cp(eg_value(term_w)) << " | "
+ << std::setw(5) << to_cp(mg_value(term_b)) << " "
+ << std::setw(5) << to_cp(eg_value(term_b)) << " | "
+ << std::showpos
+ << std::setw(6) << to_cp(mg_value(term_w - term_b)) << " "
+ << std::setw(6) << to_cp(eg_value(term_w - term_b)) << " \n";
+ }
+ }
+}
+
+
+/// trace_evaluate() 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_evaluate(const Position& pos) {
+
+ Value margin;
+ std::string totals;
+
+ TraceStream.str("");
+ TraceStream << std::showpoint << std::showpos << std::fixed << std::setprecision(2);
+ memset(TracedTerms, 0, 2 * 16 * sizeof(Score));
+
+ do_evaluate<false, 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();
}