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);
//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);
db.add_layout(&layout.houses(), true).expect("Failed to insert into DB");
}

43
src/optimization.rs

@ -8,7 +8,7 @@ pub enum RectangleSearchError {
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)]
enum LastStep {
None,
@ -16,17 +16,25 @@ pub fn iterate_improvements(mut layout: &mut HouseLayout, mut rng: &mut StdRng,
MergingPairs,
}
let mut improved = false;
let mut first_iteration = true;
let mut last_improved_step = LastStep::None;
loop {
if last_improved_step == LastStep::MovingIndividual { break; }
if print_progress {
eprintln!("Starting moving individual houses...");
}
if improve_move_individual_houses(&mut layout, &mut rng) {
last_improved_step = LastStep::MovingIndividual;
}
if print_progress {
eprintln!("Finished moving individual houses...");
if merge_first && first_iteration {
first_iteration = false;
} else {
if last_improved_step == LastStep::MovingIndividual { break; }
if print_progress {
eprintln!("Starting moving individual houses...");
}
if improve_move_individual_houses(&mut layout, &mut rng) {
last_improved_step = LastStep::MovingIndividual;
improved = true;
}
if print_progress {
eprintln!("Finished moving individual houses...");
}
}
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) {
last_improved_step = LastStep::MergingPairs;
improved = true;
}
if print_progress {
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; }
}
improved
}
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
// tried by hashing their values because they can and do move throughout the layout Vec
// 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 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) => {}
}
}

2
src/optimize-subcity.rs

@ -113,7 +113,7 @@ fn main() {
//eprintln!("Starting random population {}", i);
population::populate_random(&mut layout, &mut rng);
//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());
layout.houses().clone()
}));

Loading…
Cancel
Save