Browse Source

Add an option to merge first before moving single houses

(seems to be fairly useless and it's slower)
master
Jirka Sejkora 4 years ago
parent
commit
4b19801594
  1. 2
      src/main.rs
  2. 43
      src/optimization.rs
  3. 2
      src/optimize-subcity.rs

2
src/main.rs

@ -52,7 +52,7 @@ fn main() {
//populate_from_saved_layout(&mut layout, &best_layout); //populate_from_saved_layout(&mut layout, &best_layout);
//eprintln!("Finished loading DB layout ID {}, price: {}, houses: {}", best_layout.id(), layout.price(), layout.houses().len()); //eprintln!("Finished loading DB layout ID {}, price: {}, houses: {}", best_layout.id(), layout.price(), layout.houses().len());
optimization::iterate_improvements(&mut layout, &mut rng, true); optimization::iterate_improvements(&mut layout, &mut rng, true, START_WITH_MERGE);
dump_layout(&layout, &mut best_price, seed); dump_layout(&layout, &mut best_price, seed);
db.add_layout(&layout.houses(), true).expect("Failed to insert into DB"); db.add_layout(&layout.houses(), true).expect("Failed to insert into DB");
} }

43
src/optimization.rs

@ -8,7 +8,7 @@ pub enum RectangleSearchError {
Unsatisfiable, Unsatisfiable,
} }
pub fn iterate_improvements(mut layout: &mut HouseLayout, mut rng: &mut StdRng, print_progress: bool) { pub fn iterate_improvements(mut layout: &mut HouseLayout, mut rng: &mut StdRng, print_progress: bool, merge_first: bool) -> bool {
#[derive(Eq, PartialEq)] #[derive(Eq, PartialEq)]
enum LastStep { enum LastStep {
None, None,
@ -16,17 +16,25 @@ pub fn iterate_improvements(mut layout: &mut HouseLayout, mut rng: &mut StdRng,
MergingPairs, MergingPairs,
} }
let mut improved = false;
let mut first_iteration = true;
let mut last_improved_step = LastStep::None; let mut last_improved_step = LastStep::None;
loop { loop {
if last_improved_step == LastStep::MovingIndividual { break; } if merge_first && first_iteration {
if print_progress { first_iteration = false;
eprintln!("Starting moving individual houses..."); } else {
} if last_improved_step == LastStep::MovingIndividual { break; }
if improve_move_individual_houses(&mut layout, &mut rng) { if print_progress {
last_improved_step = LastStep::MovingIndividual; eprintln!("Starting moving individual houses...");
} }
if print_progress { if improve_move_individual_houses(&mut layout, &mut rng) {
eprintln!("Finished moving individual houses..."); last_improved_step = LastStep::MovingIndividual;
improved = true;
}
if print_progress {
eprintln!("Finished moving individual houses...");
}
} }
if last_improved_step == LastStep::MergingPairs { break; } if last_improved_step == LastStep::MergingPairs { break; }
@ -35,18 +43,15 @@ pub fn iterate_improvements(mut layout: &mut HouseLayout, mut rng: &mut StdRng,
} }
if improve_merge_pairwise(&mut layout, print_progress) { if improve_merge_pairwise(&mut layout, print_progress) {
last_improved_step = LastStep::MergingPairs; last_improved_step = LastStep::MergingPairs;
improved = true;
} }
if print_progress { if print_progress {
eprintln!("Finished pairwise house merge"); eprintln!("Finished pairwise house merge");
} }
//eprintln!("Starting pairwise house move...");
//if optimization::improve_move_houses_pairwise(&mut layout) {
// dump_layout(&layout, &mut best_price, seed);
// improved = true;
//}
//eprintln!("Finished pairwise house move");
if last_improved_step == LastStep::None { break; } if last_improved_step == LastStep::None { break; }
} }
improved
} }
fn get_valid_move_rectangle_multiple(layout: &HouseLayout, houses: &Vec<House>) -> Result<Rectangle, RectangleSearchError> { fn get_valid_move_rectangle_multiple(layout: &HouseLayout, houses: &Vec<House>) -> Result<Rectangle, RectangleSearchError> {
@ -229,6 +234,8 @@ pub fn improve_merge_pairwise(layout: &mut HouseLayout, print_progress: bool) ->
// We instead go through the houses repeatedly and remember which pairs we have already // We instead go through the houses repeatedly and remember which pairs we have already
// tried by hashing their values because they can and do move throughout the layout Vec // tried by hashing their values because they can and do move throughout the layout Vec
// as it's being modified. // as it's being modified.
// TODO: This may lead to some pairs still being mergeable thanks to another merge
// that happened before.
let mut checked = HashSet::new(); let mut checked = HashSet::new();
let mut loop_improved = false; let mut loop_improved = false;
@ -277,7 +284,9 @@ pub fn improve_merge_pairwise(layout: &mut HouseLayout, print_progress: bool) ->
} }
} }
} }
Err(RectangleSearchError::Useless) => eprintln!("Found useless pair of houses, not solving!"), Err(RectangleSearchError::Useless) => {
//eprintln!("Found useless pair of houses, not solving!")
},
Err(RectangleSearchError::Unsatisfiable) => {} Err(RectangleSearchError::Unsatisfiable) => {}
} }
} }

2
src/optimize-subcity.rs

@ -113,7 +113,7 @@ fn main() {
//eprintln!("Starting random population {}", i); //eprintln!("Starting random population {}", i);
population::populate_random(&mut layout, &mut rng); population::populate_random(&mut layout, &mut rng);
//eprintln!("Finished random init {}, price: {}, houses: {}", i, layout.price(), layout.houses().len()); //eprintln!("Finished random init {}, price: {}, houses: {}", i, layout.price(), layout.houses().len());
optimization::iterate_improvements(&mut layout, &mut rng, false); optimization::iterate_improvements(&mut layout, &mut rng, false, false);
//eprintln!("Finished iterated improvements {}, price: {}, houses: {}", i, layout.price(), layout.houses().len()); //eprintln!("Finished iterated improvements {}, price: {}, houses: {}", i, layout.price(), layout.houses().len());
layout.houses().clone() layout.houses().clone()
})); }));

Loading…
Cancel
Save