-
Notifications
You must be signed in to change notification settings - Fork 1
/
ga-island.spec.ts
153 lines (137 loc) · 3.18 KB
/
ga-island.spec.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
import { FullOptions, GaIsland, populateOptions } from '../src/ga-island'
import { expect } from 'chai'
import { best } from '../src'
describe('ga-island TestSuit', function () {
let n = 60
function randomBoolean() {
return Math.random() < 0.5
}
type Gene = { pattern: string }
function mutate(gene: Gene, output: Gene): void {
let s = ''
for (let i = 0; i < n; i++) {
let c = gene.pattern[i]
if (randomBoolean()) {
c = c === '0' ? '1' : '0'
}
s += c
}
output.pattern = s
}
function crossover(a: Gene, b: Gene, child: Gene): void {
let c = ''
for (let i = 0; i < n; i++) {
if (randomBoolean()) {
c += a.pattern[i]
} else {
c += b.pattern[i]
}
}
child.pattern = c
}
function fitness(gene: Gene): number {
let acc = 0
for (let i = 0; i < n; i++) {
if (gene.pattern[i] === '1') {
acc++
}
}
return acc
}
function randomIndividual(): Gene {
let s = ''
for (let i = 0; i < n; i++) {
s += randomBoolean() ? '0' : '1'
}
return { pattern: s }
}
function checkFunction(f: any, length: number) {
expect(f).is.a('function')
expect(f.length).equals(length)
}
function checkOptions(options: FullOptions<Gene>) {
expect(options).is.an('object')
checkFunction(options.mutate, 2)
checkFunction(options.crossover, 3)
checkFunction(options.fitness, 1)
checkFunction(options.doesABeatB, 2)
expect(options.population).is.a('Array')
expect(options.populationSize).is.a('number')
checkFunction(options.randomIndividual, 0)
}
it('should populate options', function () {
expect(() =>
populateOptions({
mutate,
crossover,
fitness,
population: [],
}),
).to.throw('zero population')
checkOptions(
populateOptions({
mutate,
crossover,
fitness,
population: [randomIndividual()],
}),
)
checkOptions(
populateOptions({
mutate,
crossover,
fitness,
randomIndividual,
}),
)
})
it('should infer population size', function () {
let populationSize = 123
expect(
populateOptions({
mutate,
crossover,
fitness,
populationSize,
randomIndividual,
}).populationSize,
).equals(populationSize)
expect(() =>
populateOptions({
mutate,
crossover,
fitness,
populationSize,
population: [],
}),
).to.throw('no population for randomIndividual to seed from')
expect(
populateOptions({
mutate,
crossover,
fitness,
population: new Array(populationSize),
}).populationSize,
).equals(populationSize)
})
it('should solve the problem', function () {
let ga = new GaIsland({
mutate,
crossover,
fitness,
randomIndividual,
})
for (let generation = 1; ; generation++) {
ga.evolve()
let { gene, fitness } = best(ga.options)
console.log({
generation,
bestGene: gene.pattern,
fitness,
})
if (fitness === n) {
return
}
}
})
})