Skip to content

Commit

Permalink
refactor: calculate reductions and extensions before next depth
Browse files Browse the repository at this point in the history
  • Loading branch information
jovialen committed Mar 1, 2023
1 parent a92a944 commit 1e8da08
Showing 1 changed file with 37 additions and 20 deletions.
57 changes: 37 additions & 20 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ impl MoveSearcher {
self.data.nodes += 1;

let side = self.board.side_to_move();
let tthit = self.transposition.get(&self.board.position).is_some();
let tthit = self.transposition.contains_key(&self.board.position);

if depth == 0 || (depth <= 3 && !tthit) {
return self.quiesce(alpha, beta, distance_from_root);
Expand All @@ -355,17 +355,28 @@ impl MoveSearcher {
}

let next_distance = distance_from_root + 1;
let mut next_depth = depth - 1;

let mut reductions = 0;
let mut extensions = 0;

// All pv nodes not in the transposition table are reduced.
if !tthit {
self.data.reductions += 1;
next_depth = next_depth.saturating_sub(2);
reductions += 3;
}

// Check extension
if self.board.in_check() {
extensions += 1;
}

let mut moves = self.board.moves().clone();
moves.sort_by_key(|m| self.score_move(*m));

self.data.reductions += reductions;
let next_depth = (depth + extensions)
.saturating_sub(reductions + 1)
.min(max_depth - distance_from_root);

let mut best_move = None;
for m in moves {
self.board.make_move(m).expect("Legal move");
Expand Down Expand Up @@ -459,53 +470,58 @@ impl MoveSearcher {
};
let next_distance = distance_from_root + 1;
let next_beta = -beta + 1;
let mut next_depth = depth - 1;

let mut extensions = 0;
let mut reductions = 0;

// Null-move reduction
if current_eval >= beta && !self.board.in_check() {
let reduction = 3 + usize::from(depth > 6);
let next_depth = depth.saturating_sub(3 + usize::from(depth > 6));

self.board.make_null_move();
let null_eval = -self.zw_search(
next_node_type,
next_beta,
depth.saturating_sub(reduction),
next_depth,
max_depth,
next_distance,
);
self.board.undo();

if null_eval >= beta && !null_eval.is_mate() {
self.data.reductions += 1;
next_depth = next_depth.saturating_sub(6);
reductions += 6;

if next_depth == 0 {
if depth <= 6 {
self.data.prunes += 1;
return self.evaluate(distance_from_root);
}
}
}

// Cut node reduction
if next_depth >= 1 && node_type == NodeType::Cut {
self.data.reductions += 1;
next_depth -= 1;
if node_type == NodeType::Cut {
reductions += 2;
}

let mut moves = self.board.moves().clone();
moves.sort_by_key(|m| self.score_move(*m));

// One reply extension
if moves.len() == 1 {
extensions += 1;
}

// Multi-cut pruning.
if node_type == NodeType::Cut && moves.len() > self.mc_moves && !self.board.in_check() {
let mc_depth = next_depth.saturating_sub(self.mc_reduction);
let next_depth = depth.saturating_sub(self.mc_reduction + 1);

let mut count = 0;
for m in moves.iter().take(self.mc_moves) {
self.board.make_move(*m).expect("Legal move");
let score = -self.zw_search(
next_node_type,
next_beta,
mc_depth,
next_depth,
max_depth,
next_distance,
);
Expand All @@ -521,17 +537,18 @@ impl MoveSearcher {
}
}

self.data.reductions += reductions;
let next_depth = (depth + extensions)
.saturating_sub(reductions + 1)
.min(max_depth - distance_from_root);

// Search children.
for m in moves {
self.board.make_move(m).expect("Legal move");

// Check extension
let extension = usize::from(self.board.in_check() && distance_from_root < max_depth);

let score = -self.zw_search(
next_node_type,
next_beta,
next_depth + extension,
next_depth,
max_depth,
next_distance,
);
Expand Down

0 comments on commit 1e8da08

Please # to comment.