-
Notifications
You must be signed in to change notification settings - Fork 96
/
models_test.go
195 lines (190 loc) · 4.07 KB
/
models_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
package eaopt
import (
"math"
"testing"
)
var (
// Valid models
validModels = []Model{
ModGenerational{
Selector: SelTournament{1},
MutRate: 0.2,
},
ModGenerational{
Selector: SelTournament{1},
CrossRate: 0.7,
},
ModSteadyState{
Selector: SelTournament{1},
KeepBest: false,
MutRate: 0.2,
},
ModSteadyState{
Selector: SelTournament{1},
KeepBest: false,
CrossRate: 0.7,
},
ModSteadyState{
Selector: SelTournament{1},
KeepBest: true,
MutRate: 0.2,
},
ModDownToSize{
NOffsprings: 5,
SelectorA: SelTournament{1},
SelectorB: SelElitism{},
MutRate: 0.2,
},
ModRing{
Selector: SelTournament{1},
MutRate: 0.2,
},
ModMutationOnly{
Strict: false,
},
ModMutationOnly{
Strict: true,
},
ModSimulatedAnnealing{
Accept: func(g, ng uint, e0, e1 float64) float64 {
t := 1.0 - float64(g)/float64(ng)
return math.Exp(-3.0 * t)
},
},
}
// Invalid models
invalidModels = []Model{
ModGenerational{
Selector: nil,
MutRate: 0.2,
},
ModGenerational{
Selector: SelTournament{0},
MutRate: 0.2,
},
ModGenerational{
Selector: SelTournament{1},
MutRate: -1,
},
ModGenerational{
Selector: SelTournament{1},
CrossRate: -1,
},
ModSteadyState{
Selector: nil,
KeepBest: false,
MutRate: 0.2,
},
ModSteadyState{
Selector: SelTournament{0},
KeepBest: true,
MutRate: 0.2,
},
ModSteadyState{
Selector: SelTournament{1},
KeepBest: true,
MutRate: -1,
},
ModSteadyState{
Selector: SelTournament{1},
KeepBest: true,
CrossRate: -1,
},
ModDownToSize{
NOffsprings: 0,
SelectorA: SelTournament{1},
SelectorB: SelElitism{},
MutRate: 0.2,
},
ModDownToSize{
NOffsprings: 5,
SelectorA: nil,
SelectorB: SelElitism{},
MutRate: 0.2,
},
ModDownToSize{
NOffsprings: 5,
SelectorA: SelTournament{0},
SelectorB: SelElitism{},
MutRate: 0.2,
},
ModDownToSize{
NOffsprings: 5,
SelectorA: SelTournament{1},
SelectorB: nil,
MutRate: 0.2,
},
ModDownToSize{
NOffsprings: 5,
SelectorA: SelTournament{1},
SelectorB: SelTournament{0},
MutRate: 0.2,
},
ModDownToSize{
NOffsprings: 5,
SelectorA: SelTournament{1},
SelectorB: SelElitism{},
MutRate: -1,
},
ModRing{
Selector: nil,
MutRate: 0.2,
},
ModRing{
Selector: SelTournament{0},
MutRate: 0.2,
},
ModRing{
Selector: SelTournament{1},
MutRate: -1,
},
ModSimulatedAnnealing{},
}
)
// TestGenerateOffsprings checks that GenerateOffsprings works as intended by
// producing the desired number of offsprings.
func TestGenerateOffsprings(t *testing.T) {
var (
rng = newRand()
indis = newIndividuals(20, false, NewVector, rng)
)
for _, n := range []uint{0, 1, 3, 10} {
var offsprings, _ = generateOffsprings(n, indis, SelTournament{1}, 1.0, rng)
if len(offsprings) != int(n) {
t.Error("GenerateOffsprings didn't produce the expected number of offsprings")
}
}
}
// TestModelsValidate checks that each model's Validate method doesn't return
// an error in case of a valid model and vice-versa
func TestModelsValidate(t *testing.T) {
// Check valid models do not raise an error
for _, model := range validModels {
if model.Validate() != nil {
t.Error("The model validation should not have raised an error")
}
}
// Check invalid models raise an error
for _, model := range invalidModels {
if model.Validate() == nil {
t.Error("The model validation should have raised error")
}
}
}
// TestModelsConstantSize checks that each model doesn't change the size of a
// population when applied.
func TestModelsConstantSize(t *testing.T) {
var rng = newRand()
for _, n := range []uint{1, 2, 3, 42} {
for _, model := range validModels {
var pop = newPopulation(n, false, NewVector, rng)
// Check the size of the population doesn't change for a few iterations
for i := 0; i < 5; i++ {
model.Apply(&pop)
if len(pop.Individuals) != int(n) {
t.Error("A model application changed the population size")
}
}
}
}
}