Modernized GAlib  3.0.0 current
GAStatistics.h
1 /* ----------------------------------------------------------------------------
2  mbwall 14jul95
3  Copyright (c) 1995 Massachusetts Institute of Technology
4  - all rights reserved
5 ---------------------------------------------------------------------------- */
6 
7 #pragma once
8 
9 #include "GAGenome.h"
10 #include <GAPopulation.h>
11 #include <gaconfig.h>
12 #include <gatypes.h>
13 
14 // Default settings and their names.
15 extern int gaDefNumBestGenomes;
16 extern int gaDefScoreFrequency1;
17 extern int gaDefScoreFrequency2;
18 extern int gaDefFlushFrequency;
19 extern std::string gaDefScoreFilename;
20 
30 {
31  public:
32  enum
33  {
34  NoScores = 0x00,
35  Mean = 0x01,
36  Maximum = 0x02,
37  Minimum = 0x04,
38  Deviation = 0x08,
39  Diversity = 0x10,
40  AllScores = 0xff
41  };
42 
43  GAStatistics();
44  GAStatistics(const GAStatistics &);
45  GAStatistics &operator=(const GAStatistics &orig)
46  {
47  copy(orig);
48  return *this;
49  }
50  virtual ~GAStatistics();
51  void copy(const GAStatistics &);
52 
53  float online() const { return on; }
54  float offlineMax() const { return offmax; }
55  float offlineMin() const { return offmin; }
56  float initial(int w = Maximum) const;
57  float current(int w = Maximum) const;
58  float maxEver() const { return maxever; }
59  float minEver() const { return minever; }
60 
61  int generation() const { return curgen; }
62  unsigned long int selections() const { return numsel; }
63  unsigned long int crossovers() const { return numcro; }
64  unsigned long int mutations() const { return nummut; }
65  unsigned long int replacements() const { return numrep; }
66  unsigned long int indEvals() const { return numeval; }
67  unsigned long int popEvals() const { return numpeval; }
68  float convergence() const;
69 
70  int nConvergence() const { return Nconv; }
71  int nConvergence(unsigned int);
72  int nBestGenomes(const GAGenome &, unsigned int);
73  int nBestGenomes() const { return (boa != nullptr ? boa->size() : 0); }
74  int scoreFrequency(unsigned int x) { return (scoreFreq = x); }
75  int scoreFrequency() const { return scoreFreq; }
76  int flushFrequency(unsigned int x);
77  int flushFrequency() const { return Nscrs; }
78  std::string scoreFilename(const std::string &filename);
79  std::string scoreFilename() const { return scorefile; }
80  int selectScores(int w) { return which = w; }
81  int selectScores() const { return which; }
82  bool recordDiversity(bool flag) { return dodiv = flag; }
83  bool recordDiversity() const { return dodiv; }
84  void flushScores();
85 
86  void update(const GAPopulation &pop);
87  void reset(const GAPopulation &pop);
88  const GAPopulation &bestPopulation() const { return *boa; }
89  const GAGenome &bestIndividual(unsigned int n = 0) const;
90 
91  int scores(const std::string &filename, int which = NoScores);
92  int scores(std::ostream &os, int which = NoScores);
93  int write(const std::string &filename) const;
94  int write(std::ostream &os) const;
95 
96  // These should be protected (accessible only to the GA class) but for now
97  // they are publicly accessible. Do not try to set these unless you know
98  // what you are doing!!
99  unsigned long int numsel; // number of selections since reset
100  unsigned long int numcro; // number of crossovers since reset
101  unsigned long int nummut; // number of mutations since reset
102  unsigned long int numrep; // number of replacements since reset
103  unsigned long int numeval; // number of individual evaluations since reset
104  unsigned long int numpeval; // number of population evals since reset
105 
106  protected:
107  unsigned int curgen; // current generation number
108  unsigned int scoreFreq; // how often (in generations) to record scores
109  bool dodiv; // should we record diversity?
110 
111  float maxever; // maximum score since initialization
112  float minever; // minimum score since initialization
113  float on; // "on-line" performance (ave of all scores)
114  float offmax; // "off-line" performance (ave of maximum)
115  float offmin; // "off-line" performance (ave of minimum)
116 
117  float aveInit; // stats from the initial population
118  float maxInit;
119  float minInit;
120  float devInit;
121  float divInit;
122 
123  float aveCur; // stats from the current population
124  float maxCur;
125  float minCur;
126  float devCur;
127  float divCur;
128 
129  unsigned int nconv, Nconv; // how many scores we're recording (flushFreq)
130  float *cscore; // best score of last n generations
131 
133  unsigned int nscrs, Nscrs;
135  std::vector<int> gen;
137  std::vector<float> aveScore;
139  std::vector<float> maxScore;
141  std::vector<float> minScore;
143  std::vector<float> devScore;
145  std::vector<float> divScore;
147  std::string scorefile;
149  int which;
152 
153  void setConvergence(float);
154  void setScore(const GAPopulation &);
155  void updateBestIndividual(const GAPopulation &, bool flag = false);
156  void writeScores();
157  void resizeScores(unsigned int);
158 
159  friend class GA;
160 };
161 
162 inline std::string GAStatistics::scoreFilename(const std::string &filename)
163 {
164  scorefile = filename;
165  return scorefile;
166 }
167 
168 inline float GAStatistics::convergence() const
169 {
170  double cnv = 0.0;
171  if (nconv >= Nconv - 1 && cscore[nconv % Nconv] != 0) {
172  cnv = static_cast<double>(cscore[(nconv + 1) % Nconv]) /
173  static_cast<double>(cscore[nconv % Nconv]);
174 }
175  return static_cast<float>(cnv);
176 }
177 
178 inline float GAStatistics::initial(int w) const
179 {
180  float val = 0.0;
181  switch (w)
182  {
183  case Mean:
184  val = aveInit;
185  break;
186  case Maximum:
187  val = maxInit;
188  break;
189  case Minimum:
190  val = minInit;
191  break;
192  case Deviation:
193  val = devInit;
194  break;
195  case Diversity:
196  val = divInit;
197  break;
198  default:
199  break;
200  }
201  return val;
202 }
203 
204 inline float GAStatistics::current(int w) const
205 {
206  float val = 0.0;
207  switch (w)
208  {
209  case Mean:
210  val = aveCur;
211  break;
212  case Maximum:
213  val = maxCur;
214  break;
215  case Minimum:
216  val = minCur;
217  break;
218  case Deviation:
219  val = devCur;
220  break;
221  case Diversity:
222  val = divCur;
223  break;
224  default:
225  break;
226  }
227  return val;
228 }
229 
230 inline std::ostream &operator<<(std::ostream &os, const GAStatistics &s)
231 {
232  s.write(os);
233  return os;
234 }
The base genome class just defines the genome interface - how to mutate, crossover,...
Definition: GAGenome.h:200
Definition: GAPopulation.h:66
Statistics class.
Definition: GAStatistics.h:30
std::string scorefile
name of file to which scores get written
Definition: GAStatistics.h:147
GAPopulation * boa
keep a copy of the best genomes
Definition: GAStatistics.h:151
std::vector< float > aveScore
average scores of each generation
Definition: GAStatistics.h:137
unsigned int nscrs
how many scores do we have?
Definition: GAStatistics.h:133
std::vector< float > devScore
stddev of each generation
Definition: GAStatistics.h:143
std::vector< float > divScore
diversity of each generation
Definition: GAStatistics.h:145
std::vector< float > minScore
worst scores of each generation
Definition: GAStatistics.h:141
std::vector< int > gen
generation number corresponding to scores
Definition: GAStatistics.h:135
std::vector< float > maxScore
best scores of each generation
Definition: GAStatistics.h:139
int which
which data to write to file
Definition: GAStatistics.h:149