|
@ -6,7 +6,9 @@ use itertools::iproduct; |
|
|
use std::collections::VecDeque; |
|
|
use std::collections::VecDeque; |
|
|
use std::collections::vec_deque::Iter; |
|
|
use std::collections::vec_deque::Iter; |
|
|
|
|
|
|
|
|
pub fn try_combine(city: &City, left_layouts: &Vec<Vec<House>>, right_layouts: &Vec<Vec<House>>, db: &mut LayoutDB, transposed: bool) { |
|
|
pub fn try_combine(city: &City, left_layouts: &Vec<Vec<House>>, right_layouts: &Vec<Vec<House>>, db: &mut LayoutDB, transposed: bool) -> bool { |
|
|
|
|
|
let mut improved = false; |
|
|
|
|
|
|
|
|
// Sorted in reverse so we can remove from the end
|
|
|
// Sorted in reverse so we can remove from the end
|
|
|
let mut left_houses_sorted: Vec<Vec<House>> = left_layouts.iter() |
|
|
let mut left_houses_sorted: Vec<Vec<House>> = left_layouts.iter() |
|
|
.map(|l| l.iter().sorted_by(|h1, h2| h2.x.cmp(&h1.x)).map(|x| *x).collect()) |
|
|
.map(|l| l.iter().sorted_by(|h1, h2| h2.x.cmp(&h1.x)).map(|x| *x).collect()) |
|
@ -35,13 +37,11 @@ pub fn try_combine(city: &City, left_layouts: &Vec<Vec<House>>, right_layouts: & |
|
|
.map(|layout| city::get_price(&city, layout)) |
|
|
.map(|layout| city::get_price(&city, layout)) |
|
|
.min(); |
|
|
.min(); |
|
|
|
|
|
|
|
|
|
|
|
let axis = if transposed { "y" } else { "x" }; |
|
|
|
|
|
|
|
|
// x is the last left coordinate, x+1 is right
|
|
|
// x is the last left coordinate, x+1 is right
|
|
|
for x in 0..SIZE { |
|
|
for x in 0..SIZE { |
|
|
if transposed { |
|
|
eprintln!("Starting {} {}", axis, x); |
|
|
eprintln!("Starting y {}", x); |
|
|
|
|
|
} else { |
|
|
|
|
|
eprintln!("Starting x {}", x); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Update the lines
|
|
|
// Update the lines
|
|
|
for (left_line, left_houses) in lefts.iter_mut().zip(left_houses_sorted.iter_mut()) { |
|
|
for (left_line, left_houses) in lefts.iter_mut().zip(left_houses_sorted.iter_mut()) { |
|
@ -78,9 +78,8 @@ pub fn try_combine(city: &City, left_layouts: &Vec<Vec<House>>, right_layouts: & |
|
|
if is_compatible(city, &left, &right) { |
|
|
if is_compatible(city, &left, &right) { |
|
|
if best_price.is_none() || price < best_price.unwrap() { |
|
|
if best_price.is_none() || price < best_price.unwrap() { |
|
|
best_price = Some(price); |
|
|
best_price = Some(price); |
|
|
let axis = if transposed { "y" } else { "x" }; |
|
|
eprintln!("{} - new best score, cut on {} {}, left {} - right {}, printing", price, axis, x, left_i, right_i); |
|
|
eprintln!("{} - new best score, cut on {} {}, left {} - right {}, printing", axis, price, x, left_i, right_i); |
|
|
println!("{} - new best score, cut on {} {}, left {} - right {}", price, axis, x, left_i, right_i); |
|
|
println!("{} - new best score, cut on {} {}, left {} - right {}", axis, price, x, left_i, right_i); |
|
|
|
|
|
let mut new_houses: Vec<_> = left.houses().copied().chain(right.houses().copied()).collect(); |
|
|
let mut new_houses: Vec<_> = left.houses().copied().chain(right.houses().copied()).collect(); |
|
|
if transposed { |
|
|
if transposed { |
|
|
new_houses = transpose_layout(&new_houses); |
|
|
new_houses = transpose_layout(&new_houses); |
|
@ -92,6 +91,7 @@ pub fn try_combine(city: &City, left_layouts: &Vec<Vec<House>>, right_layouts: & |
|
|
|
|
|
|
|
|
// We only add best results to avoid overfilling the database with similar layouts
|
|
|
// We only add best results to avoid overfilling the database with similar layouts
|
|
|
db.add_layout(&new_houses, true); |
|
|
db.add_layout(&new_houses, true); |
|
|
|
|
|
improved = true; |
|
|
} |
|
|
} |
|
|
compatibles += 1; |
|
|
compatibles += 1; |
|
|
|
|
|
|
|
@ -104,6 +104,8 @@ pub fn try_combine(city: &City, left_layouts: &Vec<Vec<House>>, right_layouts: & |
|
|
|
|
|
|
|
|
eprintln!("{} incompatibles checked before {} compatible", incompatibles, compatibles); |
|
|
eprintln!("{} incompatibles checked before {} compatible", incompatibles, compatibles); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
improved |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fn is_compatible(city: &City, left: &LeftLine, right: &RightLine) -> bool { |
|
|
fn is_compatible(city: &City, left: &LeftLine, right: &RightLine) -> bool { |
|
|