+/GENN
genetically evolving neural networks
git clone https://git.julia.blue/+/GENN
cc48e92
v1.0
1#include "../include/Network.h" 2 3Network ::Network (Genome & g ) { 4for (int i = 0 ;i < g .inputs ;++ i ) { 5inputs .push_back (new Neuron (i )); 6 } 7for (int i = 0 ;i < g .outputs ;++ i ) { 8outputs .push_back (new Neuron (g .inputs + i )); 9 } 10for (int i = 0 ;i < g .structure .size ();++ i ) { 11//Only constructs enabled connections for performance 12if (g .structure [i ].enabled ) { 13//Computes Inputs 14structure .push_back (Connection (g .structure [i ].weight )); 15if (g .structure [i ].input < g .inputs ) { 16structure .back ().in = inputs [g .structure [i ].input ]; 17 }else if (g .structure [i ].input < g .inputs + g .outputs ) { 18structure .back ().in = inputs [g .structure [i ].input - g .inputs ]; 19 }else { 20bool temp = true; 21for (int j = 0 ;j < hiddenNodes .size ();++ j ) { 22if (hiddenNodes [j ]-> id == g .structure [i ].input ) { 23temp = false; 24structure .back ().in = hiddenNodes [j ]; 25 } 26 } 27if (temp ) { 28hiddenNodes .push_back (new Neuron (g .structure [i ].input )); 29structure .back ().in = hiddenNodes .back (); 30 } 31 } 32//Computes Outputs 33if (g .structure [i ].output < g .inputs + g .outputs ) { 34structure .back ().out = outputs [g .structure [i ].output - g .inputs ]; 35 }else { 36bool temp = true; 37for (int j = 0 ;j < hiddenNodes .size ();++ j ) { 38if (hiddenNodes [j ]-> id == g .structure [i ].output ) { 39temp = false; 40structure .back ().out = hiddenNodes [j ]; 41 } 42 } 43if (temp ) { 44hiddenNodes .push_back (new Neuron (g .structure [i ].output )); 45structure .back ().out = hiddenNodes .back (); 46 } 47 } 48 } 49 } 50} 51 52double Network ::sigmoid (double a ) { 53return 2. / (1 + exp (-4.9 * a ))- 1 ; 54} 55 56void Network ::propagate (double a [],double b []) { 57for (int i = 0 ;i < inputs .size ();++ i ) { 58inputs [i ]-> value = a [i ]; 59 } 60for (int i = 0 ;i < hiddenNodes .size ();++ i ) { 61hiddenNodes [i ]-> sum = 0 ; 62 } 63for (int i = 0 ;i < outputs .size ();++ i ) { 64outputs [i ]-> sum = 0 ; 65 } 66for (int i = 0 ;i < structure .size ();++ i ) { 67structure [i ].out -> sum += structure [i ].in -> value * structure [i ].weight ; 68//Recomputes values in real time. might be a performance hit 69//solution would be to do it as a batch after propagation 70//that would take more steps to accurately propagate though 71structure [i ].out -> value = sigmoid (structure [i ].out -> sum ); 72 } 73for (int i = 0 ;i < outputs .size ();++ i ) { 74b [i ]= outputs [i ]-> value ; 75 } 76} 77 78void Network ::reset () { 79for (int i = 0 ;i < inputs .size ();++ i ) { 80inputs [i ]-> value = 0 ; 81 } 82for (int i = 0 ;i < hiddenNodes .size ();++ i ) { 83hiddenNodes [i ]-> value = 0 ; 84 } 85for (int i = 0 ;i < outputs .size ();++ i ) { 86outputs [i ]-> value = 0 ; 87 } 88} 89 90Network ::~Network () { 91for (int i = 0 ;i < inputs .size ();++ i ) { 92delete inputs [i ]; 93 } 94for (int i = 0 ;i < hiddenNodes .size ();++ i ) { 95delete hiddenNodes [i ]; 96 } 97for (int i = 0 ;i < outputs .size ();++ i ) { 98delete outputs [i ]; 99 } 100}