+/GENN
genetically evolving neural networks
git clone https://git.julia.blue/+/GENN
eb6e103
v0.2
1#include "NEAT.h" 2 3#define WEIGHTMUTATE 0 4#define PERTURBMUTATE 1 5#define ENABLEMUTATE 2 6#define STEPSIZE 3 7#define LINKMUTATE 4 8#define NODEMUTATE 5 9#define CROSSOVER 6 10 11NEAT ::NEAT (int inputs ,int outputs ) { 12 std::srand (time (NULL )); 13double m [7 ]; 14population = 150 ; 15f .open ("log.txt" ); 16m [WEIGHTMUTATE ]= .8 ; 17m [PERTURBMUTATE ]= .9 ; 18m [ENABLEMUTATE ]= .02 ; 19m [STEPSIZE ]= .4 ; 20m [LINKMUTATE ]= .05 ; 21m [NODEMUTATE ]= .03 ; 22m [CROSSOVER ]= .75 ; 23c1 = 1 ; 24c2 = 1 ; 25c3 = .4 ; 26staleThreshold = 15 ; 27distanceThreshold = 3 ; 28maxSpecies = 20 ; 29generation = 0 ; 30for (int i = 0 ;i < population ;i ++ ) { 31pool .push_back (new Genome (inputs ,outputs ,m )); 32 } 33classify (); 34} 35 36Genome * NEAT ::mate (Genome * a ,Genome * b ) { 37if (b -> fitness > a -> fitness ) { 38Genome * temp = a ; 39a = b ; 40b = temp ; 41 } 42Genome * child = new Genome (a -> encode ()); 43if (double (rand ()) /RAND_MAX < child -> mutationRates [CROSSOVER ]) { 44for (int i = 0 ;i < a -> totalConnections ;i ++ ) { 45double newWeight = a -> structure [i ]-> weight ; 46for (int j = 0 ;j < b -> totalConnections ;j ++ ) { 47if (b -> structure [j ]-> input -> id == a -> structure [i ]-> input -> id && 48b -> structure [j ]-> output -> id == a -> structure [i ]-> output -> id ) { 49child -> structure [i ]-> weight = (newWeight + b -> structure [j ]-> weight ) /2 ; 50 } 51 } 52 } 53 } 54mutate (* child ); 55return child ; 56} 57 58//doesn't check if combination exists, must check innovationExists() before 59int NEAT ::newInnovation (int a ,int b ) { 60 std::array < int ,2 > temp = {a ,b }; 61innovation .push_back (temp ); 62return innovation .size ()- 1 ; 63} 64 65bool NEAT ::innovationExists (int a ,int b ) { 66for (int i = 0 ;i < innovation .size ();i ++ ) { 67if (innovation [i ][0 ]== a && innovation [i ][1 ]== b ) { 68return false; 69 } 70 } 71return true; 72} 73 74void NEAT ::mutate (Genome & g ) { 75//ENABLE 76if (double (rand ()) /RAND_MAX < g .mutationRates [ENABLEMUTATE ]) { 77int temp = rand () %g .totalConnections ; 78g .structure [temp ]-> enabled = !g .structure [temp ]-> enabled ; 79 } 80//WEIGHTS 81for (int i = 0 ;i < g .totalConnections ;i ++ ) { 82if (double (rand ()) /RAND_MAX < g .mutationRates [WEIGHTMUTATE ]) { 83if (rand () /RAND_MAX < g .mutationRates [PERTURBMUTATE ]) { 84g .structure [i ]-> weight += ((rand () %2 )* 2 - 1 )* (rand () /RAND_MAX )* g .mutationRates [STEPSIZE ]; 85 }else { 86g .structure [i ]-> weight = double (rand ())* 4. /RAND_MAX - 2 ; 87 } 88 } 89 } 90//ADD CONNECTION 91if (double (rand ()) /RAND_MAX < g .mutationRates [LINKMUTATE ]) { 92int a = rand () %g .totalNodes ; 93int b = rand () % (g .totalNodes - g .inputs ); 94if (!g .hasConnection (a ,b )) { 95Neuron * tempStart = NULL ; 96Neuron * tempEnd = NULL ; 97for (int i = 0 ;i < g .totalNodes ;i ++ ) { 98if (a == g .nodes [i ]-> id ) { 99tempStart = g .nodes [i ]; 100 } 101if (b == g .nodes [i ]-> id ) { 102tempEnd = g .nodes [i ]; 103 } 104 } 105g .totalConnections ++ ; 106g .structure .push_back (new Connection (tempStart ,tempEnd ,double (rand ())* 4. /RAND_MAX - 2 )); 107 } 108 } 109//ADD NODE 110if (double (rand ()) /RAND_MAX < g .mutationRates [NODEMUTATE ]) { 111Connection * temp = g .structure [rand () %g .totalConnections ]; 112temp -> enabled = false; 113int newNodeID = 0 ; 114for (int i = 0 ;i < g .totalNodes ;i ++ ) { 115if (g .nodes [i ]-> id > newNodeID ) { 116newNodeID = g .nodes [i ]-> id ; 117 } 118 } 119Neuron * newNode = new Neuron (newNodeID + 1 ); 120g .nodes .push_back (newNode ); 121g .structure .push_back (new Connection (temp -> input ,newNode ,1 )); 122g .structure .push_back (new Connection (temp -> input ,newNode ,temp -> weight )); 123g .totalNodes ++ ; 124g .totalConnections += 2 ; 125 } 126//slightly alter all mutation rates (.05 is the volatility) 127for (int i = 0 ;i < 7 ;i ++ ) { 128g .mutationRates [i ]+= ((rand () %2 )* 2 - 1 )* .05 * g .mutationRates [i ]; 129 } 130} 131 132double NEAT ::distance (Genome * a ,Genome * b ) { 133if (b -> fitness > a -> fitness ) { 134Genome * temp = a ; 135a = b ; 136b = temp ; 137 } 138int disjoint = b -> totalConnections ; 139int n = a -> totalConnections ; 140double sum = 0 ; 141for (int i = 0 ;i < n ;i ++ ) { 142for (int j = 0 ;j < b -> totalConnections ;j ++ ) { 143if (a -> structure [i ]-> input -> id == b -> structure [j ]-> input -> id && 144a -> structure [i ]-> output -> id == b -> structure [j ]-> output -> id ) { 145sum += fabs (a -> structure [i ]-> weight - b -> structure [j ]-> weight ); 146disjoint -- ; 147break ; 148 } 149 } 150 } 151return std::abs (c1 * disjoint /n + c2 * (n - b -> totalConnections )+ c3 * sum / (b -> totalConnections - disjoint )); 152} 153 154void NEAT ::adjustFitness () { 155for (int i = 0 ;i < species .size ();i ++ ) { 156 std::sort (species [i ].genomes .begin (),species [i ].genomes .end (),Genome ::compare ); 157for (int j = 0 ;j < species [i ].genomes .size ();j ++ ) { 158species [i ].genomes [j ]-> adjustedFitness = species [i ].genomes [j ]-> fitness ; 159//species[i].genomes[j]->adjustedFitness /= species[i].genomes.size(); 160 } 161if (species [i ].genomes [0 ]-> fitness > species [i ].maxFitness ) { 162species [i ].maxFitness = species [i ].genomes [0 ]-> adjustedFitness ; 163species [i ].staleness = 0 ; 164 }else { 165species [i ].staleness ++ ; 166 } 167 } 168 std::sort (species .begin (),species .end (),Species ::compare ); 169} 170 171void NEAT ::cull () { 172for (int i = 0 ;i < species .size ();i ++ ) { 173if (species [i ].staleness >=staleThreshold ) { 174species .erase (species .begin ()+ i ); 175i -- ; 176 } 177 } 178} 179 180void NEAT ::repopulate () { 181 std::vector < Genome *> newPool ; 182for (int i = 0 ;i < species .size ();i ++ ) { 183newPool .push_back (new Genome (species [i ].genomes [0 ]-> encode ())); 184for (int j = 0 ;j < species [i ].genomes .size ()- 1 ;j ++ ) { 185newPool .push_back (mate (species [i ].genomes [rand () % std::max ((int )species [i ].genomes .size () /5 ,1 )], 186species [i ].genomes [rand () % std::max ((int )species [i ].genomes .size () /5 ,1 )])); 187 } 188 } 189while (newPool .size ()< population ) { 190newPool .push_back (mate (pool [rand () %population ],pool [rand () %population ])); 191 } 192for (int i = 0 ;i < population ;i ++ ) { 193delete pool [i ]; 194 } 195pool = newPool ; 196for (int i = 0 ;i < species .size ();i ++ ) { 197species [i ].genomes .clear (); 198species [i ].genomes .push_back (pool [i ]); 199 } 200} 201 202 203void NEAT ::classify () { 204for (int i = species .size ();i < population ;i ++ ) { 205bool newSpecies = true; 206for (int j = 0 ;j < species .size ();j ++ ) { 207if (distance (pool [i ],species [j ].genomes [0 ])< distanceThreshold ) { 208newSpecies = false; 209species [j ].genomes .push_back (pool [i ]); 210break ; 211 } 212 } 213if (newSpecies ) { 214species .push_back (Species ()); 215species .back ().genomes .push_back (pool [i ]); 216 } 217 } 218if (species .size ()> maxSpecies ){ 219distanceThreshold += .1 ; 220 } 221} 222 223void NEAT ::nextGen () { 224adjustFitness (); 225log (); 226status (); 227cull (); 228repopulate (); 229classify (); 230generation ++ ; 231} 232 233NEAT ::~NEAT () { 234for (int i = 0 ;i < population ;i ++ ) { 235delete pool [i ]; 236 } 237f .close (); 238} 239 240 241void NEAT ::log () { 242f <<"Gen: " <<generation << std::endl ; 243f <<"Max Fitness: " <<species [0 ].genomes [0 ]-> fitness << std::endl ; 244f <<"Champion: " <<species [0 ].genomes [0 ]-> encode () << std::endl ; 245} 246 247void NEAT ::status () { 248double averageSize = 0 ; 249double averageFitness = 0 ; 250for (int i = 0 ;i < population ;i ++ ) { 251averageSize += pool [i ]-> totalConnections ; 252averageFitness += pool [i ]-> fitness ; 253 } 254averageSize /=population ; 255averageFitness /=population ; 256 std::cout <<"Gen: " <<generation <<" Max Fitness: " <<species [0 ].genomes [0 ]-> fitness <<" Species: " 257 <<species .size () <<" Average Size: " <<averageSize << std::endl ; 258} 259 260