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 // migrator.h 00006 //--------------------------------------------------------------------- 00007 // 00008 // Copyright 1996, 1999, 2002, 2003, 2004, 2005 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(LIBEVOCOSM_MIGRATOR_H) 00036 #define LIBEVOCOSM_MIGRATOR_H 00037 00038 // libevocosm 00039 #include "organism.h" 00040 00041 namespace libevocosm 00042 { 00044 00052 template <class OrganismType> 00053 class migrator : protected globals 00054 { 00055 public: 00057 00064 virtual ~migrator() 00065 { 00066 // nada 00067 } 00068 00070 00080 virtual void migrate(vector< vector<OrganismType> > & a_populations) = 0; 00081 }; 00082 00084 00089 template <class OrganismType> 00090 class null_migrator : public migrator<OrganismType> 00091 { 00092 public: 00094 00098 virtual void migrate(vector< vector<OrganismType> > & a_populations) 00099 { 00100 // nada 00101 } 00102 }; 00103 00105 00110 template <class OrganismType> 00111 class random_pool_migrator : public migrator<OrganismType> 00112 { 00113 public: 00115 00118 random_pool_migrator(size_t a_how_many) 00119 : m_how_many(a_how_many) 00120 { 00121 // nada 00122 } 00123 00125 00129 virtual void migrate(vector< vector<OrganismType> > & a_populations) 00130 { 00131 // don't do anything is the value is zero 00132 if (m_how_many == 0) 00133 return; 00134 00135 // get this value now so we don't keep calling size() 00136 size_t pop_count = a_populations.size(); 00137 00138 // keeps track of how many organisms have change in a given population 00139 size_t * move_count = new size_t [pop_count]; 00140 00141 for (size_t n = 0; n < pop_count; ++n) 00142 move_count[n] = 0; 00143 00144 // migrate organisms 00145 for (size_t p = 0; p < pop_count; ++p) 00146 { 00147 // don't move any more organisms if this population is at its limit 00148 if (move_count[p] < m_how_many) 00149 { 00150 size_t pop_size = a_populations[p].size(); 00151 size_t number_to_move = m_how_many - move_count[p]; 00152 00153 for (size_t n = 0; n < number_to_move; ++n); 00154 { 00155 // pick a random organism 00156 size_t org1 = globals::g_random.get_rand_index(pop_size); 00157 00158 // pick another population 00159 size_t trader = globals::g_random.get_rand_index(pop_count); 00160 00161 // Sequential search for someone who hasn't reached their trade limit; 00162 // can't keep randomly selecting because we might never hit the "one" 00163 // open trader late in the sequence. Can't skip the current "p" 00164 // population because it might be the only one that hasn't swapped yet! 00165 while (move_count[trader] >= m_how_many) 00166 { 00167 ++trader; 00168 00169 if (trader == pop_count) 00170 trader = 0; 00171 } 00172 00173 if (trader != p) 00174 { 00175 // pick random member of other population 00176 size_t org2 = globals::g_random.get_rand_index(a_populations[trader].size()); 00177 00178 // exchange organisms 00179 OrganismType temp = a_populations[p][org1]; 00180 a_populations[p][org1] = a_populations[trader][org2]; 00181 a_populations[trader][org2] = temp; 00182 00183 // increment counts 00184 ++move_count[p]; 00185 ++move_count[trader]; 00186 } 00187 } 00188 } 00189 } 00190 00191 delete [] move_count; 00192 } 00193 00194 private: 00195 // How many to migrate 00196 size_t m_how_many; 00197 }; 00198 00199 }; 00200 00201 #endif
© 1996-2005 Scott Robert Ladd. All rights reserved.
HTML documentation generated by Dimitri van Heesch's excellent Doxygen tool.