forked from MaxHalford/eaopt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oes_test.go
109 lines (100 loc) · 2.4 KB
/
oes_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
package eaopt
import (
"fmt"
"math"
"math/rand"
"reflect"
"testing"
)
func ExampleOES() {
// Instantiate DiffEvo
var oes, err = NewDefaultOES()
if err != nil {
fmt.Println(err)
return
}
// Fix random number generation
oes.GA.RNG = rand.New(rand.NewSource(42))
// Define function to minimize
var rastrigin = func(x []float64) (y float64) {
y = 10 * float64(len(x))
for _, xi := range x {
y += math.Pow(xi, 2) - 10*math.Cos(2*math.Pi*xi)
}
return y
}
// Run minimization
X, y, err := oes.Minimize(rastrigin, []float64{0, 0})
if err != nil {
fmt.Println(err)
return
}
// Output best encountered solution
fmt.Printf("Found minimum of %.5f in %v\n", y, X)
// Output:
// Found minimum of 0.02270 in [0.006807861794722094 -0.008251984117745246]
}
func TestPointCrossover(t *testing.T) {
var oes, err = NewDefaultOES()
if err != nil {
t.Errorf("Expected nil, got %v", err)
}
oes.Mu = []float64{1, 1}
var (
rng = newRand()
p1 = oes.newPoint(rng).(*oesPoint)
p2 = oes.newPoint(rng).(*oesPoint)
p1c = p1.Clone().(*oesPoint)
p2c = p2.Clone().(*oesPoint)
)
if reflect.DeepEqual(p1.x, p2.x) {
t.Errorf("Expected mismatch")
}
if !reflect.DeepEqual(p1.x, p1c.x) {
t.Errorf("Expected no mismatch")
}
if !reflect.DeepEqual(p2.x, p2c.x) {
t.Errorf("Expected no mismatch")
}
p1.Crossover(p2, rng)
if !reflect.DeepEqual(p1.x, p1c.x) {
t.Errorf("Expected no mismatch")
}
if !reflect.DeepEqual(p2.x, p2c.x) {
t.Errorf("Expected no mismatch")
}
}
func TestNewOES(t *testing.T) {
var testCases = []struct {
f func() error
}{
{func() error { _, err := NewOES(2, 30, 1, 0.1, false, nil); return err }},
{func() error { _, err := NewOES(100, 0, 1, 0.1, false, nil); return err }},
{func() error { _, err := NewOES(100, 30, 0, 0.1, false, nil); return err }},
{func() error { _, err := NewOES(100, 30, 1, 0, false, nil); return err }},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("TC %d", i), func(t *testing.T) {
var err = tc.f()
if err == nil {
t.Errorf("Expected error, got nil")
}
})
}
}
func TestNewDefaultOES(t *testing.T) {
var oes, err = NewDefaultOES()
oes.GA.ParallelEval = true
if err != nil {
t.Errorf("Expected nil, got %v", err)
}
var bowl = func(X []float64) (y float64) {
for _, x := range X {
y += x * x
}
return
}
if _, _, err = oes.Minimize(bowl, []float64{5, 5}); err != nil {
t.Errorf("Expected nil, got %v", err)
}
}