aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortslil clingman <tslil@posteo.de>2022-09-27 00:27:37 +0200
committertslil clingman <tslil@posteo.de>2022-09-27 00:27:37 +0200
commit19e4b5bead91b3c5eaefec77d5f703df9c1b2305 (patch)
tree799aed04e0b5c171270bd5ded09dbcc4d72a267d /src
parente7cd7f1d821b071a83cf003d02f8897523c8a70c (diff)
Penalty for index under-usage
Diffstat (limited to 'src')
-rw-r--r--src/corpus.rs27
-rw-r--r--src/layout.rs29
-rw-r--r--src/main.rs2
3 files changed, 29 insertions, 29 deletions
diff --git a/src/corpus.rs b/src/corpus.rs
index 2677cd2..9b522e0 100644
--- a/src/corpus.rs
+++ b/src/corpus.rs
@@ -8,8 +8,9 @@ const NUM_BIGRAMS: usize = NUM_KEYS * NUM_KEYS;
pub struct Corpus {
bigram_count: [u32; NUM_BIGRAMS],
- character_count: [u32; NUM_KEYS],
+ character_count: [u32; 256],
total_count: u32,
+ index_threshold: u32,
}
fn pair_to_index(x: char, y: char) -> usize {
@@ -23,7 +24,7 @@ impl Corpus {
let contents = fs::read_to_string(path)?;
let mut bigram_count = [0; NUM_BIGRAMS];
- let mut character_count = [0; NUM_KEYS];
+ let mut character_count = [0; 256];
let mut total_count = 0;
let mut last_char = None;
@@ -37,22 +38,25 @@ impl Corpus {
}
}
last_char = Some(c);
- character_count[CHAR_TO_INDEX[c as usize]] += 1;
+ character_count[c as usize] += 1;
total_count += 1;
} else {
last_char = None;
}
}
+ let index_threshold = (total_count as f32 * 0.15) as u32;
+
return Ok(Corpus {
bigram_count,
character_count,
total_count,
+ index_threshold,
});
}
pub fn prelayout_fitness(&self, layout: &Prelayout) -> u32 {
- let mut sfb: u32 = 0;
+ let mut score: u32 = 0;
for i in 0..6 {
let keys = layout.get_standard_column(i);
@@ -60,7 +64,7 @@ impl Corpus {
let k2 = keys[1];
let k3 = keys[2];
- sfb += self.bigram_count[pair_to_index(k1, k2)]
+ score += self.bigram_count[pair_to_index(k1, k2)]
+ self.bigram_count[pair_to_index(k1, k3)]
+ self.bigram_count[pair_to_index(k2, k3)];
}
@@ -73,7 +77,12 @@ impl Corpus {
let k4 = keys[3];
let k5 = keys[4];
let k6 = keys[5];
- sfb += self.bigram_count[pair_to_index(k1, k2)]
+
+ // We want index usage!
+ let index_count: u32 = keys.iter().map(|&k| self.character_count[k as usize]).sum();
+ score += (self.index_threshold as i64 - index_count as i64).abs() as u32 / 32;
+
+ score += self.bigram_count[pair_to_index(k1, k2)]
+ self.bigram_count[pair_to_index(k1, k3)]
+ self.bigram_count[pair_to_index(k2, k3)]
+ self.bigram_count[pair_to_index(k4, k5)]
@@ -90,7 +99,7 @@ impl Corpus {
+ self.bigram_count[pair_to_index(k3, k6)];
}
- sfb
+ score
}
pub fn evaluate_layout(&self, layout: &Layout) -> Evaluation {
@@ -98,7 +107,9 @@ impl Corpus {
let mut sfb: [u32; 8] = [0; 8];
for (i, &c) in self.character_count.iter().enumerate() {
- keypress[layout.translate_index(i)] = c;
+ if c > 0 {
+ keypress[layout.get_index((i as u8) as char)] = c;
+ }
}
// TODO: We make assumptions about NUM_KEYS here
diff --git a/src/layout.rs b/src/layout.rs
index 2650a51..bc476c8 100644
--- a/src/layout.rs
+++ b/src/layout.rs
@@ -22,7 +22,7 @@ impl Prelayout {
let mut standard_columns = pl.standard_columns.clone();
let mut index_columns = pl.index_columns.clone();
- let mut count = rng.gen_range(1..=NUM_KEYS - 1);
+ let mut count = rng.gen_range(1..NUM_KEYS);
while count > 0 {
let source_index: bool = rng.gen();
let target_index: bool = rng.gen();
@@ -120,8 +120,6 @@ impl Prelayout {
#[derive(Copy, Clone)]
pub struct Layout {
keys: [char; NUM_KEYS],
- // index in KEY_CHAR -> index in layout
- translate_index: [usize; NUM_KEYS],
}
impl Layout {
@@ -134,31 +132,22 @@ impl Layout {
Layout::char_array_to_layout(pl.to_char_array())
}
- fn char_array_to_layout(layout: [char; NUM_KEYS]) -> Layout {
- let mut translate_index: [usize; NUM_KEYS] = [0; NUM_KEYS];
-
- for (i, seek) in KEY_CHARS.iter().enumerate() {
- for (j, found) in layout.iter().enumerate() {
- if seek == found {
- translate_index[i] = j;
- }
- }
+ pub fn get_index(&self, c: char) -> usize {
+ let mut found_key = 0;
+ while self.keys[found_key] != c {
+ found_key += 1
}
+ found_key
+ }
- return Layout {
- keys: layout,
- translate_index,
- };
+ fn char_array_to_layout(keys: [char; NUM_KEYS]) -> Layout {
+ return Layout { keys };
}
pub fn get_key(&self, index: usize) -> char {
self.keys[index]
}
- pub fn translate_index(&self, i: usize) -> usize {
- self.translate_index[i]
- }
-
pub fn from_verbose(inp: &str) -> Option<Layout> {
let mut layout: [char; NUM_KEYS] = ['x'; NUM_KEYS];
diff --git a/src/main.rs b/src/main.rs
index dfc791e..041730d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,7 +12,7 @@ use layout::*;
use std::time::Instant;
-const NUM_CONTESTANTS: usize = 64;
+const NUM_CONTESTANTS: usize = 128;
const NUM_PERSIST: usize = 8;
// TODO: Layout is really pre-layout, have something that takes a layout and