2023-02-01 13:05:28 +01:00
|
|
|
const co = require("co");
|
|
|
|
const ui = require("./ui");
|
|
|
|
const { text, trigrams, code } = require("./data");
|
|
|
|
const Stats = require("./stats");
|
|
|
|
const Cluster = require("./cluster");
|
|
|
|
const Population = require("./population");
|
|
|
|
const {
|
|
|
|
JCUKEN,
|
|
|
|
Diktor,
|
|
|
|
Diktor_VoronovMod,
|
|
|
|
Redaktor,
|
|
|
|
JUIYAF,
|
|
|
|
Zubachev,
|
|
|
|
Ruhal,
|
|
|
|
} = require("./presets");
|
|
|
|
const {
|
|
|
|
use_elites,
|
|
|
|
mutate_levels,
|
|
|
|
mutate_thresholds,
|
|
|
|
max_no_chage,
|
|
|
|
max_generation,
|
|
|
|
population_size,
|
|
|
|
POPULATION_SIZE,
|
|
|
|
} = require("./config");
|
|
|
|
let { mutate_level } = require("./config");
|
|
|
|
|
|
|
|
const data = text;
|
|
|
|
const cluster = new Cluster(data);
|
|
|
|
const seed_layouts = [
|
|
|
|
JCUKEN,
|
|
|
|
Diktor,
|
|
|
|
Diktor_VoronovMod,
|
|
|
|
Redaktor,
|
|
|
|
JUIYAF,
|
|
|
|
Zubachev,
|
|
|
|
Ruhal,
|
|
|
|
];
|
|
|
|
|
|
|
|
co(boot).catch((e) => console.log(e.stack));
|
|
|
|
|
|
|
|
function* boot() {
|
|
|
|
if (mutate_levels.length != mutate_thresholds.length) {
|
2022-08-17 21:11:13 +02:00
|
|
|
die("Mutate settings are incorrect");
|
|
|
|
}
|
2016-11-12 06:58:54 +01:00
|
|
|
for (const promise of cluster.schedule(seed_layouts)) {
|
|
|
|
const { layout, result } = yield promise;
|
|
|
|
ui.addResult(layout, new Stats(result));
|
2016-03-20 09:21:58 +01:00
|
|
|
}
|
|
|
|
|
2022-08-17 19:37:09 +02:00
|
|
|
yield start(Population.random(), max_generation);
|
2016-03-20 09:21:58 +01:00
|
|
|
}
|
2016-03-20 00:12:42 +01:00
|
|
|
|
2023-02-01 13:05:28 +01:00
|
|
|
function* start(population, count) {
|
2016-03-25 01:29:22 +01:00
|
|
|
let last_layout, last_score, no_changes_in;
|
2022-08-17 21:11:13 +02:00
|
|
|
let rank = 0;
|
2016-03-23 11:38:18 +01:00
|
|
|
|
2023-02-01 13:05:28 +01:00
|
|
|
for (let i = 0; i < count; i++) {
|
2016-03-25 01:29:22 +01:00
|
|
|
const [layout, score] = yield handle(population);
|
|
|
|
|
|
|
|
if (!last_layout || last_layout.toString() !== layout.toString()) {
|
|
|
|
no_changes_in = 0;
|
2023-02-01 13:05:28 +01:00
|
|
|
if (rank < mutate_levels.length) {
|
|
|
|
if (score.total > mutate_thresholds[rank]) {
|
2022-08-17 21:11:13 +02:00
|
|
|
mutate_level = mutate_levels[rank];
|
2023-02-01 13:05:28 +01:00
|
|
|
rank++;
|
2022-08-17 21:11:13 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-25 01:29:22 +01:00
|
|
|
} else {
|
2023-02-01 13:05:28 +01:00
|
|
|
no_changes_in++;
|
2016-03-25 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.addResult(layout, score, no_changes_in, max_no_chage);
|
2023-02-01 13:05:28 +01:00
|
|
|
last_layout = layout;
|
|
|
|
last_score = score;
|
2016-03-25 01:29:22 +01:00
|
|
|
|
|
|
|
if (no_changes_in == max_no_chage) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-02-01 13:05:28 +01:00
|
|
|
population = population.next({ elite: use_elites, mutate: mutate_level });
|
2016-03-20 00:12:42 +01:00
|
|
|
}
|
|
|
|
|
2022-08-18 14:57:47 +02:00
|
|
|
const vs = ui.vs_result(last_score);
|
|
|
|
const overheads = ui.getOverheads();
|
2016-03-23 11:38:18 +01:00
|
|
|
|
2022-08-17 21:11:13 +02:00
|
|
|
ui.destroy();
|
2023-02-01 13:05:28 +01:00
|
|
|
|
|
|
|
const fs = require("fs");
|
|
|
|
const file = new console.Console(
|
|
|
|
fs.createWriteStream(`./results/output${new Date().getTime()}.txt`)
|
|
|
|
);
|
|
|
|
|
2022-08-17 21:11:13 +02:00
|
|
|
console.log(`Total: ${last_score.total}, Dist: ${last_score.position},\n`);
|
2023-02-01 13:05:28 +01:00
|
|
|
console.log(
|
|
|
|
`Symmetry: ${last_score.symmetry}%, Evenness: ${last_score.evenness}%,\n`
|
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
`Hands: ${last_score.handsUsage.map((v) => v + "%").join(" | ")},\n`
|
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
`Overheads: Finger:${overheads.finger}%, Hand:${overheads.hand}%, Shift:${overheads.shift}%\n`
|
|
|
|
);
|
2022-08-18 14:57:47 +02:00
|
|
|
console.log(`Fingers ${last_score.fingersUsage}\n`);
|
2022-08-17 21:11:13 +02:00
|
|
|
console.log(`vs.Diktor: ${vs[0]}`);
|
2022-08-18 14:57:47 +02:00
|
|
|
console.log(`vs.JCUKEN: ${vs[1]}\n`);
|
2022-08-17 19:07:15 +02:00
|
|
|
console.log(last_layout.toCyrillic(), "\n");
|
2016-03-25 01:29:22 +01:00
|
|
|
console.log(last_layout.config);
|
2023-02-01 13:05:28 +01:00
|
|
|
|
|
|
|
file.log(`Total: ${last_score.total}, Dist: ${last_score.position},\n`);
|
|
|
|
file.log(
|
|
|
|
`Symmetry: ${last_score.symmetry}%, Evenness: ${last_score.evenness}%,\n`
|
|
|
|
);
|
|
|
|
file.log(
|
|
|
|
`Hands: ${last_score.handsUsage.map((v) => v + "%").join(" | ")},\n`
|
|
|
|
);
|
|
|
|
file.log(
|
|
|
|
`Overheads: Finger:${overheads.finger}%, Hand:${overheads.hand}%, Shift:${overheads.shift}%\n`
|
|
|
|
);
|
|
|
|
file.log(`Fingers ${last_score.fingersUsage}\n`);
|
|
|
|
file.log(`vs.Diktor: ${vs[0]}`);
|
|
|
|
file.log(`vs.JCUKEN: ${vs[1]}\n`);
|
|
|
|
file.log(last_layout.toCyrillic(), "\n");
|
|
|
|
file.log(last_layout.config);
|
2016-03-20 00:12:42 +01:00
|
|
|
}
|
|
|
|
|
2023-02-01 13:05:28 +01:00
|
|
|
function* handle(population) {
|
|
|
|
ui.newPopulation(
|
|
|
|
population,
|
|
|
|
max_generation,
|
|
|
|
use_elites,
|
|
|
|
mutate_level,
|
|
|
|
POPULATION_SIZE
|
|
|
|
);
|
2016-03-03 11:41:37 +01:00
|
|
|
|
2023-02-01 13:05:28 +01:00
|
|
|
const layouts = population.genomes.map((g) => g.toLayout());
|
2016-11-12 06:58:54 +01:00
|
|
|
|
|
|
|
for (const promise of cluster.schedule(layouts)) {
|
|
|
|
const { layout, result } = yield promise;
|
|
|
|
const score = new Stats(result);
|
2016-03-20 09:21:58 +01:00
|
|
|
ui.logGrade(layout, score);
|
2016-11-12 06:58:54 +01:00
|
|
|
layouts.forEach((l, i) => {
|
|
|
|
if (l.config === layout.config) {
|
|
|
|
population.scores[i] = score;
|
|
|
|
}
|
|
|
|
});
|
2016-03-20 00:12:42 +01:00
|
|
|
}
|
2016-03-03 11:41:37 +01:00
|
|
|
|
2016-03-04 09:31:00 +01:00
|
|
|
const best_gene = population.best();
|
2016-03-03 11:41:37 +01:00
|
|
|
const best_layout = best_gene.toLayout();
|
2016-03-16 23:12:40 +01:00
|
|
|
const score = population.scoreFor(best_gene);
|
2016-03-03 11:41:37 +01:00
|
|
|
|
2016-03-20 00:12:42 +01:00
|
|
|
return [best_layout, score];
|
2016-03-19 06:52:09 +01:00
|
|
|
}
|