-
Notifications
You must be signed in to change notification settings - Fork 96
/
speciation_test.go
144 lines (139 loc) · 3.68 KB
/
speciation_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
package eaopt
import (
"errors"
"fmt"
"testing"
)
func TestSpecKMedoidsApply(t *testing.T) {
var (
rng = newRand()
testCases = []struct {
indis Individuals
kmeds SpecKMedoids
speciesSizes []int
err error
}{
// Example dataset from https://www.wikiwand.com/en/K-medoids
{
indis: Individuals{
NewIndividual(Vector{2, 6}, rng),
NewIndividual(Vector{3, 4}, rng),
NewIndividual(Vector{3, 8}, rng),
NewIndividual(Vector{4, 7}, rng),
NewIndividual(Vector{6, 2}, rng),
NewIndividual(Vector{6, 4}, rng),
NewIndividual(Vector{7, 3}, rng),
NewIndividual(Vector{7, 4}, rng),
NewIndividual(Vector{8, 5}, rng),
NewIndividual(Vector{7, 6}, rng),
},
kmeds: SpecKMedoids{2, 1, l1Distance, 10},
speciesSizes: []int{4, 6},
err: nil,
},
{
indis: Individuals{
NewIndividual(Vector{1, 1}, rng),
NewIndividual(Vector{1, 1}, rng),
},
kmeds: SpecKMedoids{2, 1, l1Distance, 10},
speciesSizes: []int{1, 1},
err: nil,
},
{
indis: Individuals{
NewIndividual(Vector{1, 1}, rng),
NewIndividual(Vector{1, 2}, rng),
},
kmeds: SpecKMedoids{3, 1, l1Distance, 10},
speciesSizes: []int{1, 1},
err: errors.New("K > len(indis)"),
},
}
)
for i, tc := range testCases {
t.Run(fmt.Sprintf("TC %d", i), func(t *testing.T) {
var species, err = tc.kmeds.Apply(tc.indis, rng)
// Check the number of species is correct
if err == nil && len(species) != int(tc.kmeds.K) {
t.Error("Wrong number of species")
}
// Check size of each specie
if err == nil {
for j, specie := range species {
if len(specie) != tc.speciesSizes[j] {
t.Error("Wrong specie size")
}
}
}
// Check error is nil or not
if (err == nil) != (tc.err == nil) {
if err.Error() != "missing 0 individuals to be able to rebalance the clusters" {
t.Error("Wrong error")
}
}
})
}
}
func TestSpecKMedoidsValidate(t *testing.T) {
var spec = SpecKMedoids{2, 1, l1Distance, 1}
if err := spec.Validate(); err != nil {
t.Error("Validation should not have raised error")
}
// Set K lower than 2
spec.K = 1
if err := spec.Validate(); err == nil {
t.Error("Validation should have raised error")
}
spec.K = 2
// Nullify Metric
spec.Metric = nil
if err := spec.Validate(); err == nil {
t.Error("Validation should have raised error")
}
spec.Metric = l1Distance
// Set MaxIterations lower than 1
spec.MaxIterations = 0
if err := spec.Validate(); err == nil {
t.Error("Validation should have raised error")
}
}
func TestSpecFitnessIntervalApply(t *testing.T) {
var (
nIndividuals = []uint{1, 2, 3}
nSpecies = []uint{1, 2, 3}
rng = newRand()
)
for _, nbi := range nIndividuals {
for _, nbs := range nSpecies {
var (
m = minInt(int(float64(nbi/nbs)), int(nbi))
indis = newIndividuals(nbi, false, NewVector, rng)
spec = SpecFitnessInterval{K: nbs}
species, _ = spec.Apply(indis, rng)
)
// Check the cluster sizes are equal to min(n-i, m) where i is a
// multiple of m
for i, specie := range species {
var (
expected = minInt(int(nbi)-i*m, m)
obtained = len(specie)
)
if obtained != expected {
t.Errorf("Wrong number of individuals, expected %d got %d", expected, obtained)
}
}
}
}
}
func TestSpecFitnessIntervalValidate(t *testing.T) {
var spec = SpecFitnessInterval{2}
if err := spec.Validate(); err != nil {
t.Error("Validation should not have raised error")
}
// Set K lower than 2
spec.K = 1
if err := spec.Validate(); err == nil {
t.Error("Validation should have raised error")
}
}