aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: cee008c99533581871d7af206f215d80285482f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
mod config;
mod corpus;
mod layout;

use rayon::prelude::*;

use rand::prelude::*;
use rand_pcg::*;

use corpus::*;
use layout::*;

const NUM_CONTESTANTS: usize = 24;
const SURVIVE_THRESHOLD: usize = 2;

// TODO: Layout is really pre-layout, have something that takes a layout and
// re-orders columns to balance hands, and keys per column (block really
// consider index fingers) to balance rows.

// TODO: command line arguments to start at a given layout string, maybe read
// from file? Might as well make num_contestants and survive_threshold
// configurable, and number of swaps when generating new layout

fn main() {
    let corpus = Corpus::load("new.txt").unwrap();
    println!("{}", corpus);

    let starting_layout = Layout::from_verbose(
        "
        Y W F L M  K P O , Q
        U R S N H  D T E A I
        Z X C V J  B G ' . /
        ",
    )
    .unwrap();

    let mut rng = Pcg64::from_entropy();

    type Fun = Vec<(Layout, u32)>;

    let mut best = std::u32::MAX;
    let mut layouts: Fun = (0..NUM_CONTESTANTS)
        .into_iter()
        .map(|_| {
            let layout;
            layout = Layout::new_random_from(&starting_layout, &mut rng);
            return (layout, corpus.layout_fitness(&layout));
        })
        .collect();

    loop {
        let new_layouts = layouts
            .iter()
            .enumerate()
            .map(|(i, (kbd, _))| {
                if i > SURVIVE_THRESHOLD {
                    let parent = rng.gen_range(0..=SURVIVE_THRESHOLD);
                    Layout::new_random_from(&layouts[parent].0, &mut rng)
                } else {
                    kbd.clone()
                }
            })
            .collect::<Vec<Layout>>();

        new_layouts
            .par_iter()
            .map(|layout| {
                return (*layout, corpus.layout_fitness(layout));
            })
            .collect_into_vec(&mut layouts);

        layouts.sort_by(|(_, lfit), (_, rfit)| lfit.cmp(rfit));

        if layouts[0].1 < best {
            best = layouts[0].1;
            let evl = corpus.evaluate_layout(&layouts[0].0);
            println!("");
            println!("================================================================================\nLayout:\n{}",
                         layouts[0].0
                    );

            println!("{}\n{}", evl, layouts[0].1);
        }
    }
}