Generates neural net instances through genetic algorithm for use in task learning by m3pi. Reward determined by detection of black areas on arena floor, or by amount of area explored in limited time. Due to memory size, limited # of nets.

Dependencies:   Ping m3pimaze mbed

Committer:
parkbanks
Date:
Thu Apr 23 20:16:51 2015 +0000
Revision:
0:ac93d9db8dbd
Initial commit, bot fully working.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
parkbanks 0:ac93d9db8dbd 1 #ifndef CGENALG_H
parkbanks 0:ac93d9db8dbd 2 #define CGENALG_H
parkbanks 0:ac93d9db8dbd 3
parkbanks 0:ac93d9db8dbd 4 #include <vector>
parkbanks 0:ac93d9db8dbd 5 #include <algorithm>
parkbanks 0:ac93d9db8dbd 6 #include <iostream>
parkbanks 0:ac93d9db8dbd 7 #include <fstream>
parkbanks 0:ac93d9db8dbd 8 #include "utils.h"
parkbanks 0:ac93d9db8dbd 9
parkbanks 0:ac93d9db8dbd 10 using namespace std;
parkbanks 0:ac93d9db8dbd 11
parkbanks 0:ac93d9db8dbd 12 // structure to hold genomes
parkbanks 0:ac93d9db8dbd 13 struct SGenome
parkbanks 0:ac93d9db8dbd 14 {
parkbanks 0:ac93d9db8dbd 15 vector <float> vecWeights;
parkbanks 0:ac93d9db8dbd 16 float dFitness;
parkbanks 0:ac93d9db8dbd 17 SGenome():dFitness(0){}
parkbanks 0:ac93d9db8dbd 18 SGenome( vector <float> w, float f): vecWeights(w), dFitness(f){}
parkbanks 0:ac93d9db8dbd 19 //overload '<' for sorting
parkbanks 0:ac93d9db8dbd 20 friend bool operator<(const SGenome& lhs, const SGenome& rhs)
parkbanks 0:ac93d9db8dbd 21 {
parkbanks 0:ac93d9db8dbd 22 return (lhs.dFitness < rhs.dFitness);
parkbanks 0:ac93d9db8dbd 23 }
parkbanks 0:ac93d9db8dbd 24 };
parkbanks 0:ac93d9db8dbd 25
parkbanks 0:ac93d9db8dbd 26 // GA class
parkbanks 0:ac93d9db8dbd 27 class CGenAlg
parkbanks 0:ac93d9db8dbd 28 {
parkbanks 0:ac93d9db8dbd 29 private:
parkbanks 0:ac93d9db8dbd 30 vector <SGenome> m_vecPop;//vector of chromosome population
parkbanks 0:ac93d9db8dbd 31 int m_iPopSize;//population size
parkbanks 0:ac93d9db8dbd 32 int m_iChromoLength;//# weights per chromosome
parkbanks 0:ac93d9db8dbd 33 float m_dTotalFitness;//total fitness of population
parkbanks 0:ac93d9db8dbd 34 float m_dBestFitness;//best fitness
parkbanks 0:ac93d9db8dbd 35 float m_dWorstFitness;//worst fitness
parkbanks 0:ac93d9db8dbd 36 int m_iFittestGenome;//keeps track of the best genome
parkbanks 0:ac93d9db8dbd 37 float m_dMutationRate;//probability of mutation, 0.05-0.3 ideal
parkbanks 0:ac93d9db8dbd 38 float m_dCrossoverRate;//probability of chromosone cross-over, .7 ideal
parkbanks 0:ac93d9db8dbd 39 int m_cGeneration;//generation counter
parkbanks 0:ac93d9db8dbd 40
parkbanks 0:ac93d9db8dbd 41 void Crossover(const vector<float> &mum,
parkbanks 0:ac93d9db8dbd 42 const vector<float> &dad,
parkbanks 0:ac93d9db8dbd 43 vector<float> &baby1,
parkbanks 0:ac93d9db8dbd 44 vector<float> &baby2);
parkbanks 0:ac93d9db8dbd 45
parkbanks 0:ac93d9db8dbd 46 void Mutate(vector<float> &chromo);
parkbanks 0:ac93d9db8dbd 47
parkbanks 0:ac93d9db8dbd 48 SGenome GetChromoRoulette();
parkbanks 0:ac93d9db8dbd 49
parkbanks 0:ac93d9db8dbd 50 //introduce elitism
parkbanks 0:ac93d9db8dbd 51 void GrabNBest(int NBest, const int NumCopies, vector<SGenome> &vecPop);
parkbanks 0:ac93d9db8dbd 52 void CalculateBestWorstAvTot();
parkbanks 0:ac93d9db8dbd 53 void Reset();
parkbanks 0:ac93d9db8dbd 54
parkbanks 0:ac93d9db8dbd 55 public:
parkbanks 0:ac93d9db8dbd 56 CGenAlg(int popsize, float MutRat, float CrossRat, int numweights);
parkbanks 0:ac93d9db8dbd 57
parkbanks 0:ac93d9db8dbd 58 //runs GA for one generation.
parkbanks 0:ac93d9db8dbd 59 vector<SGenome> Epoch(vector<SGenome> &old_pop);
parkbanks 0:ac93d9db8dbd 60
parkbanks 0:ac93d9db8dbd 61 //accessor functions
parkbanks 0:ac93d9db8dbd 62 vector<SGenome> GetChromos()const{return m_vecPop;}
parkbanks 0:ac93d9db8dbd 63 float AverageFitness()const{return m_dTotalFitness / m_iPopSize;}
parkbanks 0:ac93d9db8dbd 64 float BestFitness()const{return m_dBestFitness;}
parkbanks 0:ac93d9db8dbd 65 };
parkbanks 0:ac93d9db8dbd 66
parkbanks 0:ac93d9db8dbd 67 #endif