Skip to content

Commit

Permalink
feat: Add killer moves and raise bench depth (#11)
Browse files Browse the repository at this point in the history
Passed STC:

Elo   | 123.13 +- 25.79 (95%)
SPRT  | 8.0+0.08s Threads=1 Hash=16MB
LLR   | 2.98 (-2.94, 2.94) [0.00, 5.00]
Games | N: 576 W: 316 L: 120 D: 140
Penta | [11, 29, 84, 81, 83]
https://openbench.chessnibble.com/test/96/

BENCH: 27480710
  • Loading branch information
IbaiBuR committed Jul 10, 2024
1 parent 6ef065e commit 1ce3862
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ int main(const int argc, const char *argv[]) {
board::bitboards::attacks::init();

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

search::searcher searcher;
search::bench::run(searcher, bench_depth);
Expand Down
17 changes: 15 additions & 2 deletions src/moves/movelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ constexpr std::array<std::array<score, constants::num_piece_types>, constants::n
};
// clang-format on

void move_list::score_moves(const move tt_move, const board::position& pos) {
constexpr int mvv_lva_base_bonus = 1000;
constexpr int killer_moves_base_bonus = mvv_lva_base_bonus - 1;

void move_list::score_moves(const move tt_move,
const board::position& pos,
const search::search_data& search_data,
const int ply) {
for (usize i = 0; i < m_size; ++i) {
const auto current_move = move_at(i);

Expand All @@ -36,7 +42,14 @@ void move_list::score_moves(const move tt_move, const board::position& pos) {
board::pieces::piece_to_piece_type(pos.piece_on(current_move.to()));

m_moves[i].move_score = mvv_lva[std::to_underlying(attacker_piece_type)]
[std::to_underlying(victim_piece_type)];
[std::to_underlying(victim_piece_type)]
+ mvv_lva_base_bonus;
}
else {
if (current_move == search_data.first_killer(ply))
m_moves[i].move_score = killer_moves_base_bonus;
else if (current_move == search_data.second_killer(ply))
m_moves[i].move_score = killer_moves_base_bonus - 1;
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/moves/movelist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "move.hpp"
#include "../board/piece.hpp"
#include "../board/position.hpp"
#include "../search/search.hpp"

namespace moves {

Expand All @@ -30,7 +31,10 @@ class move_list {

void clear() { m_size = 0; }

void score_moves(move tt_move, const board::position& pos);
void score_moves(move tt_move,
const board::position& pos,
const search::search_data& search_data,
int ply);

void sort() {
std::stable_sort(begin(), end(), [](const scored_move a, const scored_move b) {
Expand Down
13 changes: 9 additions & 4 deletions src/search/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ constexpr int rfp_margin = 70;

constexpr int nmp_base_reduction = 3;

void searcher::reset_info() {
void searcher::reset() {
m_info.stopped = false;
m_info.searched_nodes = 0ULL;

m_info.pv.clear();
m_data.clear_killers();
}

void searcher::set_limits(const u64 nodes_limit, const u64 time_limit, const u32 depth_limit) {
Expand Down Expand Up @@ -55,7 +57,7 @@ void searcher::parse_time_control(const std::vector<std::string>& command, const
}

void searcher::main_search(const board::position& pos) {
reset_info();
reset();
auto best_move = moves::move::null();

// Iterative deepening loop
Expand Down Expand Up @@ -122,7 +124,7 @@ score searcher::qsearch(const board::position& pos, score alpha, const score bet

moves::move_list move_list;
generate_all_captures(pos, move_list);
move_list.score_moves(tt_move, pos);
move_list.score_moves(tt_move, pos, m_data, ply);
move_list.sort();

for (usize i = 0; i < move_list.size(); i++) {
Expand Down Expand Up @@ -240,7 +242,7 @@ score searcher::negamax(const board::position& pos,

moves::move_list move_list;
generate_all_moves(pos, move_list);
move_list.score_moves(tt_move, pos);
move_list.score_moves(tt_move, pos, m_data, ply);
move_list.sort();

for (usize i = 0; i < move_list.size(); ++i) {
Expand Down Expand Up @@ -276,6 +278,9 @@ score searcher::negamax(const board::position& pos,
best_move = current_move;
pv.update(current_move, child_pv);

if (best_move.is_quiet())
m_data.update_killers(best_move, ply);

if (alpha >= beta)
break;
}
Expand Down
32 changes: 31 additions & 1 deletion src/search/search.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <algorithm>
#include <format>
#include <vector>

Expand Down Expand Up @@ -50,11 +51,39 @@ struct search_info {
bool stopped;
};

struct search_data {
using killers = std::array<moves::move, 2>;

std::array<killers, constants::max_ply> killer_moves;

search_data() :
killer_moves() {
clear_killers();
}

void clear_killers() {
std::ranges::for_each(killer_moves.begin(), killer_moves.end(),
[](killers& current_killers) {
current_killers[0] = current_killers[1] = moves::move::none();
});
}

void update_killers(const moves::move move, const int ply) {
if (move != killer_moves[0][ply]) {
killer_moves[1][ply] = killer_moves[0][ply];
killer_moves[0][ply] = move;
}
}

[[nodiscard]] auto first_killer(const int ply) const { return killer_moves[0][ply]; }
[[nodiscard]] auto second_killer(const int ply) const { return killer_moves[1][ply]; }
};

class searcher {
public:
[[nodiscard]] u64 searched_nodes() const { return m_info.searched_nodes; }

void reset_info();
void reset();
void set_limits(u64 nodes_limit, u64 time_limit, u32 depth_limit);
void set_start_time(u64 time);
void parse_time_control(const std::vector<std::string>& command, color stm);
Expand All @@ -64,6 +93,7 @@ class searcher {
void main_search(const board::position& pos);

private:
search_data m_data;
search_info m_info{};
search_limits m_limits{};
time_manager m_timer{};
Expand Down

0 comments on commit 1ce3862

Please sign in to comment.