Skip to content

Commit

Permalink
feat: Add Late Move Reductions (#15)
Browse files Browse the repository at this point in the history
BENCH: 21337504
  • Loading branch information
IbaiBuR authored Sep 14, 2024
1 parent 1d955f4 commit a6797c6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/board/bitboard/attacks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ inline constexpr std::array<bitboard, constants::num_squares> black_pawn_attacks

/// @brief Pre-calculated lookup table for pawn attacks
inline constexpr utils::mdarray<bitboard, constants::num_colors, constants::num_squares>
pawn_attacks = {white_pawn_attacks, black_pawn_attacks};
pawn_attacks = {{{{white_pawn_attacks}, {black_pawn_attacks}}}};

/// @brief Pre-calculated lookup table for knight attacks
inline constexpr std::array<bitboard, constants::num_squares> knight_attacks = {
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int main(const int argc, const char *argv[]) {
board::bitboards::attacks::init();

if (argc > 1 && !strcmp(argv[1], "bench")) {
constexpr int bench_depth = 8;
constexpr int bench_depth = 11;

search::searcher searcher;
search::bench::run(searcher, bench_depth);
Expand Down
5 changes: 4 additions & 1 deletion src/search/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ void search::bench::run(searcher& searcher, const u32 depth) {
const auto seconds = elapsed / 1000;

std::cout << std::format("\ninfo string {} seconds", seconds) << std::endl;
std::cout << std::format("{} nodes {} nps", total_nodes, total_nodes / seconds) << std::endl;
std::cout << std::format(
"{} nodes {} nps", total_nodes,
static_cast<u64>(static_cast<double>(total_nodes) / static_cast<double>(seconds)))
<< std::endl;
}
50 changes: 44 additions & 6 deletions src/search/search.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "search.hpp"

#include <cmath>
#include <format>
#include <iostream>

#include "tt.hpp"

#include "../eval/eval.hpp"
#include "../moves/movegen.hpp"
#include "../utils/mdarray.hpp"
#include "../utils/time.hpp"

namespace search {
Expand All @@ -18,6 +20,25 @@ constexpr int rfp_margin = 70;

constexpr int nmp_base_reduction = 3;

constexpr auto lmr_quiet_factor = 1.00;
constexpr auto lmr_quiet_divisor = 2.00;

static const auto lmr_table = [] {
utils::mdarray<int, constants::max_depth, constants::max_moves> lmr_table;

for (int i = 1; i < constants::max_depth; ++i) {
for (int j = 1; j < constants::max_moves; ++j) {
lmr_table[i, j] =
static_cast<int>(lmr_quiet_factor + std::log(i) * std::log(j) / lmr_quiet_divisor);
}
}

return lmr_table;
}();

constexpr int lmr_min_depth = 2;
constexpr int lmr_move_threshold = 3;

} // namespace heuristics

void searcher::reset() {
Expand Down Expand Up @@ -264,16 +285,33 @@ score searcher::negamax(const board::position& pos,

score current_score;

// Search the first move with a full window
if (legal_moves == 1)
// Search the first move with a full window
current_score = -negamax<pv_node>(copy, -beta, -alpha, depth - 1, ply + 1, child_pv);
else {
// Do a null window search to see if we find a better move
current_score = -negamax<false>(copy, -alpha - 1, -alpha, depth - 1, ply + 1, child_pv);

// Apply LMR: Search moves that are late in move ordering with reduced depth
const auto reduction = depth > heuristics::lmr_min_depth
&& legal_moves > heuristics::lmr_move_threshold
&& current_move.is_quiet()
? heuristics::lmr_table[depth, legal_moves]
: 0;

// Ensure the reduced depth is not negative
const auto new_depth = depth - 1;
const auto reduced_depth = std::clamp(new_depth - reduction, 0, new_depth);

// Perform a null window search at reduced depth
current_score =
-negamax<false>(copy, -alpha - 1, -alpha, reduced_depth, ply + 1, child_pv);

// Full depth search
if (current_score > alpha && reduced_depth < new_depth)
current_score =
-negamax<false>(copy, -alpha - 1, -alpha, new_depth, ply + 1, child_pv);

// If we found a better move, do a full window search
if (current_score > alpha && pv_node)
// If we found a better move, do a full re-search
current_score = -negamax<true>(copy, -beta, -alpha, depth - 1, ply + 1, child_pv);
current_score = -negamax<true>(copy, -beta, -alpha, new_depth, ply + 1, child_pv);
}

if (current_score > best_score) {
Expand Down
1 change: 0 additions & 1 deletion src/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <cstddef>
#include <cstdint>

using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
Expand Down

0 comments on commit a6797c6

Please sign in to comment.