-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathevolution_strategies_test.go
64 lines (61 loc) · 1.67 KB
/
evolution_strategies_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
package hego
import (
"testing"
)
func TestVerifyESSettings(t *testing.T) {
settings := ESSettings{}
err := settings.Verify()
if err == nil {
t.Error("expected es settings verification to fail with no custom settings")
}
settings.LearningRate = 0.1
err = settings.Verify()
if err == nil {
t.Error("expected es settings verification to fail without population size set")
}
settings.PopulationSize = 10
err = settings.Verify()
if err == nil {
t.Error("expected es settings verification to fail without sigma set")
}
settings.NoiseSigma = 0.5
err = settings.Verify()
if err != nil {
t.Error("settings verification should pass with valid lr, size, sigma")
}
}
func TestES(t *testing.T) {
f := func(x []float64) float64 {
return x[0] * x[0]
}
x0 := []float64{10.0}
settings := ESSettings{}
_, err := ES(f, x0, settings)
if err == nil {
t.Error("ES should fail with invalid settings")
}
settings.MaxIterations = 10
settings.Verbose = 10
settings.LearningRate = 1.0
settings.NoiseSigma = 0.1
settings.PopulationSize = 10
res, err := ES(f, x0, settings)
if err != nil {
t.Errorf("Unexpected error in ES algorithm: %v", err)
}
best := res.BestCandidate
if best[0] > 0.5 || best[0] < -0.5 {
t.Errorf("ES algorithm produced unexpected result. Wanted ~0.0, got %v", best[0])
}
if len(res.BestObjectives) != 0 {
t.Errorf("didn't expect best objectives to contain values, got %v values", len(res.BestObjectives))
}
settings.KeepHistory = true
res, err = ES(f, x0, settings)
if err != nil {
t.Errorf("Unexpected error in ES algorithm: %v", err)
}
if len(res.BestObjectives) == 0 {
t.Error("with KeepHistory set, bestObjectives should contain values")
}
}