Řešení KSP úlohy 33-3-4 Obsazování území https://ksp.mff.cuni.cz/h/ulohy/33/zadani3.html#task-33-3-4
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.1 KiB

use rand::prelude::{StdRng, SliceRandom};
use rand::{SeedableRng, Rng, thread_rng};
use std::fmt;
use std::fmt::Formatter;
use std::collections::{HashMap, HashSet};
use city::{HouseLayout, City, House};
use crate::db::LayoutDB;
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());
let city = City::read_from_file("01.in");
eprintln!("Loaded the city file, {} houses", city.get_house_count());
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);
let mut rng = StdRng::seed_from_u64(seed);
let mut layout = HouseLayout::new(&city);
//eprintln!("Starting random population...");
//population::populate_random(&mut layout, &mut rng);
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());
//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 == 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 = LastStep::MovingIndividual;
}
eprintln!("Finished moving individual houses...");
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 = LastStep::MergingPairs;
}
eprintln!("Finished pairwise house merge");
//eprintln!("Starting pairwise house move...");
//if optimization::improve_move_houses_pairwise(&mut layout) {
// dump_layout(&layout, &mut best_price, seed);
// 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");
}
}
fn print_houses(houses: &Vec<House>) {
println!("{}", houses.len());
for house in houses {
println!("{} {}", house.y, house.x);
}
}
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!();
}
}