LCFIVertex  0.7.2
GeneticAlgorithm.h
1 #ifndef GENETICALGORITHM_H
2 #define GENETICALGORITHM_H
3 
4 #include "NeuralNetConfig.h"
5 
6 #include "NeuralNet.h"
7 #include "NeuralNetDataSet.h"
8 
9 #ifdef __CINT__
10 #include "Genome.h"
11 #include "InputNormaliser.h"
12 #else
13 //namespace nnet added 15/08/06 by Mark Grimes (mark.grimes@bristol.ac.uk) for the LCFI vertex package
14 namespace nnet
15 {
16 class Genome;
17 class InputNormaliser;
18 }
19 #endif
20 
21 #include <vector>
22 
23 //namespace nnet added 15/08/06 by Mark Grimes (mark.grimes@bristol.ac.uk) for the LCFI vertex package
24 namespace nnet
25 {
26 
27 class
28 #ifndef __CINT__
29 NEURALNETDLL
30 #endif
32 {
33 public:
34  GeneticAlgorithm(NeuralNet &theNetwork,const int populationSize,const double mutationRate=0.1,
35  const double crossoverRate=0.7);
36  ~GeneticAlgorithm(void);
37  void setNumberOfEliteGenomes(const int n)
38  { _numberOfEliteCrossGenerationGenomes = n;}
39  void setEliteGenomeReplicationCount(const int count);
40  void newGeneration();
41  void evaluatePopulationFitness(const std::vector<double> &inputValues,
42  const std::vector<double> &desiredOutput);
43  void batchTrain(const int numberOfEpochs,const NeuralNetDataSet &dataSet,
44  const NeuralNet::InputNormalisationSelect normaliseTrainingData=NeuralNet::PassthroughNormalised);
45  void batchTrain(const int numberOfEpochs,const NeuralNetDataSet &dataSet,const std::vector<InputNormaliser *> &inputNormalisers);
46  void setProgressPrintoutFrequency(const int frequency) {_progressPrintoutFrequency = frequency;}
47  void setMaximumGenomeFitness(const double fitness) {_maxGenomeFitness = fitness;}
48  std::vector<double> getTrainingErrorValuesPerEpoch() const {return _savedEpochErrorValues;}
49 
50 protected:
51  void trainWithDataSet(const int numberOfEpochs,const NeuralNetDataSet &dataSet);
52  void pickBest();
53  void exterminate();
54  double totalPopulationFitness();
55  int pickGenomeByRoulette();
56  void addEliteGenomesToNextGeneration(std::vector<Genome *> &nextGen);
57  virtual double calculateGenomeFitness(const double error);
58  void processDataSet(const NeuralNetDataSet &dataSet);
59  double error(const NeuralNetDataSet &dataSet) const;
60 
61 
62 private:
63  NeuralNet &_theNetwork;
64  std::vector<Genome *> _thePopulation;
65  int _populationSize;
66  int _numberOfEvaluations;
67 
68  // Parameters for elitism.
69  // Allow this many of the best genomes a free ride
70  // into the next generation.
71  int _numberOfEliteCrossGenerationGenomes;
72 
73  // Allow this many copies of them
74  int _numberOfCopiesOfEliteGenomes;
75 
76  int _progressPrintoutFrequency;
77  double _maxGenomeFitness;
78  std::vector<double> _savedEpochErrorValues;
79 };
80 
81 }//namespace nnet
82 
83 #endif