Skip to content

Commit

Permalink
Improve mate finding - #43
Browse files Browse the repository at this point in the history
Improve mate finding

STC simplification:
ELO   | -1.67 +- 1.89 (95%)
SPRT  | 10.0+0.10s Threads=1 Hash=16MB
LLR   | -2.97 (-2.94, 2.94) [-3.00, 1.00]
GAMES | N: 68536 W: 17970 L: 18299 D: 32267

LTC simplification:
ELO   | 1.78 +- 3.68 (95%)
SPRT  | 60.0+0.60s Threads=1 Hash=256MB
LLR   | 3.00 (-2.94, 2.94) [-4.00, 1.00]
GAMES | N: 16616 W: 4076 L: 3991 D: 8549

Bench: 3091908
  • Loading branch information
SzilBalazs authored Aug 7, 2023
2 parents 492bbde + d0e8017 commit ffae08d
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/search/search_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ namespace search {

History history;

template<bool to_tt>
static Score convert_tt_score(Score score, Ply ply) {

if constexpr (to_tt) {
ply *= -1;
}

if (score > WORST_MATE) {
score -= ply;
} else if (score < -WORST_MATE) {
score += ply;
}
return score;
}

void search() {
init_search();
iterative_deepening();
Expand Down Expand Up @@ -221,8 +236,15 @@ namespace search {
return UNKNOWN_SCORE;
}

if (non_root_node && board.is_draw()) {
return 0;
if (non_root_node) {
if (board.is_draw()) {
return 0;
}

alpha = std::max(alpha, -MATE_VALUE + ss->ply);
beta = std::min(beta, MATE_VALUE - ss->ply - 1);
if (alpha >= beta)
return alpha;
}

if (in_check) {
Expand All @@ -231,11 +253,12 @@ namespace search {

std::optional<TTEntry> entry = shared.tt.probe(board.get_hash());
TTFlag flag = TT_ALPHA;
Score tt_score = entry ? convert_tt_score<false>(entry->eval, ss->ply) : UNKNOWN_SCORE;
core::Move hash_move = entry ? entry->hash_move : core::NULL_MOVE;

if (entry && non_pv_node && entry->depth >= depth && board.get_move50() < 90 &&
(entry->flag == TT_EXACT || (entry->flag == TT_ALPHA && entry->eval <= alpha) || (entry->flag == TT_BETA && entry->eval >= beta))) {
return entry->eval;
(entry->flag == TT_EXACT || (entry->flag == TT_ALPHA && tt_score <= alpha) || (entry->flag == TT_BETA && tt_score >= beta))) {
return tt_score;
}

if (depth <= 0)
Expand Down Expand Up @@ -344,7 +367,7 @@ namespace search {
}
}

shared.tt.save(board.get_hash(), depth, beta, TT_BETA, move);
shared.tt.save(board.get_hash(), depth, convert_tt_score<true>(beta, ss->ply), TT_BETA, move);
return beta;
}

Expand All @@ -366,7 +389,7 @@ namespace search {
if (move.is_quiet()) *next_quiet_move++ = move;
}

shared.tt.save(board.get_hash(), depth, best_score, flag, best_move);
shared.tt.save(board.get_hash(), depth, convert_tt_score<true>(best_score, ss->ply), flag, best_move);
return alpha;
}

Expand Down

0 comments on commit ffae08d

Please sign in to comment.