Fixed min and benchmark logging in discarding search
authorJustin Worthe <justin@worthe-it.co.za>
Tue, 24 Jul 2018 20:21:29 +0000 (22:21 +0200)
committerJustin Worthe <justin@worthe-it.co.za>
Tue, 24 Jul 2018 20:21:29 +0000 (22:21 +0200)
src/strategy/monte_carlo.rs

index 0a51b39..127e690 100644 (file)
@@ -18,12 +18,6 @@ use rayon::prelude::*;
 pub fn choose_move<GS: GameState>(settings: &GameSettings, state: &GS, start_time: &PreciseTime, max_time: Duration) -> Command {
     let mut command_scores = CommandScore::init_command_scores(settings, state);
     let command = simulate_options_to_timeout(&mut command_scores, settings, state, start_time, max_time);
-
-    #[cfg(feature = "benchmarking")]
-    {
-        let total_iterations: u32 = command_scores.iter().map(|c| c.attempts).sum();
-        println!("Iterations: {}", total_iterations);
-    }
     
     match command {
         Some(command) => command.command,
@@ -39,16 +33,24 @@ fn simulate_options_to_timeout<'a, GS: GameState>(command_scores: &'a mut Vec<Co
             break;
         }
     }
+
+    #[cfg(feature = "benchmarking")]
+    {
+        let total_iterations: u32 = command_scores.iter().map(|c| c.attempts).sum();
+        println!("Iterations: {}", total_iterations);
+    }
+
     command_scores.iter().max_by_key(|&c| c.win_ratio())
 }
 
 #[cfg(feature = "discard-poor-performers")]
 fn simulate_options_to_timeout<'a, GS: GameState>(command_scores: &'a mut Vec<CommandScore>, settings: &GameSettings, state: &GS, start_time: &PreciseTime, max_time: Duration) -> Option<&'a CommandScore> {
     use std::cmp;
+    let min_options = cmp::min(command_scores.len(), 5);
     
     let maxes = [max_time / 4, max_time / 2, max_time * 3 / 4, max_time];
     for (i, &max) in maxes.iter().enumerate() {
-        let new_length = cmp::max(20, command_scores.len() / (2usize.pow(i as u32)));
+        let new_length = cmp::max(min_options, command_scores.len() / (2usize.pow(i as u32)));
         let active_scores = &mut command_scores[0..new_length];
         loop {
             simulate_all_options_once(active_scores, settings, state);
@@ -58,6 +60,13 @@ fn simulate_options_to_timeout<'a, GS: GameState>(command_scores: &'a mut Vec<Co
         }
         active_scores.sort_unstable_by_key(|c| -c.win_ratio());
     }
+
+    #[cfg(feature = "benchmarking")]
+    {
+        let total_iterations: u32 = command_scores.iter().map(|c| c.attempts).sum();
+        println!("Iterations: {}", total_iterations);
+    }
+
     command_scores.first()
 }