From ade33395c28d250c821e4124badfed3fe24959aa Mon Sep 17 00:00:00 2001 From: Felix Bauckholt Date: Wed, 27 Feb 2019 12:32:47 +0100 Subject: [PATCH] Add option to only generate outputs for lost games --- src/main.rs | 10 +++++++--- src/simulator.rs | 6 +++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index beeea2f..d8f2d0c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,6 +76,8 @@ fn main() { "Print a table of results for each strategy"); opts.optflag("", "write-results-table", "Update the results table in README.md"); + opts.optflag("", "losses-only", + "When saving JSON outputs, save lost games only"); let matches = match opts.parse(&args[1..]) { Ok(m) => { m } Err(f) => { @@ -120,11 +122,12 @@ fn main() { let n_threads = u32::from_str(&matches.opt_str("t").unwrap_or("1".to_string())).unwrap(); let json_output_pattern = matches.opt_str("j"); + let json_losses_only = matches.opt_present("losses-only"); let n_players = u32::from_str(&matches.opt_str("p").unwrap_or("4".to_string())).unwrap(); let strategy_str : &str = &matches.opt_str("g").unwrap_or("cheat".to_string()); - sim_games(n_players, strategy_str, seed, n_trials, n_threads, progress_info, json_output_pattern).info(); + sim_games(n_players, strategy_str, seed, n_trials, n_threads, progress_info, json_output_pattern, json_losses_only).info(); } fn sim_games( @@ -135,6 +138,7 @@ fn sim_games( n_threads: u32, progress_info: Option, json_output_pattern: Option, + json_losses_only: bool, ) -> simulator::SimResult { let hand_size = match n_players { 2 => 5, @@ -172,7 +176,7 @@ fn sim_games( panic!("Unexpected strategy argument {}", strategy_str); }, }; - simulator::simulate(&game_opts, strategy_config, seed, n_trials, n_threads, progress_info, json_output_pattern) + simulator::simulate(&game_opts, strategy_config, seed, n_trials, n_threads, progress_info, json_output_pattern, json_losses_only) } fn get_results_table() -> String { @@ -208,7 +212,7 @@ fn get_results_table() -> String { &|n_players| (format_players(n_players), dashes_long.clone())); let mut body = strategies.iter().map(|strategy| { make_twolines(&player_nums, (format_name(strategy), space.clone()), &|n_players| { - let simresult = sim_games(n_players, strategy, Some(seed), n_trials, n_threads, None, None); + let simresult = sim_games(n_players, strategy, Some(seed), n_trials, n_threads, None, None, false); ( format_score(simresult.average_score(), simresult.score_stderr()), format_percent(simresult.percent_perfect(), simresult.percent_perfect_stderr()) diff --git a/src/simulator.rs b/src/simulator.rs index a5ee3f9..56b4d2e 100644 --- a/src/simulator.rs +++ b/src/simulator.rs @@ -164,6 +164,7 @@ pub fn simulate( n_threads: u32, progress_info: Option, json_output_pattern: Option, + json_losses_only: bool, ) -> SimResult where T: GameStrategyConfig + Sync { @@ -203,14 +204,13 @@ pub fn simulate( lives_histogram.insert(game.board.lives_remaining); score_histogram.insert(score); if score != PERFECT_SCORE { non_perfect_seeds.push(seed); } - match json_output_pattern_ref { - Some(file_pattern) => { + if let Some(file_pattern) = json_output_pattern_ref { + if !(score == PERFECT_SCORE && json_losses_only) { let file_pattern = file_pattern.clone().replace("%s", &seed.to_string()); let path = std::path::Path::new(&file_pattern); let file = std::fs::File::create(path).unwrap(); serde_json::to_writer(file, &json_output.unwrap()).unwrap(); } - None => { } } } if progress_info.is_some() {