This commit is contained in:
Arne Keller 2019-04-28 22:43:16 +02:00
parent 47c3052fbe
commit 9e76234fc3

View File

@ -37,18 +37,32 @@ impl cmp::PartialOrd for World {
} }
} }
fn random_world(rng: &mut SmallRng, tris: &mut [Triangle], angles: &mut [f32], flips: &mut [bool]) -> World { fn random_world(rng: &mut SmallRng, tris: &mut Vec<Triangle>, angles: &mut [f32], flips: &mut Vec<bool>) -> World {
tris.shuffle(rng); //tris.shuffle(rng);
*tris = vec![tris[5], tris[10], tris[3], tris[0], tris[2], tris[7], tris[9], tris[11], tris[4], tris[6], tris[1], tris[8]];
*flips = vec![false, false, false, true, true, false, false, true, true, false, false, true];
angles[0] = 0.8 * PI;
angles[1] = 2.0 * PI - (130.0/360.0) * 2.0 * PI;
angles[2] = 2.0 * PI - (300.0/360.0) * 2.0 * PI;
angles[3] = 1.2 * PI;
angles[4] = 2.0 * PI - 0.0;
angles[5] = 2.0 * PI - 1.8 * PI;
angles[6] = 2.0 * PI - 0.7 * PI;
angles[7] = 2.0 * PI - 0.0;
angles[8] = 0.8 * PI;
angles[9] = 2.0 * PI - (26.0/36.0) * 2.0 * PI;
angles[10] = 2.0 * PI - (1.0/3.6) * 2.0 * PI;
angles[11] = 0.77 * 2.0 * PI;
let mut world = World { tris: Vec::with_capacity(tris.len()), width: 0.0 }; let mut world = World { tris: Vec::with_capacity(tris.len()), width: 0.0 };
for (idx, tri) in tris.iter().enumerate() { for (idx, tri) in tris.iter().enumerate() {
let mut tri = *tri; let mut tri = *tri;
flips[idx] = rng.gen(); //flips[idx] = rng.gen();
if flips[idx] { if flips[idx] {
tri.0.x *= -1.0; tri.0.x *= -1.0;
tri.1.x *= -1.0; tri.1.x *= -1.0;
tri.2.x *= -1.0; tri.2.x *= -1.0;
} }
angles[idx] = rng.gen_range(0.0, 2.0*PI); //angles[idx] = rng.gen_range(0.0, 2.0*PI);
tri = rotate(tri, angles[idx]); tri = rotate(tri, angles[idx]);
let minx = min(min(tri.0.x, tri.1.x), tri.2.x); let minx = min(min(tri.0.x, tri.1.x), tri.2.x);
tri.0.x -= minx; tri.0.x -= minx;
@ -161,7 +175,7 @@ fn main() {
println!("save prefix: {}", save_prefix); println!("save prefix: {}", save_prefix);
loop { loop {
i += 1; i += 1;
let new_best = one_main(&mut tris); let new_best = one_main(&mut tris.clone());
if new_best.width < best { if new_best.width < best {
best = new_best.width; best = new_best.width;
//eprint!("\r"); //eprint!("\r");
@ -175,13 +189,16 @@ fn main() {
} }
} }
fn one_main(tris: &mut [Triangle]) -> World { fn one_main(tris: &mut Vec<Triangle>) -> World {
let mut angles = vec![0.0; tris.len()]; let mut angles = vec![0.0; tris.len()];
let mut flips = vec![false; tris.len()]; let mut flips = vec![false; tris.len()];
let mut rng = SmallRng::from_entropy(); let mut rng = SmallRng::from_entropy();
let mut best_width = f32::MAX; let mut best_width = f32::MAX;
let mut best_all = f32::MAX; let mut best_all = f32::MAX;
display::save_world("/tmp/initial4.svg", &construct_world(&*tris, &angles, &flips));
panic!();
let _random = random_world(&mut rng, tris, &mut angles, &mut flips); let _random = random_world(&mut rng, tris, &mut angles, &mut flips);
display::save_world("/tmp/random.svg", &_random);
optimize(69999, &mut best_all, &mut best_width, &tris, &mut angles, &mut flips, &mut rng, "rand_", 0) optimize(69999, &mut best_all, &mut best_width, &tris, &mut angles, &mut flips, &mut rng, "rand_", 0)
} }
@ -202,17 +219,19 @@ fn optimize(iters: usize, best_all: &mut f32, best_width: &mut f32, tris: &[Tria
for a in &mut angles { for a in &mut angles {
*a += rng.gen_range(-0.2, 0.2); *a += rng.gen_range(-0.2, 0.2);
} }
/*
for f in &mut flips { for f in &mut flips {
if flip_choice.sample(rng) { if flip_choice.sample(rng) {
*f = !*f; *f = !*f;
} }
} }
*/
// construct new world // construct new world
let new = construct_world(tris, &angles, &flips); let new = construct_world(tris, &angles, &flips);
if new.width < *best_width { if new.width < *best_width {
if new.width < *best_all { if new.width < *best_all {
*best_all = new.width; *best_all = new.width;
//eprint!("\r[{}O{}o{}] at {:.3}", save_counter, iteration, i, new.width); eprint!("\r[{}O{}o{}] at {:.3}", save_counter, iteration, i, new.width);
//display::save_world(&format!("{}{}_{}_{}.svg", save_prefix, save_counter, iteration, i), &new); //display::save_world(&format!("{}{}_{}_{}.svg", save_prefix, save_counter, iteration, i), &new);
//iters = 99999; //iters = 99999;
best = new; best = new;
@ -230,13 +249,13 @@ fn optimize(iters: usize, best_all: &mut f32, best_width: &mut f32, tris: &[Tria
} }
angles = best_angles.clone(); angles = best_angles.clone();
for idx in 0..tris.len() { for idx in 0..tris.len() {
for _ in 0..999 { for i in 0..999 {
let angle = rng.gen_range(-0.1, 0.1); let angle = rng.gen_range(-0.1, 0.1);
angles[idx] += angle; angles[idx] += angle;
let new = construct_world(tris, &angles, &best_flips); let new = construct_world(tris, &angles, &best_flips);
if new.width < *best_width { if new.width < *best_width {
*best_all = new.width; *best_all = new.width;
//eprint!("\r[{}O{}o{}] at {:.3}", save_counter, iteration, i, new.width); eprint!("\r[{}O{}I{}E{}] at {:.3}", save_counter, iteration, idx, i, new.width);
//display::save_world(&format!("{}{}_{}_{}.svg", save_prefix, save_counter, iteration, i), &new); //display::save_world(&format!("{}{}_{}_{}.svg", save_prefix, save_counter, iteration, i), &new);
//iters = 99999; //iters = 99999;
best = new; best = new;