From: Marco Costalba Date: Sat, 2 Feb 2013 16:13:33 +0000 (+0100) Subject: Unify History and Gains under a single Stats class X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=ddbe6082c47befcfe2bd2e778866c8fbda33b8e2 Unify History and Gains under a single Stats class Handling of History and Gains is almost the same, with the exception of the update logic, so unify both classes under a single Stats struct. No functional change. --- diff --git a/src/movepick.cpp b/src/movepick.cpp index e2bb9741..e6b16cc4 100644 --- a/src/movepick.cpp +++ b/src/movepick.cpp @@ -18,7 +18,6 @@ along with this program. If not, see . */ -#include #include #include "movegen.h" @@ -52,23 +51,6 @@ namespace { } -/// History class method definitions - -void History::clear() { - memset(history, 0, sizeof(history)); - memset(gains, 0, sizeof(gains)); -} - -void History::update(Piece p, Square to, Value bonus) { - if (abs(history[p][to] + bonus) < History::Max) - history[p][to] += bonus; -} - -void History::update_gain(Piece p, Square to, Value gain) { - gains[p][to] = std::max(gain, gains[p][to] - 1); -} - - /// Constructors of the MovePicker class. As arguments we pass information /// to help it to return the presumably good moves first, to decide which /// moves to return (in the quiescence search, for instance, we only want to @@ -76,7 +58,7 @@ void History::update_gain(Piece p, Square to, Value gain) { /// move ordering is at the current node. MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h, - Search::Stack* s, Value beta) : pos(p), H(h), depth(d) { + Search::Stack* s, Value beta) : pos(p), Hist(h), depth(d) { assert(d > DEPTH_ZERO); @@ -109,7 +91,7 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h, } MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h, - Square sq) : pos(p), H(h), cur(moves), end(moves) { + Square sq) : pos(p), Hist(h), cur(moves), end(moves) { assert(d <= DEPTH_ZERO); @@ -141,7 +123,7 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h, } MovePicker::MovePicker(const Position& p, Move ttm, const History& h, PieceType pt) - : pos(p), H(h), cur(moves), end(moves) { + : pos(p), Hist(h), cur(moves), end(moves) { assert(!pos.checkers()); @@ -197,7 +179,7 @@ void MovePicker::score_noncaptures() { for (MoveStack* it = moves; it != end; ++it) { m = it->move; - it->score = H[pos.piece_moved(m)][to_sq(m)]; + it->score = Hist[pos.piece_moved(m)][to_sq(m)]; } } @@ -220,7 +202,7 @@ void MovePicker::score_evasions() { it->score = PieceValue[MG][pos.piece_on(to_sq(m))] - type_of(pos.piece_moved(m)) + History::Max; else - it->score = H[pos.piece_moved(m)][to_sq(m)]; + it->score = Hist[pos.piece_moved(m)][to_sq(m)]; } } diff --git a/src/movepick.h b/src/movepick.h index 7d6651bb..3be429ed 100644 --- a/src/movepick.h +++ b/src/movepick.h @@ -20,35 +20,46 @@ #if !defined MOVEPICK_H_INCLUDED #define MOVEPICK_H_INCLUDED +#include // For std::max +#include // For memset + #include "position.h" #include "search.h" #include "types.h" -/// The History class stores statistics about how often different moves -/// have been successful or unsuccessful during the current search. These -/// statistics are used for reduction and move ordering decisions. History -/// entries are stored according only to moving piece and destination square, -/// in particular two moves with different origin but same destination and -/// same piece will be considered identical. - -class History { -public: +/// The Stats struct stores moves statistics. According to the template parameter +/// the class can store both History and Gains type statistics. History records +/// how often different moves have been successful or unsuccessful during the +/// current search and is used for reduction and move ordering decisions. Gains +/// records the move's best evaluation gain from one ply to the next and is used +/// for pruning decisions. Entries are stored according only to moving piece and +/// destination square, in particular two moves with different origin but same +/// destination and same piece will be considered identical. +template +struct Stats { static const Value Max = Value(2000); - const Value* operator[](Piece p) const { return &history[p][0]; } - Value gain(Piece p, Square to) const { return gains[p][to]; } + const Value* operator[](Piece p) const { return &table[p][0]; } + void clear() { memset(table, 0, sizeof(table)); } - void clear(); - void update(Piece p, Square to, Value bonus); - void update_gain(Piece p, Square to, Value gain); + void update(Piece p, Square to, Value v) { + + if (Gain) + table[p][to] = std::max(v, table[p][to] - 1); + + else if (abs(table[p][to] + v) < Max) + table[p][to] += v; + } private: - Value history[PIECE_NB][SQUARE_NB]; - Value gains[PIECE_NB][SQUARE_NB]; + Value table[PIECE_NB][SQUARE_NB]; }; +typedef Stats History; +typedef Stats Gains; + /// MovePicker class is used to pick one pseudo legal move at a time from the /// current position. The most important method is next_move(), which returns a @@ -74,7 +85,7 @@ private: void generate_next(); const Position& pos; - const History& H; + const History& Hist; Search::Stack* ss; Depth depth; Move ttMove; diff --git a/src/search.cpp b/src/search.cpp index aa5ef13c..61932263 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -86,7 +86,8 @@ namespace { TimeManager TimeMgr; int BestMoveChanges; Value DrawValue[COLOR_NB]; - History H; + History Hist; + Gains Gain; template Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth); @@ -299,7 +300,8 @@ namespace { bestValue = delta = -VALUE_INFINITE; ss->currentMove = MOVE_NULL; // Hack to skip update gains TT.new_search(); - H.clear(); + Hist.clear(); + Gain.clear(); PVSize = Options["MultiPV"]; Skill skill(Options["Skill Level"]); @@ -617,7 +619,7 @@ namespace { && type_of(move) == NORMAL) { Square to = to_sq(move); - H.update_gain(pos.piece_on(to), to, -(ss-1)->staticEval - ss->staticEval); + Gain.update(pos.piece_on(to), to, -(ss-1)->staticEval - ss->staticEval); } // Step 6. Razoring (is omitted in PV nodes) @@ -727,7 +729,7 @@ namespace { assert((ss-1)->currentMove != MOVE_NONE); assert((ss-1)->currentMove != MOVE_NULL); - MovePicker mp(pos, ttMove, H, pos.captured_piece_type()); + MovePicker mp(pos, ttMove, Hist, pos.captured_piece_type()); CheckInfo ci(pos); while ((move = mp.next_move()) != MOVE_NONE) @@ -759,7 +761,7 @@ namespace { split_point_start: // At split points actual search starts from here - MovePicker mp(pos, ttMove, depth, H, ss, PvNode ? -VALUE_INFINITE : beta); + MovePicker mp(pos, ttMove, depth, Hist, ss, PvNode ? -VALUE_INFINITE : beta); CheckInfo ci(pos); value = bestValue; // Workaround a bogus 'uninitialized' warning under gcc singularExtensionNode = !RootNode @@ -878,7 +880,7 @@ split_point_start: // At split points actual search starts from here // but fixing this made program slightly weaker. Depth predictedDepth = newDepth - reduction(depth, moveCount); futilityValue = ss->staticEval + ss->evalMargin + futility_margin(predictedDepth, moveCount) - + H.gain(pos.piece_moved(move), to_sq(move)); + + Gain[pos.piece_moved(move)][to_sq(move)]; if (futilityValue < beta) { @@ -1070,13 +1072,13 @@ split_point_start: // At split points actual search starts from here // Increase history value of the cut-off move Value bonus = Value(int(depth) * int(depth)); - H.update(pos.piece_moved(bestMove), to_sq(bestMove), bonus); + Hist.update(pos.piece_moved(bestMove), to_sq(bestMove), bonus); // Decrease history of all the other played non-capture moves for (int i = 0; i < playedMoveCount - 1; i++) { Move m = movesSearched[i]; - H.update(pos.piece_moved(m), to_sq(m), -bonus); + Hist.update(pos.piece_moved(m), to_sq(m), -bonus); } } } @@ -1188,7 +1190,7 @@ split_point_start: // At split points actual search starts from here // to search the moves. Because the depth is <= 0 here, only captures, // queen promotions and checks (only if depth >= DEPTH_QS_CHECKS) will // be generated. - MovePicker mp(pos, ttMove, depth, H, to_sq((ss-1)->currentMove)); + MovePicker mp(pos, ttMove, depth, Hist, to_sq((ss-1)->currentMove)); CheckInfo ci(pos); // Loop through the moves until no moves remain or a beta cutoff occurs diff --git a/src/thread.cpp b/src/thread.cpp index 316ed76b..1a396516 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -371,8 +371,8 @@ void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits, RootMoves.clear(); for (MoveList ml(pos); !ml.end(); ++ml) - if ( searchMoves.empty() - || std::count(searchMoves.begin(), searchMoves.end(), ml.move())) + if ( searchMoves.empty() + || std::count(searchMoves.begin(), searchMoves.end(), ml.move())) RootMoves.push_back(RootMove(ml.move())); main_thread()->thinking = true;