mirror of
https://github.com/HChaZZY/Stockfish.git
synced 2025-12-20 09:06:45 +08:00
Reformat trace code
Apart from usual renaiming, take advantage of C++11 function template default parmeter to get rid of Eval trampoline functions. Some triviality fixes while there. No functional change.
This commit is contained in:
@@ -143,7 +143,6 @@ void benchmark(const Position& current, istream& is) {
|
||||
}
|
||||
|
||||
uint64_t nodes = 0;
|
||||
Search::StateStackPtr st;
|
||||
TimePoint elapsed = now();
|
||||
|
||||
for (size_t i = 0; i < fens.size(); ++i)
|
||||
@@ -157,6 +156,7 @@ void benchmark(const Position& current, istream& is) {
|
||||
|
||||
else
|
||||
{
|
||||
Search::StateStackPtr st;
|
||||
Threads.start_thinking(pos, limits, st);
|
||||
Threads.main()->join();
|
||||
nodes += Search::RootPos.nodes_searched();
|
||||
|
||||
211
src/evaluate.cpp
211
src/evaluate.cpp
@@ -30,34 +30,50 @@
|
||||
|
||||
namespace {
|
||||
|
||||
namespace Tracing {
|
||||
namespace Trace {
|
||||
|
||||
enum Term { // First 8 entries are for PieceType
|
||||
MATERIAL = 8, IMBALANCE, MOBILITY, THREAT, PASSED, SPACE, TOTAL, TERM_NB
|
||||
};
|
||||
|
||||
Score scores[COLOR_NB][TERM_NB];
|
||||
double scores[TERM_NB][COLOR_NB][PHASE_NB];
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, Term idx);
|
||||
double to_cp(Value v) { return double(v) / PawnValueEg; }
|
||||
|
||||
double to_cp(Value v);
|
||||
void write(int idx, Color c, Score s);
|
||||
void write(int idx, Score w, Score b = SCORE_ZERO);
|
||||
std::string do_trace(const Position& pos);
|
||||
void add(int idx, Color c, Score s) {
|
||||
scores[idx][c][MG] = to_cp(mg_value(s));
|
||||
scores[idx][c][EG] = to_cp(eg_value(s));
|
||||
}
|
||||
|
||||
void add(int idx, Score w, Score b = SCORE_ZERO) {
|
||||
add(idx, WHITE, w); add(idx, BLACK, b);
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, Term t) {
|
||||
|
||||
if (t == MATERIAL || t == IMBALANCE || t == Term(PAWN) || t == TOTAL)
|
||||
os << " --- --- | --- --- | ";
|
||||
else
|
||||
os << std::setw(5) << scores[t][WHITE][MG] << " "
|
||||
<< std::setw(5) << scores[t][WHITE][EG] << " | "
|
||||
<< std::setw(5) << scores[t][BLACK][MG] << " "
|
||||
<< std::setw(5) << scores[t][BLACK][EG] << " | ";
|
||||
|
||||
os << std::setw(5) << scores[t][WHITE][MG] - scores[t][BLACK][MG] << " "
|
||||
<< std::setw(5) << scores[t][WHITE][EG] - scores[t][BLACK][EG] << " \n";
|
||||
|
||||
return os;
|
||||
}
|
||||
}
|
||||
|
||||
using namespace Trace;
|
||||
|
||||
// Struct EvalInfo contains various information computed and collected
|
||||
// by the evaluation functions.
|
||||
struct EvalInfo {
|
||||
|
||||
// Pointers to material and pawn hash table entries
|
||||
Material::Entry* mi;
|
||||
Pawns::Entry* pi;
|
||||
|
||||
// attackedBy[color][piece type] is a bitboard representing all squares
|
||||
// attacked by a given color and piece type, attackedBy[color][ALL_PIECES]
|
||||
// contains all squares attacked by the given color.
|
||||
// attacked by a given color and piece type (can be also ALL_PIECES).
|
||||
Bitboard attackedBy[COLOR_NB][PIECE_TYPE_NB];
|
||||
|
||||
// kingRing[color] is the zone around the king which is considered
|
||||
@@ -86,6 +102,7 @@ namespace {
|
||||
int kingAdjacentZoneAttacksCount[COLOR_NB];
|
||||
|
||||
Bitboard pinnedPieces[COLOR_NB];
|
||||
Pawns::Entry* pi;
|
||||
};
|
||||
|
||||
|
||||
@@ -213,8 +230,9 @@ namespace {
|
||||
const Square Down = (Us == WHITE ? DELTA_S : DELTA_N);
|
||||
|
||||
ei.pinnedPieces[Us] = pos.pinned_pieces(Us);
|
||||
ei.attackedBy[Us][ALL_PIECES] = ei.attackedBy[Us][PAWN] = ei.pi->pawn_attacks(Us);
|
||||
Bitboard b = ei.attackedBy[Them][KING] = pos.attacks_from<KING>(pos.square<KING>(Them));
|
||||
ei.attackedBy[Them][ALL_PIECES] |= b;
|
||||
ei.attackedBy[Us][ALL_PIECES] |= ei.attackedBy[Us][PAWN] = ei.pi->pawn_attacks(Us);
|
||||
|
||||
// Init king safety tables only if we are going to use them
|
||||
if (pos.non_pawn_material(Us) >= QueenValueMg)
|
||||
@@ -231,7 +249,7 @@ namespace {
|
||||
|
||||
// evaluate_pieces() assigns bonuses and penalties to the pieces of a given color
|
||||
|
||||
template<PieceType Pt, Color Us, bool Trace>
|
||||
template<PieceType Pt, Color Us, bool DoTrace>
|
||||
Score evaluate_pieces(const Position& pos, EvalInfo& ei, Score* mobility, Bitboard* mobilityArea) {
|
||||
|
||||
Bitboard b;
|
||||
@@ -332,11 +350,11 @@ namespace {
|
||||
}
|
||||
}
|
||||
|
||||
if (Trace)
|
||||
Tracing::write(Pt, Us, score);
|
||||
if (DoTrace)
|
||||
Trace::add(Pt, Us, score);
|
||||
|
||||
// Recursively call evaluate_pieces() of next piece type until KING excluded
|
||||
return score - evaluate_pieces<NextPt, Them, Trace>(pos, ei, mobility, mobilityArea);
|
||||
return score - evaluate_pieces<NextPt, Them, DoTrace>(pos, ei, mobility, mobilityArea);
|
||||
}
|
||||
|
||||
template<>
|
||||
@@ -347,7 +365,7 @@ namespace {
|
||||
|
||||
// evaluate_king() assigns bonuses and penalties to a king of a given color
|
||||
|
||||
template<Color Us, bool Trace>
|
||||
template<Color Us, bool DoTrace>
|
||||
Score evaluate_king(const Position& pos, const EvalInfo& ei) {
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
@@ -378,9 +396,9 @@ namespace {
|
||||
attackUnits = std::min(74, ei.kingAttackersCount[Them] * ei.kingAttackersWeight[Them])
|
||||
+ 8 * ei.kingAdjacentZoneAttacksCount[Them]
|
||||
+ 25 * popcount<Max15>(undefended)
|
||||
+ 11 * (ei.pinnedPieces[Us] != 0)
|
||||
- mg_value(score) / 8
|
||||
- !pos.count<QUEEN>(Them) * 60;
|
||||
+ 11 * !!ei.pinnedPieces[Us]
|
||||
- 60 * !pos.count<QUEEN>(Them)
|
||||
- mg_value(score) / 8;
|
||||
|
||||
// Analyse the enemy's safe queen contact checks. Firstly, find the
|
||||
// undefended squares around the king reachable by the enemy queen...
|
||||
@@ -443,8 +461,8 @@ namespace {
|
||||
score -= KingDanger[std::max(std::min(attackUnits, 399), 0)];
|
||||
}
|
||||
|
||||
if (Trace)
|
||||
Tracing::write(KING, Us, score);
|
||||
if (DoTrace)
|
||||
Trace::add(KING, Us, score);
|
||||
|
||||
return score;
|
||||
}
|
||||
@@ -453,7 +471,7 @@ namespace {
|
||||
// evaluate_threats() assigns bonuses according to the type of attacking piece
|
||||
// and the type of attacked one.
|
||||
|
||||
template<Color Us, bool Trace>
|
||||
template<Color Us, bool DoTrace>
|
||||
Score evaluate_threats(const Position& pos, const EvalInfo& ei) {
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
@@ -541,8 +559,8 @@ namespace {
|
||||
if (b)
|
||||
score += popcount<Max15>(b) * PawnAttackThreat;
|
||||
|
||||
if (Trace)
|
||||
Tracing::write(Tracing::THREAT, Us, score);
|
||||
if (DoTrace)
|
||||
Trace::add(THREAT, Us, score);
|
||||
|
||||
return score;
|
||||
}
|
||||
@@ -550,7 +568,7 @@ namespace {
|
||||
|
||||
// evaluate_passed_pawns() evaluates the passed pawns of the given color
|
||||
|
||||
template<Color Us, bool Trace>
|
||||
template<Color Us, bool DoTrace>
|
||||
Score evaluate_passed_pawns(const Position& pos, const EvalInfo& ei) {
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
@@ -623,8 +641,8 @@ namespace {
|
||||
score += make_score(mbonus, ebonus);
|
||||
}
|
||||
|
||||
if (Trace)
|
||||
Tracing::write(Tracing::PASSED, Us, score * Weights[PassedPawns]);
|
||||
if (DoTrace)
|
||||
Trace::add(PASSED, Us, score * Weights[PassedPawns]);
|
||||
|
||||
// Add the scores to the middlegame and endgame eval
|
||||
return score * Weights[PassedPawns];
|
||||
@@ -655,10 +673,10 @@ namespace {
|
||||
behind |= (Us == WHITE ? behind >> 8 : behind << 8);
|
||||
behind |= (Us == WHITE ? behind >> 16 : behind << 16);
|
||||
|
||||
// Since SpaceMask[Us] is fully on our half of the board
|
||||
// Since SpaceMask[Us] is fully on our half of the board...
|
||||
assert(unsigned(safe >> (Us == WHITE ? 32 : 0)) == 0);
|
||||
|
||||
// Count safe + (behind & safe) with a single popcount
|
||||
// ...count safe + (behind & safe) with a single popcount
|
||||
int bonus = popcount<Full>((Us == WHITE ? safe << 32 : safe >> 32) | (behind & safe));
|
||||
int weight = pos.count<KNIGHT>(Us) + pos.count<BISHOP>(Us)
|
||||
+ pos.count<KNIGHT>(Them) + pos.count<BISHOP>(Them);
|
||||
@@ -666,11 +684,14 @@ namespace {
|
||||
return make_score(bonus * weight * weight, 0);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// do_evaluate() is the evaluation entry point, called directly from evaluate()
|
||||
|
||||
template<bool Trace>
|
||||
Value do_evaluate(const Position& pos) {
|
||||
/// evaluate() is the main evaluation function. It returns a static evaluation
|
||||
/// of the position always from the point of view of the side to move.
|
||||
|
||||
template<bool DoTrace>
|
||||
Value Eval::evaluate(const Position& pos) {
|
||||
|
||||
assert(!pos.checkers());
|
||||
|
||||
@@ -683,25 +704,23 @@ namespace {
|
||||
score = pos.psq_score();
|
||||
|
||||
// Probe the material hash table
|
||||
ei.mi = Material::probe(pos);
|
||||
score += ei.mi->imbalance();
|
||||
Material::Entry* me = Material::probe(pos);
|
||||
score += me->imbalance();
|
||||
|
||||
// If we have a specialized evaluation function for the current material
|
||||
// configuration, call it and return.
|
||||
if (ei.mi->specialized_eval_exists())
|
||||
return ei.mi->evaluate(pos);
|
||||
if (me->specialized_eval_exists())
|
||||
return me->evaluate(pos);
|
||||
|
||||
// Probe the pawn hash table
|
||||
ei.pi = Pawns::probe(pos);
|
||||
score += ei.pi->pawns_score() * Weights[PawnStructure];
|
||||
|
||||
// Initialize attack and king safety bitboards
|
||||
ei.attackedBy[WHITE][ALL_PIECES] = ei.attackedBy[BLACK][ALL_PIECES] = 0;
|
||||
init_eval_info<WHITE>(pos, ei);
|
||||
init_eval_info<BLACK>(pos, ei);
|
||||
|
||||
ei.attackedBy[WHITE][ALL_PIECES] |= ei.attackedBy[WHITE][KING];
|
||||
ei.attackedBy[BLACK][ALL_PIECES] |= ei.attackedBy[BLACK][KING];
|
||||
|
||||
// Pawns blocked or on ranks 2 and 3. Will be excluded from the mobility area
|
||||
Bitboard blockedPawns[] = {
|
||||
pos.pieces(WHITE, PAWN) & (shift_bb<DELTA_S>(pos.pieces()) | Rank2BB | Rank3BB),
|
||||
@@ -716,21 +735,21 @@ namespace {
|
||||
};
|
||||
|
||||
// Evaluate pieces and mobility
|
||||
score += evaluate_pieces<KNIGHT, WHITE, Trace>(pos, ei, mobility, mobilityArea);
|
||||
score += evaluate_pieces<KNIGHT, WHITE, DoTrace>(pos, ei, mobility, mobilityArea);
|
||||
score += (mobility[WHITE] - mobility[BLACK]) * 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)
|
||||
- evaluate_king<BLACK, Trace>(pos, ei);
|
||||
score += evaluate_king<WHITE, DoTrace>(pos, ei)
|
||||
- evaluate_king<BLACK, DoTrace>(pos, ei);
|
||||
|
||||
// Evaluate tactical threats, we need full attack information including king
|
||||
score += evaluate_threats<WHITE, Trace>(pos, ei)
|
||||
- evaluate_threats<BLACK, Trace>(pos, ei);
|
||||
score += evaluate_threats<WHITE, DoTrace>(pos, ei)
|
||||
- evaluate_threats<BLACK, DoTrace>(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);
|
||||
score += evaluate_passed_pawns<WHITE, DoTrace>(pos, ei)
|
||||
- evaluate_passed_pawns<BLACK, DoTrace>(pos, ei);
|
||||
|
||||
// If both sides have only pawns, score for potential unstoppable pawns
|
||||
if (!pos.non_pawn_material(WHITE) && !pos.non_pawn_material(BLACK))
|
||||
@@ -749,11 +768,11 @@ namespace {
|
||||
|
||||
// Scale winning side if position is more drawish than it appears
|
||||
Color strongSide = eg_value(score) > VALUE_DRAW ? WHITE : BLACK;
|
||||
ScaleFactor sf = ei.mi->scale_factor(pos, strongSide);
|
||||
ScaleFactor sf = me->scale_factor(pos, strongSide);
|
||||
|
||||
// If we don't already have an unusual scale factor, check for certain
|
||||
// types of endgames, and use a lower scale for those.
|
||||
if ( ei.mi->game_phase() < PHASE_MIDGAME
|
||||
if ( me->game_phase() < PHASE_MIDGAME
|
||||
&& (sf == SCALE_FACTOR_NORMAL || sf == SCALE_FACTOR_ONEPAWN))
|
||||
{
|
||||
if (pos.opposite_bishops())
|
||||
@@ -778,60 +797,41 @@ namespace {
|
||||
}
|
||||
|
||||
// Interpolate between a middlegame and a (scaled by 'sf') endgame score
|
||||
Value v = mg_value(score) * int(ei.mi->game_phase())
|
||||
+ eg_value(score) * int(PHASE_MIDGAME - ei.mi->game_phase()) * sf / SCALE_FACTOR_NORMAL;
|
||||
Value v = mg_value(score) * int(me->game_phase())
|
||||
+ eg_value(score) * int(PHASE_MIDGAME - me->game_phase()) * sf / SCALE_FACTOR_NORMAL;
|
||||
|
||||
v /= int(PHASE_MIDGAME);
|
||||
|
||||
// In case of tracing add all single evaluation terms for both white and black
|
||||
if (Trace)
|
||||
// In case of tracing add all single evaluation terms
|
||||
if (DoTrace)
|
||||
{
|
||||
Tracing::write(Tracing::MATERIAL, pos.psq_score());
|
||||
Tracing::write(Tracing::IMBALANCE, ei.mi->imbalance());
|
||||
Tracing::write(PAWN, ei.pi->pawns_score());
|
||||
Tracing::write(Tracing::MOBILITY, mobility[WHITE] * Weights[Mobility]
|
||||
Trace::add(MATERIAL, pos.psq_score());
|
||||
Trace::add(IMBALANCE, me->imbalance());
|
||||
Trace::add(PAWN, ei.pi->pawns_score());
|
||||
Trace::add(MOBILITY, mobility[WHITE] * Weights[Mobility]
|
||||
, mobility[BLACK] * Weights[Mobility]);
|
||||
Tracing::write(Tracing::SPACE, evaluate_space<WHITE>(pos, ei) * Weights[Space]
|
||||
Trace::add(SPACE, evaluate_space<WHITE>(pos, ei) * Weights[Space]
|
||||
, evaluate_space<BLACK>(pos, ei) * Weights[Space]);
|
||||
Tracing::write(Tracing::TOTAL, score);
|
||||
Trace::add(TOTAL, score);
|
||||
}
|
||||
|
||||
return (pos.side_to_move() == WHITE ? v : -v) + Eval::Tempo; // Side to move point of view
|
||||
}
|
||||
}
|
||||
|
||||
// Explicit template instantiations
|
||||
template Value Eval::evaluate<true >(const Position&);
|
||||
template Value Eval::evaluate<false>(const Position&);
|
||||
|
||||
|
||||
// Tracing functions
|
||||
/// trace() is like evaluate(), but instead of returning a value, it returns
|
||||
/// a string (suitable for outputting to stdout) that contains the detailed
|
||||
/// descriptions and values of each evaluation term. Useful for debugging.
|
||||
|
||||
double Tracing::to_cp(Value v) { return double(v) / PawnValueEg; }
|
||||
|
||||
void Tracing::write(int idx, Color c, Score s) { scores[c][idx] = s; }
|
||||
|
||||
void Tracing::write(int idx, Score w, Score b) {
|
||||
scores[WHITE][idx] = w, scores[BLACK][idx] = b;
|
||||
}
|
||||
|
||||
std::ostream& Tracing::operator<<(std::ostream& os, Term t) {
|
||||
|
||||
double wScore[] = { to_cp(mg_value(scores[WHITE][t])), to_cp(eg_value(scores[WHITE][t])) };
|
||||
double bScore[] = { to_cp(mg_value(scores[BLACK][t])), to_cp(eg_value(scores[BLACK][t])) };
|
||||
|
||||
if (t == MATERIAL || t == IMBALANCE || t == Term(PAWN) || t == TOTAL)
|
||||
os << " --- --- | --- --- | ";
|
||||
else
|
||||
os << std::setw(5) << wScore[MG] << " " << std::setw(5) << wScore[EG] << " | "
|
||||
<< std::setw(5) << bScore[MG] << " " << std::setw(5) << bScore[EG] << " | ";
|
||||
|
||||
os << std::setw(5) << wScore[MG] - bScore[MG] << " "
|
||||
<< std::setw(5) << wScore[EG] - bScore[EG] << " \n";
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
std::string Tracing::do_trace(const Position& pos) {
|
||||
std::string Eval::trace(const Position& pos) {
|
||||
|
||||
std::memset(scores, 0, sizeof(scores));
|
||||
|
||||
Value v = do_evaluate<true>(pos);
|
||||
Value v = evaluate<true>(pos);
|
||||
v = pos.side_to_move() == WHITE ? v : -v; // White's point of view
|
||||
|
||||
std::stringstream ss;
|
||||
@@ -857,33 +857,12 @@ namespace {
|
||||
ss << "\nTotal Evaluation: " << to_cp(v) << " (white side)\n";
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
}
|
||||
|
||||
|
||||
namespace Eval {
|
||||
/// init() computes evaluation weights, usually at startup
|
||||
|
||||
/// evaluate() is the main evaluation function. It returns a static evaluation
|
||||
/// of the position always from the point of view of the side to move.
|
||||
|
||||
Value evaluate(const Position& pos) {
|
||||
return do_evaluate<false>(pos);
|
||||
}
|
||||
|
||||
|
||||
/// trace() is like evaluate(), but instead of returning a value, it returns
|
||||
/// a string (suitable for outputting to stdout) that contains the detailed
|
||||
/// descriptions and values of each evaluation term. It's mainly used for
|
||||
/// debugging.
|
||||
std::string trace(const Position& pos) {
|
||||
return Tracing::do_trace(pos);
|
||||
}
|
||||
|
||||
|
||||
/// init() computes evaluation weights, usually at startup
|
||||
|
||||
void init() {
|
||||
void Eval::init() {
|
||||
|
||||
const int MaxSlope = 8700;
|
||||
const int Peak = 1280000;
|
||||
@@ -894,6 +873,4 @@ namespace Eval {
|
||||
t = std::min(Peak, std::min(i * i * 27, t + MaxSlope));
|
||||
KingDanger[i] = make_score(t / 1000, 0) * Weights[KingSafety];
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Eval
|
||||
}
|
||||
|
||||
@@ -31,9 +31,10 @@ namespace Eval {
|
||||
const Value Tempo = Value(17); // Must be visible to search
|
||||
|
||||
void init();
|
||||
Value evaluate(const Position& pos);
|
||||
std::string trace(const Position& pos);
|
||||
|
||||
template<bool DoTrace = false>
|
||||
Value evaluate(const Position& pos);
|
||||
}
|
||||
|
||||
#endif // #ifndef EVALUATE_H_INCLUDED
|
||||
|
||||
Reference in New Issue
Block a user