forked from tleyden/neurvolve
/
topology_mutating_trainer.go
111 lines (82 loc) · 2.72 KB
/
topology_mutating_trainer.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
package neurvolve
import (
"fmt"
"github.com/couchbaselabs/logg"
ng "github.com/maxxk/neurgo"
)
type TopologyMutatingTrainer struct {
MaxIterationsBeforeRestart int
MaxAttempts int
StochasticHillClimber *StochasticHillClimber
}
func (tmt *TopologyMutatingTrainer) Train(cortex *ng.Cortex, scape Scape) (fittestCortex *ng.Cortex, succeeded bool) {
ng.SeedRandom()
shc := tmt.StochasticHillClimber
includeNonTopological := false
mutators := CortexMutatorsNonRecurrent(includeNonTopological)
originalCortex := cortex.Copy()
currentCortex := cortex
// Apply NN to problem and save fitness
logg.LogTo("MAIN", "Get initial fitness")
fitness := scape.Fitness(currentCortex)
logg.LogTo("MAIN", "Initial fitness: %v", fitness)
if fitness > shc.FitnessThreshold {
succeeded = true
return
}
for i := 0; ; i++ {
logg.LogTo("MAIN", "Before mutate. i/max: %d/%d", i, tmt.MaxAttempts)
// before we mutate the cortex, we need to init it,
// otherwise things like Outsplice will fail because
// there are no DataChan's.
currentCortex.Init()
// mutate the network
randInt := RandomIntInRange(0, len(mutators))
mutator := mutators[randInt]
ok, _ := mutator(currentCortex)
if !ok {
logg.LogTo("MAIN", "Mutate didn't work, retrying...")
continue
}
isValid := currentCortex.Validate()
if !isValid {
logg.LogPanic("Cortex did not validate")
}
filenameJson := fmt.Sprintf("cortex-%v.json", i)
currentCortex.MarshalJSONToFile(filenameJson)
filenameSvg := fmt.Sprintf("cortex-%v.svg", i)
currentCortex.RenderSVGFile(filenameSvg)
logg.LogTo("MAIN", "Post mutate cortex svg: %v json: %v", filenameSvg, filenameJson)
logg.LogTo("MAIN", "Run stochastic hill climber..")
// memetic step: call stochastic hill climber and see if it can solve it
fittestCortex, _, succeeded = shc.Train(currentCortex, scape)
logg.LogTo("MAIN", "stochastic hill climber finished. succeeded: %v", succeeded)
if succeeded {
succeeded = true
break
}
if i >= tmt.MaxAttempts {
succeeded = false
break
}
if ng.IntModuloProper(i, tmt.MaxIterationsBeforeRestart) {
logg.LogTo("MAIN", "** Restart . i/max: %d/%d", i, tmt.MaxAttempts)
currentCortex = originalCortex.Copy()
isValid := currentCortex.Validate()
if !isValid {
currentCortex.Repair() // TODO: remove workaround
isValid = currentCortex.Validate()
if !isValid {
logg.LogPanic("Cortex could not be repaired")
}
}
}
}
return
}
func (tmt *TopologyMutatingTrainer) TrainExamples(cortex *ng.Cortex, examples []*ng.TrainingSample) (fittestCortex *ng.Cortex, succeeded bool) {
trainingSampleScape := &TrainingSampleScape{
examples: examples,
}
return tmt.Train(cortex, trainingSampleScape)
}