Browse Source

Add proper last step checks to layout gen

master
Jirka Sejkora 4 years ago
parent
commit
109b5fac01
  1. 43
      src/main.rs
  2. 8
      src/population.rs

43
src/main.rs

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

8
src/population.rs

@ -1,7 +1,7 @@
use rand::Rng;
use crate::city::{SIZE, House, HouseLayout, City};
use rand::prelude::*;
use crate::db::LayoutDB;
use crate::db::{LayoutDB, SavedLayout};
use crate::city;
use std::collections::HashMap;
use std::ops::Add;
@ -74,3 +74,9 @@ pub fn populate_using_db(layout: &mut HouseLayout, mut rng: &mut StdRng, db: &La
}
}
}
pub fn populate_from_saved_layout(layout: &mut HouseLayout, saved_layout: &SavedLayout) {
for house in saved_layout.houses() {
layout.add_house(*house);
}
}

Loading…
Cancel
Save