Skip to content
This repository was archived by the owner on Dec 11, 2018. It is now read-only.

Commit 7aa4ca4

Browse files
committed
progress
1 parent fb3daf6 commit 7aa4ca4

File tree

4 files changed

+64
-32
lines changed

4 files changed

+64
-32
lines changed

genome.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,35 @@ func (g *Genome) MutateAddConn(rate float64) {
272272
}
273273
}
274274

275-
newConn := NewConnGene(selectedNode0, selectedNode1, rand.NormFloat64()*6.0)
276-
g.ConnGenes = append(g.ConnGenes, newConn)
275+
if g.NodeGenes[selectedNode1].Type == "input" ||
276+
g.NodeGenes[selectedNode0].Type == "output" {
277+
return
278+
}
279+
280+
if !g.pathExists(selectedNode1, selectedNode0) {
281+
g.ConnGenes = append(g.ConnGenes, NewConnGene(selectedNode0,
282+
selectedNode1, rand.NormFloat64()*6.0))
283+
}
284+
285+
}
286+
}
287+
288+
// pathExists returns true if there is a path from the source to the
289+
// destination. Helper method of MutateAddConn.
290+
func (g *Genome) pathExists(src, dst int) bool {
291+
if src == dst {
292+
return true
293+
}
294+
295+
for _, edge := range g.ConnGenes {
296+
if edge.From == src {
297+
if g.pathExists(edge.To, dst) {
298+
return true
299+
}
300+
}
277301
}
302+
303+
return false
278304
}
279305

280306
// Crossover returns a new child genome by performing crossover between the two

neat.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func (n *NEAT) Reproduce() {
184184
// reproduction of this species is only executed, if there is enough room.
185185
if numSurvived > 2 && numEliminated > 0 {
186186
// adjust the fitness of each member genome of this species.
187-
s.ExplicitFitnessSharing()
187+
//s.ExplicitFitnessSharing()
188188

189189
sort.Slice(s.Members, func(i, j int) bool {
190190
return n.Comparison(s.Members[i], s.Members[j])
@@ -248,8 +248,8 @@ func (n *NEAT) randActivationFunc() *ActivationFunc {
248248
return n.Activations[rand.Intn(len(n.Activations))]
249249
}
250250

251-
// Run executes evolution.
252-
func (n *NEAT) Run() {
251+
// Run executes evolution and return the best genome.
252+
func (n *NEAT) Run() *Genome {
253253
if n.Config.Verbose {
254254
n.Config.Summarize()
255255
}
@@ -258,6 +258,18 @@ func (n *NEAT) Run() {
258258
for i := 0; i < n.Config.NumGenerations; i++ {
259259
n.Evaluate()
260260

261+
// update the best genome
262+
for _, genome := range n.Population {
263+
if n.Comparison(genome, n.Best) {
264+
n.Best = genome.Copy()
265+
}
266+
}
267+
268+
n.Statistics.Update(i, n)
269+
if n.Config.Verbose {
270+
n.Summarize(i)
271+
}
272+
261273
// speciate genomes and reproduce children genomes
262274
n.Speciate()
263275
n.Reproduce()
@@ -273,17 +285,7 @@ func (n *NEAT) Run() {
273285
}
274286
n.Species = survived
275287
}
276-
277-
// update the best genome
278-
for _, genome := range n.Population {
279-
if n.Comparison(genome, n.Best) {
280-
n.Best = genome.Copy()
281-
}
282-
}
283-
284-
n.Statistics.Update(i, n)
285-
if n.Config.Verbose {
286-
n.Summarize(i)
287-
}
288288
}
289+
290+
return n.Best
289291
}

neat_test.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ func NEATUnitTest() {
1818

1919
fmt.Println("\x1b[32m=Testing NEAT with XOR test...\x1b[0m")
2020
n0 := New(configXOR, XORTest())
21-
n0.Run()
21+
best := n0.Run()
2222

23-
nn := NewNeuralNetwork(n0.Best)
23+
nn := NewNeuralNetwork(best)
2424
output, _ := nn.FeedForward([]float64{1.0, 1.0, 1.0})
2525
fmt.Println(output)
2626
output, _ = nn.FeedForward([]float64{1.0, 0.0, 1.0})
@@ -30,19 +30,22 @@ func NEATUnitTest() {
3030
output, _ = nn.FeedForward([]float64{1.0, 0.0, 0.0})
3131
fmt.Println(output)
3232

33-
fmt.Println("\x1b[32m=Testing NEAT with pole balancing test...\x1b[0m")
34-
configPole, err := NewConfigJSON("config_pole_balancing.json")
35-
if err != nil {
36-
fmt.Println("\x1b[31mFAIL\x1b[0m")
37-
}
38-
configPole.Summarize()
39-
n1 := New(configPole, PoleBalancingTest(false, 120000))
40-
n1.Run()
41-
42-
fmt.Println("\x1b[32m=Testing NEAT with pole balancing (random)...\x1b[0m")
43-
configPole.Summarize()
44-
n2 := New(configPole, PoleBalancingTest(true, 120000))
45-
n2.Run()
33+
/*
34+
fmt.Println("\x1b[32m=Testing NEAT with pole balancing test...\x1b[0m")
35+
configPole, err := NewConfigJSON("config_pole_balancing.json")
36+
if err != nil {
37+
fmt.Println("\x1b[31mFAIL\x1b[0m")
38+
}
39+
configPole.Summarize()
40+
n1 := New(configPole, PoleBalancingTest(false, 120000))
41+
n1.Run()
42+
43+
fmt.Println("\x1b[32m=Testing NEAT with pole balancing (random)...\x1b[0m")
44+
configPole.Summarize()
45+
n2 := New(configPole, PoleBalancingTest(true, 120000))
46+
n2.Run()
47+
48+
*/
4649
}
4750

4851
func TestNEAT(t *testing.T) {

neural_network.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ func (n *NeuralNetwork) FeedForward(inputs []float64) ([]float64, error) {
156156

157157
// reset all neurons
158158
for _, neuron := range n.Neurons {
159+
neuron.Signal = 0.0
159160
neuron.activated = false
160161
}
161162

0 commit comments

Comments
 (0)