+/GENN
genetically evolving neural networks
git clone https://git.julia.blue/+/GENN
eb6e103
v0.2
1#include "Genome.h" 2 3 4Genome ::Genome (std::string g ) { 5decode (g ); 6// structure = g.structure; 7// nodes = g.nodes; 8// totalNodes = g.totalNodes; 9// totalConnections = g.totalConnections; 10// inputs = g.inputs; 11// outputs = g.outputs; 12// std::copy(std::begin(g.mutationRates), std::end(g.mutationRates), mutationRates); 13} 14 15Genome ::Genome (int a ,int b ,double m []) { 16totalNodes = a + b ; 17inputs = a ; 18outputs = b ; 19totalConnections = a * b ; 20for (int i = 0 ;i < a ;i ++ ) { 21nodes .push_back (new Neuron (i ,0 )); 22 } 23for (int i = 0 ;i < b ;i ++ ) { 24nodes .push_back (new Neuron (i + a ,1 )); 25 } 26for (int i = 0 ;i < inputs ;i ++ ) { 27for (int j = 0 ;j < outputs ;j ++ ) { 28//Initializes all connections with random weight 0-2 29structure .push_back (new Connection (nodes [i ],nodes [inputs + j ],double (rand ())* 4. /RAND_MAX - 2 )); 30 } 31 } 32for (int i = 0 ;i < 8 ;i ++ ) { 33mutationRates [i ]= m [i ]; 34 } 35recomputeInputs (); 36} 37 38void Genome ::propagate (double a [],double b []) { 39for (int i = 0 ;i < inputs ;i ++ ) { 40nodes [i ]-> value = a [i ]; 41 } 42for (int i = inputs - 1 ;i < totalNodes ;i ++ ) { 43nodes [i ]-> sum = 0 ; 44for (int j = 0 ;j < nodes [i ]-> inputs .size ();j ++ ) { 45nodes [i ]-> sum += 46nodes [i ]-> inputs [j ]-> input -> value * nodes [i ]-> inputs [j ]-> weight * int (nodes [i ]-> inputs [j ]-> enabled ); 47 } 48nodes [i ]-> value = sigmoid (nodes [i ]-> sum ); 49 } 50for (int i = 0 ;i < outputs ;i ++ ) { 51b [i ]= nodes [inputs + i ]-> value ; 52 } 53} 54 55double Genome ::sigmoid (double a ) { 56return 2 / (1 + exp (-4.9 * a ))- 1 ; 57} 58 59std::string Genome ::encode () { 60//writes to a space delimited string with the format: 61//totalNodes inputs outputs nodeN.id nodeN.type totalConnections conN.weight conN.enabled conN.input conN.output mutationN 62 std::ostringstream ss ; 63ss <<totalNodes <<' ' <<inputs <<' ' <<outputs <<' ' ; 64for (int i = 0 ;i < totalNodes ;i ++ ) { 65ss <<nodes [i ]-> id <<' ' <<nodes [i ]-> type <<' ' ; 66 } 67ss <<totalConnections <<' ' ; 68for (int i = 0 ;i < totalConnections ;i ++ ) { 69ss <<structure [i ]-> weight <<' ' <<structure [i ]-> enabled <<' ' 70 <<structure [i ]-> input -> id <<' ' <<structure [i ]-> output -> id <<' ' ; 71 } 72for (int i = 0 ;i < 7 ;i ++ ) { 73ss <<mutationRates [i ] <<' ' ; 74 } 75return ss .str (); 76} 77 78void Genome ::decode (std::string s ) { 79 std::stringstream ss (s ); 80ss >>totalNodes ; 81ss >>inputs ; 82ss >>outputs ; 83nodes .resize (totalNodes ); 84for (int i = 0 ;i < totalNodes ;i ++ ) { 85int tempid ,temptype ; 86ss >>tempid ; 87ss >>temptype ; 88nodes [i ]= new Neuron (tempid ,temptype ); 89 } 90ss >>totalConnections ; 91structure .resize (totalConnections ); 92int temp ; 93for (int i = 0 ;i < totalConnections ;i ++ ) { 94structure [i ]= new Connection (); 95ss >>structure [i ]-> weight ; 96ss >>structure [i ]-> enabled ; 97ss >>temp ; 98for (int j = 0 ;j < totalNodes ;j ++ ) { 99if (nodes [j ]-> id == temp ) { 100structure [i ]-> input = nodes [j ]; 101break ; 102 } 103 } 104ss >>temp ; 105for (int j = 0 ;j < totalNodes ;j ++ ) { 106if (nodes [j ]-> id == temp ) { 107structure [i ]-> output = nodes [j ]; 108break ; 109 } 110 } 111 } 112for (int i = 0 ;i < 7 ;i ++ ) { 113ss >>mutationRates [i ]; 114 } 115recomputeInputs (); 116} 117 118void Genome ::recomputeInputs () { 119for (int i = 0 ;i < totalNodes ;i ++ ) { 120for (int j = 0 ;j < totalConnections ;j ++ ) { 121if (nodes [i ]== structure [j ]-> output ) { 122nodes [i ]-> inputs .push_back (structure [j ]); 123 } 124 } 125 } 126} 127 128bool Genome ::hasConnection (int a ,int b ) { 129for (int i = 0 ;i < structure .size ();i ++ ) { 130if (structure [i ]-> input -> id == structure [i ]-> output -> id ) { 131return true; 132 } 133 } 134return false; 135} 136 137Genome ::~Genome () { 138for (int i = 0 ;i < structure .size ();i ++ ) { 139delete structure [i ]; 140 } 141for (int i = 0 ;i < nodes .size ();i ++ ) { 142delete nodes [i ]; 143 } 144} 145 146void Genome ::reset () { 147for (int i = 0 ;i < nodes .size ();i ++ ) { 148nodes [i ]-> value = 0 ; 149 } 150} 151