This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
forked from MaxHalford/eaopt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff_evo_test.go
115 lines (106 loc) · 2.68 KB
/
diff_evo_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
package eaopt
import (
"fmt"
"math"
"math/rand"
"reflect"
"testing"
)
func ExampleDiffEvo() {
// Instantiate DiffEvo
var de, err = NewDefaultDiffEvo()
if err != nil {
fmt.Println(err)
return
}
// Fix random number generation
de.GA.RNG = rand.New(rand.NewSource(42))
// Define function to minimize
var ackley = func(x []float64) float64 {
var (
a, b, c = 20.0, 0.2, 2 * math.Pi
s1, s2 float64
d = float64(len(x))
)
for _, xi := range x {
s1 += xi * xi
s2 += math.Cos(c * xi)
}
return -a*math.Exp(-b*math.Sqrt(s1/d)) - math.Exp(s2/d) + a + math.Exp(1)
}
// Run minimization
x, y, err := de.Minimize(ackley, 2)
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.00137 in [0.0004420129693826938 0.000195924625132926]
}
func TestAgentCrossover(t *testing.T) {
var de, err = NewDefaultDiffEvo()
if err != nil {
t.Errorf("Expected nil, got %v", err)
}
de.NDims = 2
var (
rng = newRand()
p1 = de.newAgent(rng).(*Agent)
p2 = de.newAgent(rng).(*Agent)
p1c = p1.Clone().(*Agent)
p2c = p2.Clone().(*Agent)
)
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 TestNewDiffEvo(t *testing.T) {
var testCases = []struct {
f func() error
}{
{func() error { _, err := NewDiffEvo(0, 30, -5, 5, 0.5, 0.2, false, nil); return err }},
{func() error { _, err := NewDiffEvo(1, 30, -5, 5, 0.5, 0.2, false, nil); return err }},
{func() error { _, err := NewDiffEvo(2, 30, -5, 5, 0.5, 0.2, false, nil); return err }},
{func() error { _, err := NewDiffEvo(3, 30, -5, 5, 0.5, 0.2, false, nil); return err }},
{func() error { _, err := NewDiffEvo(40, 0, -5, 5, 0.5, 0.2, false, nil); return err }},
{func() error { _, err := NewDiffEvo(40, 30, 5, -5, 0.5, 0.2, 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 TestNewDefaultDiffEvo(t *testing.T) {
var de, err = NewDefaultDiffEvo()
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 = de.Minimize(bowl, 2); err != nil {
t.Errorf("Expected nil, got %v", err)
}
}