along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <algorithm>
#include <cassert>
#include "movegen.h"
}
-/// 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
/// 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);
}
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);
}
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());
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)];
}
}
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)];
}
}
#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
void generate_next();
const Position& pos;
- const History& H;
+ const History& Hist;
Search::Stack* ss;
Depth depth;
Move ttMove;
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);
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"]);
&& 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)
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)
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
// 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)
{
// 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);
}
}
}
// 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
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;