forked from MaxHalford/eaopt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection_test.go
117 lines (109 loc) · 2.76 KB
/
selection_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
package gago
import (
"testing"
)
var (
validSelectors = []Selector{
SelElitism{},
SelTournament{3},
SelRoulette{},
}
invalidSelectors = []Selector{
SelTournament{0},
SelTournament{-1},
}
)
func TestSelectionSize(t *testing.T) {
var (
rng = newRandomNumberGenerator()
indis = newIndividuals(30, NewVector, rng)
selectors = []Selector{
SelTournament{
NContestants: 3,
},
SelElitism{},
}
)
for _, selector := range selectors {
for _, n := range []int{3, 10, 20} {
var selected, _, _ = selector.Apply(n, indis, rng)
if len(selected) != n {
t.Error("Selector didn't select the expected number of individuals")
}
}
}
}
func TestSelElitism(t *testing.T) {
var (
rng = newRandomNumberGenerator()
indis = newIndividuals(30, NewVector, rng)
selector = SelElitism{}
)
indis.Evaluate()
for _, n := range []int{1, 2, 10, 30} {
var _, indexes, _ = selector.Apply(n, indis, rng)
for i, index := range indexes {
if index != i {
t.Error("SelElitism didn't select the expected individuals")
}
}
}
}
func TestSelTournament(t *testing.T) {
var (
rng = newRandomNumberGenerator()
indis = newIndividuals(30, NewVector, rng)
)
indis.Evaluate()
var selected, _, _ = SelTournament{len(indis)}.Apply(1, indis, rng)
if selected[0].Fitness != indis.FitMin() {
t.Error("Full SelTournament didn't select the best individual")
}
}
func TestBuildWheel(t *testing.T) {
var testCases = []struct {
fitnesses []float64
weights []float64
}{
{[]float64{-10, -8, -5}, []float64{6.0 / 11, 10.0 / 11, 1}},
{[]float64{-2, 0, 2, 3}, []float64{6.0 / 13, 10.0 / 13, 12.0 / 13, 1}},
}
for _, test := range testCases {
var weights = buildWheel(test.fitnesses)
for i := range weights {
if weights[i] != test.weights[i] {
t.Error("buildWheel didn't work as expected")
}
}
}
}
func TestSelRoulette(t *testing.T) {
var (
rng = newRandomNumberGenerator()
indis = newIndividuals(30, NewVector, rng)
sel = SelRoulette{}
)
indis.Evaluate()
for _, n := range []int{0, 1, 10, 30} {
var selected, _, _ = sel.Apply(n, indis, rng)
if len(selected) != n {
t.Error("SelRoulette didn't select the right number of individuals")
}
}
}
// TestSelectorsValidate checks that each selector's Validate method doesn't
// return an error in case of a valid model and that it does for invalid models.
func TestSelectorsValidate(t *testing.T) {
// Check valid selectors do not raise an error
for _, sel := range validSelectors {
if sel.Validate() != nil {
t.Error("The selector validation should not have raised an error")
}
}
// Check invalid selectors raise an error
for _, sel := range invalidSelectors {
if sel.Validate() == nil {
t.Error("The selector validation should have raised error")
}
}
}