From 012b079e46376fbd632b84a06fdedaf1117e34ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Sejkora?= Date: Sun, 21 Feb 2021 00:00:42 +0100 Subject: [PATCH] Fix combine not retrying the same axis multiple times --- src/combine.rs | 54 ++++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/combine.rs b/src/combine.rs index 36d5006..7b5c6a8 100644 --- a/src/combine.rs +++ b/src/combine.rs @@ -50,45 +50,47 @@ fn choose_layouts(db: &TDB, city: &City, top_layout_count: usize) } pub fn iterate_combines(mut db: &mut TDB, top_layout_count: usize, city: &City, mut cache: &mut CompatibilityCache, print_progress: bool) { - #[derive(Eq, PartialEq)] - enum LastStep { - None, - Vertical, - Horizontal, - } + let mut no_improvements_in_a_row = 0; if print_progress { eprintln!("Building a transposed city..."); } let transposed_city = transpose_city(&city); if print_progress { eprintln!("Finished building a transposed city"); } - if print_progress { eprintln!("Building right distances..."); } - let right_distances = city::build_distances_right(&city); - if print_progress { eprintln!("Building right distances (transposed)..."); } - let right_distances_transposed = city::build_distances_right(&city); - if print_progress { eprintln!("Finished building right distances"); } - - let mut last_improve_step = LastStep::None; loop { - if last_improve_step == LastStep::Vertical { break; } + if no_improvements_in_a_row == 2 { break; } if print_progress { eprintln!("Starting to combine {} top houses DB; vertical cuts", top_layout_count); } - let chosen_layouts = choose_layouts(db, &city, top_layout_count); - if create_new_best_combination(&city, &chosen_layouts, &chosen_layouts, db, &mut cache, &right_distances, false, print_progress) { - last_improve_step = LastStep::Vertical; + { + let chosen_layouts = choose_layouts(db, &city, top_layout_count); + if print_progress { eprintln!("Building right distances..."); } + let right_distances = city::build_distances_right(&city); + if print_progress { eprintln!("Finished building right distances"); } + if create_new_best_combination(&city, &chosen_layouts, &chosen_layouts, db, &mut cache, &right_distances, false, print_progress) { + no_improvements_in_a_row = 0; + } else { + no_improvements_in_a_row += 1; + } + if print_progress { eprintln!("Finished vertical cuts, improvement: {}", no_improvements_in_a_row == 0); } } - if print_progress { eprintln!("Finished vertical cuts, improvement: {}", last_improve_step == LastStep::Vertical); } - if last_improve_step == LastStep::Horizontal { break; } + if no_improvements_in_a_row == 2 { break; } - let chosen_layouts = choose_layouts(db, &city, top_layout_count); - let transposed_chosen_layouts: Vec<_> = chosen_layouts.iter().map(|x| transpose_saved_layout(x)).collect(); + { + let chosen_layouts = choose_layouts(db, &city, top_layout_count); + let transposed_chosen_layouts: Vec<_> = chosen_layouts.iter().map(|x| transpose_saved_layout(x)).collect(); + if print_progress { eprintln!("Building right distances (transposed)..."); } + let right_distances_transposed = city::build_distances_right(&transposed_city); + if print_progress { eprintln!("Finished building right distances"); } - if print_progress { eprintln!("Starting to combine {} top houses DB; horizontal cuts", top_layout_count); } - if create_new_best_combination(&transposed_city, &transposed_chosen_layouts, &transposed_chosen_layouts, db, &mut cache, &right_distances_transposed, true, print_progress) { - last_improve_step = LastStep::Horizontal; + if print_progress { eprintln!("Starting to combine {} top houses DB; horizontal cuts", top_layout_count); } + if create_new_best_combination(&transposed_city, &transposed_chosen_layouts, &transposed_chosen_layouts, db, &mut cache, &right_distances_transposed, true, print_progress) { + no_improvements_in_a_row = 0; + } else { + no_improvements_in_a_row += 1; + } + if print_progress { eprintln!("Finished horizontal cuts, improvement: {}", no_improvements_in_a_row == 0); } } - if print_progress { eprintln!("Finished horizontal cuts, improvement: {}", last_improve_step == LastStep::Horizontal); } - if last_improve_step == LastStep::None { break; } + if no_improvements_in_a_row == 2 { break; } } }