]> git.sesse.net Git - stockfish/commitdiff
Unify History and Gains under a single Stats class
authorMarco Costalba <mcostalba@gmail.com>
Sat, 2 Feb 2013 16:13:33 +0000 (17:13 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Sat, 2 Feb 2013 16:45:09 +0000 (17:45 +0100)
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.

src/movepick.cpp
src/movepick.h
src/search.cpp
src/thread.cpp

index e2bb974170a00afc11a10a1a625166f591dae4a7..e6b16cc495d238ff078324bf9057e85ca09aa6df 100644 (file)
@@ -18,7 +18,6 @@
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include <algorithm>
 #include <cassert>
 
 #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)];
   }
 }
 
index 7d6651bb5da07c8c1bd87b9c6b26e7cb86384732..3be429ed1be2a0193a5d32afe95121c102fae3a9 100644 (file)
 #if !defined MOVEPICK_H_INCLUDED
 #define MOVEPICK_H_INCLUDED
 
+#include <algorithm> // For std::max
+#include <cstring>   // 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<bool Gain>
+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<false> History;
+typedef Stats<true> 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;
index aa5ef13cc933f64cc85fd2a948fe620b42c7f47c..61932263258540783fa17186cca97b339fd98783 100644 (file)
@@ -86,7 +86,8 @@ namespace {
   TimeManager TimeMgr;
   int BestMoveChanges;
   Value DrawValue[COLOR_NB];
-  History H;
+  History Hist;
+  Gains Gain;
 
   template <NodeType NT>
   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<false>()) != 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<PvNode>(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
index 316ed76ba63783b4542ca2cc7bd5f89e80043dd1..1a39651673c2212aa40bb39a3d5b2c7b00ca694e 100644 (file)
@@ -371,8 +371,8 @@ void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
   RootMoves.clear();
 
   for (MoveList<LEGAL> 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;