Evocosm - A C++ Framework for Evolutionary Computing

Main Index

Created by Scott Robert Ladd at Coyote Gulch Productions.


stats.h
1 /*
2  Evocosm is a C++ framework for implementing evolutionary algorithms.
3 
4  Copyright 2011 Scott Robert Ladd. All rights reserved.
5 
6  Evocosm is user-supported open source software. Its continued development is dependent
7  on financial support from the community. You can provide funding by visiting the Evocosm
8  website at:
9 
10  http://www.coyotegulch.com
11 
12  You may license Evocosm in one of two fashions:
13 
14  1) Simplified BSD License (FreeBSD License)
15 
16  Redistribution and use in source and binary forms, with or without modification, are
17  permitted provided that the following conditions are met:
18 
19  1. Redistributions of source code must retain the above copyright notice, this list of
20  conditions and the following disclaimer.
21 
22  2. Redistributions in binary form must reproduce the above copyright notice, this list
23  of conditions and the following disclaimer in the documentation and/or other materials
24  provided with the distribution.
25 
26  THIS SOFTWARE IS PROVIDED BY SCOTT ROBERT LADD ``AS IS'' AND ANY EXPRESS OR IMPLIED
27  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
28  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SCOTT ROBERT LADD OR
29  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
32  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 
36  The views and conclusions contained in the software and documentation are those of the
37  authors and should not be interpreted as representing official policies, either expressed
38  or implied, of Scott Robert Ladd.
39 
40  2) Closed-Source Proprietary License
41 
42  If your project is a closed-source or proprietary project, the Simplified BSD License may
43  not be appropriate or desirable. In such cases, contact the Evocosm copyright holder to
44  arrange your purchase of an appropriate license.
45 
46  The author can be contacted at:
47 
48  scott.ladd@coyotegulch.com
49  scott.ladd@gmail.com
50  http:www.coyotegulch.com
51 */
52 
53 #if !defined(LIBEVOCOSM_STATS_H)
54 #define LIBEVOCOSM_STATS_H
55 
56 // libevocosm
57 #include "organism.h"
58 
59 // Standard C Library
60 #include <cmath>
61 
62 // Standard C++
63 #include <limits>
64 
65 namespace libevocosm
66 {
68 
73  template <class OrganismType>
75  {
76  private:
77 
78  double min;
79  double max;
80  double mean;
81  double variance;
82  double sigma;
83  OrganismType * best;
84  OrganismType * worst;
85 
86  public:
87 
89 
95  fitness_stats(const vector<OrganismType> & a_population)
96  : best(new OrganismType(a_population[0])),
97  worst(new OrganismType(a_population[0]))
98  {
99  // calculate max, average, and minimum fitness for the population
100  max = std::numeric_limits<double>::min();
101  min = std::numeric_limits<double>::max();
102  mean = 0.0;
103  variance = 0.0;
104 
105  for (int n = 0; n < a_population.size(); ++n)
106  {
107  // do we have a new maximum?
108  if (a_population[n].fitness > max)
109  {
110  max = a_population[n].fitness;
111 
112  if (best != NULL)
113  delete best;
114 
115  best = new OrganismType(a_population[n]);
116  }
117 
118  // do we have a new minimum?
119  if (a_population[n].fitness < min)
120  {
121  min = a_population[n].fitness;
122 
123  if (worst != NULL)
124  delete worst;
125 
126  worst = new OrganismType(a_population[n]);
127  }
128 
129  // accumulate for average
130  mean += a_population[n].fitness;
131  }
132 
133  mean /= double(a_population.size());
134 
135  for (int n = 0; n < a_population.size(); ++n)
136  {
137  double diff = a_population[n].fitness - mean;
138  variance += (diff * diff);
139  }
140 
141  variance /= static_cast<double>(a_population.size() - 1);
142 
143  // calculate 2 times the std. deviation (sigma)
144  sigma = sqrt(variance);
145  }
146 
148 
151  virtual ~fitness_stats()
152  {
153  if (best != NULL)
154  delete best;
155 
156  if (worst != NULL)
157  delete worst;
158  }
159 
160 
162  double getMin() { return min; }
163 
165  double getMax() { return max; }
166 
168  double getMean() { return mean; }
169 
171  double getVariance() { return variance; }
172 
174  double getSigma() { return sigma; }
175 
177  OrganismType getBest() { return *best; }
178 
180  OrganismType getWorst() { return *worst; }
181  };
182 
183 };
184 
185 #endif
double getMin()
Get the minimum fitness value for analyzed population.
Definition: stats.h:162
virtual ~fitness_stats()
Destructor.
Definition: stats.h:151
double getMean()
Get the mean (average) fitness value for analyzed population.
Definition: stats.h:168
A toolkit and framework for implementing evolutionary algorithms.
Definition: analyzer.h:60
double getSigma()
Get the standard deviation (sigma) value for fitness.
Definition: stats.h:174
OrganismType getBest()
Get the organism with the highest fitness for analyzed population.
Definition: stats.h:177
OrganismType getWorst()
Get the organism with the lowest fitness for analyzed population.
Definition: stats.h:180
double getVariance()
Get the fitness variance for analyzed population.
Definition: stats.h:171
double getMax()
Get the maximum fitness value for analyzed population.
Definition: stats.h:165
fitness_stats(const vector< OrganismType > &a_population)
Construct a statistics object for a specific population.
Definition: stats.h:95
Population fitness statistics.
Definition: stats.h:74

© 1996-2005 Scott Robert Ladd. All rights reserved.
HTML documentation generated by Dimitri van Heesch's excellent Doxygen tool.