Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxHalford committed Apr 17, 2017
1 parent c2b48fc commit aa2285a
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 49 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ func (X Vector) Crossover(Y gago.Genome, rng *rand.Rand) (gago.Genome, gago.Geno
return Vector(o1), Vector(o2)
}

// MakeVector returns a random vector by generating 5 values uniformally
// MakeVector returns a random vector by generating 2 values uniformally
// distributed between -10 and 10.
func MakeVector(rng *rand.Rand) gago.Genome {
return Vector(gago.InitUnifFloat64(4, -10, 10, rng))
return Vector(gago.InitUnifFloat64(2, -10, 10, rng))
}

func main() {
Expand All @@ -137,16 +137,16 @@ func main() {
```

```sh
>>> Best fitness at generation 0: -0.119248
>>> Best fitness at generation 1: -0.286210
>>> Best fitness at generation 2: -0.286210
>>> Best fitness at generation 3: -0.513424
>>> Best fitness at generation 4: -0.513424
>>> Best fitness at generation 5: -0.821705
>>> Best fitness at generation 6: -0.980396
>>> Best fitness at generation 7: -0.999257
>>> Best fitness at generation 8: -0.999257
>>> Best fitness at generation 9: -0.999257
>>> Best fitness at generation 0: -0.550982
>>> Best fitness at generation 1: -0.924220
>>> Best fitness at generation 2: -0.987282
>>> Best fitness at generation 3: -0.987282
>>> Best fitness at generation 4: -0.987282
>>> Best fitness at generation 5: -0.987282
>>> Best fitness at generation 6: -0.987282
>>> Best fitness at generation 7: -0.997961
>>> Best fitness at generation 8: -0.999954
>>> Best fitness at generation 9: -0.999995
```

**More examples**
Expand Down
4 changes: 0 additions & 4 deletions distance.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ func (dm *DistanceMemoizer) GetDistance(a, b Individual) float64 {
}
if _, ok := dm.Distances[b.ID]; !ok {
dm.Distances[b.ID] = make(map[string]float64)
} else {
if dist, ok := dm.Distances[b.ID][a.ID]; ok {
return dist
}
}
// Calculate the distance between the genomes and memoize it
var dist = dm.Metric(a, b)
Expand Down
4 changes: 2 additions & 2 deletions individual.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func (indi Individual) IdxOfClosest(indis Individuals, dm DistanceMemoizer) (i i
// be called declaratively.
type Individuals []Individual

// Clone returns the same exact same slice of individuals but with a different
// pointer.
// Clone returns the same exact same slice of individuals but with different
// pointers and ID fields.
func (indis Individuals) Clone(rng *rand.Rand) Individuals {
var clones = make(Individuals, len(indis))
for i, indi := range indis {
Expand Down
17 changes: 16 additions & 1 deletion individual_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
)

func TestDeepCopyIndividual(t *testing.T) {
func TestCloneIndividual(t *testing.T) {
var (
rng = makeRandomNumberGenerator()
genome = MakeVector(rng)
Expand Down Expand Up @@ -71,6 +71,21 @@ func TestMakeIndividuals(t *testing.T) {
}
}

func TestCloneIndividuals(t *testing.T) {
var (
rng = makeRandomNumberGenerator()
indis = makeIndividuals(20, MakeVector, rng)
clones = indis.Clone(rng)
)
for _, indi := range indis {
for _, clone := range clones {
if &indi == &clone || indi.ID == clone.ID {
t.Error("Cloning did not work as expected")
}
}
}
}

func TestEvaluateIndividuals(t *testing.T) {
var indis = makeIndividuals(10, MakeVector, makeRandomNumberGenerator())
for _, indi := range indis {
Expand Down
12 changes: 12 additions & 0 deletions migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,15 @@ func TestMigSizes(t *testing.T) {
}
}
}

func TestMigRingValidate(t *testing.T) {
var mig = MigRing{1}
if err := mig.Validate(); err != nil {
t.Error("Validation should not have raised error")
}
// Set NMigrants lower than 1
mig.NMigrants = 0
if err := mig.Validate(); err == nil {
t.Error("Validation should raised error")
}
}
16 changes: 16 additions & 0 deletions mutate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ func TestMutUniformString(t *testing.T) {
}
}

func TestMutPermuteSingleGene(t *testing.T) {
var (
rng = makeRandomNumberGenerator()
genome = []int{42}
)
MutPermuteInt(genome, 1, rng)
// Check the length of the mutated genome
if len(genome) != 1 {
t.Error("Mutated genome has the wrong length")
}
// Check the value of the mutated genome
for genome[0] != 42 {
t.Error("Mutated genome has the wrong value")
}
}

func TestMutPermuteInt(t *testing.T) {
var (
rng = makeRandomNumberGenerator()
Expand Down
13 changes: 8 additions & 5 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ package gago

import "testing"

func TestFindIndex(t *testing.T) {
func TestSearch(t *testing.T) {
var s = StringSlice{"イ", "ー", "ス", "タ", "ー"}
if i, err := search("イ", s); i != 0 || err != nil {
t.Error("Problem with search")
t.Error("Problem with search 1")
}
if i, err := search("ー", s); i != 1 || err != nil {
t.Error("Problem with search")
t.Error("Problem with search 2")
}
if i, err := search("ス", s); i != 2 || err != nil {
t.Error("Problem with search")
t.Error("Problem with search 3")
}
if i, err := search("タ", s); i != 3 || err != nil {
t.Error("Problem with search")
t.Error("Problem with search 4")
}
if _, err := search("|", s); err == nil {
t.Error("Problem with search 5")
}
}

Expand Down
62 changes: 37 additions & 25 deletions speciation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
)

func TestSpecKMedoids(t *testing.T) {
func TestSpecKMedoidsApply(t *testing.T) {
// Example dataset from https://www.wikiwand.com/en/K-medoids
var (
rng = makeRandomNumberGenerator()
Expand Down Expand Up @@ -36,7 +36,31 @@ func TestSpecKMedoids(t *testing.T) {
}
}

func TestSpecFitnessInterval(t *testing.T) {
func TestSpecKMedoidsValidate(t *testing.T) {
var spec = SpecKMedoids{2, l1Distance, 1}
if err := spec.Validate(); err != nil {
t.Error("Validation should not have raised error")
}
// Set K lower than 2
spec.K = 1
if err := spec.Validate(); err == nil {
t.Error("Validation should have raised error")
}
spec.K = 2
// Nullify Metric
spec.Metric = nil
if err := spec.Validate(); err == nil {
t.Error("Validation should have raised error")
}
spec.Metric = l1Distance
// Set MaxIterations lower than 1
spec.MaxIterations = 0
if err := spec.Validate(); err == nil {
t.Error("Validation should have raised error")
}
}

func TestSpecFitnessIntervalApply(t *testing.T) {
var (
nIndividuals = []int{1, 2, 3}
nSpecies = []int{1, 2, 3}
Expand Down Expand Up @@ -65,26 +89,14 @@ func TestSpecFitnessInterval(t *testing.T) {
}
}

// func TestSpeciationMergeIndividuals(t *testing.T) {
// var (
// nbrIndividuals = []int{1, 2, 3}
// nbrClusters = []int{1, 2, 3}
// rng = makeRandomNumberGenerator()
// )
// for _, nbi := range nbrIndividuals {
// for _, nbc := range nbrClusters {
// var species = make(Populations, nbc)
// // Fill the species with individuals
// for i := 0; i < nbc; i++ {
// species[i] = Population{
// Individuals: makeIndividuals(nbi, MakeVector, rng),
// }
// }
// var indis = species.mergeIndividuals()
// // Check the species of individuals
// if len(indis) != nbi*nbc {
// t.Error("Merge didn't work properly")
// }
// }
// }
// }
func TestSpecFitnessIntervalValidate(t *testing.T) {
var spec = SpecFitnessInterval{2}
if err := spec.Validate(); err != nil {
t.Error("Validation should not have raised error")
}
// Set K lower than 2
spec.K = 1
if err := spec.Validate(); err == nil {
t.Error("Validation should have raised error")
}
}

0 comments on commit aa2285a

Please sign in to comment.