Conthist #79

Merged
rektdie merged 13 commits from conthist into main 2025-07-20 13:06:45 +02:00
3 changed files with 72 additions and 6 deletions

No files matched your search

+43 -6
View File
@@ -25,7 +25,7 @@ void InitLMRTable() {
static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) {
TTEntry *current = ctx->TT.GetRawEntry(board.hashKey); TTEntry *current = ctx->TT.GetRawEntry(board.hashKey);
if (current->hashKey == board.hashKey && current->bestMove == move) { if (current->hashKey == board.hashKey && current->bestMove == move) {
return 50000; return 100000;
} }
if (move.IsCapture()) { if (move.IsCapture()) {
@@ -36,15 +36,26 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) {
targetType = Pawn; targetType = Pawn;
} }
return 30000 * ((SEE(board, move, -100))) + moveScoreTable[attackerType][targetType]; return 50000 * ((SEE(board, move, -100))) + moveScoreTable[attackerType][targetType];
} else { } else {
if (ctx->killerMoves[0][ply] == move) { if (ctx->killerMoves[0][ply] == move) {
return 20000; return 41000;
} else if (ctx->killerMoves[1][ply] == move) { } else if (ctx->killerMoves[1][ply] == move) {
return 18000; return 40000;
} else { } else {
// Max 16384 int historyScore = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()];
return ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()]; int conthistScore = 0;
if (ply > 0) {
int prevType = ctx->ss[ply-1].pieceType;
int prevTo = ctx->ss[ply-1].moveTo;
int pieceType = board.GetPieceType(move.MoveFrom());
int to = move.MoveTo();
conthistScore = ctx->conthist[board.sideToMove][prevType][prevTo][pieceType][to];
}
return 20000 + historyScore + conthistScore;
} }
} }
@@ -258,6 +269,9 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
int isLegal = copy.MakeMove(board.moveList[i]); int isLegal = copy.MakeMove(board.moveList[i]);
if (!isLegal) continue; if (!isLegal) continue;
ctx->ss[ply].pieceType = board.GetPieceType(board.moveList[i].MoveFrom());
ctx->ss[ply].moveTo = board.moveList[i].MoveTo();
if (!SEE(board, board.moveList[i], 0)) if (!SEE(board, board.moveList[i], 0))
continue; continue;
@@ -392,6 +406,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
if (!isPV && !board.InCheck() && currMove.IsQuiet() && notMated) { if (!isPV && !board.InCheck() && currMove.IsQuiet() && notMated) {
int lmpBase = 7; int lmpBase = 7;
int lmpThreshold = lmpBase + 4 * depth; int lmpThreshold = lmpBase + 4 * depth;
if (moveSeen >= lmpThreshold) { if (moveSeen >= lmpThreshold) {
@@ -413,6 +428,10 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
bool isLegal = copy.MakeMove(currMove); bool isLegal = copy.MakeMove(currMove);
if (!isLegal) continue; if (!isLegal) continue;
ctx->ss[ply].pieceType = board.GetPieceType(currMove.MoveFrom());
ctx->ss[ply].moveTo = currMove.MoveTo();
if (copy.positionIndex >= ctx->positionHistory.size()) { if (copy.positionIndex >= ctx->positionHistory.size()) {
ctx->positionHistory.resize(copy.positionIndex + 100); ctx->positionHistory.resize(copy.positionIndex + 100);
} }
@@ -475,6 +494,24 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
ctx->history.Update(board.sideToMove, currMove, bonus); ctx->history.Update(board.sideToMove, currMove, bonus);
if (ply > 0) {
int prevType = ctx->ss[ply-1].pieceType;
int prevTo = ctx->ss[ply-1].moveTo;
int pieceType = ctx->ss[ply].pieceType;
int to = ctx->ss[ply].moveTo;
ctx->conthist.Update(board.sideToMove, prevType, prevTo, pieceType, to, bonus);
// Malus
for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) {
pieceType = board.GetPieceType(seenQuiets[moveIndex].MoveFrom());
to = seenQuiets[moveIndex].MoveTo();
ctx->conthist.Update(board.sideToMove, prevType, prevTo, pieceType, to, -bonus);
}
}
// Malus // Malus
for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) { for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) {
ctx->history.Update(board.sideToMove, seenQuiets[moveIndex], -bonus); ctx->history.Update(board.sideToMove, seenQuiets[moveIndex], -bonus);
+27
View File
@@ -34,6 +34,27 @@ constexpr std::array<int, 6> SEEPieceValues = {
void InitLMRTable(); void InitLMRTable();
class ContHistory {
private:
// indexed by [stm][prev move piece][prev move to][piece][to]
MultiArray<int16_t, 2, 6, 64, 6, 64> contHistMoves;
public:
void Update(bool stm, int prevType, int prevTo, int type, int to, int bonus) {
int clampedBonus = std::clamp(bonus, -MAX_HISTORY, MAX_HISTORY);
contHistMoves[stm][prevType][prevTo][type][to] +=
clampedBonus - contHistMoves[stm][prevType][prevTo][type][to] * std::abs(clampedBonus) / MAX_HISTORY;
}
void Clear() {
std::fill(&contHistMoves[0][0][0][0][0], &contHistMoves[0][0][0][0][0] + sizeof(contHistMoves) / sizeof(int16_t), 0);
}
auto& operator[](int index) {
return contHistMoves[index];
}
};
class History { class History {
private: private:
MultiArray<int, 2, 64, 64> historyMoves; MultiArray<int, 2, 64, 64> historyMoves;
@@ -88,6 +109,9 @@ public:
class Stack { class Stack {
public: public:
int32_t eval = ScoreNone; int32_t eval = ScoreNone;
int pieceType = -1;
int moveTo = -1;
}; };
class SearchContext { class SearchContext {
@@ -103,6 +127,7 @@ public:
PVLine pvLine; PVLine pvLine;
History history; History history;
ContHistory conthist;
std::array<std::array<int, MAX_PLY>, 2> killerMoves{}; std::array<std::array<int, MAX_PLY>, 2> killerMoves{};
@@ -117,9 +142,11 @@ public:
SearchContext(){ SearchContext(){
pvLine.Clear(); pvLine.Clear();
history.Clear(); history.Clear();
conthist.Clear();
TT.Clear(); TT.Clear();
sw.Restart(); sw.Restart();
killerMoves = {}; killerMoves = {};
ss = {};
positionHistory.reserve(1000); positionHistory.reserve(1000);
} }
}; };
+2
View File
@@ -139,7 +139,9 @@ void UCILoop(Board &board) {
ctx->killerMoves = {}; ctx->killerMoves = {};
ctx->history.Clear(); ctx->history.Clear();
ctx->conthist.Clear();
ctx->positionHistory = {}; ctx->positionHistory = {};
ctx->ss = {};
continue; continue;
} }