Created by Scott Robert Ladd at Coyote Gulch Productions.
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(EVOCOSM_SELECTOR_H) 00054 #define EVOCOSM_SELECTOR_H 00055 00056 // Standard C++ Library 00057 #include <algorithm> 00058 00059 // libevocosm 00060 #include "organism.h" 00061 00062 namespace libevocosm 00063 { 00065 00078 template <class OrganismType> 00079 class selector : protected globals 00080 { 00081 public: 00083 00090 virtual ~selector() 00091 { 00092 // nada 00093 } 00094 00096 00102 virtual vector<OrganismType> select_survivors(vector<OrganismType> & a_population) = 0; 00103 }; 00104 00106 00111 template <class OrganismType> 00112 class null_selector : public selector<OrganismType> 00113 { 00114 public: 00115 // Do-nothing selection function 00120 virtual vector<OrganismType> select_survivors(vector<OrganismType> & a_population) 00121 { 00122 return vector<OrganismType>(); // an empty vector 00123 } 00124 }; 00125 00127 00132 template <class OrganismType> 00133 class all_selector : public selector<OrganismType> 00134 { 00135 public: 00136 // Do-nothing selection function 00141 virtual vector<OrganismType> select_survivors(vector<OrganismType> & a_population) 00142 { 00143 vector<OrganismType> result; 00144 00145 for (int n = 0; n < a_population.size(); ++n) 00146 result.push_back(a_population[n]); 00147 00148 return result; 00149 } 00150 }; 00151 00153 00158 template <class OrganismType> 00159 class elitism_selector : public selector<OrganismType> 00160 { 00161 public: 00163 00168 elitism_selector(double a_factor = 0.9) 00169 : m_factor(a_factor) 00170 { 00171 // nada 00172 } 00173 00175 00179 elitism_selector(const elitism_selector<OrganismType> & a_source) 00180 : m_factor(a_source.m_factor) 00181 { 00182 // nada 00183 } 00184 00186 00190 elitism_selector & operator = (const elitism_selector<OrganismType> & a_source) 00191 { 00192 m_factor = a_source.m_factor; 00193 } 00194 00196 00202 virtual vector<OrganismType> select_survivors(vector<OrganismType> & a_population); 00203 00204 private: 00205 // number of organisms to keep 00206 double m_factor; 00207 }; 00208 00209 template <class OrganismType> 00210 vector<OrganismType> elitism_selector<OrganismType>::select_survivors(vector<OrganismType> & a_population) 00211 { 00212 // create a new vector 00213 vector<OrganismType> chosen_ones; 00214 00215 // get population stats 00216 fitness_stats<OrganismType> stats(a_population); 00217 00218 // calculate survival based on percentage of best fitness 00219 double threshold = m_factor * stats.getBest().fitness; 00220 00221 // pick survivors 00222 for (int n = 0; n < a_population.size(); ++n) 00223 { 00224 if (a_population[n].fitness > threshold) 00225 chosen_ones.push_back(a_population[n]); 00226 } 00227 00228 // return result 00229 return chosen_ones; 00230 } 00231 00232 }; 00233 00234 #endif
© 1996-2005 Scott Robert Ladd. All rights reserved.
HTML documentation generated by Dimitri van Heesch's excellent Doxygen tool.