Skip to content

Commit

Permalink
Make rollouts about 1 percent faster by not evaluating obvious positions
Browse files Browse the repository at this point in the history
  • Loading branch information
carsten-wenderdel committed Oct 7, 2023
1 parent dff6767 commit f285030
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions crates/engine/src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ pub trait Evaluator {
where
F: Fn(&Probabilities) -> f32,
{
// Two optimizations so that we don't have to call eval that often.
// The function would also work without the next 6 lines.
if positions.len() == 1 {
return positions.first().unwrap();
}
if let Some(end_of_game_position) = positions.iter().find(|p| p.has_lost()) {
return end_of_game_position;
}

// No obvious position found, so now we have to call `eval` for all of them.
positions
.iter()
.map(|pos| (pos, value(&self.eval(pos))))
Expand Down
6 changes: 6 additions & 0 deletions crates/engine/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ impl Position {
self.pips[pip]
}

pub fn has_lost(&self) -> bool {
self.o_off == NO_OF_CHECKERS
}

pub fn game_state(&self) -> GameState {
debug_assert!(
self.x_off < 15 || self.o_off < 15,
Expand Down Expand Up @@ -499,10 +503,12 @@ mod tests {
fn game_state_normal() {
let given = pos!(x 19:14; o);
assert_eq!(given.game_state(), GameOver(LoseNormal));
assert!(given.has_lost());
assert_eq!(
given.switch_sides().game_state(),
GameOver(LoseNormal.reverse())
);
assert!(!given.switch_sides().has_lost());
}

#[test]
Expand Down

0 comments on commit f285030

Please sign in to comment.