Created by Scott Robert Ladd at Coyote Gulch Productions.
00001 //--------------------------------------------------------------------- 00002 // Algorithmic Conjurings @ http://www.coyotegulch.com 00003 // Evocosm -- An Object-Oriented Framework for Evolutionary Algorithms 00004 // 00005 // selector.h 00006 //--------------------------------------------------------------------- 00007 // 00008 // Copyright 1996, 1999, 2002, 2003, 2004, 2005, 2007 Scott Robert Ladd 00009 // 00010 // This program is free software; you can redistribute it and/or modify 00011 // it under the terms of the GNU General Public License as published by 00012 // the Free Software Foundation; either version 2 of the License, or 00013 // (at your option) any later version. 00014 // 00015 // This program is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 // GNU General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU General Public License 00021 // along with this program; if not, write to the 00022 // Free Software Foundation, Inc. 00023 // 59 Temple Place - Suite 330 00024 // Boston, MA 02111-1307, USA. 00025 // 00026 //----------------------------------------------------------------------- 00027 // 00028 // For more information on this software package, please visit 00029 // Scott's web site, Coyote Gulch Productions, at: 00030 // 00031 // http://www.coyotegulch.com 00032 // 00033 //----------------------------------------------------------------------- 00034 00035 #if !defined(EVOCOSM_SELECTOR_H) 00036 #define EVOCOSM_SELECTOR_H 00037 00038 // Standard C++ Library 00039 #include <algorithm> 00040 00041 // libevocosm 00042 #include "organism.h" 00043 00044 namespace libevocosm 00045 { 00047 00060 template <class OrganismType> 00061 class selector : protected globals 00062 { 00063 public: 00065 00072 virtual ~selector() 00073 { 00074 // nada 00075 } 00076 00078 00084 virtual vector<OrganismType> select_survivors(vector<OrganismType> & a_population) = 0; 00085 }; 00086 00088 00093 template <class OrganismType> 00094 class null_selector : public selector<OrganismType> 00095 { 00096 public: 00097 // Do-nothing selection function 00102 virtual vector<OrganismType> select_survivors(vector<OrganismType> & a_population) 00103 { 00104 return vector<OrganismType>(); // an empty vector 00105 } 00106 }; 00107 00109 00114 template <class OrganismType> 00115 class elitism_selector : public selector<OrganismType> 00116 { 00117 public: 00119 00124 elitism_selector(size_t a_how_many = 1) 00125 : m_how_many(a_how_many) 00126 { 00127 // nada 00128 } 00129 00131 00135 elitism_selector(const elitism_selector<OrganismType> & a_source) 00136 : m_how_many(a_source.how_many) 00137 { 00138 // nada 00139 } 00140 00142 00146 elitism_selector & operator = (const elitism_selector<OrganismType> & a_source) 00147 { 00148 m_how_many = a_source.m_how_many; 00149 } 00150 00152 00158 virtual vector<OrganismType> select_survivors(vector<OrganismType> & a_population); 00159 00160 private: 00161 // number of organisms to keep 00162 size_t m_how_many; 00163 }; 00164 00165 template <class OrganismType> 00166 vector<OrganismType> elitism_selector<OrganismType>::select_survivors(vector<OrganismType> & a_population) 00167 { 00168 // create a new vector 00169 vector<OrganismType> chosen_ones; 00170 00171 if (m_how_many > 0) 00172 { 00173 for (typename vector<OrganismType>::iterator i = a_population.begin(); i != a_population.end(); ++i) 00174 { 00175 typename vector<OrganismType>::iterator c = chosen_ones.begin(); 00176 size_t n = 0; 00177 00178 while ((c != chosen_ones.end()) && (n < m_how_many)) 00179 { 00180 if (i->fitness() > c->fitness()) 00181 break; 00182 00183 ++c; 00184 ++n; 00185 } 00186 00187 if (n < m_how_many) 00188 chosen_ones.insert(c,*i); 00189 00190 if (chosen_ones.size() > m_how_many) 00191 chosen_ones.pop_back(); 00192 } 00193 00194 } 00195 00196 // return result 00197 return chosen_ones; 00198 } 00199 00200 }; 00201 00202 #endif
© 1996-2005 Scott Robert Ladd. All rights reserved.
HTML documentation generated by Dimitri van Heesch's excellent Doxygen tool.