From baa4f9950f49ffbb451c4796a764738f48b29504 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Tue, 14 Aug 2018 23:28:47 +0200 Subject: Added extra logging of the win ratios --- src/strategy/monte_carlo.rs | 50 +++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 20 deletions(-) (limited to 'src/strategy') diff --git a/src/strategy/monte_carlo.rs b/src/strategy/monte_carlo.rs index 2b98eed..ba2ef5c 100644 --- a/src/strategy/monte_carlo.rs +++ b/src/strategy/monte_carlo.rs @@ -3,6 +3,8 @@ use engine::status::GameStatus; use engine::bitwise_engine::{Player, BitwiseGameState}; use engine::constants::*; +use std::fmt; + use rand::{Rng, XorShiftRng, SeedableRng}; const MAX_MOVES: u16 = 400; @@ -18,12 +20,28 @@ use rayon::prelude::*; pub fn choose_move(state: &BitwiseGameState, start_time: PreciseTime, max_time: Duration) -> Command { let mut command_scores = CommandScore::init_command_scores(state); - let command = simulate_options_to_timeout(&mut command_scores, state, start_time, max_time); - - match command { - Some(command) => command.command, - _ => Command::Nothing + + let command = { + let best_command_score = simulate_options_to_timeout(&mut command_scores, state, start_time, max_time); + match best_command_score { + Some(best_command_score) => best_command_score.command, + _ => Command::Nothing + } + }; + + #[cfg(feature = "benchmarking")] + { + let total_iterations: u32 = command_scores.iter().map(|c| c.attempts).sum(); + println!("Iterations: {}", total_iterations); + } + #[cfg(feature = "debug-decisions")] + { + for score in command_scores { + println!("{}", score); + } } + + command } #[cfg(not(feature = "discard-poor-performers"))] @@ -34,13 +52,6 @@ fn simulate_options_to_timeout(command_scores: &'a mut Vec, settin 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()) } @@ -61,13 +72,6 @@ fn simulate_options_to_timeout<'a>(command_scores: &'a mut Vec, st } 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() } @@ -214,6 +218,13 @@ impl CommandScore { } } +impl fmt::Display for CommandScore { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{},{}", self.command, self.win_ratio()) + } +} + + #[cfg(not(feature = "energy-cutoff"))] fn sensible_buildings(player: &Player) -> Vec { let mut result = Vec::with_capacity(4); @@ -234,7 +245,6 @@ fn sensible_buildings(player: &Player) -> Vec { result } - //TODO: Heuristic that avoids building the initial energy towers all in the same row? Max energy in a row? #[cfg(feature = "energy-cutoff")] fn sensible_buildings(player: &Player) -> Vec { -- cgit v1.2.3