|
@ -5,13 +5,23 @@ use std::fmt::Formatter; |
|
|
use std::collections::{HashMap, HashSet}; |
|
|
use std::collections::{HashMap, HashSet}; |
|
|
use city::{HouseLayout, City, House}; |
|
|
use city::{HouseLayout, City, House}; |
|
|
use crate::db::LayoutDB; |
|
|
use crate::db::LayoutDB; |
|
|
use crate::population::build_house_probabilities; |
|
|
use crate::population::{build_house_probabilities, populate_from_saved_layout}; |
|
|
|
|
|
use itertools::Itertools; |
|
|
|
|
|
|
|
|
mod optimization; |
|
|
mod optimization; |
|
|
mod population; |
|
|
mod population; |
|
|
mod city; |
|
|
mod city; |
|
|
mod db; |
|
|
mod db; |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Eq, PartialEq)] |
|
|
|
|
|
enum LastStep { |
|
|
|
|
|
None, MovingIndividual, MergingPairs |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
enum RunType { |
|
|
|
|
|
GenNew, TryImproveBest |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
fn main() { |
|
|
fn main() { |
|
|
let mut db = LayoutDB::from_file("layouts.sqlite").expect("Failed to load the DB"); |
|
|
let mut db = LayoutDB::from_file("layouts.sqlite").expect("Failed to load the DB"); |
|
|
eprintln!("Loaded the DB, {} stored layouts", db.layouts().len()); |
|
|
eprintln!("Loaded the DB, {} stored layouts", db.layouts().len()); |
|
@ -19,12 +29,17 @@ fn main() { |
|
|
let city = City::read_from_file("01.in"); |
|
|
let city = City::read_from_file("01.in"); |
|
|
eprintln!("Loaded the city file, {} houses", city.get_house_count()); |
|
|
eprintln!("Loaded the city file, {} houses", city.get_house_count()); |
|
|
|
|
|
|
|
|
const MIN_WEIGHT_SCORE: f64 = 600000.; |
|
|
const MIN_WEIGHT_SCORE: f64 = 540000.; |
|
|
const MAX_WEIGHT_SCORE: f64 = 700000.; |
|
|
const MAX_WEIGHT_SCORE: f64 = 570000.; |
|
|
const DB_CHOICE_PROBABILITY: f64 = 0.99; |
|
|
const DB_CHOICE_PROBABILITY: f64 = 0.99; |
|
|
|
|
|
|
|
|
|
|
|
//let best_layouts = db.layouts().iter()
|
|
|
|
|
|
// .sorted_by(|x, y| city::get_price(&city, x.houses()).cmp(&city::get_price(&city, y.houses())))
|
|
|
|
|
|
// .take_while(|x| city::get_price(&city, x.houses()) < 550000);
|
|
|
|
|
|
|
|
|
let mut best_price: Option<u32> = None; |
|
|
let mut best_price: Option<u32> = None; |
|
|
|
|
|
|
|
|
|
|
|
//for best_layout in best_layouts {
|
|
|
loop { |
|
|
loop { |
|
|
let seed: u64 = thread_rng().gen(); |
|
|
let seed: u64 = thread_rng().gen(); |
|
|
eprintln!("Starting seed {}", seed); |
|
|
eprintln!("Starting seed {}", seed); |
|
@ -32,29 +47,30 @@ fn main() { |
|
|
let mut layout = HouseLayout::new(&city); |
|
|
let mut layout = HouseLayout::new(&city); |
|
|
//eprintln!("Starting random population...");
|
|
|
//eprintln!("Starting random population...");
|
|
|
//population::populate_random(&mut layout, &mut rng);
|
|
|
//population::populate_random(&mut layout, &mut rng);
|
|
|
eprintln!("Starting random population, using DB ({}-{} {})...", |
|
|
|
|
|
|
|
|
eprintln!("Starting random weighted population, using DB ({}-{} {})...", |
|
|
MIN_WEIGHT_SCORE, MAX_WEIGHT_SCORE, DB_CHOICE_PROBABILITY); |
|
|
MIN_WEIGHT_SCORE, MAX_WEIGHT_SCORE, DB_CHOICE_PROBABILITY); |
|
|
population::populate_using_db(&mut layout, &mut rng, &db, MIN_WEIGHT_SCORE, MAX_WEIGHT_SCORE, DB_CHOICE_PROBABILITY); |
|
|
population::populate_using_db(&mut layout, &mut rng, &db, MIN_WEIGHT_SCORE, MAX_WEIGHT_SCORE, DB_CHOICE_PROBABILITY); |
|
|
eprintln!("Finished random init, price: {}, houses: {}", layout.price(), layout.houses().len()); |
|
|
eprintln!("Finished random init, price: {}, houses: {}", layout.price(), layout.houses().len()); |
|
|
let mut last_improved_step = -1; |
|
|
|
|
|
|
|
|
//populate_from_saved_layout(&mut layout, &best_layout);
|
|
|
|
|
|
//eprintln!("Finished loading DB layout ID {}, price: {}, houses: {}", best_layout.id(), layout.price(), layout.houses().len());
|
|
|
|
|
|
|
|
|
|
|
|
let mut last_improved_step = LastStep::None; |
|
|
loop { |
|
|
loop { |
|
|
if last_improved_step == 1 { |
|
|
if last_improved_step == LastStep::MovingIndividual { break; } |
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
eprintln!("Starting moving individual houses..."); |
|
|
eprintln!("Starting moving individual houses..."); |
|
|
if optimization::improve_move_individual_houses(&mut layout, &mut rng) { |
|
|
if optimization::improve_move_individual_houses(&mut layout, &mut rng) { |
|
|
dump_layout(&layout, &mut best_price, seed); |
|
|
dump_layout(&layout, &mut best_price, seed); |
|
|
last_improved_step = 1; |
|
|
last_improved_step = LastStep::MovingIndividual; |
|
|
} |
|
|
} |
|
|
eprintln!("Finished moving individual houses..."); |
|
|
eprintln!("Finished moving individual houses..."); |
|
|
|
|
|
|
|
|
if last_improved_step == 2 { |
|
|
if last_improved_step == LastStep::MergingPairs { break; } |
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
eprintln!("Starting pairwise house merge..."); |
|
|
eprintln!("Starting pairwise house merge..."); |
|
|
if optimization::improve_merge_pairwise(&mut layout) { |
|
|
if optimization::improve_merge_pairwise(&mut layout) { |
|
|
dump_layout(&layout, &mut best_price, seed); |
|
|
dump_layout(&layout, &mut best_price, seed); |
|
|
last_improved_step = 2; |
|
|
last_improved_step = LastStep::MergingPairs; |
|
|
} |
|
|
} |
|
|
eprintln!("Finished pairwise house merge"); |
|
|
eprintln!("Finished pairwise house merge"); |
|
|
//eprintln!("Starting pairwise house move...");
|
|
|
//eprintln!("Starting pairwise house move...");
|
|
@ -63,6 +79,7 @@ fn main() { |
|
|
// improved = true;
|
|
|
// improved = true;
|
|
|
//}
|
|
|
//}
|
|
|
//eprintln!("Finished pairwise house move");
|
|
|
//eprintln!("Finished pairwise house move");
|
|
|
|
|
|
if last_improved_step == LastStep::None { break; } |
|
|
} |
|
|
} |
|
|
db.add_layout(&layout.houses(), true).expect("Failed to insert into DB"); |
|
|
db.add_layout(&layout.houses(), true).expect("Failed to insert into DB"); |
|
|
} |
|
|
} |
|
|