Find layouts using randomness
This commit is contained in:
parent
7e05556e99
commit
43c6eb4298
137
Cargo.lock
generated
137
Cargo.lock
generated
@ -5,6 +5,7 @@ name = "aufgabe2"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"geo 0.12.0 (git+https://github.com/FliegendeWurst/geo)",
|
||||
"rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"svg 0.5.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
@ -35,6 +36,11 @@ dependencies = [
|
||||
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.35"
|
||||
@ -45,6 +51,14 @@ name = "cfg-if"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "cloudabi"
|
||||
version = "0.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "failure"
|
||||
version = "0.1.5"
|
||||
@ -65,6 +79,11 @@ dependencies = [
|
||||
"synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fuchsia-cprng"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "geo"
|
||||
version = "0.12.0"
|
||||
@ -124,6 +143,110 @@ dependencies = [
|
||||
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.6.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_hc 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_isaac 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_jitter 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_pcg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "rand_hc"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_isaac"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_jitter"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_os"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_pcg"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_xorshift"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rdrand"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rstar"
|
||||
version = "0.2.0"
|
||||
@ -201,10 +324,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a6d640bee2da49f60a4068a7fae53acde8982514ab7bae8b8cea9e88cbcfd799"
|
||||
"checksum backtrace 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "f106c02a3604afcdc0df5d36cc47b44b55917dbaf3d808f71c163a0ddba64637"
|
||||
"checksum backtrace-sys 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)" = "797c830ac25ccc92a7f8a7b9862bde440715531514594a6154e3d4a54dd769b6"
|
||||
"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
|
||||
"checksum cc 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)" = "5e5f3fee5eeb60324c2781f1e41286bdee933850fff9b3c672587fed5ec58c83"
|
||||
"checksum cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "11d43355396e872eefb45ce6342e4374ed7bc2b3a502d1b28e36d6e23c05d1f4"
|
||||
"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
|
||||
"checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2"
|
||||
"checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1"
|
||||
"checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
|
||||
"checksum geo 0.12.0 (git+https://github.com/FliegendeWurst/geo)" = "<none>"
|
||||
"checksum geo-types 0.4.1 (git+https://github.com/FliegendeWurst/geo)" = "<none>"
|
||||
"checksum libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)" = "bedcc7a809076656486ffe045abeeac163da1b558e963a31e29fbfbeba916917"
|
||||
@ -213,6 +339,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum pdqselect 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4ec91767ecc0a0bbe558ce8c9da33c068066c57ecc8bb8477ef8c1ad3ef77c27"
|
||||
"checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915"
|
||||
"checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1"
|
||||
"checksum rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca"
|
||||
"checksum rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef"
|
||||
"checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
|
||||
"checksum rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d0e7a549d590831370895ab7ba4ea0c1b6b011d106b5ff2da6eee112615e6dc0"
|
||||
"checksum rand_hc 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7b40677c7be09ae76218dc623efbf7b18e34bced3f38883af07bb75630a21bc4"
|
||||
"checksum rand_isaac 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08"
|
||||
"checksum rand_jitter 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b9ea758282efe12823e0d952ddb269d2e1897227e464919a554f2a03ef1b832"
|
||||
"checksum rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071"
|
||||
"checksum rand_pcg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44"
|
||||
"checksum rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c"
|
||||
"checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
|
||||
"checksum rstar 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "120bfe4837befb82c5a637a5a8c490a27d25524ac19fffec5b4e555ca6e36ee8"
|
||||
"checksum rustc-demangle 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "adacaae16d02b6ec37fdc7acfcddf365978de76d1983d3ee22afc260e1ca9619"
|
||||
"checksum svg 0.5.12 (registry+https://github.com/rust-lang/crates.io-index)" = "a863ec1f8e7cfd4ea449f77445cca06aac240b9a677ccf12b0f65ef020db52c7"
|
||||
|
@ -7,3 +7,4 @@ edition = "2018"
|
||||
[dependencies]
|
||||
geo = { git = "https://github.com/FliegendeWurst/geo" }
|
||||
svg = "0.5.12"
|
||||
rand = "0.6.5"
|
||||
|
179
src/main.rs
179
src/main.rs
@ -1,4 +1,6 @@
|
||||
use geo::prelude::*;
|
||||
use rand::distributions::{Bernoulli, Distribution};
|
||||
use rand::prelude::*;
|
||||
|
||||
use std::cmp;
|
||||
use std::collections::BinaryHeap;
|
||||
@ -35,8 +37,183 @@ impl cmp::PartialOrd for World {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::cyclomatic_complexity)]
|
||||
fn random_world(rng: &mut SmallRng, tris: &mut [Triangle], angles: &mut [f32], flips: &mut [bool]) -> World {
|
||||
tris.shuffle(rng);
|
||||
let mut world = World { tris: Vec::with_capacity(tris.len()), width: 0.0 };
|
||||
for (idx, tri) in tris.iter().enumerate() {
|
||||
let mut tri = *tri;
|
||||
flips[idx] = rng.gen();
|
||||
if flips[idx] {
|
||||
tri.0.x *= -1.0;
|
||||
tri.1.x *= -1.0;
|
||||
tri.2.x *= -1.0;
|
||||
}
|
||||
angles[idx] = rng.gen_range(0.0, 2.0*PI);
|
||||
tri = rotate(tri, angles[idx]);
|
||||
let minx = min(min(tri.0.x, tri.1.x), tri.2.x);
|
||||
tri.0.x -= minx;
|
||||
tri.1.x -= minx;
|
||||
tri.2.x -= minx;
|
||||
let miny = min(min(tri.0.y, tri.1.y), tri.2.y);
|
||||
tri.0.y -= miny;
|
||||
tri.1.y -= miny;
|
||||
tri.2.y -= miny;
|
||||
if world.tris.is_empty() {
|
||||
world.tris.push((0, tri));
|
||||
world.normalize();
|
||||
} else {
|
||||
tri.0.x += 1.0;
|
||||
tri.1.x += 1.0;
|
||||
tri.2.x += 1.0;
|
||||
for &delta in &[
|
||||
/*10.0, 5.0,*/ 1.0, 0.6, 0.32, 0.18, 0.1, 0.06, 0.03, 0.01
|
||||
] {
|
||||
//println!("moving {:?}", tri);
|
||||
//println!("world {:?}", world);
|
||||
let initial_state = world.tris[0..idx].iter().any(|(_, x)| x.intersects(&tri));
|
||||
let dx = if initial_state { delta } else { -delta };
|
||||
while world.tris[0..idx].iter().any(|(_, x)| x.intersects(&tri)) == initial_state {
|
||||
tri.0.x += dx;
|
||||
tri.1.x += dx;
|
||||
tri.2.x += dx;
|
||||
}
|
||||
}
|
||||
world.tris.push((0, tri));
|
||||
world.normalize();
|
||||
}
|
||||
}
|
||||
world.calc_width();
|
||||
world
|
||||
}
|
||||
|
||||
fn construct_world(tris: &[Triangle], angles: &[f32], flips: &[bool]) -> World {
|
||||
let mut world = World { tris: Vec::with_capacity(tris.len()), width: 0.0 };
|
||||
for (idx, tri) in tris.iter().enumerate() {
|
||||
let mut tri = *tri;
|
||||
if flips[idx] {
|
||||
tri.0.x *= -1.0;
|
||||
tri.1.x *= -1.0;
|
||||
tri.2.x *= -1.0;
|
||||
}
|
||||
tri = rotate(tri, angles[idx]);
|
||||
let minx = min(min(tri.0.x, tri.1.x), tri.2.x);
|
||||
tri.0.x -= minx;
|
||||
tri.1.x -= minx;
|
||||
tri.2.x -= minx;
|
||||
let miny = min(min(tri.0.y, tri.1.y), tri.2.y);
|
||||
tri.0.y -= miny;
|
||||
tri.1.y -= miny;
|
||||
tri.2.y -= miny;
|
||||
if world.tris.is_empty() {
|
||||
world.tris.push((0, tri));
|
||||
world.normalize();
|
||||
} else {
|
||||
tri.0.x += 1.0;
|
||||
tri.1.x += 1.0;
|
||||
tri.2.x += 1.0;
|
||||
for &delta in &[
|
||||
/*10.0, 5.0,*/ 1.0, 0.6, 0.32, 0.18, 0.1, 0.06, 0.03, 0.01
|
||||
] {
|
||||
let initial_state = world.tris[0..idx].iter().any(|(_, x)| x.intersects(&tri));
|
||||
let dx = if initial_state { delta } else { -delta };
|
||||
while world.tris[0..idx].iter().any(|(_, x)| x.intersects(&tri)) == initial_state {
|
||||
tri.0.x += dx;
|
||||
tri.1.x += dx;
|
||||
tri.2.x += dx;
|
||||
}
|
||||
}
|
||||
world.tris.push((0, tri));
|
||||
world.normalize();
|
||||
}
|
||||
}
|
||||
world.calc_width();
|
||||
world
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut tris = input::read_input();
|
||||
let mut angles = vec![0.0; tris.len()];
|
||||
let mut flips = vec![false; tris.len()];
|
||||
let mut rng = SmallRng::from_entropy();
|
||||
let save_prefix = "rand_";
|
||||
let mut save_counter = 0;
|
||||
let mut last_improvement = 0;
|
||||
let mut best_width = f32::MAX;
|
||||
let mut best_all = f32::MAX;
|
||||
let mut random = random_world(&mut rng, &mut tris, &mut angles, &mut flips);
|
||||
loop {
|
||||
save_counter += 1;
|
||||
if save_counter % 100_000 == 0 {
|
||||
eprint!("\r[{}]", save_counter);
|
||||
}
|
||||
if random.width < best_width {
|
||||
best_width = random.width;
|
||||
last_improvement = save_counter;
|
||||
if best_width < best_all {
|
||||
best_all = best_width;
|
||||
println!("[{}] BEST: {}", save_counter, random.width);
|
||||
display::save_world(&format!("{}{}.svg", save_prefix, save_counter), &random);
|
||||
optimize(99999, &mut best_all, &mut best_width, &tris, &mut angles, &mut flips, &mut rng, save_prefix, save_counter);
|
||||
} else {
|
||||
//eprint!("[{}] best: {} (min: {})", save_counter, random.width, best_all);
|
||||
eprint!(".");
|
||||
optimize(24999, &mut best_all, &mut best_width, &tris, &mut angles, &mut flips, &mut rng, save_prefix, save_counter);
|
||||
}
|
||||
}
|
||||
random = random_world(&mut rng, &mut tris, &mut angles, &mut flips);
|
||||
if save_counter - last_improvement > 200_000 {
|
||||
// "restart"
|
||||
best_width += 5.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn optimize(iters: usize, best_all: &mut f32, best_width: &mut f32, tris: &[Triangle], best_angles: &mut Vec<f32>, best_flips: &mut Vec<bool>, rng: &mut SmallRng, save_prefix: &'static str, save_counter: usize) {
|
||||
let mut found_better = true;
|
||||
let mut iteration = 0;
|
||||
let flip_choice = Bernoulli::from_ratio(1, 11);
|
||||
while found_better {
|
||||
found_better = false;
|
||||
// slightly modify angles
|
||||
let start_angles = best_angles.clone();
|
||||
let start_flips = best_flips.clone();
|
||||
let mut angles = start_angles.clone();
|
||||
let mut flips = start_flips.clone();
|
||||
for i in 0..iters {
|
||||
for a in &mut angles {
|
||||
*a += rng.gen_range(-0.2, 0.2);
|
||||
}
|
||||
for f in &mut flips {
|
||||
if flip_choice.sample(rng) {
|
||||
*f = !*f;
|
||||
}
|
||||
}
|
||||
// construct new world
|
||||
let new = construct_world(tris, &angles, &flips);
|
||||
if new.width < *best_width {
|
||||
if new.width < *best_all {
|
||||
*best_all = new.width;
|
||||
println!("[{}O{}o{}] BEST: {}", save_counter, iteration, i, new.width);
|
||||
display::save_world(&format!("{}{}_{}_{}.svg", save_prefix, save_counter, iteration, i), &new);
|
||||
} else {
|
||||
//eprint!("[{}O{}o{}] best: {} (min: {})", save_counter, iteration, i, new.width, best_all);
|
||||
eprint!(".");
|
||||
}
|
||||
*best_width = new.width;
|
||||
found_better = true;
|
||||
*best_angles = angles;
|
||||
*best_flips = flips;
|
||||
}
|
||||
angles = start_angles.clone();
|
||||
flips = start_flips.clone();
|
||||
}
|
||||
iteration += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::cyclomatic_complexity)]
|
||||
pub fn main_old() {
|
||||
let tris = input::read_input();
|
||||
let count_tris = tris.len();
|
||||
let tris = transformations(&tris);
|
||||
|
Loading…
Reference in New Issue
Block a user