Skip to content

Commit c832e3d

Browse files
committed
Improved error handling and formatting.
1 parent 09922a2 commit c832e3d

File tree

6 files changed

+242
-226
lines changed

6 files changed

+242
-226
lines changed

experiments/maze/common.go

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"fmt"
66
"github.com/yaricom/goNEAT/neat"
77
"github.com/yaricom/goNEAT/neat/genetics"
8+
"github.com/yaricom/goNEAT/neat/network"
89
"github.com/yaricom/goNEAT_NS/neatns"
910
"math"
10-
"github.com/yaricom/goNEAT/neat/network"
1111
)
1212

1313
// The simulation results for one trial
@@ -16,111 +16,112 @@ var trialSim mazeSimResults
1616
// The structure to hold maze simulator evaluation results
1717
type mazeSimResults struct {
1818
// The record store for evaluated agents
19-
records *RecordStore
19+
records *RecordStore
2020
// The novelty archive
21-
archive *neatns.NoveltyArchive
21+
archive *neatns.NoveltyArchive
2222

2323
// The current trial
24-
trialID int
24+
trialID int
2525
// The evaluated individuals counter within current trial
26-
individCounter int
26+
individualsCounter int
2727
}
2828

2929
// calculates item-wise difference between two vectors
30-
func histDiff(in1, in2 []float64) float64 {
31-
size := len(in1)
32-
diff_accum := 0.0
30+
func histDiff(left, right []float64) float64 {
31+
size := len(left)
32+
diffAccum := 0.0
3333
for i := 0; i < size; i++ {
34-
diff := in1[i] - in2[i]
35-
diff_accum += math.Abs(diff)
34+
diff := left[i] - right[i]
35+
diffAccum += math.Abs(diff)
3636
}
37-
return diff_accum / float64(size)
37+
return diffAccum / float64(size)
3838
}
3939

40-
4140
// To evaluate an individual organism within provided maze environment and to create corresponding novelty point.
4241
// If maze was solved during simulation the second returned parameter will be true.
4342
func mazeSimulationEvaluate(env *Environment, org *genetics.Organism, record *AgentRecord, pathPoints []Point) (*neatns.NoveltyItem, bool, error) {
44-
n_item := neatns.NewNoveltyItem()
43+
nItem := neatns.NewNoveltyItem()
4544

4645
// initialize maze simulation's environment specific to the provided organism - this will be a copy
4746
// of primordial environment provided
48-
org_env, err := mazeSimulationInit(*env, org)
47+
orgEnv, err := mazeSimulationInit(*env, org)
4948
if err != nil {
5049
return nil, false, err
5150
}
5251

5352
// do specified amount of time steps emulations or while exit not found
5453
steps := 0
55-
for i := 0; i < org_env.TimeSteps && !org_env.ExitFound; i++ {
56-
err := mazeSimulationStep(org_env, org)
54+
for i := 0; i < orgEnv.TimeSteps && !orgEnv.ExitFound; i++ {
55+
err := mazeSimulationStep(orgEnv, org)
5756
if err != nil {
5857
return nil, false, err
5958
}
6059
// store agent path points at given sample size
61-
if (org_env.TimeSteps - i) % org_env.SampleSize == 0 {
62-
n_item.Data = append(n_item.Data, org_env.Hero.Location.X)
63-
n_item.Data = append(n_item.Data, org_env.Hero.Location.Y)
60+
if (orgEnv.TimeSteps-i)%orgEnv.SampleSize == 0 {
61+
nItem.Data = append(nItem.Data, orgEnv.Hero.Location.X)
62+
nItem.Data = append(nItem.Data, orgEnv.Hero.Location.Y)
6463
}
6564

6665
// store all path points if requested
6766
if pathPoints != nil {
68-
pathPoints[i] = org_env.Hero.Location
67+
pathPoints[i] = orgEnv.Hero.Location
6968
}
7069
steps++
7170
}
7271

73-
if org_env.ExitFound {
72+
if orgEnv.ExitFound {
7473
neat.InfoLog(fmt.Sprintf("Maze solved in: %d steps\n", steps))
7574
}
7675

7776
// calculate fitness of an organism as closeness to target
78-
fitness := org_env.AgentDistanceToExit()
77+
fitness := orgEnv.AgentDistanceToExit()
7978

8079
// normalize fitness value in range (0;1] and store it
8180
fitness = (env.initialDistance - fitness) / env.initialDistance
8281
if fitness <= 0 {
8382
fitness = 0.01
8483
}
8584

86-
n_item.Fitness = fitness
85+
nItem.Fitness = fitness
8786

8887
// store final agent coordinates as organism's novelty characteristics
89-
n_item.Data = append(n_item.Data, org_env.Hero.Location.X)
90-
n_item.Data = append(n_item.Data, org_env.Hero.Location.Y)
88+
nItem.Data = append(nItem.Data, orgEnv.Hero.Location.X)
89+
nItem.Data = append(nItem.Data, orgEnv.Hero.Location.Y)
9190

9291
if record != nil {
9392
record.Fitness = fitness
94-
record.X = org_env.Hero.Location.X
95-
record.Y = org_env.Hero.Location.Y
96-
record.GotExit = org_env.ExitFound
93+
record.X = orgEnv.Hero.Location.X
94+
record.Y = orgEnv.Hero.Location.Y
95+
record.GotExit = orgEnv.ExitFound
9796
}
9897

99-
return n_item, org_env.ExitFound, nil
98+
return nItem, orgEnv.ExitFound, nil
10099
}
101100

102-
103101
// To initialize the maze simulation within provided environment copy and for given organism.
104102
// Returns new environment for simulation against given organism
105103
func mazeSimulationInit(env Environment, org *genetics.Organism) (*Environment, error) {
106104

107105
// get Organism phenotype's network depth
108-
net_depth, err := org.Phenotype.MaxDepth() // The max depth of the network to be activated
106+
netDepth, err := org.Phenotype.MaxDepth() // The max depth of the network to be activated
109107
if err != nil {
110108
if err != network.NetErrDepthCalculationFailedLoopDetected {
111109
return nil, err
112110
} else {
113111
neat.InfoLog(fmt.Sprintf("Network loop detected, using default depth: %d for organism: %d\n",
114-
net_depth, org.Genotype.Id))
112+
netDepth, org.Genotype.Id))
115113
}
116114
}
117-
neat.DebugLog(fmt.Sprintf("Network depth: %d for organism: %d\n", net_depth, org.Genotype.Id))
118-
if net_depth == 0 {
115+
neat.DebugLog(fmt.Sprintf("Network depth: %d for organism: %d\n", netDepth, org.Genotype.Id))
116+
if netDepth == 0 {
119117
neat.DebugLog(fmt.Sprintf("ALERT: Network depth is ZERO for Genome: %s", org.Genotype))
120118
}
121119

122120
// flush the neural net
123-
org.Phenotype.Flush()
121+
if _, err = org.Phenotype.Flush(); err != nil {
122+
neat.ErrorLog("Failed to flush phenotype")
123+
return nil, err
124+
}
124125
// update the maze
125126
err = env.Update()
126127
if err != nil {
@@ -142,14 +143,14 @@ func mazeSimulationInit(env Environment, org *genetics.Organism) (*Environment,
142143
// propagate input through the phenotype net
143144

144145
// Relax phenotype net and get output
145-
if _, err = org.Phenotype.Activate();err != nil && err != network.NetErrExceededMaxActivationAttempts {
146+
if _, err = org.Phenotype.Activate(); err != nil && err != network.NetErrExceededMaxActivationAttempts {
146147
neat.ErrorLog("Failed to activate network init 1")
147148
return nil, err
148149
}
149150

150151
// use depth to ensure relaxation at each layer
151-
for relax := 0; relax <= net_depth; relax++ {
152-
if _, err = org.Phenotype.Activate();err != nil && err != network.NetErrExceededMaxActivationAttempts {
152+
for relax := 0; relax <= netDepth; relax++ {
153+
if _, err = org.Phenotype.Activate(); err != nil && err != network.NetErrExceededMaxActivationAttempts {
153154
neat.ErrorLog("Failed to activate network init 2")
154155
return nil, err
155156
}
@@ -161,30 +162,29 @@ func mazeSimulationInit(env Environment, org *genetics.Organism) (*Environment,
161162
// To execute a time step of the maze simulation evaluation within given Environment for provided Organism
162163
func mazeSimulationStep(env *Environment, org *genetics.Organism) error {
163164
// get simulation parameters as inputs to organism's network
164-
inputs, err := env.GetInputs()
165-
if err != nil {
165+
if inputs, err := env.GetInputs(); err != nil {
166+
return err
167+
} else if err = org.Phenotype.LoadSensors(inputs); err != nil {
168+
neat.ErrorLog("Failed to load sensors")
166169
return err
167170
}
168-
org.Phenotype.LoadSensors(inputs)
169-
_, err = org.Phenotype.Activate()
171+
_, err := org.Phenotype.Activate()
170172
if err != nil && err != network.NetErrExceededMaxActivationAttempts {
171173
neat.ErrorLog("Failed to activate network simulation")
172174
return err
173175
}
174176

175177
// use the net's outputs to change heading and velocity of maze agent
176-
err = env.ApplyOutputs(org.Phenotype.Outputs[0].Activation, org.Phenotype.Outputs[1].Activation)
177-
if err != nil {
178+
if err = env.ApplyOutputs(org.Phenotype.Outputs[0].Activation, org.Phenotype.Outputs[1].Activation); err != nil {
178179
neat.ErrorLog("Failed to apply outputs")
179180
return err
180181
}
181182

182183
// update the environment
183-
err = env.Update()
184-
if err != nil {
184+
if err = env.Update(); err != nil {
185185
neat.ErrorLog("Failed to update environment")
186186
return err
187187
}
188188

189189
return nil
190-
}
190+
}

0 commit comments

Comments
 (0)