mirror of
https://github.com/sockspls/badfish
synced 2025-07-11 19:49:14 +00:00
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.
This commit is contained in:
parent
53051eefc7
commit
ddbe6082c4
4 changed files with 46 additions and 51 deletions
|
@ -18,7 +18,6 @@
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
#include "movegen.h"
|
#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
|
/// Constructors of the MovePicker class. As arguments we pass information
|
||||||
/// to help it to return the presumably good moves first, to decide which
|
/// 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
|
/// 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.
|
/// move ordering is at the current node.
|
||||||
|
|
||||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h,
|
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);
|
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,
|
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);
|
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)
|
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());
|
assert(!pos.checkers());
|
||||||
|
|
||||||
|
@ -197,7 +179,7 @@ void MovePicker::score_noncaptures() {
|
||||||
for (MoveStack* it = moves; it != end; ++it)
|
for (MoveStack* it = moves; it != end; ++it)
|
||||||
{
|
{
|
||||||
m = it->move;
|
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))]
|
it->score = PieceValue[MG][pos.piece_on(to_sq(m))]
|
||||||
- type_of(pos.piece_moved(m)) + History::Max;
|
- type_of(pos.piece_moved(m)) + History::Max;
|
||||||
else
|
else
|
||||||
it->score = H[pos.piece_moved(m)][to_sq(m)];
|
it->score = Hist[pos.piece_moved(m)][to_sq(m)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,35 +20,46 @@
|
||||||
#if !defined MOVEPICK_H_INCLUDED
|
#if !defined MOVEPICK_H_INCLUDED
|
||||||
#define MOVEPICK_H_INCLUDED
|
#define MOVEPICK_H_INCLUDED
|
||||||
|
|
||||||
|
#include <algorithm> // For std::max
|
||||||
|
#include <cstring> // For memset
|
||||||
|
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
|
||||||
/// The History class stores statistics about how often different moves
|
/// The Stats struct stores moves statistics. According to the template parameter
|
||||||
/// have been successful or unsuccessful during the current search. These
|
/// the class can store both History and Gains type statistics. History records
|
||||||
/// statistics are used for reduction and move ordering decisions. History
|
/// how often different moves have been successful or unsuccessful during the
|
||||||
/// entries are stored according only to moving piece and destination square,
|
/// current search and is used for reduction and move ordering decisions. Gains
|
||||||
/// in particular two moves with different origin but same destination and
|
/// records the move's best evaluation gain from one ply to the next and is used
|
||||||
/// same piece will be considered identical.
|
/// for pruning decisions. Entries are stored according only to moving piece and
|
||||||
|
/// destination square, in particular two moves with different origin but same
|
||||||
class History {
|
/// destination and same piece will be considered identical.
|
||||||
public:
|
template<bool Gain>
|
||||||
|
struct Stats {
|
||||||
|
|
||||||
static const Value Max = Value(2000);
|
static const Value Max = Value(2000);
|
||||||
|
|
||||||
const Value* operator[](Piece p) const { return &history[p][0]; }
|
const Value* operator[](Piece p) const { return &table[p][0]; }
|
||||||
Value gain(Piece p, Square to) const { return gains[p][to]; }
|
void clear() { memset(table, 0, sizeof(table)); }
|
||||||
|
|
||||||
void clear();
|
void update(Piece p, Square to, Value v) {
|
||||||
void update(Piece p, Square to, Value bonus);
|
|
||||||
void update_gain(Piece p, Square to, Value gain);
|
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:
|
private:
|
||||||
Value history[PIECE_NB][SQUARE_NB];
|
Value table[PIECE_NB][SQUARE_NB];
|
||||||
Value gains[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
|
/// 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
|
/// current position. The most important method is next_move(), which returns a
|
||||||
|
@ -74,7 +85,7 @@ private:
|
||||||
void generate_next();
|
void generate_next();
|
||||||
|
|
||||||
const Position& pos;
|
const Position& pos;
|
||||||
const History& H;
|
const History& Hist;
|
||||||
Search::Stack* ss;
|
Search::Stack* ss;
|
||||||
Depth depth;
|
Depth depth;
|
||||||
Move ttMove;
|
Move ttMove;
|
||||||
|
|
|
@ -86,7 +86,8 @@ namespace {
|
||||||
TimeManager TimeMgr;
|
TimeManager TimeMgr;
|
||||||
int BestMoveChanges;
|
int BestMoveChanges;
|
||||||
Value DrawValue[COLOR_NB];
|
Value DrawValue[COLOR_NB];
|
||||||
History H;
|
History Hist;
|
||||||
|
Gains Gain;
|
||||||
|
|
||||||
template <NodeType NT>
|
template <NodeType NT>
|
||||||
Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth);
|
Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth);
|
||||||
|
@ -299,7 +300,8 @@ namespace {
|
||||||
bestValue = delta = -VALUE_INFINITE;
|
bestValue = delta = -VALUE_INFINITE;
|
||||||
ss->currentMove = MOVE_NULL; // Hack to skip update gains
|
ss->currentMove = MOVE_NULL; // Hack to skip update gains
|
||||||
TT.new_search();
|
TT.new_search();
|
||||||
H.clear();
|
Hist.clear();
|
||||||
|
Gain.clear();
|
||||||
|
|
||||||
PVSize = Options["MultiPV"];
|
PVSize = Options["MultiPV"];
|
||||||
Skill skill(Options["Skill Level"]);
|
Skill skill(Options["Skill Level"]);
|
||||||
|
@ -617,7 +619,7 @@ namespace {
|
||||||
&& type_of(move) == NORMAL)
|
&& type_of(move) == NORMAL)
|
||||||
{
|
{
|
||||||
Square to = to_sq(move);
|
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)
|
// Step 6. Razoring (is omitted in PV nodes)
|
||||||
|
@ -727,7 +729,7 @@ namespace {
|
||||||
assert((ss-1)->currentMove != MOVE_NONE);
|
assert((ss-1)->currentMove != MOVE_NONE);
|
||||||
assert((ss-1)->currentMove != MOVE_NULL);
|
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);
|
CheckInfo ci(pos);
|
||||||
|
|
||||||
while ((move = mp.next_move<false>()) != MOVE_NONE)
|
while ((move = mp.next_move<false>()) != MOVE_NONE)
|
||||||
|
@ -759,7 +761,7 @@ namespace {
|
||||||
|
|
||||||
split_point_start: // At split points actual search starts from here
|
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);
|
CheckInfo ci(pos);
|
||||||
value = bestValue; // Workaround a bogus 'uninitialized' warning under gcc
|
value = bestValue; // Workaround a bogus 'uninitialized' warning under gcc
|
||||||
singularExtensionNode = !RootNode
|
singularExtensionNode = !RootNode
|
||||||
|
@ -878,7 +880,7 @@ split_point_start: // At split points actual search starts from here
|
||||||
// but fixing this made program slightly weaker.
|
// but fixing this made program slightly weaker.
|
||||||
Depth predictedDepth = newDepth - reduction<PvNode>(depth, moveCount);
|
Depth predictedDepth = newDepth - reduction<PvNode>(depth, moveCount);
|
||||||
futilityValue = ss->staticEval + ss->evalMargin + futility_margin(predictedDepth, 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)
|
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
|
// Increase history value of the cut-off move
|
||||||
Value bonus = Value(int(depth) * int(depth));
|
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
|
// Decrease history of all the other played non-capture moves
|
||||||
for (int i = 0; i < playedMoveCount - 1; i++)
|
for (int i = 0; i < playedMoveCount - 1; i++)
|
||||||
{
|
{
|
||||||
Move m = movesSearched[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,
|
// to search the moves. Because the depth is <= 0 here, only captures,
|
||||||
// queen promotions and checks (only if depth >= DEPTH_QS_CHECKS) will
|
// queen promotions and checks (only if depth >= DEPTH_QS_CHECKS) will
|
||||||
// be generated.
|
// 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);
|
CheckInfo ci(pos);
|
||||||
|
|
||||||
// Loop through the moves until no moves remain or a beta cutoff occurs
|
// Loop through the moves until no moves remain or a beta cutoff occurs
|
||||||
|
|
Loading…
Add table
Reference in a new issue