forked from michaeldickens/Typing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitness.c
451 lines (389 loc) · 12.1 KB
/
fitness.c
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
* fitness.c
* Typing
*
* Created by Michael Dickens on 8/10/09.
*
* Contains functions to calculate the fitness of a keyboard.
*/
#include "keyboard.h"
// For all score calculators, it is assumed that loc0 and loc1 are on the same hand.
// Calculates the stats for k. Useful for human-readable output.
// The function returns 0 if completion occurs as expected and
// non-0 otherwise.
int calcFitnessDirect(Keyboard *const k)
{
if( digraphs == NULL ) {
internalError(022);
return 1;
}
if( digraphs->kvt_table == NULL ) {
internalError(027);
return 1;
}
calcFitness(k); // Otherwise, k->fitness, k->fingerUsage, and k->fingerWork
// would not get assigned properly.
uint64_t i;
k->distance = 0;
k->inRoll = 0;
k->outRoll = 0;
k->sameHand = 0;
k->sameFinger = 0;
k->rowChange = 0;
k->homeJump = 0;
k->ringJump = 0;
k->toCenter = 0;
k->toOutside = 0;
extern void scoreAllDigraphsDirectly(Keyboard *const k);
scoreAllDigraphsDirectly(k);
char key;
Value value;
if( monographs == NULL ) {
internalError(032);
exit(0);
}
const uint64_t used = monographs->kvt_used;
for (i = 0; i < used; ++i) {
key = getFirstKeystroke(monographs, i);
value = getKVValue(monographs, i);
k->distance += trueDistance[loc(k, key)] * (value / 100); // meters
}
return 0;
}
// Scores all digraphs for the Keyboard object pointed to by
// parameter 'k' using the location information in parameter
// 'locs'.
void scoreAllDigraphs(Keyboard *k, int64_t locs[128]) {
if( digraphs == NULL ) {
internalError(033);
exit(0);
}
const int64_t used = digraphs->kvt_used;
const KeystrokeValue *theTable = digraphs->kvt_table;
KeystrokeValue kv;
Keystroke theStroke;
Value value;
int64_t i;
for (i = used - 1; i >= 0; --i) {
kv = theTable[i];
theStroke = kv.theStroke;
value = kv.theValue;
scoreDigraph(k, theStroke, value, locs);
}
}
// Directly scores all digraphs for the Keyboard object pointed to by
// parameter 'k'.
void scoreAllDigraphsDirectly(Keyboard *const k)
{
if( digraphs == NULL ) {
internalError(034);
exit(0);
}
const int64_t used = digraphs->kvt_used;
const KeystrokeValue *theTable = digraphs->kvt_table;
KeystrokeValue kv;
Keystroke theStroke;
Value value;
int64_t i;
for (i = 0; i < used; ++i) {
kv = theTable[i];
theStroke = kv.theStroke;
value = kv.theValue;
scoreDigraphDirect(k, theStroke, value);
}
}
int scoreDigraphDirect(Keyboard *const k, const char digraph[], const Value multiplier)
{
int locs[2];
int i;
for (i = 0; i < 2; ++i) locs[i] = loc(k, digraph[i]);
// These all require that the hand be the same.
if (hand[locs[0]] == hand[locs[1]]) {
if (calcInRoll (locs[0], locs[1]) != 0) k->inRoll += multiplier;
if (calcOutRoll (locs[0], locs[1]) != 0) k->outRoll += multiplier;
k->sameHand += multiplier;
if (calcSameFinger(locs[0], locs[1]) != 0) k->sameFinger += multiplier;
if (calcRowChange (locs[0], locs[1]) != 0) k->rowChange += multiplier;
if (calcHomeJump (locs[0], locs[1]) != 0) k->homeJump += multiplier;
if (calcRingJump (locs[0], locs[1]) != 0) k->ringJump += multiplier;
if (calcToCenter (locs[0], locs[1]) != 0) k->toCenter += multiplier;
if (calcToOutside (locs[0], locs[1]) != 0) k->toOutside += multiplier;
}
return 0;
}
int calcFitness(Keyboard *const k)
{
int64_t i;
for (i = 0; i < FINGER_COUNT; ++i) k->fingerUsage[i] = 0;
k->fitness = 0;
k->distance = 0;
k->inRoll = 0;
k->outRoll = 0;
k->sameHand = 0;
k->sameFinger = 0;
k->rowChange = 0;
k->homeJump = 0;
k->ringJump = 0;
k->toCenter = 0;
k->toOutside = 0;
int64_t locs[128];
for (i = 0; i < 128; ++i) locs[i] = -1;
/* Calculate all the locations beforehand. This saves a lot of time. */
for (i = 0; i < ksize; ++i)
if (printable[i]) {
locs[k->layout[i]] = i;
locs[k->shiftedLayout[i]] = ksize + i;
}
scoreAllDigraphs(k, locs);
char key;
Value value;
if( monographs == NULL ) {
internalError(035);
exit(0);
}
const int64_t used = monographs->kvt_used;
/* Calculate distance. Done here and not in scoreDigraph because it uses
* monographs instead of digraphs.
*/
for (i = 0; i < used; ++i) {
key = getFirstKeystroke(monographs, i);
value = getKVValue(monographs, i);
const int lc = locs[key] % ksize;
if (lc >= 0) k->distance += distanceCosts[lc] * value * distance;
else {
internalError( 9 );
return 0;
}
if (hand[lc] == LEFT) k->fingerUsage[finger[lc]] += value;
else k->fingerUsage[FINGER_COUNT - 1 - finger[lc]] += value;
}
calcFingerWork(k);
k->fitness = k->distance + k->fingerWork + k->inRoll + k->outRoll +
k->sameHand + k->sameFinger + k->rowChange + k->homeJump + k->ringJump +
k->toCenter + k->toOutside;
if (keepZXCV) k->fitness += calcShortcuts(k);
if (keepQWERTY) k->fitness += calcQWERTY(k);
if (keepParentheses) k->fitness += calcParentheses(k);
if (keepNumbersShifted) k->fitness += calcNumbersShifted(k);
return 0;
}
int scoreDigraph(Keyboard *const k, const char digraph[], const int64_t multiplier, const int64_t allLocs[])
{
if( ksize == 0 ) {
internalError(040);
exit(0);
}
const int loc0 = allLocs[digraph[0]] % ksize;
const int loc1 = allLocs[digraph[1]] % ksize;
if (loc0 == -1 || loc1 == -1) {
return 1;
}
/* TODO: Once modifier keys are added, change this to include the cost of
* hitting Shift, plus an extra penalty.
*/
if (allLocs[digraph[0]] >= ksize && allLocs[digraph[1]] >= ksize) {
k->distance += doubleShiftCost * multiplier;
} else if ((allLocs[digraph[0]] >= ksize) != (allLocs[digraph[1]] >= ksize)) {
k->distance += shiftCost * multiplier;
}
/* These all require that the hand be the same. */
if (hand[loc0] == hand[loc1]) {
k->sameHand += sameHand * multiplier;
k->sameFinger += calcSameFinger(loc0, loc1) * multiplier;
/* These factors are meaningless for the thumbs, because thumbs are
* relatively separate from the rest of the fingers.
*/
if (finger[loc0] != THUMB && finger[loc1] != THUMB) {
k->inRoll += calcInRoll (loc0, loc1) * multiplier;
k->outRoll += calcOutRoll (loc0, loc1) * multiplier;
k->rowChange += calcRowChange (loc0, loc1) * multiplier;
k->homeJump += calcHomeJump (loc0, loc1) * multiplier;
k->ringJump += calcRingJump (loc0, loc1) * multiplier;
k->toCenter += calcToCenter (loc0, loc1) * multiplier;
k->toOutside += calcToOutside (loc0, loc1) * multiplier;
}
}
return 0;
}
int64_t calcShortcuts(const Keyboard *const k)
{
if( monographs == NULL ) {
internalError(036);
exit(0);
}
int64_t result;
result =
shortcutCosts[loc(k, 'z')] * (int64_t) zCost
+ shortcutCosts[loc(k, 'x')] * (int64_t) xCost
+ shortcutCosts[loc(k, 'c')] * (int64_t) cCost
+ shortcutCosts[loc(k, 'v')] * (int64_t) vCost;
const int64_t used = monographs->kvt_used;
return result * (totalMon / used);
}
int64_t calcQWERTY(const Keyboard *const k)
{
NOT_WORK_WITH_full_keyboard("calcQWERTY")
int64_t result = 0;
const int64_t averageMon = totalMon / 30;
int i, pos;
for (i = 0; i < 30; ++i) {
if ((pos = loc(k, qwerty[i])) != i) result += qwertyPosCost * averageMon;
if (finger[pos] != finger[i]) result += qwertyFingerCost * averageMon;
if (hand[pos] != hand[i]) result += qwertyHandCost * averageMon;
}
return result;
}
int64_t calcParentheses(const Keyboard *const k)
{
return calcParensGeneric(k, '(', ')') + calcParensGeneric(k, '<', '>') +
calcParensGeneric(k, '[', ']') + calcParensGeneric(k, '{', '}');
}
int64_t calcParensGeneric(const Keyboard *const k, const char openChar, const char closeChar)
{
if( ksize == 0 ) {
internalError(041);
exit(0);
}
int openPar = locWithShifted(k, openChar);
if (openPar == -1) return -1;
int closePar = locWithShifted(k, closeChar);
if (closePar == -1) return -1;
const int openShifted = openPar >= ksize;
const int closeShifted = closePar >= ksize;
openPar %= ksize;
closePar %= ksize;
/* Parenthses have to be either next to each other or on opposite side of
* the keyboard, and must both be either shifted or unshifted.
*/
if (openShifted == closeShifted &&
((openPar+1 == closePar && row[openPar] == row[closePar]) ||
(hand[openPar] == LEFT && hand[closePar] == RIGHT &&
column[openPar] == column[closePar] &&
row[openPar] == row[closePar])))
return 0;
else return parenthesesCost / DIVISOR;
}
int64_t calcNumbersShifted(const Keyboard *const k)
{
int64_t score = 0;
char c;
for (c = '0'; c <= '9'; ++c)
if (strchr(k->shiftedLayout, c))
score += numbersShiftedCost;
return score;
}
/* Requires that k->fingerUsage has been calculated. */
int calcFingerWork(Keyboard *const k)
{
int64_t total = 0;
int i;
double usage;
double percentMax;
double difference;
for (i = 0; i < FINGER_COUNT; ++i) total += k->fingerUsage[i];
k->fingerWork = 0;
for (i = 0; i < FINGER_COUNT; ++i) {
usage = k->fingerUsage[i];
percentMax = fingerPercentMaxes[i];
if (1000*usage/total > percentMax*10) {
difference = usage - (percentMax*total/100);
k->fingerWork += (int64_t) difference * fingerWorkCosts[i];
}
}
return 0;
}
/*
* In and out rolls could be defined in two ways: either two keys
* pressed next to each other, or two keys moving in one direction.
* The former definition is currently used.
*
* Rolls must occur on the same row.
*/
int calcInRoll(const int loc0, const int loc1)
{
if (finger[loc1] == finger[loc0] + 1 && row[loc0] == row[loc1] &&
!isCenterOrOutside[loc0] && !isCenterOrOutside[loc1])
return inRoll;
else return 0;
// if (finger[loc1] > finger[loc0] && row[loc0] == row[loc1] && !isCenterOrOutside[loc0] && !isCenterOrOutside[loc1]) return inRoll;
// else return 0;
}
int calcOutRoll(const int loc0, const int loc1)
{
if (finger[loc1] == finger[loc0] - 1 && row[loc0] == row[loc1] &&
!isCenterOrOutside[loc0] && !isCenterOrOutside[loc1])
return outRoll;
else return 0;
// if (finger[loc1] < finger[loc0] && row[loc0] == row[loc1] && !isCenterOrOutside[loc0] && !isCenterOrOutside[loc1]) return outRoll;
// else return 0;
}
int calcSameFinger(const int loc0, const int loc1)
{
if (finger[loc0] == finger[loc1]) {
if (loc0 == loc1) return 0;
else switch (finger[loc0]) {
case PINKY:
return sameFingerP;
case RING:
return sameFingerR;
case MIDDLE:
return sameFingerM;
case INDEX:
return sameFingerI;
case THUMB:
return sameFingerT;
}
}
return 0;
}
// If the row changes on the same hand, there is a penalty.
// Jumping over the home row is an additional penalty, but
// isn't calculated here (see calcHomeJump()).
int calcRowChange(const int loc0, const int loc1)
{
if (row[loc0] != row[loc1]) {
if (row[loc0] < row[loc1]) { // loc1 is a lower row
return rowChangeTableDown[finger[loc0]][finger[loc1]];
} else { // loc1 is a higher row
return rowChangeTableUp[finger[loc0]][finger[loc1]];
}
}
return 0;
}
int calcHomeJump(const int loc0, const int loc1)
{
const int row0 = row[loc0];
const int row1 = row[loc1];
if (row0 < row1) {
if (homeRow <= row0 || homeRow >= row1) return 0;
} else if (row0 > row1) {
if (homeRow <= row1 || homeRow >= row0) return 0;
} else return 0;
if (abs(row0 - row1) == 2)
if ((row0 > row1 && finger[loc0] == INDEX && (finger[loc1] == MIDDLE || finger[loc1] == RING)) ||
(row1 > row0 && finger[loc1] == INDEX && (finger[loc0] == MIDDLE || finger[loc0] == RING))) return homeJump + homeJumpIndex;
else return homeJump;
else if (abs(row0 - row1) > 2)
if ((row0 > row1 && finger[loc0] == INDEX && (finger[loc1] == MIDDLE || finger[loc1] == RING)) ||
(row1 > row0 && finger[loc1] == INDEX && (finger[loc0] == MIDDLE || finger[loc0] == RING))) return doubleJump + homeJumpIndex;
else return doubleJump;
return 0;
}
int calcRingJump(const int loc0, const int loc1)
{
if ((finger[loc0] == PINKY && finger[loc1] == MIDDLE) ||
(finger[loc0] == MIDDLE && finger[loc1] == PINKY)) return ringJump;
else return 0;
}
int calcToCenter(const int loc0, const int loc1)
{
if (isCenter[loc0] ^ isCenter[loc1]) return toCenter;
else return 0;
}
/* Only applies for the extended keyboard.
*/
int calcToOutside(const int loc0, const int loc1)
{
if (fullKeyboard != FK_NO && (isOutside[loc0] ^ isOutside[loc1])) return toOutside;
else return 0;
}