forked from MaxHalford/eaopt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathga_test.go
240 lines (225 loc) · 5.56 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
package gago
import (
"errors"
"math"
"testing"
"time"
)
func TestValidationSuccess(t *testing.T) {
var err = ga.Validate()
if err != nil {
t.Error("GA parameters are invalid")
}
}
func TestValidationGenomeFactory(t *testing.T) {
var genomeFactory = ga.GenomeFactory
ga.GenomeFactory = nil
if ga.Validate() == nil {
t.Error("Nil GenomeFactory should return an error")
}
ga.GenomeFactory = 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.Enhance() != 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.Best.Fitness > indi.Fitness {
t.Error("The current best individual is not the overall best")
}
}
}
}
func TestFindBest(t *testing.T) {
// Check sure the findBest method works as expected
var fitness = ga.Populations[0].Individuals[0].Fitness
ga.Populations[0].Individuals[0].Fitness = math.Inf(-1)
ga.findBest()
if ga.Best.Fitness != math.Inf(-1) {
t.Error("findBest didn't work")
}
ga.Populations[0].Individuals[0].Fitness = fitness
// Check the best individual doesn't a share a pointer with anyone
fitness = ga.Best.Fitness
ga.Best.Fitness = 42
if ga.Populations[0].Individuals[0].Fitness == 42 {
t.Error("Best individual shares a pointer with an individual in the populations")
}
ga.Best.Fitness = fitness
}
// 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 = newRandomNumberGenerator()
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 {
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.Enhance()
if counter != 2 {
t.Error("Counter was not incremented by the callback at enhancement")
}
}