30#include "GeneticAlgorithm.h"
31#include "ArtificialNeuralNet.h"
38GeneticAlgorithm::GeneticAlgorithm(Sint32 popsize,
double MutRat,
double CrossRat,
double MaxPerturbation, Sint32 NumElite, Sint32 NumCopiesElite, Sint32 numweights)
40 iPopulation = popsize;
41 dMutationRate = MutRat;
42 dCrossoverRate = CrossRat;
43 iChromoLength = numweights;
48 dWorstFitness = 99999999;
50 dMaxPerturbation = MaxPerturbation;
52 iNumCopiesElite = NumCopiesElite;
53 iNumMutation = iNumCrossOver = 0;
57void GeneticAlgorithm::init(Sint32 iPopPosition,vector<double> chromosomes)
62 if(iPopPosition > iPopulation)
return;
63 if(chromosomes.size() != iChromoLength)
return;
66 vPopulation.push_back(Genome());
69 for(i = 0; i<iChromoLength; ++i)
71 vPopulation[iPopPosition].vWeights.push_back(chromosomes[i]);
76void GeneticAlgorithm::mutate(vector<double>& chromo)
79 Tool &mTool = Main::Instance().ITool();
81 for(i = 0; i < (Sint32)chromo.size(); ++i)
84 if (mTool.randRealWELL() < dMutationRate)
87 chromo[i] += generateWeights() * dMaxPerturbation;
96Genome GeneticAlgorithm::getChromoRoulette()
99 double FitnessSoFar = 0;
103 double Slice = (double)(Main::Instance().ITool().randRealWELL() * dTotalFitness);
105 for(i = 0; i < iPopulation; ++i)
107 FitnessSoFar += vPopulation[i].dFitness;
109 if(FitnessSoFar >= Slice)
111 TheChosenOne = vPopulation[i];
119void GeneticAlgorithm::crossover(
const vector<double>& mum,
const vector<double>& dad, vector<double>& baby1, vector<double>& baby2)
122 Tool &mTool = Main::Instance().ITool();
125 if((mTool.randRealWELL() > dCrossoverRate) || (mum == dad))
135 cp = mTool.randWELL() % (iChromoLength);
138 for(i = 0; i < cp; ++i)
140 baby1.push_back(mum[i]);
141 baby2.push_back(dad[i]);
144 for(i = cp; i < (Sint32)mum.size(); ++i)
146 baby1.push_back(dad[i]);
147 baby2.push_back(mum[i]);
157vector<Genome> GeneticAlgorithm::epoch(vector<Genome>& old_pop)
159 vPopulation = old_pop;
163 sort(vPopulation.begin(), vPopulation.end());
169 vector <Genome> vecNewPop;
172 if((iNumCopiesElite * iNumElite) < iPopulation)
174 grabNBest(iNumElite, iNumCopiesElite, vecNewPop);
178 while((Sint32)vecNewPop.size() < iPopulation)
180 vector<double> baby1, baby2;
183 Genome mum = getChromoRoulette();
184 Genome dad = getChromoRoulette();
187 crossover(mum.vWeights, dad.vWeights, baby1, baby2);
193 vecNewPop.push_back(Genome(baby1, 0));
194 if((Sint32)vecNewPop.size() < iPopulation) vecNewPop.push_back(Genome(baby2, 0));
198 vPopulation = vecNewPop;
202vector<Genome> GeneticAlgorithm::getChromos()
const
207double GeneticAlgorithm::fitnessAverage()
const
209 return dFitnessAverage;
212double GeneticAlgorithm::fitnessBest()
const
217Sint32 GeneticAlgorithm::getMutation()
const
222Sint32 GeneticAlgorithm::getCrossOver()
const
224 return iNumCrossOver;
229void GeneticAlgorithm::grabNBest(Sint32 NBest,
const Sint32 NumCopies, vector<Genome>& Pop)
236 for(i = 0; i < NumCopies; ++i)
238 Pop.push_back(vPopulation[(iPopulation - 1) - NBest]);
244void GeneticAlgorithm::calculateStats()
247 double HighestSoFar = 0;
248 double LowestSoFar = 9999999;
251 for(i = 0; i < iPopulation; ++i)
253 if(vPopulation[i].dFitness > HighestSoFar)
255 HighestSoFar = vPopulation[i].dFitness;
257 dFitnessBest = HighestSoFar;
260 if(vPopulation[i].dFitness < LowestSoFar)
262 LowestSoFar = vPopulation[i].dFitness;
263 dWorstFitness = LowestSoFar;
265 dTotalFitness += vPopulation[i].dFitness;
268 dFitnessAverage = dTotalFitness / iPopulation;
272void GeneticAlgorithm::reset()
276 dWorstFitness = 9999999;
278 iNumMutation = iNumCrossOver = 0;