Skip to content

Commit

Permalink
Counter history
Browse files Browse the repository at this point in the history
Bench: 10446323
  • Loading branch information
SzilBalazs committed Sep 2, 2023
1 parent 31e342d commit 380eeab
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 27 deletions.
49 changes: 40 additions & 9 deletions src/search/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,21 @@
#include "../chess/move.h"

namespace search {

struct SearchStack {
Ply ply;
chess::Move move;
PieceType pt;
Score eval;
};

class History {

public:
chess::Move killer_moves[MAX_PLY + 10][2];
chess::Move counter_moves[64][64];
Score butterfly[64][64];
Score counter_history[6][64][64][64];

/**
* Adds a beta-cutoff to the History.
Expand All @@ -35,10 +44,14 @@ namespace search {
* @param depth Search depth
* @param ply Distance from root
*/
void add_cutoff(chess::Move move, chess::Move last_move, Depth depth, Ply ply) {
update_killer_moves(move, ply);
update_counter_moves(move, last_move);
update_butterfly_history(move, depth * 100);
void add_cutoff(chess::Move move, Depth depth, SearchStack *ss) {
update_killer_moves(move, ss->ply);
update_history(butterfly[move.get_from()][move.get_to()], depth * 100);

if ((ss - 1)->move.is_ok()) {
update_counter_moves(move, (ss - 1)->move);
update_history(counter_history[(ss - 1)->pt][(ss - 1)->move.get_to()][move.get_from()][move.get_to()], depth * 100);
}
}

/**
Expand All @@ -47,8 +60,20 @@ namespace search {
* @param move The weak move
* @param depth Search depth
*/
void decrease_history(chess::Move move, Depth depth) {
update_butterfly_history(move, -depth * 100);
void decrease_history(chess::Move move, Depth depth, SearchStack *ss) {
update_history(butterfly[move.get_from()][move.get_to()], -depth * 100);

if ((ss - 1)->move.is_ok()) {
update_history(counter_history[(ss - 1)->pt][(ss - 1)->move.get_to()][move.get_from()][move.get_to()], -depth * 100);
}
}

Score get_history(chess::Move move, SearchStack *ss) const {
Score value = butterfly[move.get_from()][move.get_to()];
if ((ss - 1)->move.is_ok()) {
value += counter_history[(ss - 1)->pt][(ss - 1)->move.get_to()][move.get_from()][move.get_to()];
}
return value;
}

/**
Expand All @@ -60,6 +85,12 @@ namespace search {
}
for (int i = 0; i < 64; i++) {
for (int j = 0; j < 64; j++) {
for (int k = 0; k < 64; k++) {
for (int pt = 0; pt < 6; pt++) {
counter_history[pt][i][j][k] = 0;
}
}

butterfly[i][j] = 0;
counter_moves[i][j] = chess::NULL_MOVE;
}
Expand All @@ -76,9 +107,9 @@ namespace search {
counter_moves[last_move.get_from()][last_move.get_to()] = move;
}

void update_butterfly_history(chess::Move move, int bonus) {
int scaled = bonus - butterfly[move.get_from()][move.get_to()] * std::abs(bonus) / 32768;
butterfly[move.get_from()][move.get_to()] += scaled;
static void update_history(Score &entry, Score bonus) {
int scaled = bonus - entry * std::abs(bonus) / 32768;
entry += scaled;
}
};
} // namespace search
7 changes: 4 additions & 3 deletions src/search/move_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,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 History &history, SearchStack *ss) : current(0), board(board), ss(ss),
hash_move(hash_move), last_move((ss - 1)->move), history(history), ply(ss->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 Expand Up @@ -79,6 +79,7 @@ namespace search {
unsigned int size, current;
int scores[200];
const chess::Board &board;
SearchStack *ss;
const chess::Move &hash_move;
const chess::Move &last_move;
const History &history;
Expand All @@ -104,7 +105,7 @@ namespace search {
} else if (move == history.counter_moves[last_move.get_from()][last_move.get_to()]) {
return MOVE_SCORE_COUNTER;
} else {
return history.butterfly[move.get_from()][move.get_to()];
return history.get_history(move, ss);
}
}
};
Expand Down
24 changes: 9 additions & 15 deletions src/search/search_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ namespace search {
}
};

struct SearchStack {
Ply ply;
chess::Move move;
Score eval;
};

class SearchThread {
public:
SearchThread(SharedMemory &shared_memory, unsigned int thread_id) : nnue(), shared(shared_memory), id(thread_id) {}
Expand Down Expand Up @@ -256,7 +250,6 @@ namespace search {
constexpr bool pv_node = node_type != NON_PV_NODE;
constexpr bool non_pv_node = !pv_node;

const chess::Move last_move = ss->ply >= 1 ? (ss - 1)->move : chess::NULL_MOVE;
const Score mate_ply = -MATE_VALUE + ss->ply;
const bool in_check = board.is_check();

Expand Down Expand Up @@ -305,7 +298,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 +310,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 +341,7 @@ namespace search {
}

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

if (move_list.empty()) {
return in_check ? mate_ply : 0;
Expand All @@ -363,6 +356,7 @@ namespace search {
int made_moves = 0;
while (!move_list.empty()) {
chess::Move move = ss->move = move_list.next_move();
ss->pt = board.piece_at(move.get_from()).type;

if (skip_quiets && move.is_quiet() && !move.is_promo()) continue;

Expand Down Expand Up @@ -438,9 +432,9 @@ namespace search {
if (score >= beta) {

if (move.is_quiet()) {
history.add_cutoff(move, last_move, depth, ss->ply);
history.add_cutoff(move, depth, ss);
for (chess::Move *current_move = quiet_moves; current_move != next_quiet_move; current_move++) {
history.decrease_history(*current_move, depth);
history.decrease_history(*current_move, depth, ss);
}
}

Expand Down Expand Up @@ -477,7 +471,7 @@ 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;
Expand All @@ -492,7 +486,7 @@ namespace search {
alpha = static_eval;
}

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

while (!move_list.empty()) {
chess::Move move = move_list.next_move();
Expand All @@ -506,7 +500,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 380eeab

Please sign in to comment.