-
Notifications
You must be signed in to change notification settings - Fork 18
/
fitness.c
410 lines (345 loc) · 11.4 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
/*
* fitness.c
* Typing
*
* Created by Michael Dickens on 8/10/09.
*
* Contains functions to calculate the fitness of a keyboard.
*/
#include "keyboard.h"
/*
* Calculates fitness without score multipliers. Useful for producing
* human-readable output.
*/
int calcFitnessDirect(Keyboard *k)
{
calcFitness(k); // Otherwise, k->fitness, k->fingerUsage, and k->fingerWork
// would not get assigned properly.
int i;
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;
for (i = 0; i < diLen; ++i) scoreDigraphDirect(k, digraphs[i].key,
digraphs[i].value);
return 0;
}
inline int scoreDigraphDirect(Keyboard *k, char digraph[], int64_t multiplier)
{
int locs[2];
int i;
for (i = 0; i < 2; ++i) locs[i] = locIgnoreShifted(k, digraph[i]);
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 preCalculateFitness()
{
int i, j;
memset(allDigraphCosts, 0, sizeof(allDigraphCosts));
for (i = 0; i < 2 * ksize; ++i) {
for (j = 0; j < 2 * ksize; ++j) {
if (i >= ksize && j >= ksize) {
allDigraphCosts[i][j] += doubleShiftCost;
} else if ((i >= ksize) != (j >= ksize)) {
allDigraphCosts[i][j] += shiftCost;
}
int loc0 = i % ksize;
int loc1 = j % ksize;
if (hand[loc0] == hand[loc1]) {
allDigraphCosts[i][j] += sameHand;
allDigraphCosts[i][j] += calcSameFinger(loc0, loc1);
if (finger[i] != THUMB && finger[j] != THUMB) {
allDigraphCosts[i][j] += calcInRoll (loc0, loc1);
allDigraphCosts[i][j] += calcOutRoll (loc0, loc1);
allDigraphCosts[i][j] += calcRowChange(loc0, loc1);
allDigraphCosts[i][j] += calcHomeJump (loc0, loc1);
allDigraphCosts[i][j] += calcRingJump (loc0, loc1);
allDigraphCosts[i][j] += calcToCenter (loc0, loc1);
allDigraphCosts[i][j] += calcToOutside(loc0, loc1);
}
}
}
}
return 0;
}
/*
* Not guaranteed to set any variables in k other than k->fitness. For
* generating detailed output, use calcFitnessDirect().
*/
int calcFitness(Keyboard *k)
{
int 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;
int 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[(int) k->layout[i]] = i;
locs[(int) k->shiftedLayout[i]] = ksize + i;
}
for (i = 0; i < diLen; ++i) {
scoreDigraph(k, digraphs[i].key, digraphs[i].value, locs);
}
/* Calculate distance. Done here and not in scoreDigraph because it uses
* monographs instead of digraphs.
*/
for (i = 0; i < monLen; ++i) {
int lc = locs[(int) monographs[i].key] % ksize;
if (lc >= 0)
k->distance += distanceCosts[lc] * monographs[i].value * distance;
if (hand[lc] == LEFT) k->fingerUsage[finger[lc]] += monographs[i].value;
else k->fingerUsage[FINGER_COUNT - 1 - finger[lc]] +=
monographs[i].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 (keepBrackets) k->fitness += calcBrackets(k);
if (keepNumbersShifted) k->fitness += calcNumbersShifted(k);
return 0;
}
inline int scoreDigraph(Keyboard *k, char digraph[], int64_t multiplier,
int allLocs[])
{
int loc0 = allLocs[(int) digraph[0]];
int loc1 = allLocs[(int) digraph[1]];
#ifdef USE_COST_ARRAY
k->fitness += allDigraphCosts[loc0][loc1] * multiplier;
return 0;
#else
if (loc0 >= ksize && loc1 >= ksize) {
k->distance += doubleShiftCost * multiplier;
} else if ((loc0 >= ksize) ^ (loc1 >= ksize)) {
k->distance += shiftCost * multiplier;
}
loc0 %= ksize;
loc1 %= ksize;
/* 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;
#endif
}
inline int64_t calcShortcuts(Keyboard *k)
{
int64_t result;
result =
shortcutCosts[locIgnoreShifted(k, 'z')] * (int64_t) zCost
+ shortcutCosts[locIgnoreShifted(k, 'x')] * (int64_t) xCost
+ shortcutCosts[locIgnoreShifted(k, 'c')] * (int64_t) cCost
+ shortcutCosts[locIgnoreShifted(k, 'v')] * (int64_t) vCost;
return result * (totalMon / monLen);
}
inline int64_t calcQWERTY(Keyboard *k)
{
NOT_WORK_WITH_FULL_KEYBOARD("calcQWERTY")
int64_t result = 0;
int64_t averageMon = totalMon / 30;
int i, pos;
for (i = 0; i < 30; ++i) {
if ((pos = locIgnoreShifted(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;
}
inline int64_t calcBrackets(Keyboard *k)
{
return calcBracketsGeneric(k, '(', ')') + calcBracketsGeneric(k, '<', '>') +
calcBracketsGeneric(k, '[', ']') + calcBracketsGeneric(k, '{', '}');
}
inline int64_t calcBracketsGeneric(Keyboard *k, char openChar, char closeChar)
{
/* Some layouts do not contain brackets. That's okay; just return 0. */
int openPar = locWithShifted(k, openChar);
if (openPar < 0) return 0;
int closePar = locWithShifted(k, closeChar);
if (closePar < 0) return 0;
int openShifted = openPar >= ksize;
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 bracketsCost / DIVISOR;
}
inline int64_t calcNumbersShifted(Keyboard *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. */
inline int calcFingerWork(Keyboard *k)
{
int64_t total = 0;
int i;
double usage, percentMax, 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 (100*usage/total > percentMax) {
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.
*/
inline int calcInRoll(int loc0, 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;
}
inline int calcOutRoll(int loc0, 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;
}
inline int calcSameFinger(int loc0, 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()).
inline int calcRowChange(int loc0, int loc1)
{
int row0 = row[loc0];
int row1 = row[loc1];
if (row0 != row1) {
if (row0 < row1) { // 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;
}
inline int calcHomeJump(int loc0, int loc1)
{
int row0 = row[loc0];
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;
}
inline int calcRingJump(int loc0, int loc1)
{
if ((finger[loc0] == PINKY && finger[loc1] == MIDDLE) ||
(finger[loc0] == MIDDLE && finger[loc1] == PINKY)) return ringJump;
else return 0;
}
inline int calcToCenter(int loc0, int loc1)
{
if (isCenter[loc0] ^ isCenter[loc1]) return toCenter;
else return 0;
}
/* Only applies for the extended keyboard.
*/
inline int calcToOutside(int loc0, int loc1)
{
if (fullKeyboard != K_NO && (isOutside[loc0] ^ isOutside[loc1])) return toOutside;
else return 0;
}