Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PV Search! #8

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions src/search/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ void searcher::main_search(const board::position& pos) {

// Iterative deepening loop
for (int current_depth = 1; current_depth <= m_limits.depth_limit; ++current_depth) {
const score best_score = negamax(pos, -constants::score_infinite, constants::score_infinite,
current_depth, 0, m_info.pv);
const score best_score =
negamax<true>(pos, -constants::score_infinite, constants::score_infinite, current_depth,
0, m_info.pv);

if (m_info.stopped) {
// If search stopped too early and we don't have a best move, we update it in order to
Expand All @@ -76,6 +77,7 @@ void searcher::main_search(const board::position& pos) {
std::cout << std::format("bestmove {}", best_move.to_string()) << std::endl;
}

template <bool pv_node>
score searcher::qsearch(const board::position& pos, score alpha, const score beta, const int ply) {
++m_info.searched_nodes;

Expand All @@ -93,7 +95,7 @@ score searcher::qsearch(const board::position& pos, score alpha, const score bet
const auto tt_score = tt_hit ? tt::score_from_tt(entry.value(), ply) : constants::score_none;
const auto tt_move = tt_hit ? entry.move() : moves::move::null();

if (tt_score != constants::score_none && entry.can_use_score(alpha, beta))
if (!pv_node && tt_score != constants::score_none && entry.can_use_score(alpha, beta))
return tt_score;

const score static_eval = eval::evaluate(pos);
Expand Down Expand Up @@ -124,10 +126,7 @@ score searcher::qsearch(const board::position& pos, score alpha, const score bet
if (!copy.was_legal())
continue;

const score current_score = -qsearch(copy, -beta, -alpha, ply + 1);

if (m_info.stopped)
return 0;
const score current_score = -qsearch<pv_node>(copy, -beta, -alpha, ply + 1);

if (current_score > best_score) {
best_score = current_score;
Expand All @@ -140,6 +139,9 @@ score searcher::qsearch(const board::position& pos, score alpha, const score bet
break;
}
}

if (m_info.stopped)
return 0;
}

const auto tt_flag = best_score >= beta ? tt::tt_entry::tt_flag::lower_bound
Expand All @@ -151,6 +153,7 @@ score searcher::qsearch(const board::position& pos, score alpha, const score bet
return best_score;
}

template <bool pv_node>
score searcher::negamax(const board::position& pos,
score alpha,
const score beta,
Expand All @@ -169,7 +172,7 @@ score searcher::negamax(const board::position& pos,
}

if (depth <= 0)
return qsearch(pos, alpha, beta, ply);
return qsearch<pv_node>(pos, alpha, beta, ply);

const bool root_node = ply == 0;

Expand All @@ -186,7 +189,7 @@ score searcher::negamax(const board::position& pos,
const auto tt_move = tt_hit ? entry.move() : moves::move::null();
const u8 tt_depth = entry.depth();

if (!root_node && tt_score != constants::score_none && tt_depth >= depth
if (!pv_node && tt_score != constants::score_none && tt_depth >= depth
&& entry.can_use_score(alpha, beta))
return tt_score;

Expand All @@ -213,7 +216,19 @@ score searcher::negamax(const board::position& pos,

++legal_moves;

const score current_score = -negamax(copy, -beta, -alpha, depth - 1, ply + 1, child_pv);
score current_score;

if (i == 0)
// 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);

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);
}

if (current_score > best_score) {
best_score = current_score;
Expand Down
4 changes: 4 additions & 0 deletions src/search/search.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,18 @@ class searcher {
time_manager m_timer{};

/// @brief Quiescence search, to get rid of the horizon effect
/// @tparam pv_node Indicates if the current node is from the principal variation
/// @param pos Position to search from
/// @param alpha Best score for the maximizing player
/// @param beta Best score for the minimizing player
/// @param ply Internal depth of the search tree (seldepth)
/// @returns The best score found
/// @note See https://en.wikipedia.org/wiki/Quiescence_search for reference
template <bool pv_node>
score qsearch(const board::position& pos, score alpha, score beta, int ply);

/// @brief Fail-soft negamax algorithm with alpha-beta pruning
/// @tparam pv_node Indicates if the current node is from the principal variation
/// @param pos Position to search from
/// @param alpha Best score for the maximizing player
/// @param beta Best score for the minimizing player
Expand All @@ -86,6 +89,7 @@ class searcher {
/// @param pv PV-List on the stack, to keep track of the principal variation
/// @returns The best score found
/// @note See https://en.wikipedia.org/wiki/Negamax for reference
template <bool pv_node>
score negamax(
const board::position& pos, score alpha, score beta, int depth, int ply, pv_line& pv);

Expand Down