From 5a2a493cd299705105a14fe1dfcd7bd1ddd104d7 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Tue, 24 Jul 2018 00:02:11 +0200 Subject: Modified pruning to be friendlier to calculating benchmarks --- src/strategy/monte_carlo.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/strategy/monte_carlo.rs b/src/strategy/monte_carlo.rs index d735074..0a51b39 100644 --- a/src/strategy/monte_carlo.rs +++ b/src/strategy/monte_carlo.rs @@ -17,10 +17,7 @@ use rayon::prelude::*; pub fn choose_move(settings: &GameSettings, state: &GS, start_time: &PreciseTime, max_time: Duration) -> Command { let mut command_scores = CommandScore::init_command_scores(settings, state); - - simulate_options_to_timeout(&mut command_scores, settings, state, start_time, max_time); - - let command = command_scores.iter().max_by_key(|&c| c.win_ratio()); + let command = simulate_options_to_timeout(&mut command_scores, settings, state, start_time, max_time); #[cfg(feature = "benchmarking")] { @@ -35,29 +32,33 @@ pub fn choose_move(settings: &GameSettings, state: &GS, start_tim } #[cfg(not(feature = "discard-poor-performers"))] -fn simulate_options_to_timeout(command_scores: &mut Vec, settings: &GameSettings, state: &GS, start_time: &PreciseTime, max_time: Duration) { +fn simulate_options_to_timeout<'a, GS: GameState>(command_scores: &'a mut Vec, settings: &GameSettings, state: &GS, start_time: &PreciseTime, max_time: Duration) -> Option<&'a CommandScore> { loop { simulate_all_options_once(command_scores, settings, state); if start_time.to(PreciseTime::now()) > max_time { break; } } + command_scores.iter().max_by_key(|&c| c.win_ratio()) } #[cfg(feature = "discard-poor-performers")] -fn simulate_options_to_timeout(command_scores: &mut Vec, settings: &GameSettings, state: &GS, start_time: &PreciseTime, max_time: Duration) { +fn simulate_options_to_timeout<'a, GS: GameState>(command_scores: &'a mut Vec, settings: &GameSettings, state: &GS, start_time: &PreciseTime, max_time: Duration) -> Option<&'a CommandScore> { + use std::cmp; + let maxes = [max_time / 4, max_time / 2, max_time * 3 / 4, max_time]; - for &max in maxes.iter() { + for (i, &max) in maxes.iter().enumerate() { + let new_length = cmp::max(20, command_scores.len() / (2usize.pow(i as u32))); + let active_scores = &mut command_scores[0..new_length]; loop { - simulate_all_options_once(command_scores, settings, state); + simulate_all_options_once(active_scores, settings, state); if start_time.to(PreciseTime::now()) > max { break; } } - command_scores.sort_unstable_by_key(|c| -c.win_ratio()); - let new_length = command_scores.len()/2; - command_scores.truncate(new_length); + active_scores.sort_unstable_by_key(|c| -c.win_ratio()); } + command_scores.first() } #[cfg(feature = "single-threaded")] -- cgit v1.2.3