Skip to content

Commit

Permalink
refactor history
Browse files Browse the repository at this point in the history
bench 999324
  • Loading branch information
xu-shawn committed Jan 6, 2025
1 parent c76c179 commit 4fc9e92
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 40 deletions.
79 changes: 41 additions & 38 deletions src/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,17 @@ inline int non_pawn_index(const Position& pos) {
return pos.non_pawn_key(c) & (CORRECTION_HISTORY_SIZE - 1);
}

// StatsEntry stores the stat table value. It is usually a number but could
// be a move or even a nested history. We use a class instead of a naked value
// to directly call history update operator<<() on the entry so to use stats
// tables at caller sites as simple multi-dim arrays.
// StatsEntry is the container of various numerical statistics. We use a class
// instead of a naked value to directly call history update operator<<() on
// the entry. The first template parameter T is the base type of the array,
// and the second template parameter D limits the range of updates in [-D, D]
// when we update values with the << operator
template<typename T, int D>
class StatsEntry {

static_assert(std::is_arithmetic<T>::value, "Not an arithmetic type");
static_assert(D <= std::numeric_limits<T>::max(), "D overflows T");

T entry;

public:
Expand All @@ -82,11 +86,9 @@ class StatsEntry {
}
T* operator&() { return &entry; }
T* operator->() { return &entry; }
operator const T&() const { return entry; }
operator const T&() const { return entry; }

void operator<<(int bonus) {
static_assert(D <= std::numeric_limits<T>::max(), "D overflows T");

// Make sure that bonus is in range [-D, D]
int clampedBonus = std::clamp(bonus, -D, D);
entry += clampedBonus - entry * std::abs(clampedBonus) / D;
Expand All @@ -95,17 +97,14 @@ class StatsEntry {
}
};

template<typename T, int D, std::size_t Size, std::size_t... Sizes>
struct StatsHelper;

// Stats is a generic N-dimensional array used to store various statistics.
// The first template parameter T is the base type of the array, and the second
// template parameter D limits the range of updates in [-D, D] when we update
// values with the << operator, while the last parameters (Size and Sizes)
// encode the dimensions of the array.
template<typename T, int D, std::size_t Size, std::size_t... Sizes>
class Stats {
using child_type = typename StatsHelper<T, D, Size, Sizes...>::child_type;
template<typename T, std::size_t Size, std::size_t... Sizes>
struct MultiArrayHelper;

// MultiArray is a generic N-dimensional array used by various statistics.
// The template parameters (Size and Sizes) encode the dimensions of the array.
template<typename T, std::size_t Size, std::size_t... Sizes>
class MultiArray {
using child_type = typename MultiArrayHelper<T, Size, Sizes...>::child_type;
using array_type = std::array<child_type, Size>;
array_type data;

Expand All @@ -122,7 +121,9 @@ class Stats {
auto cbegin() const { return data.cbegin(); }
auto cend() const { return data.cend(); }

void fill(const T& v) {
template<typename U>
void fill(const U& v) {
static_assert(std::is_assignable_v<T, U>, "Cannot assign fill value to entry type");
for (auto& ele : data)
{
if constexpr (sizeof...(Sizes) == 0)
Expand All @@ -133,49 +134,49 @@ class Stats {
}
};

template<typename T, int D, std::size_t Size, std::size_t... Sizes>
struct StatsHelper {
using child_type = Stats<T, D, Sizes...>;
template<typename T, std::size_t Size, std::size_t... Sizes>
struct MultiArrayHelper {
using child_type = MultiArray<T, Sizes...>;
};

template<typename T, int D, std::size_t Size>
struct StatsHelper<T, D, Size> {
using child_type = StatsEntry<T, D>;
template<typename T, std::size_t Size>
struct MultiArrayHelper<T, Size> {
using child_type = T;
};

// In stats table, D=0 means that the template parameter is not used
enum StatsParams {
NOT_USED = 0
};
enum StatsType {
NoCaptures,
Captures
};

template<int D>
using HistoryEntry = StatsEntry<std::int16_t, D>;

// ButterflyHistory records how often quiet moves have been successful or unsuccessful
// during the current search, and is used for reduction and move ordering decisions.
// It uses 2 tables (one for each color) indexed by the move's from and to squares,
// see https://www.chessprogramming.org/Butterfly_Boards (~11 elo)
using ButterflyHistory = Stats<int16_t, 7183, COLOR_NB, int(SQUARE_NB) * int(SQUARE_NB)>;
using ButterflyHistory = MultiArray<HistoryEntry<7183>, COLOR_NB, int(SQUARE_NB) * int(SQUARE_NB)>;

// LowPlyHistory is adressed by play and move's from and to squares, used
// to improve move ordering near the root
using LowPlyHistory = Stats<int16_t, 7183, LOW_PLY_HISTORY_SIZE, int(SQUARE_NB) * int(SQUARE_NB)>;
using LowPlyHistory =
MultiArray<HistoryEntry<7183>, LOW_PLY_HISTORY_SIZE, int(SQUARE_NB) * int(SQUARE_NB)>;

// CapturePieceToHistory is addressed by a move's [piece][to][captured piece type]
using CapturePieceToHistory = Stats<int16_t, 10692, PIECE_NB, SQUARE_NB, PIECE_TYPE_NB>;
using CapturePieceToHistory = MultiArray<HistoryEntry<10692>, PIECE_NB, SQUARE_NB, PIECE_TYPE_NB>;

// PieceToHistory is like ButterflyHistory but is addressed by a move's [piece][to]
using PieceToHistory = Stats<int16_t, 30000, PIECE_NB, SQUARE_NB>;
using PieceToHistory = MultiArray<HistoryEntry<30000>, PIECE_NB, SQUARE_NB>;

// ContinuationHistory is the combined history of a given pair of moves, usually
// the current one given a previous one. The nested history table is based on
// PieceToHistory instead of ButterflyBoards.
// (~63 elo)
using ContinuationHistory = Stats<PieceToHistory, NOT_USED, PIECE_NB, SQUARE_NB>;
using ContinuationHistory = MultiArray<PieceToHistory, PIECE_NB, SQUARE_NB>;

// PawnHistory is addressed by the pawn structure and a move's [piece][to]
using PawnHistory = Stats<int16_t, 8192, PAWN_HISTORY_SIZE, PIECE_NB, SQUARE_NB>;
using PawnHistory = MultiArray<HistoryEntry<8192>, PAWN_HISTORY_SIZE, PIECE_NB, SQUARE_NB>;

// Correction histories record differences between the static evaluation of
// positions and their search score. It is used to improve the static evaluation
Expand All @@ -190,19 +191,21 @@ enum CorrHistType {
Continuation, // Combined history of move pairs
};

using CorrHistEntry = StatsEntry<std::int16_t, CORRECTION_HISTORY_LIMIT>;

template<CorrHistType _>
struct CorrHistTypedef {
using type = Stats<int16_t, CORRECTION_HISTORY_LIMIT, COLOR_NB, CORRECTION_HISTORY_SIZE>;
using type = MultiArray<CorrHistEntry, COLOR_NB, CORRECTION_HISTORY_SIZE>;
};

template<>
struct CorrHistTypedef<PieceTo> {
using type = Stats<int16_t, CORRECTION_HISTORY_LIMIT, PIECE_NB, SQUARE_NB>;
using type = MultiArray<CorrHistEntry, PIECE_NB, SQUARE_NB>;
};

template<>
struct CorrHistTypedef<Continuation> {
using type = Stats<CorrHistTypedef<PieceTo>::type, NOT_USED, PIECE_NB, SQUARE_NB>;
using type = MultiArray<CorrHistTypedef<PieceTo>::type, PIECE_NB, SQUARE_NB>;
};

template<CorrHistType T>
Expand Down
4 changes: 2 additions & 2 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,13 @@ void Search::Worker::clear() {

for (auto& to : continuationCorrectionHistory)
for (auto& h : to)
h->fill(0);
h.fill(0);

for (bool inCheck : {false, true})
for (StatsType c : {NoCaptures, Captures})
for (auto& to : continuationHistory[inCheck][c])
for (auto& h : to)
h->fill(-427);
h.fill(-427);

for (size_t i = 1; i < reductions.size(); ++i)
reductions[i] = int(19.43 * std::log(i));
Expand Down

0 comments on commit 4fc9e92

Please sign in to comment.