-
Notifications
You must be signed in to change notification settings - Fork 2
/
mutator.go
684 lines (529 loc) · 18.2 KB
/
mutator.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
package neurvolve
import (
"github.com/couchbaselabs/logg"
ng "github.com/tleyden/neurgo"
"log"
"math"
)
type OutboundChooser func(*ng.Neuron) *ng.OutboundConnection
type NeuronMutator func(*ng.Neuron) (bool, MutateResult)
type MutateResult interface{}
type CortexMutator func(*ng.Cortex) (bool, MutateResult)
func CortexMutatorsNonTopological() []CortexMutator {
mutators := []CortexMutator{
AddBias,
RemoveBias,
MutateWeights,
ResetWeights,
MutateActivation,
}
return mutators
}
func CortexMutatorsRecurrent(includeNonTopological bool) []CortexMutator {
recurrentMutators := []CortexMutator{
AddNeuronRecurrent,
AddInlinkRecurrent,
AddOutlinkRecurrent,
OutspliceRecurrent,
}
if includeNonTopological {
commonMutators := CortexMutatorsNonTopological()
return append(recurrentMutators, commonMutators...)
} else {
return recurrentMutators
}
}
func CortexMutatorsNonRecurrent(includeNonTopological bool) []CortexMutator {
nonRecurrentMutators := []CortexMutator{
AddNeuronNonRecurrent,
AddInlinkNonRecurrent,
AddOutlinkNonRecurrent,
OutspliceNonRecurrent,
}
if includeNonTopological {
commonMutators := CortexMutatorsNonTopological()
return append(nonRecurrentMutators, commonMutators...)
} else {
return nonRecurrentMutators
}
}
func inboundConnectionCandidates(neuron *ng.Neuron) []*ng.NodeId {
if neuron == nil {
log.Panicf("neuron is nil")
}
cortex := neuron.Cortex
if cortex == nil {
log.Panicf("neuron has no cortex associated: %v", neuron)
}
neuronNodeIds := cortex.NeuronNodeIds()
sensorNodeIds := cortex.SensorNodeIds()
availableNodeIds := append(neuronNodeIds, sensorNodeIds...)
// hackish way to delete a vew elements from this slice.
// put in a map and delete from map, then back to slice. TODO: fixme
availableNodeIdMap := make(map[string]*ng.NodeId)
for _, nodeId := range availableNodeIds {
availableNodeIdMap[nodeId.UUID] = nodeId
}
// remove things we already have inbound connections from
for _, inboundConnection := range neuron.Inbound {
nodeId := inboundConnection.NodeId
delete(availableNodeIdMap, nodeId.UUID)
}
availableNodeIds = make([]*ng.NodeId, 0)
for _, nodeId := range availableNodeIdMap {
availableNodeIds = append(availableNodeIds, nodeId)
}
return availableNodeIds
}
func AddNeuronNonRecurrent(cortex *ng.Cortex) (bool, MutateResult) {
numAttempts := len(cortex.AllNodeIds()) * 5
for i := 0; i < numAttempts; i++ {
nodeIdLayerMap := cortex.NodeIdLayerMap()
neuronLayerMap := cortex.NeuronLayerMap()
randomLayer := neuronLayerMap.ChooseRandomLayer()
upstreamNodeId := nodeIdLayerMap.ChooseNodeIdPrecedingLayer(randomLayer)
if upstreamNodeId == nil {
continue
}
downstreamNodeId := findDownstreamNodeId(cortex, nodeIdLayerMap, randomLayer)
if downstreamNodeId == nil {
continue
}
neuron := cortex.CreateNeuronInLayer(randomLayer)
neuronAddInlinkFrom(neuron, upstreamNodeId)
neuronAddOutlinkTo(neuron, downstreamNodeId)
return true, neuron
}
return false, nil
}
func AddNeuronRecurrent(cortex *ng.Cortex) (bool, MutateResult) {
numAttempts := len(cortex.AllNodeIds()) * 5
for i := 0; i < numAttempts; i++ {
nodeIdLayerMap := cortex.NodeIdLayerMap()
neuronLayerMap := cortex.NeuronLayerMap()
randomLayer := neuronLayerMap.ChooseRandomLayer()
inboundNodeId := findRecurrentInboundNodeId(cortex,
nodeIdLayerMap,
randomLayer)
if inboundNodeId == nil {
log.Printf("Warn: unable to find inbound node id")
continue
}
if randomLayer == inboundNodeId.LayerIndex {
continue
}
neuron := cortex.CreateNeuronInLayer(randomLayer)
outboundNodeId := findRecurrentOutboundNodeId(cortex,
nodeIdLayerMap,
randomLayer)
if outboundNodeId == nil {
log.Printf("Warn: unable to find outbound node id")
continue
}
neuronAddInlinkFrom(neuron, inboundNodeId)
neuronAddOutlinkTo(neuron, outboundNodeId)
return true, neuron
}
logg.LogTo("NEURVOLVE", "return false, nil")
return false, nil
}
func Outsplice(cortex *ng.Cortex, chooseOutbound OutboundChooser) (bool, *ng.Neuron) {
numAttempts := len(cortex.AllNodeIds()) * 5
for i := 0; i < numAttempts; i++ {
neuronA := randomNeuron(cortex)
outbound := chooseOutbound(neuronA)
if outbound == nil {
continue
}
if neuronA.NodeId.UUID == outbound.NodeId.UUID {
continue
}
nodeIdB := outbound.NodeId
// figure out which layer neuronK will go in
nodeIdLayerMap := cortex.NodeIdLayerMap()
layerA := neuronA.NodeId.LayerIndex
layerB := nodeIdB.LayerIndex
layerK := nodeIdLayerMap.LayerBetweenOrNew(layerA, layerB)
// create neuron K
neuronK := cortex.CreateNeuronInLayer(layerK)
// disconnect neuronA <-> nodeB
nodeBConnector := cortex.FindInboundConnector(nodeIdB)
ng.DisconnectOutbound(neuronA, nodeIdB)
ng.DisconnectInbound(nodeBConnector, neuronA)
// connect neuronA -> neuronK
weights := randomWeights(1)
ng.ConnectOutbound(neuronA, neuronK)
ng.ConnectInboundWeighted(neuronK, neuronA, weights)
// connect neuronK -> nodeB
switch nodeIdB.NodeType {
case ng.NEURON:
neuronB := cortex.FindNeuron(nodeIdB)
ng.ConnectOutbound(neuronK, neuronB)
ng.ConnectInboundWeighted(nodeBConnector, neuronK, weights)
case ng.ACTUATOR:
actuatorB := cortex.FindActuator(nodeIdB)
ng.ConnectOutbound(neuronK, actuatorB)
ng.ConnectInbound(nodeBConnector, neuronK)
}
return true, neuronK
}
return false, nil
}
func OutspliceRecurrent(cortex *ng.Cortex) (bool, MutateResult) {
chooseOutboundFunction := randomOutbound
return Outsplice(cortex, chooseOutboundFunction)
}
func OutspliceNonRecurrent(cortex *ng.Cortex) (bool, MutateResult) {
chooseOutboundFunction := randomNonRecurrentOutbound
return Outsplice(cortex, chooseOutboundFunction)
}
func randomNonRecurrentOutbound(neuron *ng.Neuron) *ng.OutboundConnection {
for i := 0; i < len(neuron.Outbound); i++ {
randIndex := RandomIntInRange(0, len(neuron.Outbound))
outbound := neuron.Outbound[randIndex]
if neuron.IsConnectionRecurrent(outbound) {
continue
} else {
return outbound
}
}
return nil
}
func randomOutbound(neuron *ng.Neuron) *ng.OutboundConnection {
for i := 0; i < len(neuron.Outbound); i++ {
randIndex := RandomIntInRange(0, len(neuron.Outbound))
return neuron.Outbound[randIndex]
}
return nil
}
func randomNeuron(cortex *ng.Cortex) *ng.Neuron {
neurons := cortex.Neurons
randIndex := RandomIntInRange(0, len(neurons))
return neurons[randIndex]
}
// Find a nodeId suitable for use as an inbound node for a newly created
// neuron. This can either be a sensor node or another neuron node (including
// the new neuron itself), but it cannot be an actuator node.
func findRecurrentInboundNodeId(cortex *ng.Cortex, layerMap ng.LayerToNodeIdMap, fromLayer float64) *ng.NodeId {
keys := layerMap.Keys()
actuatorLayer := keys[len(keys)-1]
chosenNodeId := layerMap.ChooseNodeIdPrecedingLayer(actuatorLayer)
return chosenNodeId
}
// Find a nodeId suitable for use as an outbound node for a newly created
// neuron. This can either be a either another neuron node (including
// the new neuron itself), or an actuator (if it has space), but it cannot
// be a sensor node
func findRecurrentOutboundNodeId(cortex *ng.Cortex, layerMap ng.LayerToNodeIdMap, fromLayer float64) *ng.NodeId {
numAttempts := len(cortex.AllNodeIds()) * 5
keys := layerMap.Keys()
sensorLayer := keys[0]
for i := 0; i < numAttempts; i++ {
chosenNodeId := layerMap.ChooseNodeIdFollowingLayer(sensorLayer)
if chosenNodeId.NodeType == ng.ACTUATOR {
// make sure it has capacity for new incoming
actuator := cortex.FindActuator(chosenNodeId)
if actuator.CanAddInboundConnection() == false {
continue
}
}
return chosenNodeId
}
return nil
}
func findDownstreamNodeId(cortex *ng.Cortex, layerMap ng.LayerToNodeIdMap, fromLayer float64) *ng.NodeId {
numAttempts := len(cortex.AllNodeIds()) * 5
for i := 0; i < numAttempts; i++ {
downstreamNodeId := layerMap.ChooseNodeIdFollowingLayer(fromLayer)
if downstreamNodeId == nil {
log.Printf("findDownstreamNodeId unable to find downstream neuron, cannot add neuron")
return nil
}
if downstreamNodeId.NodeType == ng.ACTUATOR {
// make sure it has capacity for new incoming
actuator := cortex.FindActuator(downstreamNodeId)
if actuator.CanAddInboundConnection() == false {
continue
}
}
return downstreamNodeId
}
return nil
}
func NeuronAddInlinkNonRecurrent(neuron *ng.Neuron) (bool, MutateResult) {
availableNodeIds := inboundConnectionCandidates(neuron)
// remove any node id's which have a layer index >= neuron.LayerIndex
nonRecurrentNodeIds := make([]*ng.NodeId, 0)
for _, nodeId := range availableNodeIds {
if nodeId.LayerIndex < neuron.NodeId.LayerIndex {
nonRecurrentNodeIds = append(nonRecurrentNodeIds, nodeId)
}
}
return neuronAddInlink(neuron, nonRecurrentNodeIds)
}
func NeuronAddInlinkRecurrent(neuron *ng.Neuron) (bool, MutateResult) {
// choose a random element B, where element B is another
// neuron or a sensor which is not already connected
// to this neuron.
availableNodeIds := inboundConnectionCandidates(neuron)
return neuronAddInlink(neuron, availableNodeIds)
}
func neuronAddInlink(neuron *ng.Neuron, availableNodeIds []*ng.NodeId) (bool, *ng.InboundConnection) {
if len(availableNodeIds) == 0 {
log.Printf("Warning: unable to add inlink to neuron: %v", neuron)
return false, nil
}
randIndex := ng.RandomIntInRange(0, len(availableNodeIds))
chosenNodeId := availableNodeIds[randIndex]
return true, neuronAddInlinkFrom(neuron, chosenNodeId)
}
func neuronAddInlinkFrom(neuron *ng.Neuron, sourceNodeId *ng.NodeId) *ng.InboundConnection {
cortex := neuron.Cortex
// create weight vector
weightVectorLength := 1
if sourceNodeId.NodeType == ng.SENSOR {
sensor := cortex.FindSensor(sourceNodeId)
weightVectorLength = sensor.VectorLength
}
weights := randomWeights(weightVectorLength)
// make an inbound connection sourceNodeId <- neuron
connection := neuron.ConnectInboundWeighted(sourceNodeId, weights)
// make an outbound connection sourceNodeId -> neuron
chosenConnector := cortex.FindConnector(sourceNodeId)
ng.ConnectOutbound(chosenConnector, neuron)
return connection
}
func outboundConnectionCandidates(neuron *ng.Neuron) []*ng.NodeId {
if neuron == nil {
log.Panicf("Neuron is nil")
}
cortex := neuron.Cortex
if cortex == nil {
log.Panicf("Neuron has no cortex associated with it: %v", neuron)
}
neuronNodeIds := cortex.NeuronNodeIds()
actuatorNodeIds := cortex.ActuatorNodeIds()
availableNodeIds := append(neuronNodeIds, actuatorNodeIds...)
// hackish way to delete a vew elements from this slice.
// put in a map and delete from map, then back to slice. TODO: fixme
availableNodeIdMap := make(map[string]*ng.NodeId)
for _, nodeId := range availableNodeIds {
availableNodeIdMap[nodeId.UUID] = nodeId
}
// remove things we are already connected to
for _, outboundConnection := range neuron.Outbound {
nodeId := outboundConnection.NodeId
delete(availableNodeIdMap, nodeId.UUID)
}
// remove actuators that can't support any more inbound connections
for _, actuatorNodeId := range actuatorNodeIds {
actuator := cortex.FindActuator(actuatorNodeId)
// does the actuator have capacity for another
// incoming connection?
if actuator.CanAddInboundConnection() == false {
delete(availableNodeIdMap, actuatorNodeId.UUID)
}
}
availableNodeIds = make([]*ng.NodeId, 0)
for _, nodeId := range availableNodeIdMap {
availableNodeIds = append(availableNodeIds, nodeId)
}
return availableNodeIds
}
func neuronAddOutlink(neuron *ng.Neuron, availableNodeIds []*ng.NodeId) (bool, *ng.OutboundConnection) {
if len(availableNodeIds) == 0 {
log.Printf("Warning: unable to add outlink to neuron: %v", neuron)
return false, nil
}
randIndex := ng.RandomIntInRange(0, len(availableNodeIds))
chosenNodeId := availableNodeIds[randIndex]
return true, neuronAddOutlinkTo(neuron, chosenNodeId)
}
func neuronAddOutlinkTo(neuron *ng.Neuron, targetNodeId *ng.NodeId) *ng.OutboundConnection {
cortex := neuron.Cortex
switch targetNodeId.NodeType {
case ng.NEURON:
// make an outbound connection neuron -> targetNodeId
chosenNeuron := cortex.FindNeuron(targetNodeId)
connection := ng.ConnectOutbound(neuron, chosenNeuron)
// make an inbound connection targetNodeId <- neuron
weights := randomWeights(1)
ng.ConnectInboundWeighted(chosenNeuron, neuron, weights)
return connection
case ng.ACTUATOR:
chosenActuator := cortex.FindActuator(targetNodeId)
// make an outbound connection neuron -> targetNodeId
connection := ng.ConnectOutbound(neuron, chosenActuator)
// make an inbound connection targetNodeId <- neuron
ng.ConnectInbound(chosenActuator, neuron)
return connection
default:
log.Panicf("unexpected chosen node type")
return nil
}
}
func NeuronAddOutlinkRecurrent(neuron *ng.Neuron) (bool, MutateResult) {
// choose a random element B, where element B is another
// neuron or a sensor which is not already connected
// to this neuron.
availableNodeIds := outboundConnectionCandidates(neuron)
return neuronAddOutlink(neuron, availableNodeIds)
}
func NeuronAddOutlinkNonRecurrent(neuron *ng.Neuron) (bool, MutateResult) {
availableNodeIds := outboundConnectionCandidates(neuron)
// remove any node id's which have a layer index >= neuron.LayerIndex
nonRecurrentNodeIds := make([]*ng.NodeId, 0)
for _, nodeId := range availableNodeIds {
if nodeId.LayerIndex > neuron.NodeId.LayerIndex {
nonRecurrentNodeIds = append(nonRecurrentNodeIds, nodeId)
}
}
return neuronAddOutlink(neuron, nonRecurrentNodeIds)
}
func NeuronMutateWeights(neuron *ng.Neuron) (bool, MutateResult) {
didPerturbAnyWeights := false
probability := parameterPerturbProbability(neuron)
for _, cxn := range neuron.Inbound {
saturationBounds := []float64{-100000, 100000}
didPerturbWeight := possiblyPerturbConnection(cxn, probability, saturationBounds)
if didPerturbWeight == true {
didPerturbAnyWeights = true
}
}
return didPerturbAnyWeights, nil
}
func NeuronMutateActivation(neuron *ng.Neuron) (bool, MutateResult) {
encodableActivations := ng.AllEncodableActivations()
for i := 0; i < 100; i++ {
// pick a random activation function from list
randomIndex := ng.RandomIntInRange(0, len(encodableActivations))
chosenActivation := encodableActivations[randomIndex]
// if we chose a different activation than current one, use it
if chosenActivation.Name != neuron.ActivationFunction.Name {
neuron.ActivationFunction = chosenActivation
return true, nil
}
}
// if we got this far, something went wrong
return false, nil
}
func NeuronResetWeights(neuron *ng.Neuron) (bool, MutateResult) {
for _, cxn := range neuron.Inbound {
for j, _ := range cxn.Weights {
cxn.Weights[j] = RandomWeight()
}
}
return true, nil
}
func NeuronAddBias(neuron *ng.Neuron) (bool, MutateResult) {
if neuron.Bias == 0 {
neuron.Bias = RandomBias()
return true, nil
}
return false, nil
}
func NeuronRemoveBias(neuron *ng.Neuron) (bool, MutateResult) {
if neuron.Bias != 0 {
neuron.Bias = 0
return true, nil
}
return false, nil
}
func RandomNeuronMutator(c *ng.Cortex, mutator NeuronMutator) (bool, MutateResult) {
neuron := randomNeuron(c)
return mutator(neuron)
}
func ReattemptingNeuronMutator(c *ng.Cortex, mutator NeuronMutator) (bool, MutateResult) {
numAttempts := len(c.AllNodeIds()) * 5
for i := 0; i < numAttempts; i++ {
neuron := randomNeuron(c)
ok, mutateResult := mutator(neuron)
if ok {
return ok, mutateResult
}
}
return false, nil
}
func AddBias(cortex *ng.Cortex) (bool, MutateResult) {
return RandomNeuronMutator(cortex, NeuronAddBias)
}
func RemoveBias(cortex *ng.Cortex) (bool, MutateResult) {
return RandomNeuronMutator(cortex, NeuronRemoveBias)
}
func MutateWeights(cortex *ng.Cortex) (bool, MutateResult) {
return RandomNeuronMutator(cortex, NeuronMutateWeights)
}
func ResetWeights(cortex *ng.Cortex) (bool, MutateResult) {
return RandomNeuronMutator(cortex, NeuronResetWeights)
}
func MutateActivation(cortex *ng.Cortex) (bool, MutateResult) {
return RandomNeuronMutator(cortex, NeuronMutateActivation)
}
func AddInlinkRecurrent(cortex *ng.Cortex) (bool, MutateResult) {
return ReattemptingNeuronMutator(cortex, NeuronAddInlinkRecurrent)
}
func AddInlinkNonRecurrent(cortex *ng.Cortex) (bool, MutateResult) {
return ReattemptingNeuronMutator(cortex, NeuronAddInlinkNonRecurrent)
}
func AddOutlinkRecurrent(cortex *ng.Cortex) (bool, MutateResult) {
return ReattemptingNeuronMutator(cortex, NeuronAddOutlinkRecurrent)
}
func AddOutlinkNonRecurrent(cortex *ng.Cortex) (bool, MutateResult) {
return ReattemptingNeuronMutator(cortex, NeuronAddOutlinkNonRecurrent)
}
func NoOpMutator(cortex *ng.Cortex) (success bool, result MutateResult) {
success = true
result = "nothing"
return
}
func MutateAllWeightsBellCurve(cortex *ng.Cortex) (success bool, result MutateResult) {
stdDev := DEFAULT_STD_DEVIATION
for _, neuron := range cortex.Neurons {
for _, inboundConnection := range neuron.Inbound {
weights := inboundConnection.Weights
for k, weight := range weights {
newWeight := perturbParameterBellCurve(weight, stdDev)
weights[k] = newWeight
}
}
newBias := perturbParameterBellCurve(neuron.Bias, stdDev)
neuron.Bias = newBias
}
success = true
result = "nothing"
return
}
func TopologyOrWeightMutator(cortex *ng.Cortex) (success bool, result MutateResult) {
randomNumber := ng.RandomIntInRange(0, 100)
if randomNumber > 95 {
logg.LogTo("NEURVOLVE", "Attempting to mutate topology")
// before we mutate the cortex, we need to init it,
// otherwise things like Outsplice will fail because
// there are no DataChan's.
cortex.Init()
// apply topological mutation
didMutate := false
includeNonTopological := false
mutators := CortexMutatorsNonRecurrent(includeNonTopological)
for i := 0; i <= 100; i++ {
randInt := RandomIntInRange(0, len(mutators))
mutator := mutators[randInt]
didMutate, _ = mutator(cortex)
if !didMutate {
logg.LogTo("NEURVOLVE", "Mutate didn't work, retrying...")
continue
}
break
}
logg.LogTo("NEURVOLVE", "did mutate: %v", didMutate)
success = didMutate
} else {
logg.LogTo("NEURVOLVE", "Attempting to mutate weights")
// mutate the weights
saturationBounds := []float64{-10 * math.Pi, 10 * math.Pi}
PerturbParameters(cortex, saturationBounds)
success = true
}
result = "nothing"
return
}