Horizontal cuts (through transpose)
This commit is contained in:
		
							parent
							
								
									dfedf7b3ae
								
							
						
					
					
						commit
						63ed9e7fe0
					
				
					 2 changed files with 56 additions and 13 deletions
				
			
		|  | @ -1,5 +1,5 @@ | ||||||
| use db::LayoutDB; | use db::LayoutDB; | ||||||
| use city::City; | use city::{City, House, SIZE}; | ||||||
| use itertools::Itertools; | use itertools::Itertools; | ||||||
| 
 | 
 | ||||||
| mod city; | mod city; | ||||||
|  | @ -18,8 +18,36 @@ fn main() { | ||||||
| 
 | 
 | ||||||
|     const TOP_LAYOUT_COUNT: usize = 100; |     const TOP_LAYOUT_COUNT: usize = 100; | ||||||
| 
 | 
 | ||||||
|     let chosen_layouts = sorted.iter().take(TOP_LAYOUT_COUNT).map(|l| l.houses()).collect(); |     let chosen_layouts: Vec<_> = sorted.iter().take(TOP_LAYOUT_COUNT).map(|l| l.houses().clone()).collect(); | ||||||
|     eprintln!("Starting to combine {} top houses DB; vertical cuts", TOP_LAYOUT_COUNT); |  | ||||||
| 
 | 
 | ||||||
|     combine::try_combine(&city, &chosen_layouts, &chosen_layouts); |     //eprintln!("Starting to combine {} top houses DB; vertical cuts", TOP_LAYOUT_COUNT);
 | ||||||
|  |     //combine::try_combine(&city, &chosen_layouts, &chosen_layouts, false);
 | ||||||
|  | 
 | ||||||
|  |     eprintln!("Transposing..."); | ||||||
|  |     let transposed_city = transpose_city(&city); | ||||||
|  |     let transposed_chosen_layouts: Vec<_> = chosen_layouts.iter().map(|x| transpose_layout(x)).collect(); | ||||||
|  |     eprintln!("Starting to combine {} top houses DB; horizontal cuts", TOP_LAYOUT_COUNT); | ||||||
|  |     combine::try_combine(&transposed_city, &transposed_chosen_layouts, &transposed_chosen_layouts, true); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn transpose_city(city: &City) -> City { | ||||||
|  |     let mut transposed_prices = vec![0u16; SIZE * SIZE]; | ||||||
|  |     for y in 0..SIZE { | ||||||
|  |         for x in 0..SIZE { | ||||||
|  |             // Sorry, cache! Not worth optimizing with blocks,
 | ||||||
|  |             // this is not going to be ran often.
 | ||||||
|  |             transposed_prices[y * SIZE + x] = city.get_price_xy(y, x); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     City::new(transposed_prices) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn transpose_layout(houses: &Vec<House>) -> Vec<House> { | ||||||
|  |     let mut transposed = Vec::new(); | ||||||
|  |     for house in houses { | ||||||
|  |         transposed.push(House::new(house.y, house.x)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     transposed | ||||||
| } | } | ||||||
|  | @ -5,7 +5,7 @@ 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>>) { | pub fn try_combine(city: &City, left_layouts: &Vec<Vec<House>>, right_layouts: &Vec<Vec<House>>, print_transposed: bool) { | ||||||
|     // 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()) | ||||||
|  | @ -33,7 +33,12 @@ pub fn try_combine(city: &City, left_layouts: &Vec<&Vec<House>>, right_layouts: | ||||||
|     let mut best_price = None; |     let mut best_price = None; | ||||||
|     // 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 print_transposed { | ||||||
|  |             eprintln!("Starting y {}", x); | ||||||
|  |         } else { | ||||||
|             eprintln!("Starting x {}", x); |             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()) { | ||||||
|             while let Some(house) = left_houses.last() { |             while let Some(house) = left_houses.last() { | ||||||
|  | @ -69,6 +74,15 @@ 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); | ||||||
|  |                     if print_transposed { | ||||||
|  |                         eprintln!("{} - new best score, cut on y {}, left {} - right {}, printing", price, x, left_i, right_i); | ||||||
|  |                         println!("{} - new best score, cut on y {}, left {} - right {}", price, x, left_i, right_i); | ||||||
|  |                         let new_houses: Vec<_> = left.houses().chain(right.houses()).collect(); | ||||||
|  |                         println!("{}", new_houses.len()); | ||||||
|  |                         for house in new_houses { | ||||||
|  |                             println!("{} {}", house.x, house.y); | ||||||
|  |                         } | ||||||
|  |                     } else { | ||||||
|                         eprintln!("{} - new best score, cut on x {}, left {} - right {}, printing", price, x, left_i, right_i); |                         eprintln!("{} - new best score, cut on x {}, left {} - right {}, printing", price, x, left_i, right_i); | ||||||
|                         println!("{} - new best score, cut on x {}, left {} - right {}", price, x, left_i, right_i); |                         println!("{} - new best score, cut on x {}, left {} - right {}", price, x, left_i, right_i); | ||||||
|                         let new_houses: Vec<_> = left.houses().chain(right.houses()).collect(); |                         let new_houses: Vec<_> = left.houses().chain(right.houses()).collect(); | ||||||
|  | @ -77,6 +91,7 @@ pub fn try_combine(city: &City, left_layouts: &Vec<&Vec<House>>, right_layouts: | ||||||
|                             println!("{} {}", house.y, house.x); |                             println!("{} {}", house.y, house.x); | ||||||
|                         } |                         } | ||||||
|                     } |                     } | ||||||
|  |                 } | ||||||
|                 compatibles += 1; |                 compatibles += 1; | ||||||
| 
 | 
 | ||||||
|                 // All other pairs would be more expensive
 |                 // All other pairs would be more expensive
 | ||||||
|  | @ -86,7 +101,7 @@ pub fn try_combine(city: &City, left_layouts: &Vec<&Vec<House>>, right_layouts: | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         eprintln!("{}/{} compatible", compatibles, compatibles + incompatibles); |         eprintln!("{} incompatibles checked before {} compatible", incompatibles, compatibles); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue