Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxHalford committed Aug 3, 2018
1 parent fb3ba45 commit 2229c08
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 2 deletions.
70 changes: 70 additions & 0 deletions diff_evo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package eaopt
import (
"fmt"
"math/rand"
"reflect"
"testing"
)

func ExampleDiffEvo() {
Expand Down Expand Up @@ -34,3 +36,71 @@ func ExampleDiffEvo() {
// Output:
// Found minimum of 0.00000 in [6.0034503946953274e-05 0.00013615643701126828]
}

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)
}
}
2 changes: 1 addition & 1 deletion distance.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func newDistanceMemoizer(metric Metric) DistanceMemoizer {
// so that if GetDistance is called twice with the two same Individuals then
// the second call will return the stored distance instead of recomputing it.
func (dm *DistanceMemoizer) GetDistance(a, b Individual) float64 {
// Check if the two individuals are the same before proceding
// Check if the two individuals are the same before proceeding
if a.ID == b.ID {
return 0
}
Expand Down
1 change: 1 addition & 0 deletions pso.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func (pso *SPSO) newParticle(rng *rand.Rand) Genome {
return &Particle{
CurrentX: X,
BestX: copyFloat64s(X),
BestY: math.Inf(1),
SPSO: pso,
Velocity: velocity,
}
Expand Down
70 changes: 69 additions & 1 deletion pso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package eaopt
import (
"fmt"
"math/rand"
"reflect"
"testing"
)

func ExampleSPSO() {
Expand Down Expand Up @@ -32,5 +34,71 @@ func ExampleSPSO() {
// Output best encountered solution
fmt.Printf("Found minimum of %.5f in %v\n", y, X)
// Output:
// Found minimum of 0.00256 in [0.032498335711881876 -0.03878537756373712]
// Found minimum of 0.00006 in [0.005257773093598095 -0.005956339175848813]
}

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)
}
}

0 comments on commit 2229c08

Please sign in to comment.