This repository has been archived by the owner on Aug 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
individual_test.go
96 lines (90 loc) · 2.05 KB
/
individual_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
package gago
import (
"fmt"
"testing"
)
func TestIndividualString(t *testing.T) {
var testCases = []struct {
indi Individual
str string
}{
{
indi: Individual{
Genome: Vector{0, 1, 2},
Fitness: 42,
Evaluated: true,
ID: "bob",
},
str: "bob - 42.000 - [0 1 2]",
},
{
indi: Individual{
Genome: Vector{0, 1, 2},
Fitness: 42,
Evaluated: false,
ID: "ALICE",
},
str: "ALICE - ??? - [0 1 2]",
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("TC %d", i), func(t *testing.T) {
if tc.indi.String() != tc.str {
t.Errorf("Expected %s, got %s", tc.str, tc.indi.String())
}
})
}
}
func TestCloneIndividual(t *testing.T) {
var (
rng = newRand()
genome = NewVector(rng)
indi1 = NewIndividual(genome, rng)
indi2 = indi1.Clone(rng)
)
if &indi1 == &indi2 || &indi1.Genome == &indi2.Genome {
t.Error("Individual was not deep copied")
}
}
func TestEvaluateIndividual(t *testing.T) {
var (
rng = newRand()
genome = NewVector(rng)
indi = NewIndividual(genome, rng)
)
if indi.Evaluated {
t.Error("Individual shouldn't have Evaluated set to True")
}
indi.Evaluate()
if !indi.Evaluated {
t.Error("Individual should have Evaluated set to True")
}
}
func TestMutateIndividual(t *testing.T) {
var (
rng = newRand()
genome = NewVector(rng)
indi = NewIndividual(genome, rng)
)
indi.Evaluate()
indi.Mutate(rng)
if indi.Evaluated {
t.Error("Individual shouldn't have Evaluated set to True")
}
}
func TestCrossoverIndividual(t *testing.T) {
var (
rng = newRand()
indi1 = NewIndividual(NewVector(rng), rng)
indi2 = NewIndividual(NewVector(rng), rng)
offspring1 = indi1.Clone(rng)
offspring2 = indi2.Clone(rng)
)
indi1.Crossover(indi2, rng)
if offspring1.Evaluated || offspring2.Evaluated {
t.Error("Offsprings shouldn't have Evaluated set to True")
}
if &offspring1 == &indi1 || &offspring1 == &indi2 || &offspring2 == &indi1 || &offspring2 == &indi2 {
t.Error("Offsprings shouldn't share pointers with parents")
}
}