Skip to content

Commit

Permalink
qsearch: generate all moves when in check
Browse files Browse the repository at this point in the history
Bench: 9836840
  • Loading branch information
SzilBalazs committed Sep 2, 2023
1 parent 31e342d commit 8590459
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
5 changes: 2 additions & 3 deletions src/search/move_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "see.h"

namespace search {
template<bool captures_only>
class MoveList {

static constexpr int MOVE_SCORE_HASH = 10'000'000;
Expand All @@ -43,8 +42,8 @@ namespace search {
* @param history History object containing information about the search
* @param ply Distance from root
*/
MoveList(const chess::Board &board, const chess::Move &hash_move, const chess::Move &last_move, const History &history, const Ply &ply) : current(0), board(board),
hash_move(hash_move), last_move(last_move), history(history), ply(ply) {
MoveList(const chess::Board &board, const chess::Move &hash_move, const chess::Move &last_move, const History &history, const Ply &ply, bool captures_only = false) : current(0), board(board),
hash_move(hash_move), last_move(last_move), history(history), ply(ply) {
size = chess::gen_moves(board, moves, captures_only) - moves;
std::transform(moves, moves + size, scores, [this](const chess::Move &move) {
return score_move(move);
Expand Down
21 changes: 13 additions & 8 deletions src/search/search_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ namespace search {
}

if (depth <= 0)
return qsearch<node_type>(alpha, beta);
return qsearch<node_type>(alpha, beta, ss);

Score static_eval = ss->eval = eval::evaluate(board, nnue);
bool improving = ss->ply >= 2 && ss->eval >= (ss - 2)->eval;
Expand All @@ -317,7 +317,7 @@ namespace search {
depth--;

if (depth <= 3 && static_eval + 150 * depth <= alpha) {
Score score = qsearch<NON_PV_NODE>(alpha, beta);
Score score = qsearch<NON_PV_NODE>(alpha, beta, ss);
if (score <= alpha)
return score;
}
Expand Down Expand Up @@ -348,7 +348,7 @@ namespace search {
}

search_moves:
MoveList<false> move_list(board, hash_move, last_move, history, ss->ply);
MoveList move_list(board, hash_move, last_move, history, ss->ply);

if (move_list.empty()) {
return in_check ? mate_ply : 0;
Expand Down Expand Up @@ -477,13 +477,14 @@ namespace search {
}

template<NodeType node_type>
Score qsearch(Score alpha, Score beta) {
Score qsearch(Score alpha, Score beta, SearchStack *ss) {

if (!shared.is_searching) {
return UNKNOWN_SCORE;
}

Score static_eval = eval::evaluate(board, nnue);
const bool in_check = board.is_check();
const Score static_eval = eval::evaluate(board, nnue);

if (static_eval >= beta) {
return beta;
Expand All @@ -492,12 +493,16 @@ namespace search {
alpha = static_eval;
}

MoveList<true> move_list(board, chess::NULL_MOVE, chess::NULL_MOVE, history, 0);
MoveList move_list(board, chess::NULL_MOVE, chess::NULL_MOVE, history, 0, !in_check);

if (in_check && move_list.empty()) {
return -MATE_VALUE + ss->ply;
}

while (!move_list.empty()) {
chess::Move move = move_list.next_move();

if (alpha > -WORST_MATE && !see(board, move, 0)) {
if (alpha > -WORST_MATE && !in_check && !see(board, move, 0)) {
stat_tracker::record_success("qsearch_see");
break;
} else {
Expand All @@ -506,7 +511,7 @@ namespace search {

shared.node_count[id]++;
board.make_move(move, &nnue);
Score score = -qsearch<node_type>(-beta, -alpha);
Score score = -qsearch<node_type>(-beta, -alpha, ss + 1);
board.undo_move(move, &nnue);

if (score == UNKNOWN_SCORE) {
Expand Down

0 comments on commit 8590459

Please sign in to comment.