This repository has been archived by the owner on Aug 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ga_test.go
364 lines (341 loc) · 7.75 KB
/
ga_test.go
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
package gago
import (
"errors"
"fmt"
"math"
"math/rand"
"testing"
"time"
)
func TestInitialized(t *testing.T) {
var ga = GA{
NewGenome: NewVector,
NPops: 1,
PopSize: 10,
}
if ga.Initialized() {
t.Error("GA should not yet be initialized")
}
ga.Initialize()
if !ga.Initialized() {
t.Error("GA should be initialized")
}
}
func TestValidationSuccess(t *testing.T) {
var err = ga.Validate()
if err != nil {
t.Error("GA parameters are invalid")
}
}
func TestValidationNewGenome(t *testing.T) {
var genomeFactory = ga.NewGenome
ga.NewGenome = nil
if ga.Validate() == nil {
t.Error("Nil NewGenome should return an error")
}
ga.NewGenome = genomeFactory
}
func TestValidationNPopulations(t *testing.T) {
var nPops = ga.NPops
ga.NPops = -1
if ga.Validate() == nil {
t.Error("Invalid number of Populations should return an error")
}
ga.NPops = nPops
}
func TestValidationNIndividuals(t *testing.T) {
var popSize = ga.PopSize
ga.PopSize = -1
if ga.Validate() == nil {
t.Error("Invalid number of Individuals should return an error")
}
ga.PopSize = popSize
}
func TestValidationModel(t *testing.T) {
var model = ga.Model
// Check nil model raises error
ga.Model = nil
if ga.Validate() == nil {
t.Error("Nil Model should return an error")
}
// Check invalid model raises error
ga.Model = ModGenerational{
Selector: SelTournament{
NContestants: 3,
},
MutRate: -1,
}
if ga.Validate() == nil {
t.Error("Invalid Model should return an error")
}
ga.Model = model
}
func TestValidationMigFrequency(t *testing.T) {
var (
migrator = ga.Migrator
migFrequency = ga.MigFrequency
)
ga.Migrator = MigRing{}
ga.MigFrequency = 0
if ga.Validate() == nil {
t.Error("Invalid MigFrequency should return an error")
}
ga.Migrator = migrator
ga.MigFrequency = migFrequency
}
func TestValidationSpeciator(t *testing.T) {
var speciator = ga.Speciator
ga.Speciator = SpecFitnessInterval{0}
if ga.Validate() == nil {
t.Error("Invalid Speciator should return an error")
}
ga.Speciator = speciator
}
func TestApplyWithSpeciator(t *testing.T) {
var speciator = ga.Speciator
ga.Speciator = SpecFitnessInterval{4}
if ga.Evolve() != nil {
t.Error("Calling Apply with a valid Speciator should not return an error")
}
ga.Speciator = speciator
}
func TestRandomNumberGenerators(t *testing.T) {
for i, pop1 := range ga.Populations {
for j, pop2 := range ga.Populations {
if i != j && &pop1.RNG == &pop2.RNG {
t.Error("Population should not share random number generators")
}
}
}
}
func TestBest(t *testing.T) {
for _, pop := range ga.Populations {
for _, indi := range pop.Individuals {
if ga.HallOfFame[0].Fitness > indi.Fitness {
t.Error("The current best individual is not the overall best")
}
}
}
}
func TestUpdateHallOfFame(t *testing.T) {
var (
testCases = []struct {
hofIn Individuals
indis Individuals
hofOut Individuals
}{
{
hofIn: Individuals{
Individual{Fitness: math.Inf(1)},
},
indis: Individuals{
Individual{Fitness: 0},
},
hofOut: Individuals{
Individual{Fitness: 0},
},
},
{
hofIn: Individuals{
Individual{Fitness: 0},
Individual{Fitness: math.Inf(1)},
},
indis: Individuals{
Individual{Fitness: 1},
},
hofOut: Individuals{
Individual{Fitness: 0},
Individual{Fitness: 1},
},
},
}
)
for i, tc := range testCases {
t.Run(fmt.Sprintf("TC %d", i), func(t *testing.T) {
updateHallOfFame(tc.hofIn, tc.indis)
// Compare the obtained hall of fame to the expected one)
for i, indi := range tc.hofIn {
if indi.Fitness != tc.hofOut[i].Fitness {
t.Errorf("Expected %v, got %v", tc.hofOut[i], indi)
}
}
})
}
}
// TestDuration verifies the sum of the duration of each population is higher
// the actual duration. This is due to the fact that each population runs on a
// separate core.
func TestDuration(t *testing.T) {
var totalDuration time.Duration
for _, pop := range ga.Populations {
totalDuration += pop.Age
}
if totalDuration < ga.Age {
t.Error("Inefficient parallelism")
}
}
func TestSpeciateEvolveMerge(t *testing.T) {
var (
rng = newRand()
testCases = []struct {
pop Population
speciator Speciator
model Model
err error
}{
{
pop: Population{
ID: "42",
RNG: rng,
Individuals: Individuals{
Individual{Fitness: 0},
Individual{Fitness: 1},
Individual{Fitness: 2},
Individual{Fitness: 3},
Individual{Fitness: 4},
},
},
speciator: SpecFitnessInterval{3},
model: ModIdentity{},
err: nil,
},
{
pop: Population{
ID: "42",
RNG: rng,
Individuals: Individuals{
Individual{Fitness: 0},
Individual{Fitness: 1},
Individual{Fitness: 2},
},
},
speciator: SpecFitnessInterval{4},
model: ModIdentity{},
err: errors.New("Invalid speciator"),
},
{
pop: Population{
ID: "42",
RNG: rng,
Individuals: Individuals{
Individual{Fitness: 0},
Individual{Fitness: 1},
Individual{Fitness: 2},
Individual{Fitness: 3},
Individual{Fitness: 4},
},
},
speciator: SpecFitnessInterval{3},
model: ModGenerational{
Selector: SelTournament{6},
MutRate: 0.5,
},
err: errors.New("Invalid model"),
},
}
)
for i, tc := range testCases {
t.Run(fmt.Sprintf("TC %d", i), func(t *testing.T) {
var err = tc.pop.speciateEvolveMerge(tc.speciator, tc.model)
if (err == nil) != (tc.err == nil) {
t.Errorf("Wrong error in test case number %d", i)
}
// If there is no error check the individuals are ordered as they were
// at they were initially
if err == nil {
for j, indi := range tc.pop.Individuals {
if indi.Fitness != float64(j) {
t.Errorf("Wrong result in test case number %d", i)
}
}
}
})
}
}
func TestCallback(t *testing.T) {
var (
counter int
incrementCounter = func(ga *GA) {
counter++
}
)
ga.Callback = incrementCounter
ga.Initialize()
if counter != 1 {
t.Error("Counter was not incremented by the callback at initialization")
}
ga.Evolve()
if counter != 2 {
t.Error("Counter was not incremented by the callback at enhancement")
}
}
func TestGAEvolveModelRuntimeError(t *testing.T) {
var model = ga.Model
ga.Model = ModRuntimeError{}
// Check invalid model doesn't raise error
if ga.Validate() != nil {
t.Errorf("Expected nil, got %s", ga.Validate())
}
// Evolve
var err = ga.Evolve()
if err == nil {
t.Error("An error should have been raised")
}
ga.Model = model
}
func TestGAEvolveSpeciatorRuntimeError(t *testing.T) {
var speciator = ga.Speciator
ga.Speciator = SpecRuntimeError{}
// Check invalid speciator doesn't raise error
if ga.Validate() != nil {
t.Errorf("Expected nil, got %s", ga.Validate())
}
// Evolve
var err = ga.Evolve()
if err == nil {
t.Error("An error should have been raised")
}
ga.Speciator = speciator
}
func TestGAConsistentResults(t *testing.T) {
var (
ga1 = GA{
NewGenome: NewVector,
NPops: 2,
PopSize: 10,
Model: ModGenerational{
Selector: SelTournament{
NContestants: 3,
},
MutRate: 0.5,
},
RNG: rand.New(rand.NewSource(42)),
}
ga2 = GA{
NewGenome: NewVector,
NPops: 2,
PopSize: 10,
Model: ModGenerational{
Selector: SelTournament{
NContestants: 3,
},
MutRate: 0.5,
},
RNG: rand.New(rand.NewSource(42)),
}
)
// Run the first GA
ga1.Initialize()
for i := 0; i < 20; i++ {
ga1.Evolve()
}
// Run the second GA
ga2.Initialize()
for i := 0; i < 20; i++ {
ga2.Evolve()
}
// Compare best individuals
if ga1.HallOfFame[0].Fitness != ga2.HallOfFame[0].Fitness {
t.Errorf("Expected %f, got %f", ga1.HallOfFame[0].Fitness, ga2.HallOfFame[0].Fitness)
}
}