-
Notifications
You must be signed in to change notification settings - Fork 96
/
pso_test.go
107 lines (98 loc) · 2.38 KB
/
pso_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
package eaopt
import (
"fmt"
"math"
"math/rand"
"reflect"
"testing"
)
func ExampleSPSO() {
// Instantiate SPSO
var spso, err = NewDefaultSPSO()
if err != nil {
fmt.Println(err)
return
}
// Fix random number generation
spso.GA.RNG = rand.New(rand.NewSource(42))
// Define function to minimize
var styblinskiTang = func(x []float64) (y float64) {
for _, xi := range x {
y += math.Pow(xi, 4) - 16*math.Pow(xi, 2) + 5*xi
}
return 0.5 * y
}
// Run minimization
x, y, err := spso.Minimize(styblinskiTang, 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 -78.23783 in [-2.8586916496046983 -2.9619895273744623]
}
func TestParticleCrossover(t *testing.T) {
var spso, err = NewDefaultSPSO()
if err != nil {
t.Errorf("Expected nil, got %v", err)
}
spso.NDims = 2
var (
rng = newRand()
p1 = spso.newParticle(rng).(*Particle)
p2 = spso.newParticle(rng).(*Particle)
p1c = p1.Clone().(*Particle)
p2c = p2.Clone().(*Particle)
)
if reflect.DeepEqual(p1.CurrentX, p2.CurrentX) {
t.Errorf("Expected mismatch")
}
if !reflect.DeepEqual(p1.CurrentX, p1c.CurrentX) {
t.Errorf("Expected no mismatch")
}
if !reflect.DeepEqual(p2.CurrentX, p2c.CurrentX) {
t.Errorf("Expected no mismatch")
}
p1.Crossover(p2, rng)
if !reflect.DeepEqual(p1.CurrentX, p1c.CurrentX) {
t.Errorf("Expected no mismatch")
}
if !reflect.DeepEqual(p2.CurrentX, p2c.CurrentX) {
t.Errorf("Expected no mismatch")
}
}
func TestNewSPSO(t *testing.T) {
var testCases = []struct {
f func() error
}{
{func() error { _, err := NewSPSO(0, 30, -5, 5, 0.5, false, nil); return err }},
{func() error { _, err := NewSPSO(40, 0, -5, 5, 0.5, false, nil); return err }},
{func() error { _, err := NewSPSO(40, 30, 5, -5, 0.5, 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 TestNewDefaultSPSO(t *testing.T) {
var spso, err = NewDefaultSPSO()
spso.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 = spso.Minimize(bowl, 2); err != nil {
t.Errorf("Expected nil, got %v", err)
}
}