diff options
| author | tslil clingman <tslil@posteo.de> | 2022-09-25 17:53:36 +0200 |
|---|---|---|
| committer | tslil clingman <tslil@posteo.de> | 2022-09-25 17:59:04 +0200 |
| commit | 6c9cee559d6f461ec660d3a1416b70e475b1c3bb (patch) | |
| tree | 5941c3f55b0d45808e18dbe251e0ce80a9d8f1a2 | |
| parent | ac5858eb9f241023224b5415b1f6d516aae514b8 (diff) | |
Fix tournament
| -rw-r--r-- | src/layout.rs | 11 | ||||
| -rw-r--r-- | src/main.rs | 27 |
2 files changed, 27 insertions, 11 deletions
diff --git a/src/layout.rs b/src/layout.rs index 0ebad27..4c77be4 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -60,7 +60,16 @@ impl Layout { pub fn new_random_from<R: RngCore>(kbd: &Layout, rng: &mut R) -> Layout { let mut layout = kbd.keys; - layout.shuffle(rng); + // layout.shuffle(rng); + let mut count = rng.gen_range(1..=15); + while count > 0 { + let a = rng.gen_range(0..NUM_KEYS); + let b = rng.gen_range(0..NUM_KEYS); + let k = layout[b]; + layout[b] = layout[a]; + layout[a] = k; + count -= 1; + } Layout::keys_to_layout(layout) } } diff --git a/src/main.rs b/src/main.rs index 905d3c6..cee008c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,17 @@ 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); @@ -28,16 +39,11 @@ fn main() { type Fun = Vec<(Layout, u32)>; let mut best = std::u32::MAX; - let mut layouts: Fun = (0..10) + let mut layouts: Fun = (0..NUM_CONTESTANTS) .into_iter() - .map(|i| { + .map(|_| { let layout; - if i >= 0 { - layout = Layout::new_random_from(&starting_layout, &mut rng); - } else { - layout = starting_layout; - } - + layout = Layout::new_random_from(&starting_layout, &mut rng); return (layout, corpus.layout_fitness(&layout)); }) .collect(); @@ -47,8 +53,9 @@ fn main() { .iter() .enumerate() .map(|(i, (kbd, _))| { - if i > 0 { - Layout::new_random_from(kbd, &mut rng) + if i > SURVIVE_THRESHOLD { + let parent = rng.gen_range(0..=SURVIVE_THRESHOLD); + Layout::new_random_from(&layouts[parent].0, &mut rng) } else { kbd.clone() } |
