Add option to only generate outputs for lost games

This commit is contained in:
Felix Bauckholt 2019-02-27 12:32:47 +01:00
parent 180d8c5f4e
commit ade33395c2
2 changed files with 10 additions and 6 deletions

View file

@ -76,6 +76,8 @@ fn main() {
"Print a table of results for each strategy"); "Print a table of results for each strategy");
opts.optflag("", "write-results-table", opts.optflag("", "write-results-table",
"Update the results table in README.md"); "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..]) { let matches = match opts.parse(&args[1..]) {
Ok(m) => { m } Ok(m) => { m }
Err(f) => { 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 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_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 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()); 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( fn sim_games(
@ -135,6 +138,7 @@ fn sim_games(
n_threads: u32, n_threads: u32,
progress_info: Option<u32>, progress_info: Option<u32>,
json_output_pattern: Option<String>, json_output_pattern: Option<String>,
json_losses_only: bool,
) -> simulator::SimResult { ) -> simulator::SimResult {
let hand_size = match n_players { let hand_size = match n_players {
2 => 5, 2 => 5,
@ -172,7 +176,7 @@ fn sim_games(
panic!("Unexpected strategy argument {}", strategy_str); 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 { fn get_results_table() -> String {
@ -208,7 +212,7 @@ fn get_results_table() -> String {
&|n_players| (format_players(n_players), dashes_long.clone())); &|n_players| (format_players(n_players), dashes_long.clone()));
let mut body = strategies.iter().map(|strategy| { let mut body = strategies.iter().map(|strategy| {
make_twolines(&player_nums, (format_name(strategy), space.clone()), &|n_players| { 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_score(simresult.average_score(), simresult.score_stderr()),
format_percent(simresult.percent_perfect(), simresult.percent_perfect_stderr()) format_percent(simresult.percent_perfect(), simresult.percent_perfect_stderr())

View file

@ -164,6 +164,7 @@ pub fn simulate<T: ?Sized>(
n_threads: u32, n_threads: u32,
progress_info: Option<u32>, progress_info: Option<u32>,
json_output_pattern: Option<String>, json_output_pattern: Option<String>,
json_losses_only: bool,
) -> SimResult ) -> SimResult
where T: GameStrategyConfig + Sync { where T: GameStrategyConfig + Sync {
@ -203,14 +204,13 @@ pub fn simulate<T: ?Sized>(
lives_histogram.insert(game.board.lives_remaining); lives_histogram.insert(game.board.lives_remaining);
score_histogram.insert(score); score_histogram.insert(score);
if score != PERFECT_SCORE { non_perfect_seeds.push(seed); } if score != PERFECT_SCORE { non_perfect_seeds.push(seed); }
match json_output_pattern_ref { if let Some(file_pattern) = json_output_pattern_ref {
Some(file_pattern) => { if !(score == PERFECT_SCORE && json_losses_only) {
let file_pattern = file_pattern.clone().replace("%s", &seed.to_string()); let file_pattern = file_pattern.clone().replace("%s", &seed.to_string());
let path = std::path::Path::new(&file_pattern); let path = std::path::Path::new(&file_pattern);
let file = std::fs::File::create(path).unwrap(); let file = std::fs::File::create(path).unwrap();
serde_json::to_writer(file, &json_output.unwrap()).unwrap(); serde_json::to_writer(file, &json_output.unwrap()).unwrap();
} }
None => { }
} }
} }
if progress_info.is_some() { if progress_info.is_some() {