forked from mr-andreas/neuralnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CGenAlg.h
131 lines (91 loc) · 2.93 KB
/
CGenAlg.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#ifndef CGENALG_H
#define CGENALG_H
//------------------------------------------------------------------------
//
// Name: CGenAlg.h
//
// Author: Mat Buckland 2002
//
// Desc: Genetic algorithm class.This is based for manipulating std::vectors
// of *real* numbers. Used to adjust the weights in a feedforward neural
// network.
//
//------------------------------------------------------------------------
#include <vector>
#include <algorithm>
#include <iostream>
#include <fstream>
#include "utils.h"
using namespace std;
//-----------------------------------------------------------------------
//
// create a structure to hold each genome
//-----------------------------------------------------------------------
struct SGenome
{
vector <double> vecWeights;
double dFitness;
SGenome():dFitness(0){}
SGenome( vector <double> w, double f): vecWeights(w), dFitness(f){}
//overload '<' used for sorting
friend bool operator<(const SGenome& lhs, const SGenome& rhs)
{
return (lhs.dFitness < rhs.dFitness);
}
};
//-----------------------------------------------------------------------
//
// the genetic algorithm class
//-----------------------------------------------------------------------
class CGenAlg
{
private:
//this holds the entire population of chromosomes
vector <SGenome> m_vecPop;
//size of population
int m_iPopSize;
//amount of weights per chromo
int m_iChromoLength;
//total fitness of population
double m_dTotalFitness;
//best fitness this population
double m_dBestFitness;
//average fitness
double m_dAverageFitness;
//worst
double m_dWorstFitness;
//keeps track of the best genome
int m_iFittestGenome;
//probability that a chromosones bits will mutate.
//Try figures around 0.05 to 0.3 ish
double m_dMutationRate;
//probability of chromosones crossing over bits
//0.7 is pretty good
double m_dCrossoverRate;
//generation counter
int m_cGeneration;
void Crossover(const vector<double> &mum,
const vector<double> &dad,
vector<double> &baby1,
vector<double> &baby2);
void Mutate(vector<double> &chromo);
SGenome GetChromoRoulette();
//use to introduce elitism
void GrabNBest(int NBest,
const int NumCopies,
vector<SGenome> &vecPop);
void CalculateBestWorstAvTot();
void Reset();
public:
CGenAlg(int popsize,
double MutRat,
double CrossRat,
int numweights);
//this runs the GA for one generation.
vector<SGenome> Epoch(vector<SGenome> &old_pop);
//-------------------accessor methods
vector<SGenome> GetChromos()const{return m_vecPop;}
double AverageFitness()const{return m_dTotalFitness / m_iPopSize;}
double BestFitness()const{return m_dBestFitness;}
};
#endif