Browse Source

Add a way to merge subcity improvements into the main city

master
Jirka Sejkora 3 years ago
parent
commit
9b02c0799a
  1. 61
      src/optimize-subcity.rs
  2. 11
      src/subcity.rs

61
src/optimize-subcity.rs

@ -1,5 +1,5 @@
use db::{MemoryLayoutDB, SqliteLayoutDB, LayoutDB, SavedLayout};
use city::{City, House, HouseLayout};
use city::{City, House, HouseLayout, get_price};
use itertools::Itertools;
use rand::{thread_rng, Rng, SeedableRng};
use rand::rngs::StdRng;
@ -20,10 +20,10 @@ fn main() {
eprintln!("Loaded the city file, {} houses", city.get_house_count());
let best_layout: SavedLayout = sqlite_db.layouts().iter()
.sorted_by(|x, y| city::get_price(&city, x.houses()).cmp(&city::get_price(&city, y.houses())))
.sorted_by(|x, y| get_price(&city, x.houses()).cmp(&get_price(&city, y.houses())))
.map(|layout| (*layout).clone())
.next().expect("No best layout found");
eprintln!("Found best layout, ID {}, price {}", best_layout.id(), city::get_price(&city, best_layout.houses()));
eprintln!("Found best layout, ID {}, price {}", best_layout.id(), get_price(&city, best_layout.houses()));
let x_range = 5533..=12000;
let y_range = 4750..=12500;
@ -58,6 +58,37 @@ fn main() {
let filename = format!("X{}_{}Y{}_{}ID{}.sqlite", x_range.start(), x_range.end(), y_range.start(), y_range.end(), best_layout.id());
let mut subcity_db = SqliteLayoutDB::from_file(&filename).unwrap();
//for layout in subcity_db.layouts().iter()
// .filter(|x| get_price(subcity.city(), x.houses()) < removed_price)
// .sorted_by(|x, y| get_price(subcity.city(), x.houses()).cmp(&get_price(subcity.city(), y.houses()))) {
// let price = get_price(subcity.city(), layout.houses());
// let mut full_houses = subcity.to_full_houses(layout.houses());
// assert!(city::is_valid(&city, &full_houses).is_some());
// let mut house_layout = HouseLayout::new(&city);
// for house in &full_houses {
// house_layout.add_house(*house);
// }
// let seed: u64 = thread_rng().gen();
// let mut rng = StdRng::seed_from_u64(seed);
// optimization::iterate_improvements(&mut house_layout, &mut rng, true);
// eprintln!("Improvements finished");
// assert!(house_layout.is_valid());
// let improved_price = city::get_price(&city, house_layout.houses());
// if improved_price < city::get_price(&city, &full_houses) {
// eprintln!("Found improvement, new price {}, updating houses", improved_price);
// full_houses = house_layout.houses().clone();
// }
// assert!(city::is_valid(&city, &full_houses).is_some());
// println!("Layout {}, price {}, full price {}", layout.id(), price, city::get_price(&city, &full_houses));
// // Be careful with duplicates here
// //sqlite_db.add_layout(&full_houses, true);
// //println!("Inserted into the global DB");
//}
//return;
// Architecture:
// Build `FULL_RANDOM_LAYOUTS` random layouts
// loop {
@ -143,27 +174,3 @@ fn main() {
eprintln!("Best {}, worst {} [{} layouts]", best_price, worst_price, subcity_db.layouts().len());
}
}
fn dump_layout(layout: &HouseLayout, best_price: &mut Option<u32>, seed: u64) {
let price = layout.price();
if best_price.is_none() || price < best_price.unwrap() {
*best_price = Some(price);
eprintln!("Printing {} - new best", price);
println!("New best!");
println!("Price {}, seed {}", price, seed);
print_houses(&layout.houses());
println!();
} else {
eprintln!("Printing {}", price);
println!("Price {}, seed {}", price, seed);
print_houses(&layout.houses());
println!();
}
}
fn print_houses(houses: &Vec<House>) {
println!("{}", houses.len());
for house in houses {
println!("{} {}", house.y, house.x);
}
}

11
src/subcity.rs

@ -20,6 +20,17 @@ impl Subcity {
pub fn y_offset(&self) -> usize {
self.y_offset
}
pub fn to_full_houses(&self, subcity_houses: &Vec<House>) -> Vec<House> {
let mut full_houses: Vec<House> = Vec::new();
for house in subcity_houses {
full_houses.push(House::new(house.x + self.x_offset, house.y + self.y_offset))
}
for house in &self.bought_houses {
full_houses.push(*house)
}
full_houses
}
}
/// Creates a new city that is a subset of the original city.

Loading…
Cancel
Save