-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgenetic_test.go
73 lines (70 loc) · 1.88 KB
/
genetic_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
package evoli
import (
"testing"
)
// To be completed
func TestNewGenetic(t *testing.T) {
cases := []struct {
s Selecter
survivorSize int
c Crosser
m Mutater
mutaionProb float64
e Evaluater
}{
{NewTruncationSelecter(), 10, crosserMock{}, mutaterMock{}, 0.01, evaluaterMock{}},
}
for _, c := range cases {
i1 := NewIndividual(1)
i2 := NewIndividual(2)
popInit := NewPopulation(2)
newPop := NewPopulation(1)
popInit.Add(i1, i2)
gen := NewGenetic(popInit, c.s, c.survivorSize, c.c, c.m, c.mutaionProb, c.e)
pop := gen.Population()
if pop != popInit {
t.Errorf("expected %v got %v", popInit, pop)
}
alpha := gen.Alpha()
if alpha != i2 {
t.Errorf("expected %v got %v", i2, alpha)
}
gen.SetPopulation(newPop)
pop = gen.Population()
if pop != newPop {
t.Errorf("expected %v got %v", newPop, pop)
}
gen = NewGeneticSync(popInit, c.s, c.survivorSize, c.c, c.m, c.mutaionProb, c.e)
pop = gen.Population()
if pop != popInit {
t.Errorf("expected %v got %v", popInit, pop)
}
alpha = gen.Alpha()
if alpha != i2 {
t.Errorf("expected %v got %v", i2, alpha)
}
gen.SetPopulation(newPop)
pop = gen.Population()
if pop != newPop {
t.Errorf("expected %v got %v", newPop, pop)
}
}
}
// To be completed
func TestGeneticNext(t *testing.T) {
i1, i2, i3, i4, i5, i6 := NewIndividual(1), NewIndividual(-2), NewIndividual(3), NewIndividual(4), NewIndividual(5), NewIndividual(6)
pop1 := population{i1, i2, i3, i4, i5, i6}
pop2 := population{i1, i2, i3, i4, i5, i6}
cases := []struct {
genetic Evolution
}{
{NewGenetic(&pop1, NewTruncationSelecter(), 5, crosserMock{}, mutaterMock{}, 1, evaluaterMock{})},
{NewGeneticSync(&pop2, NewTruncationSelecter(), 5, crosserMock{}, mutaterMock{}, 1, evaluaterMock{})},
}
for _, c := range cases {
err := c.genetic.Next()
if err != nil {
t.Error(err)
}
}
}