Evocosm - A C++ Framework for Evolutionary Computing

Main Index

Created by Scott Robert Ladd at Coyote Gulch Productions.


stats.h
00001 /*
00002     Evocosm is a C++ framework for implementing evolutionary algorithms.
00003 
00004     Copyright 2011 Scott Robert Ladd. All rights reserved.
00005 
00006     Evocosm is user-supported open source software. Its continued development is dependent
00007     on financial support from the community. You can provide funding by visiting the Evocosm
00008     website at:
00009 
00010         http://www.coyotegulch.com
00011 
00012     You may license Evocosm in one of two fashions:
00013 
00014     1) Simplified BSD License (FreeBSD License)
00015 
00016     Redistribution and use in source and binary forms, with or without modification, are
00017     permitted provided that the following conditions are met:
00018 
00019     1.  Redistributions of source code must retain the above copyright notice, this list of
00020         conditions and the following disclaimer.
00021 
00022     2.  Redistributions in binary form must reproduce the above copyright notice, this list
00023         of conditions and the following disclaimer in the documentation and/or other materials
00024         provided with the distribution.
00025 
00026     THIS SOFTWARE IS PROVIDED BY SCOTT ROBERT LADD ``AS IS'' AND ANY EXPRESS OR IMPLIED
00027     WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00028     FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SCOTT ROBERT LADD OR
00029     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00030     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00032     ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00033     NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00034     ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035 
00036     The views and conclusions contained in the software and documentation are those of the
00037     authors and should not be interpreted as representing official policies, either expressed
00038     or implied, of Scott Robert Ladd.
00039 
00040     2) Closed-Source Proprietary License
00041 
00042     If your project is a closed-source or proprietary project, the Simplified BSD License may
00043     not be appropriate or desirable. In such cases, contact the Evocosm copyright holder to
00044     arrange your purchase of an appropriate license.
00045 
00046     The author can be contacted at:
00047 
00048           scott.ladd@coyotegulch.com
00049           scott.ladd@gmail.com
00050           http:www.coyotegulch.com
00051 */
00052 
00053 #if !defined(LIBEVOCOSM_STATS_H)
00054 #define LIBEVOCOSM_STATS_H
00055 
00056 // libevocosm
00057 #include "organism.h"
00058 
00059 // Standard C Library
00060 #include <cmath>
00061 
00062 // Standard C++
00063 #include <limits>
00064 
00065 namespace libevocosm
00066 {
00068 
00073     template <class OrganismType>
00074     class fitness_stats
00075     {
00076     private:
00077 
00078         double min;
00079         double max;
00080         double mean;
00081         double variance;
00082         double sigma;
00083         OrganismType * best;
00084         OrganismType * worst;
00085 
00086     public:
00087 
00089 
00095         fitness_stats(const vector<OrganismType> & a_population)
00096           : best(new OrganismType(a_population[0])),
00097             worst(new OrganismType(a_population[0]))
00098         {
00099             // calculate max, average, and minimum fitness for the population
00100             max = std::numeric_limits<double>::min();
00101             min = std::numeric_limits<double>::max();
00102             mean = 0.0;
00103             variance = 0.0;
00104 
00105             for (int n = 0; n < a_population.size(); ++n)
00106             {
00107                 // do we have a new maximum?
00108                 if (a_population[n].fitness > max)
00109                 {
00110                     max = a_population[n].fitness;
00111 
00112                     if (best != NULL)
00113                         delete best;
00114 
00115                     best = new OrganismType(a_population[n]);
00116                 }
00117 
00118                 // do we have a new minimum?
00119                 if (a_population[n].fitness < min)
00120                 {
00121                     min = a_population[n].fitness;
00122 
00123                     if (worst != NULL)
00124                         delete worst;
00125 
00126                     worst = new OrganismType(a_population[n]);
00127                 }
00128 
00129                 // accumulate for average
00130                 mean += a_population[n].fitness;
00131             }
00132 
00133             mean /= double(a_population.size());
00134 
00135             for (int n = 0; n < a_population.size(); ++n)
00136             {
00137                 double diff = a_population[n].fitness - mean;
00138                 variance += (diff * diff);
00139             }
00140 
00141             variance /= static_cast<double>(a_population.size() - 1);
00142 
00143             // calculate 2 times the std. deviation (sigma)
00144             sigma = sqrt(variance);
00145         }
00146 
00148 
00151         virtual ~fitness_stats()
00152         {
00153             if (best != NULL)
00154                 delete best;
00155 
00156             if (worst != NULL)
00157                 delete worst;
00158         }
00159 
00160 
00162         double getMin() { return min; }
00163 
00165         double getMax() { return max; }
00166 
00168         double getMean() { return mean; }
00169 
00171         double getVariance() { return variance; }
00172 
00174         double getSigma() { return sigma; }
00175 
00177         OrganismType getBest() { return *best; }
00178 
00180         OrganismType getWorst() { return *worst; }
00181     };
00182 
00183 };
00184 
00185 #endif

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