forked from tleyden/neurvolve
/
population_trainer_test.go
201 lines (153 loc) · 5.1 KB
/
population_trainer_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package neurvolve
import (
"github.com/couchbaselabs/go.assert"
"github.com/couchbaselabs/logg"
ng "github.com/maxxk/neurgo"
"testing"
)
func init() {
logg.LogKeys["MAIN"] = true
logg.LogKeys["DEBUG"] = false
logg.LogKeys["TEST"] = true
logg.LogKeys["NEURGO"] = false
logg.LogKeys["SENSOR_SYNC"] = false
logg.LogKeys["ACTUATOR_SYNC"] = false
logg.LogKeys["NODE_PRE_SEND"] = false
logg.LogKeys["NODE_POST_SEND"] = false
logg.LogKeys["NODE_POST_RECV"] = false
logg.LogKeys["NODE_STATE"] = false
}
func TestTrain(t *testing.T) {
fakeCortexMutator := func(cortex *ng.Cortex) (success bool, result MutateResult) {
for _, neuron := range cortex.Neurons {
neuron.Bias += 1
}
result = "nothing"
success = true
return
}
pt := &PopulationTrainer{
FitnessThreshold: 1000,
MaxGenerations: 1000000,
CortexMutator: fakeCortexMutator,
NumOpponents: 0,
}
cortex1 := SingleNeuronCortex("cortex1")
cortex2 := SingleNeuronCortex("cortex2")
population := []*ng.Cortex{cortex1, cortex2}
// inputs + expected outputs
examples := []*ng.TrainingSample{
{SampleInputs: [][]float64{[]float64{1}},
ExpectedOutputs: [][]float64{[]float64{100}}},
}
scape := FakeScapeTwoPlayer{
examples: examples,
}
recorder := NewNullRecorder()
trainedPopulation, succeeded := pt.Train(population, scape, recorder)
logg.LogTo("TEST", "succeeded: %v", succeeded)
logg.LogTo("TEST", "trainedPopulation: %v", trainedPopulation)
}
type FakeScapeTwoPlayer struct {
examples []*ng.TrainingSample
}
func (scape FakeScapeTwoPlayer) FitnessAgainst(cortex *ng.Cortex, opponent *ng.Cortex) float64 {
return 0.0
}
func (scape FakeScapeTwoPlayer) Fitness(cortex *ng.Cortex) float64 {
cortexFitness := cortex.Fitness(scape.examples)
return cortexFitness
}
func TestChooseRandomOpponents(t *testing.T) {
pt := &PopulationTrainer{}
cortex := BasicCortex()
evaldCortex := EvaluatedCortex{
Cortex: cortex,
Fitness: 0.0,
}
opponent := BasicCortex()
fitOpponent := EvaluatedCortex{
Cortex: opponent,
Fitness: 0.0,
}
population := []EvaluatedCortex{evaldCortex, fitOpponent}
opponents := pt.chooseRandomOpponents(cortex, population, 1)
assert.Equals(t, len(opponents), 1)
assert.Equals(t, opponents[0], opponent)
}
func TestSortByFitness(t *testing.T) {
pt := &PopulationTrainer{}
evaldCortexHigh := EvaluatedCortex{Fitness: 100.0}
evaldCortexLow := EvaluatedCortex{Fitness: -100.0}
population := []EvaluatedCortex{evaldCortexLow, evaldCortexHigh}
sortedPopulation := pt.sortByFitness(population)
assert.Equals(t, len(population), len(sortedPopulation))
assert.Equals(t, sortedPopulation[0], evaldCortexHigh)
assert.Equals(t, sortedPopulation[1], evaldCortexLow)
}
func TestCullPopulation(t *testing.T) {
evaldCortexHigh := EvaluatedCortex{Fitness: 100.0}
evaldCortexLow := EvaluatedCortex{Fitness: -100.0}
population := []EvaluatedCortex{evaldCortexLow, evaldCortexHigh}
pt := &PopulationTrainer{}
culledPopulation := pt.cullPopulation(population)
assert.Equals(t, len(culledPopulation), 1)
assert.Equals(t, culledPopulation[0], evaldCortexHigh)
}
func TestGenerateOffspring(t *testing.T) {
fakeCortexMutator := func(cortex *ng.Cortex) (success bool, result MutateResult) {
cortex.SetSensors(make([]*ng.Sensor, 0))
result = "nothing"
success = true
return
}
pt := &PopulationTrainer{
CortexMutator: fakeCortexMutator,
}
cortex1 := BasicCortex()
cortex2 := BasicCortex()
evaldCortex1 := EvaluatedCortex{Fitness: 100.0, Cortex: cortex1}
evaldCortex2 := EvaluatedCortex{Fitness: -100.0, Cortex: cortex2}
population := []EvaluatedCortex{evaldCortex1, evaldCortex2}
offspringPopulation := pt.generateOffspring(population)
assert.Equals(t, len(offspringPopulation), 2*len(population))
offspringEvaluatedCortex := offspringPopulation[3]
assert.Equals(t, offspringEvaluatedCortex.Fitness, 0.0)
assert.Equals(t, len(offspringEvaluatedCortex.Cortex.Sensors), 0)
}
// Regression test for issue in which evaldcortexes didn't have correct parents
func TestRetainParent(t *testing.T) {
fakeCortexMutator := func(cortex *ng.Cortex) (success bool, result MutateResult) {
for _, neuron := range cortex.Neurons {
neuron.Bias += 1
}
result = "nothing"
success = true
return
}
pt := &PopulationTrainer{
FitnessThreshold: 1000,
MaxGenerations: 1000000,
CortexMutator: fakeCortexMutator,
NumOpponents: 0,
}
cortex1 := SingleNeuronCortex("cortex1")
cortexes := []*ng.Cortex{cortex1}
population := pt.addEmptyFitnessScores(cortexes)
// at this point, all evaldcortexes should have parentid == self
for _, evaldCortex := range population {
assert.Equals(t, evaldCortex.ParentId, evaldCortex.Cortex.NodeId.UUID)
}
scape := FakeScapeTwoPlayer{}
population = pt.generateOffspring(population)
// all evaldcortexes should have parentid == "cortex1"
for _, evaldCortex := range population {
assert.Equals(t, evaldCortex.ParentId, "cortex1")
}
recorder := NullRecorder{}
population = pt.computeFitness(population, scape, recorder)
// all evaldcortexes should have parentid == "cortex1"
for _, evaldCortex := range population {
assert.Equals(t, evaldCortex.ParentId, "cortex1")
}
}