From 15d265cc664e0e49fa82cccb7471da0a5612ce25 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Wed, 25 Aug 2010 19:56:01 +0100 Subject: [PATCH 1/1] Change evaluate() signature Hide EvalInfo and return just the score and the margin. No functional change. Signed-off-by: Marco Costalba --- src/evaluate.cpp | 63 +++++++++++++++++++++++++++++++++++---- src/evaluate.h | 77 ++---------------------------------------------- src/search.cpp | 32 ++++++++++---------- src/uci.cpp | 4 +-- src/value.h | 7 ----- 5 files changed, 79 insertions(+), 104 deletions(-) diff --git a/src/evaluate.cpp b/src/evaluate.cpp index 0a68387e..981c97db 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -38,6 +38,54 @@ namespace { + // Struct EvalInfo contains various information computed and collected + // by the evaluation functions. + struct EvalInfo { + + // Middle and end game position's static evaluations + Score value; + + // margin[color] stores the evaluation margins we should consider for + // the given position. This is a kind of uncertainty estimation and + // typically is used by the search for pruning decisions. + Value margin[2]; + + // Pointers to material and pawn hash table entries + MaterialInfo* mi; + PawnInfo* pi; + + // attackedBy[color][piece type] is a bitboard representing all squares + // attacked by a given color and piece type, attackedBy[color][0] contains + // all squares attacked by the given color. + Bitboard attackedBy[2][8]; + + // kingZone[color] is the zone around the enemy king which is considered + // by the king safety evaluation. This consists of the squares directly + // adjacent to the king, and the three (or two, for a king on an edge file) + // squares two ranks in front of the king. For instance, if black's king + // is on g8, kingZone[WHITE] is a bitboard containing the squares f8, h8, + // f7, g7, h7, f6, g6 and h6. + Bitboard kingZone[2]; + + // kingAttackersCount[color] is the number of pieces of the given color + // which attack a square in the kingZone of the enemy king. + int kingAttackersCount[2]; + + // kingAttackersWeight[color] is the sum of the "weight" of the pieces of the + // given color which attack a square in the kingZone of the enemy king. The + // weights of the individual piece types are given by the variables + // QueenAttackWeight, RookAttackWeight, BishopAttackWeight and + // KnightAttackWeight in evaluate.cpp + int kingAttackersWeight[2]; + + // kingAdjacentZoneAttacksCount[color] is the number of attacks to squares + // directly adjacent to the king of the given color. Pieces which attack + // more than one square are counted multiple times. For instance, if black's + // king is on g8 and there's a white knight on g5, this knight adds + // 2 to kingAdjacentZoneAttacksCount[BLACK]. + int kingAdjacentZoneAttacksCount[2]; + }; + const int Sign[2] = { 1, -1 }; // Evaluation grain size, must be a power of 2 @@ -187,7 +235,7 @@ namespace { // Function prototypes template - Value do_evaluate(const Position& pos, EvalInfo& ei); + Value do_evaluate(const Position& pos, Value margins[]); template void init_attack_tables(const Position& pos, EvalInfo& ei); @@ -229,17 +277,18 @@ void prefetchPawn(Key key, int threadID) { /// 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, EvalInfo& ei) { +Value evaluate(const Position& pos, Value margins[]) { - return CpuHasPOPCNT ? do_evaluate(pos, ei) - : do_evaluate(pos, ei); + return CpuHasPOPCNT ? do_evaluate(pos, margins) + : do_evaluate(pos, margins); } namespace { template -Value do_evaluate(const Position& pos, EvalInfo& ei) { +Value do_evaluate(const Position& pos, Value margins[]) { + EvalInfo ei; ScaleFactor factor[2]; Score mobility; @@ -344,6 +393,10 @@ Value do_evaluate(const Position& pos, EvalInfo& ei) { factor[BLACK] = sf; } + // Populate margins[] + margins[WHITE] = ei.margin[WHITE]; + margins[BLACK] = ei.margin[BLACK]; + // Interpolate between the middle game and the endgame score return Sign[pos.side_to_move()] * scale_by_game_phase(ei.value, phase, factor); } diff --git a/src/evaluate.h b/src/evaluate.h index 6a8ef7f1..538f982e 100644 --- a/src/evaluate.h +++ b/src/evaluate.h @@ -21,85 +21,14 @@ #if !defined(EVALUATE_H_INCLUDED) #define EVALUATE_H_INCLUDED -//// -//// Includes -//// +#include "color.h" +#include "value.h" -#include - -#include "material.h" -#include "pawns.h" - - -//// -//// Types -//// - - -/// The EvalInfo struct contains various information computed and collected -/// by the evaluation function. An EvalInfo object is passed as one of the -/// arguments to the evaluation function, and the search can make use of its -/// contents to make intelligent search decisions. -/// -/// At the moment, this is not utilized very much: The only part of the -/// EvalInfo object which is used by the search is margin. class Position; -struct EvalInfo { - - // Middle and end game position's static evaluations - Score value; - - // margin[color] stores the evaluation margins we should consider for - // the given position. This is a kind of uncertainty estimation and - // typically is used by the search for pruning decisions. - Value margin[2]; - - // Pointers to material and pawn hash table entries - MaterialInfo* mi; - PawnInfo* pi; - - // attackedBy[color][piece type] is a bitboard representing all squares - // attacked by a given color and piece type, attackedBy[color][0] contains - // all squares attacked by the given color. - Bitboard attackedBy[2][8]; - - // kingZone[color] is the zone around the enemy king which is considered - // by the king safety evaluation. This consists of the squares directly - // adjacent to the king, and the three (or two, for a king on an edge file) - // squares two ranks in front of the king. For instance, if black's king - // is on g8, kingZone[WHITE] is a bitboard containing the squares f8, h8, - // f7, g7, h7, f6, g6 and h6. - Bitboard kingZone[2]; - - // kingAttackersCount[color] is the number of pieces of the given color - // which attack a square in the kingZone of the enemy king. - int kingAttackersCount[2]; - - // kingAttackersWeight[color] is the sum of the "weight" of the pieces of the - // given color which attack a square in the kingZone of the enemy king. The - // weights of the individual piece types are given by the variables - // QueenAttackWeight, RookAttackWeight, BishopAttackWeight and - // KnightAttackWeight in evaluate.cpp - int kingAttackersWeight[2]; - - // kingAdjacentZoneAttacksCount[color] is the number of attacks to squares - // directly adjacent to the king of the given color. Pieces which attack - // more than one square are counted multiple times. For instance, if black's - // king is on g8 and there's a white knight on g5, this knight adds - // 2 to kingAdjacentZoneAttacksCount[BLACK]. - int kingAdjacentZoneAttacksCount[2]; -}; - - -//// -//// Prototypes -//// - -extern Value evaluate(const Position& pos, EvalInfo& ei); +extern Value evaluate(const Position& pos, Value margins[]); extern void init_eval(int threads); extern void quit_eval(); extern void read_weights(Color sideToMove); - #endif // !defined(EVALUATE_H_INCLUDED) diff --git a/src/search.cpp b/src/search.cpp index 7d9d402c..fac0444c 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -714,7 +714,7 @@ namespace { Value root_search(Position& pos, SearchStack* ss, Move* pv, RootMoveList& rml, Value* alphaPtr, Value* betaPtr) { - EvalInfo ei; + Value margins[2]; StateInfo st; CheckInfo ci(pos); int64_t nodes; @@ -739,7 +739,7 @@ namespace { // Step 5. Evaluate the position statically // At root we do this only to get reference value for child nodes - ss->eval = isCheck ? VALUE_NONE : evaluate(pos, ei); + ss->eval = isCheck ? VALUE_NONE : evaluate(pos, margins); // Step 6. Razoring (omitted at root) // Step 7. Static null move pruning (omitted at root) @@ -975,7 +975,7 @@ namespace { assert(pos.thread() >= 0 && pos.thread() < ThreadsMgr.active_threads()); Move movesSearched[256]; - EvalInfo ei; + Value margins[2]; StateInfo st; const TTEntry *tte; Key posKey; @@ -1051,13 +1051,13 @@ namespace { assert(tte->static_value() != VALUE_NONE); ss->eval = tte->static_value(); - ei.margin[pos.side_to_move()] = tte->static_value_margin(); + margins[pos.side_to_move()] = tte->static_value_margin(); refinedValue = refine_eval(tte, ss->eval, ply); } else { - refinedValue = ss->eval = evaluate(pos, ei); - TT.store(posKey, VALUE_NONE, VALUE_TYPE_NONE, DEPTH_NONE, MOVE_NONE, ss->eval, ei.margin[pos.side_to_move()]); + refinedValue = ss->eval = evaluate(pos, margins); + TT.store(posKey, VALUE_NONE, VALUE_TYPE_NONE, DEPTH_NONE, MOVE_NONE, ss->eval, margins[pos.side_to_move()]); } // Save gain for the parent non-capture move @@ -1371,7 +1371,7 @@ namespace { ValueType vt = (bestValue <= oldAlpha ? VALUE_TYPE_UPPER : bestValue >= beta ? VALUE_TYPE_LOWER : VALUE_TYPE_EXACT); move = (bestValue <= oldAlpha ? MOVE_NONE : ss->bestMove); - TT.store(posKey, value_to_tt(bestValue, ply), vt, depth, move, ss->eval, ei.margin[pos.side_to_move()]); + TT.store(posKey, value_to_tt(bestValue, ply), vt, depth, move, ss->eval, margins[pos.side_to_move()]); // Update killers and history only for non capture moves that fails high if ( bestValue >= beta @@ -1401,7 +1401,7 @@ namespace { assert(ply > 0 && ply < PLY_MAX); assert(pos.thread() >= 0 && pos.thread() < ThreadsMgr.active_threads()); - EvalInfo ei; + Value margins[2]; StateInfo st; Move ttMove, move; Value bestValue, value, futilityValue, futilityBase; @@ -1442,11 +1442,11 @@ namespace { { assert(tte->static_value() != VALUE_NONE); - ei.margin[pos.side_to_move()] = tte->static_value_margin(); + margins[pos.side_to_move()] = tte->static_value_margin(); bestValue = tte->static_value(); } else - bestValue = evaluate(pos, ei); + bestValue = evaluate(pos, margins); ss->eval = bestValue; update_gains(pos, (ss-1)->currentMove, (ss-1)->eval, ss->eval); @@ -1455,7 +1455,7 @@ namespace { if (bestValue >= beta) { if (!tte) - TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, DEPTH_NONE, MOVE_NONE, ss->eval, ei.margin[pos.side_to_move()]); + TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, DEPTH_NONE, MOVE_NONE, ss->eval, margins[pos.side_to_move()]); return bestValue; } @@ -1467,7 +1467,7 @@ namespace { deepChecks = (depth == -ONE_PLY && bestValue >= beta - PawnValueMidgame / 8); // Futility pruning parameters, not needed when in check - futilityBase = bestValue + FutilityMarginQS + ei.margin[pos.side_to_move()]; + futilityBase = bestValue + FutilityMarginQS + margins[pos.side_to_move()]; enoughMaterial = pos.non_pawn_material(pos.side_to_move()) > RookValueMidgame; } @@ -1552,7 +1552,7 @@ namespace { // Update transposition table Depth d = (depth == DEPTH_ZERO ? DEPTH_ZERO : DEPTH_ZERO - ONE_PLY); ValueType vt = (bestValue <= oldAlpha ? VALUE_TYPE_UPPER : bestValue >= beta ? VALUE_TYPE_LOWER : VALUE_TYPE_EXACT); - TT.store(pos.get_key(), value_to_tt(bestValue, ply), vt, d, ss->bestMove, ss->eval, ei.margin[pos.side_to_move()]); + TT.store(pos.get_key(), value_to_tt(bestValue, ply), vt, d, ss->bestMove, ss->eval, margins[pos.side_to_move()]); // Update killers only for checking moves that fails high if ( bestValue >= beta @@ -2242,7 +2242,7 @@ namespace { StateInfo st; TTEntry* tte; Position p(pos, pos.thread()); - EvalInfo ei; + Value margins[2]; Value v; for (int i = 0; pv[i] != MOVE_NONE; i++) @@ -2250,8 +2250,8 @@ namespace { tte = TT.retrieve(p.get_key()); if (!tte || tte->move() != pv[i]) { - v = (p.is_check() ? VALUE_NONE : evaluate(p, ei)); - TT.store(p.get_key(), VALUE_NONE, VALUE_TYPE_NONE, DEPTH_NONE, pv[i], v, ei.margin[pos.side_to_move()]); + v = (p.is_check() ? VALUE_NONE : evaluate(p, margins)); + TT.store(p.get_key(), VALUE_NONE, VALUE_TYPE_NONE, DEPTH_NONE, pv[i], v, margins[pos.side_to_move()]); } p.do_move(pv[i], st); } diff --git a/src/uci.cpp b/src/uci.cpp index a8ae266e..0225a85a 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -150,10 +150,10 @@ namespace { } else if (token == "eval") { - EvalInfo ei; + Value margins[2]; cout << "Incremental mg: " << mg_value(RootPosition.value()) << "\nIncremental eg: " << eg_value(RootPosition.value()) - << "\nFull eval: " << evaluate(RootPosition, ei) << endl; + << "\nFull eval: " << evaluate(RootPosition, margins) << endl; } else if (token == "key") cout << "key: " << hex << RootPosition.get_key() diff --git a/src/value.h b/src/value.h index faa55dd6..923a55e8 100644 --- a/src/value.h +++ b/src/value.h @@ -21,13 +21,6 @@ #if !defined(VALUE_H_INCLUDED) #define VALUE_H_INCLUDED -//// -//// Includes -//// - -#include "piece.h" - - //// //// Types //// -- 2.39.5