added lmr table + small refactors | bench 3761943

This commit is contained in:
rektdie committed 2025-03-28 00:15:48 +01:00
1 parent 410a095a09
commit 1279a11808
6 files changed
+38 -18

No files matched your search

+1 -1
View File
@@ -63,7 +63,7 @@ constexpr std::array<std::string_view, 50> fenPositions = {
void RunBenchmark() { void RunBenchmark() {
Board board; Board board;
int depth = 6; int depth = 8;
int ply = 0; int ply = 0;
benchStarted = true; benchStarted = true;
searchStopped = false; searchStopped = false;
+1 -1
View File
@@ -22,7 +22,7 @@ void Board::Reset() {
pieceThreats = std::array<Bitboard, 6>(); pieceThreats = std::array<Bitboard, 6>();
colorThreats = std::array<Bitboard, 2>(); colorThreats = std::array<Bitboard, 2>();
moveList = std::array<Move, 218>(); moveList = std::array<Move, MAX_MOVES>();
currentMoveIndex = 0; currentMoveIndex = 0;
+3 -1
View File
@@ -7,6 +7,8 @@
constexpr int nullPieceType = 100; constexpr int nullPieceType = 100;
constexpr int noEPTarget = -1; constexpr int noEPTarget = -1;
constexpr int MAX_MOVES = 218;
inline int positionIndex = 0; inline int positionIndex = 0;
inline U64 positionHistory[1000]; inline U64 positionHistory[1000];
@@ -24,7 +26,7 @@ public:
std::array<Bitboard, 6> pieceThreats; std::array<Bitboard, 6> pieceThreats;
std::array<Bitboard, 2> colorThreats; std::array<Bitboard, 2> colorThreats;
std::array<Move, 218> moveList; std::array<Move, MAX_MOVES> moveList;
int currentMoveIndex = 0; int currentMoveIndex = 0;
bool sideToMove = White; bool sideToMove = White;
+2
View File
@@ -3,11 +3,13 @@
#include "uci.h" #include "uci.h"
#include "utils.h" #include "utils.h"
#include "benchmark.h" #include "benchmark.h"
#include "search.h"
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
MOVEGEN::initLeaperAttacks(); MOVEGEN::initLeaperAttacks();
MOVEGEN::initSliderAttacks(); MOVEGEN::initSliderAttacks();
UTILS::InitZobrist(); UTILS::InitZobrist();
SEARCH::InitLMRTable();
Board board; Board board;
+19 -7
View File
@@ -18,6 +18,21 @@ inline PVLine pvLine;
Stopwatch sw; Stopwatch sw;
void InitLMRTable() {
for (int depth = 0; depth < MAX_DEPTH; depth++) {
for (int move = 0; move < MAX_MOVES; move++) {
if (depth == 0 || move == 0) {
lmrTable[depth][move] = 0;
} else {
double base = 0.77;
double divisor = 2.36;
lmrTable[depth][move] = std::floor(base + std::log(depth) * std::log(move) / divisor);
}
}
}
}
static int ScoreMove(Board &board, Move &move, int ply) { static int ScoreMove(Board &board, Move &move, int ply) {
TTEntry *current = TT.GetRawEntry(board.hashKey); TTEntry *current = TT.GetRawEntry(board.hashKey);
if (current->bestMove == move) { if (current->bestMove == move) {
@@ -48,8 +63,8 @@ static int ScoreMove(Board &board, Move &move, int ply) {
} }
static void SortMoves(Board &board, int ply) { static void SortMoves(Board &board, int ply) {
std::array<int, 218> scores; std::array<int, MAX_MOVES> scores;
std::array<int, 218> indices; std::array<int, MAX_MOVES> indices;
// Initialize scores and indices // Initialize scores and indices
for (int i = 0; i < board.currentMoveIndex; i++) { for (int i = 0; i < board.currentMoveIndex; i++) {
@@ -64,7 +79,7 @@ static void SortMoves(Board &board, int ply) {
}); });
// Create a temporary move list and reorder the moveList based on the sorted indices // Create a temporary move list and reorder the moveList based on the sorted indices
std::array<Move, 218> sortedMoves; std::array<Move, MAX_MOVES> sortedMoves;
for (int i = 0; i < board.currentMoveIndex; i++) { for (int i = 0; i < board.currentMoveIndex; i++) {
sortedMoves[i] = board.moveList[indices[i]]; sortedMoves[i] = board.moveList[indices[i]];
} }
@@ -107,10 +122,7 @@ static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int
// Late Move Reduction // Late Move Reduction
if (depth >= 3 && moveSeen >= 3 && !move.IsCapture()) { if (depth >= 3 && moveSeen >= 3 && !move.IsCapture()) {
double base = 0.77; reduction = lmrTable[depth][moveSeen];
double divisor = 2.36;
reduction = std::floor(base + std::log(depth) * std::log(moveSeen) / divisor);
} }
return reduction; return reduction;
+12 -8
View File
@@ -8,8 +8,8 @@
namespace SEARCH { namespace SEARCH {
constexpr int MATE_SCORE = -99000; constexpr int MATE_SCORE = -99000;
constexpr int maxDepth = 64; constexpr int MAX_DEPTH = 64;
constexpr int maxHistory = 16384; constexpr int MAX_HISTORY = 16384;
extern U64 nodes; extern U64 nodes;
extern bool benchStarted; extern bool benchStarted;
@@ -21,6 +21,10 @@ inline bool searchStopped = false;
// [id][ply] // [id][ply]
inline int killerMoves[2][64]; inline int killerMoves[2][64];
inline int lmrTable[MAX_DEPTH][MAX_MOVES];
void InitLMRTable();
class SearchResults { class SearchResults {
public: public:
int score = 0; int score = 0;
@@ -43,9 +47,9 @@ private:
int historyMoves[2][64][64]; int historyMoves[2][64][64];
public: public:
void Update(bool stm, Move move, int bonus) { void Update(bool stm, Move move, int bonus) {
int clampedBonus = std::clamp(bonus, -maxHistory, maxHistory); int clampedBonus = std::clamp(bonus, -MAX_HISTORY, MAX_HISTORY);
historyMoves[stm][move.MoveFrom()][move.MoveTo()] += historyMoves[stm][move.MoveFrom()][move.MoveTo()] +=
clampedBonus - historyMoves[stm][move.MoveFrom()][move.MoveTo()] * std::abs(clampedBonus) / maxHistory; clampedBonus - historyMoves[stm][move.MoveFrom()][move.MoveTo()] * std::abs(clampedBonus) / MAX_HISTORY;
} }
void Clear() { void Clear() {
@@ -67,8 +71,8 @@ inline History history;
class PVLine { class PVLine {
private: private:
int length[maxDepth] = {}; int length[MAX_DEPTH] = {};
Move table[maxDepth][maxDepth] = {}; Move table[MAX_DEPTH][MAX_DEPTH] = {};
public: public:
void SetLength(int ply) { void SetLength(int ply) {
length[ply] = ply; length[ply] = ply;
@@ -92,8 +96,8 @@ public:
} }
void Clear() { void Clear() {
length[maxDepth - 1] = {}; length[MAX_DEPTH - 1] = {};
table[maxDepth - 1][maxDepth - 1] = {}; table[MAX_DEPTH - 1][MAX_DEPTH - 1] = {};
} }
}; };