Skip to content

Commit

Permalink
Adjust corr hist
Browse files Browse the repository at this point in the history
  • Loading branch information
PikaCat-OuO committed Sep 20, 2024
1 parent 93e2ce5 commit cd8b5cd
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 55 deletions.
28 changes: 9 additions & 19 deletions src/movepick.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@

namespace Stockfish {

constexpr int PAWN_HISTORY_SIZE = 512; // has to be a power of 2
constexpr int PAWN_CORRECTION_HISTORY_SIZE = 16384; // has to be a power of 2
constexpr int MATERIAL_CORRECTION_HISTORY_SIZE = 32768; // has to be a power of 2
constexpr int MAJOR_PIECE_CORRECTION_HISTORY_SIZE = 32768; // has to be a power of 2
constexpr int MINOR_PIECE_CORRECTION_HISTORY_SIZE = 32768; // has to be a power of 2
constexpr int DEFENDER_PIECE_CORRECTION_HISTORY_SIZE = 32768; // has to be a power of 2
constexpr int NON_PAWN_CORRECTION_HISTORY_SIZE = 32768; // has to be a power of 2
constexpr int CORRECTION_HISTORY_LIMIT = 1024;
constexpr int PAWN_HISTORY_SIZE = 512; // has to be a power of 2
constexpr int PAWN_CORRECTION_HISTORY_SIZE = 16384; // has to be a power of 2
constexpr int MATERIAL_CORRECTION_HISTORY_SIZE = 32768; // has to be a power of 2
constexpr int MAJOR_PIECE_CORRECTION_HISTORY_SIZE = 32768; // has to be a power of 2
constexpr int MINOR_PIECE_CORRECTION_HISTORY_SIZE = 32768; // has to be a power of 2
constexpr int NON_PAWN_CORRECTION_HISTORY_SIZE = 32768; // has to be a power of 2
constexpr int CORRECTION_HISTORY_LIMIT = 1024;

static_assert((PAWN_HISTORY_SIZE & (PAWN_HISTORY_SIZE - 1)) == 0,
"PAWN_HISTORY_SIZE has to be a power of 2");
Expand Down Expand Up @@ -70,11 +69,6 @@ inline int major_piece_index(const Position& pos) {
inline int minor_piece_index(const Position& pos) {
return pos.minor_piece_key() & (MINOR_PIECE_CORRECTION_HISTORY_SIZE - 1);
}

inline int defender_piece_index(const Position& pos) {
return pos.defender_piece_key() & (DEFENDER_PIECE_CORRECTION_HISTORY_SIZE - 1);
}

template<Color c>
inline int non_pawn_index(const Position& pos) {
return pos.non_pawn_key(c) & (NON_PAWN_CORRECTION_HISTORY_SIZE - 1);
Expand Down Expand Up @@ -173,18 +167,14 @@ using PawnCorrectionHistory =
using MaterialCorrectionHistory =
Stats<int16_t, CORRECTION_HISTORY_LIMIT, COLOR_NB, MATERIAL_CORRECTION_HISTORY_SIZE>;

// MajorPieceCorrectionHistory is addressed by color and king/rook positions
// MajorPieceCorrectionHistory is addressed by color and king/major piece (Rook, Knight, Cannon) positions
using MajorPieceCorrectionHistory =
Stats<int16_t, CORRECTION_HISTORY_LIMIT, COLOR_NB, MAJOR_PIECE_CORRECTION_HISTORY_SIZE>;

// MinorPieceCorrectionHistory is addressed by color and king/minor piece (Knight, Cannon) positions
// MinorPieceCorrectionHistory is addressed by color and king/minor piece (Advisor, Bishop) positions
using MinorPieceCorrectionHistory =
Stats<int16_t, CORRECTION_HISTORY_LIMIT, COLOR_NB, MINOR_PIECE_CORRECTION_HISTORY_SIZE>;

// DefenderPieceCorrectionHistory is addressed by color and king/defender piece (Advisor, Bishop) positions
using DefenderPieceCorrectionHistory =
Stats<int16_t, CORRECTION_HISTORY_LIMIT, COLOR_NB, DEFENDER_PIECE_CORRECTION_HISTORY_SIZE>;

// NonPawnCorrectionHistory is addressed by color and non-pawn material positions
using NonPawnCorrectionHistory =
Stats<int16_t, CORRECTION_HISTORY_LIMIT, COLOR_NB, NON_PAWN_CORRECTION_HISTORY_SIZE>;
Expand Down
32 changes: 11 additions & 21 deletions src/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void Position::set_check_info() const {
void Position::set_state() const {

st->key = st->materialKey = 0;
st->majorPieceKey = st->minorPieceKey = st->defenderPieceKey = 0;
st->majorPieceKey = st->minorPieceKey = 0;
st->nonPawnKey[WHITE] = st->nonPawnKey[BLACK] = 0;
st->pawnKey = Zobrist::noPawns;
st->majorMaterial[WHITE] = st->majorMaterial[BLACK] = VALUE_ZERO;
Expand All @@ -238,23 +238,19 @@ void Position::set_state() const {
if (pt != KING)
{
if (pt & 1)
{
st->majorMaterial[color_of(pc)] += PieceValue[pc];

if (pt == ROOK)
st->majorPieceKey ^= Zobrist::psq[pc][s];

else if (pt == KNIGHT || pt == CANNON)
st->minorPieceKey ^= Zobrist::psq[pc][s];
}

else
st->defenderPieceKey ^= Zobrist::psq[pc][s];
st->minorPieceKey ^= Zobrist::psq[pc][s];
}

else
{
st->majorPieceKey ^= Zobrist::psq[pc][s];
st->minorPieceKey ^= Zobrist::psq[pc][s];
st->defenderPieceKey ^= Zobrist::psq[pc][s];
}
}
}
Expand Down Expand Up @@ -526,18 +522,16 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {

else
{
if (type_of(captured) & 1)
st->majorMaterial[them] -= PieceValue[captured];
st->nonPawnKey[them] ^= Zobrist::psq[captured][capsq];

if (type_of(captured) == ROOK)
if (type_of(captured) & 1)
{
st->majorMaterial[them] -= PieceValue[captured];
st->majorPieceKey ^= Zobrist::psq[captured][capsq];

else if (type_of(captured) == KNIGHT || type_of(captured) == CANNON)
st->minorPieceKey ^= Zobrist::psq[captured][capsq];
}

else
st->defenderPieceKey ^= Zobrist::psq[captured][capsq];
st->minorPieceKey ^= Zobrist::psq[captured][capsq];
}

dp.dirty_num = 2; // 1 piece moved, 1 piece captured
Expand Down Expand Up @@ -576,17 +570,13 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
{
st->majorPieceKey ^= Zobrist::psq[pc][from] ^ Zobrist::psq[pc][to];
st->minorPieceKey ^= Zobrist::psq[pc][from] ^ Zobrist::psq[pc][to];
st->defenderPieceKey ^= Zobrist::psq[pc][from] ^ Zobrist::psq[pc][to];
}

else if (type_of(pc) == ROOK)
else if (type_of(pc) & 1)
st->majorPieceKey ^= Zobrist::psq[pc][from] ^ Zobrist::psq[pc][to];

else if (type_of(pc) == KNIGHT || type_of(pc) == CANNON)
st->minorPieceKey ^= Zobrist::psq[pc][from] ^ Zobrist::psq[pc][to];

else
st->defenderPieceKey ^= Zobrist::psq[pc][from] ^ Zobrist::psq[pc][to];
st->minorPieceKey ^= Zobrist::psq[pc][from] ^ Zobrist::psq[pc][to];
}

// Move the piece.
Expand Down
3 changes: 0 additions & 3 deletions src/position.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ struct StateInfo {
Key pawnKey;
Key majorPieceKey;
Key minorPieceKey;
Key defenderPieceKey;
Key nonPawnKey[COLOR_NB];
Value majorMaterial[COLOR_NB];
int16_t check10[COLOR_NB];
Expand Down Expand Up @@ -287,8 +286,6 @@ inline Key Position::major_piece_key() const { return st->majorPieceKey; }

inline Key Position::minor_piece_key() const { return st->minorPieceKey; }

inline Key Position::defender_piece_key() const { return st->defenderPieceKey; }

inline Key Position::non_pawn_key(Color c) const { return st->nonPawnKey[c]; }

inline Value Position::major_material(Color c) const { return st->majorMaterial[c]; }
Expand Down
8 changes: 2 additions & 6 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,10 @@ Value to_corrected_static_eval(Value v, const Worker& w, const Position& pos) {
const auto mcv = w.materialCorrectionHistory[us][material_index(pos)];
const auto macv = w.majorPieceCorrectionHistory[us][major_piece_index(pos)];
const auto micv = w.minorPieceCorrectionHistory[us][minor_piece_index(pos)];
const auto decv = w.defenderPieceCorrectionHistory[us][defender_piece_index(pos)];
const auto wnpcv = w.nonPawnCorrectionHistory[WHITE][us][non_pawn_index<WHITE>(pos)];
const auto bnpcv = w.nonPawnCorrectionHistory[BLACK][us][non_pawn_index<BLACK>(pos)];
const auto cv = (98198 * pcv + 68968 * mcv + 54353 * macv + 85174 * micv + 85174 * decv
+ 85581 * (wnpcv + bnpcv))
/ 2097152;
const auto cv =
(98198 * pcv + 68968 * mcv + 54353 * macv + 85174 * micv + 85581 * (wnpcv + bnpcv)) / 2097152;
v += cv;
return std::clamp(v, VALUE_MATED_IN_MAX_PLY + 1, VALUE_MATE_IN_MAX_PLY - 1);
}
Expand Down Expand Up @@ -464,7 +462,6 @@ void Search::Worker::clear() {
materialCorrectionHistory.fill(0);
majorPieceCorrectionHistory.fill(0);
minorPieceCorrectionHistory.fill(0);
defenderPieceCorrectionHistory.fill(0);
nonPawnCorrectionHistory[WHITE].fill(0);
nonPawnCorrectionHistory[BLACK].fill(0);

Expand Down Expand Up @@ -1323,7 +1320,6 @@ Value Search::Worker::search(
thisThread->materialCorrectionHistory[us][material_index(pos)] << bonus;
thisThread->majorPieceCorrectionHistory[us][major_piece_index(pos)] << bonus;
thisThread->minorPieceCorrectionHistory[us][minor_piece_index(pos)] << bonus;
thisThread->defenderPieceCorrectionHistory[us][defender_piece_index(pos)] << bonus;
thisThread->nonPawnCorrectionHistory[WHITE][us][non_pawn_index<WHITE>(pos)] << bonus;
thisThread->nonPawnCorrectionHistory[BLACK][us][non_pawn_index<BLACK>(pos)] << bonus;
}
Expand Down
11 changes: 5 additions & 6 deletions src/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,11 @@ class Worker {
ContinuationHistory continuationHistory[2][2];
PawnHistory pawnHistory;

PawnCorrectionHistory pawnCorrectionHistory;
MaterialCorrectionHistory materialCorrectionHistory;
MajorPieceCorrectionHistory majorPieceCorrectionHistory;
MinorPieceCorrectionHistory minorPieceCorrectionHistory;
DefenderPieceCorrectionHistory defenderPieceCorrectionHistory;
NonPawnCorrectionHistory nonPawnCorrectionHistory[COLOR_NB];
PawnCorrectionHistory pawnCorrectionHistory;
MaterialCorrectionHistory materialCorrectionHistory;
MajorPieceCorrectionHistory majorPieceCorrectionHistory;
MinorPieceCorrectionHistory minorPieceCorrectionHistory;
NonPawnCorrectionHistory nonPawnCorrectionHistory[COLOR_NB];

private:
void iterative_deepening();
Expand Down

0 comments on commit cd8b5cd

Please sign in to comment.