-
Notifications
You must be signed in to change notification settings - Fork 1
/
speed-test.main.ts
155 lines (142 loc) · 3.26 KB
/
speed-test.main.ts
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
/**
* test techniques to speed up the iteration
* 1. cache : depends on implementation of doesABeatB()
* 2. multi-process : works but not linearly scalable
* */
import {
GaIsland,
genDoesABeatB,
randomBoolean,
randomNumber,
RequiredOptions,
} from '../src'
import { inspect, print, random, roundNumber } from './helpers'
import { fitness as singleCoreFitness, Gene, n } from './speed-test.shared'
import {
evalAll as threadEvalAll,
initMainThread,
} from './speed-test.thread-worker'
import {
evalAll as processEvalAll,
initMainProcess,
} from './speed-test.process-worker'
const concurrentMode = process.argv[2]
const numberOfCore = +process.argv[3]
const maxGeneration = 100 * numberOfCore
const populationSize = 20000
const singleCore = false
const [evalAll, initMain] =
concurrentMode === 'thread'
? [threadEvalAll, initMainThread]
: [processEvalAll, initMainProcess]
initMain(numberOfCore)
function randomCode(): string {
return randomNumber(random, 0, 15, 1).toString(16)
}
function randomIndividual(): Gene {
let s = ''
for (let i = 0; i < n; i++) {
s += randomCode()
}
return [-1, s]
}
function distance([_a, a]: Gene, [_b, b]: Gene): number {
let acc = 0
for (let i = 0; i < n; i++) {
let x = parseInt(a[i], 16)
let y = parseInt(b[i], 16)
let d = (x - y) / 16
acc += d * d
}
acc = Math.sqrt(acc)
return acc
}
let scores: number[]
function fitness(gene: Gene): number {
if (singleCore) {
return singleCoreFitness(gene)
}
return scores[gene[0]]
}
function evolve(ga: GaIsland<Gene>, cb: () => void) {
if (singleCore) {
ga.options.fitness = singleCoreFitness
ga.evolve()
cb()
return
}
ga.options.population.forEach((gene, i) => (gene[0] = i))
evalAll(ga.options.population, (err, outputs) => {
if (err) {
throw err
}
scores = outputs
ga.evolve()
cb()
})
}
let options: RequiredOptions<Gene> = {
mutate: (gene, output): void => {
let res = ''
for (let i = 0; i < n; i++) {
if (randomBoolean(random, 1 / n)) {
res += randomCode()
} else {
res += gene[1][i]
}
}
output[0] = -1
output[1] = res
},
crossover: ([_a, a], [_b, b], child): void => {
let c = ''
for (let i = 0; i < n; i++) {
if (randomBoolean(random)) {
c += a[i]
} else {
c += b[i]
}
}
child[0] = -1
child[1] = c
},
fitness,
populationSize,
randomIndividual,
}
options.doesABeatB = genDoesABeatB({
...options,
min_distance: 3.25,
distance,
})
let ga = new GaIsland(options)
let start = Date.now()
let generation = 0
function run() {
generation++
evolve(ga, () => {
let now = Date.now()
let t = (now - start) / 1000
let speed = generation / t
// let { gene, fitness } = best({
// fitness: require("./speed-test.worker").fitness,
// population: ga.options.population
// });
print(
'\r' +
inspect({
generation,
'gen/sec': roundNumber(speed, 3),
population: ga.options.populationSize,
// best: { gene: gene[1], fitness }
}),
)
if (generation < maxGeneration) {
return run()
}
console.log()
process.exit()
})
}
console.log({ concurrentMode, numberOfCore })
run()