Evocosm - A C++ Framework for Evolutionary Computing

Main Index

Created by Scott Robert Ladd at Coyote Gulch Productions.


migrator.h
1 //---------------------------------------------------------------------
2 // Algorithmic Conjurings @ http://www.coyotegulch.com
3 // Evocosm -- An Object-Oriented Framework for Evolutionary Algorithms
4 //
5 // migrator.h
6 //---------------------------------------------------------------------
7 //
8 // Copyright 1996, 1999, 2002, 2003, 2004, 2005, 2006, 2007 Scott Robert Ladd
9 //
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the
22 // Free Software Foundation, Inc.
23 // 59 Temple Place - Suite 330
24 // Boston, MA 02111-1307, USA.
25 //
26 //-----------------------------------------------------------------------
27 //
28 // For more information on this software package, please visit
29 // Scott's web site, Coyote Gulch Productions, at:
30 //
31 // http://www.coyotegulch.com
32 //
33 //-----------------------------------------------------------------------
34 
35 #if !defined(LIBEVOCOSM_MIGRATOR_H)
36 #define LIBEVOCOSM_MIGRATOR_H
37 
38 // libevocosm
39 #include "organism.h"
40 
41 namespace libevocosm
42 {
44 
52  template <class OrganismType>
53  class migrator : protected globals
54  {
55  public:
57 
64  virtual ~migrator()
65  {
66  // nada
67  }
68 
70 
80  virtual void migrate(vector< vector<OrganismType> > & a_populations) = 0;
81  };
82 
84 
89  template <class OrganismType>
90  class null_migrator : public migrator<OrganismType>
91  {
92  public:
94 
98  virtual void migrate(vector< vector<OrganismType> > & a_populations)
99  {
100  // nada
101  }
102  };
103 
105 
110  template <class OrganismType>
111  class random_pool_migrator : public migrator<OrganismType>
112  {
113  public:
115 
118  random_pool_migrator(size_t a_how_many)
119  : m_how_many(a_how_many)
120  {
121  // nada
122  }
123 
125 
129  virtual void migrate(vector< vector<OrganismType> > & a_populations)
130  {
131  // don't do anything is the value is zero
132  if (m_how_many == 0)
133  return;
134 
135  // get this value now so we don't keep calling size()
136  size_t pop_count = a_populations.size();
137 
138  // keeps track of how many organisms have change in a given population
139  size_t * move_count = new size_t [pop_count];
140 
141  for (size_t n = 0; n < pop_count; ++n)
142  move_count[n] = 0;
143 
144  // migrate organisms
145  for (size_t p = 0; p < pop_count; ++p)
146  {
147  // don't move any more organisms if this population is at its limit
148  if (move_count[p] < m_how_many)
149  {
150  size_t pop_size = a_populations[p].size();
151  size_t number_to_move = m_how_many - move_count[p];
152 
153  for (size_t n = 0; n < number_to_move; ++n);
154  {
155  // pick a random organism
156  size_t org1 = globals::rand_index(pop_size);
157 
158  // pick another population
159  size_t trader = globals::rand_index(pop_count);
160 
161  // Sequential search for someone who hasn't reached their trade limit;
162  // can't keep randomly selecting because we might never hit the "one"
163  // open trader late in the sequence. Can't skip the current "p"
164  // population because it might be the only one that hasn't swapped yet!
165  while (move_count[trader] >= m_how_many)
166  {
167  ++trader;
168 
169  if (trader == pop_count)
170  trader = 0;
171  }
172 
173  if (trader != p)
174  {
175  // pick random member of other population
176  size_t org2 = globals::rand_index(a_populations[trader].size());
177 
178  // exchange organisms
179  OrganismType temp = a_populations[p][org1];
180  a_populations[p][org1] = a_populations[trader][org2];
181  a_populations[trader][org2] = temp;
182 
183  // increment counts
184  ++move_count[p];
185  ++move_count[trader];
186  }
187  }
188  }
189  }
190 
191  delete [] move_count;
192  }
193 
194  private:
195  // How many to migrate
196  size_t m_how_many;
197  };
198 
199 };
200 
201 #endif
virtual void migrate(vector< vector< OrganismType > > &a_populations)=0
Removes organisms from a population for later immigration.
virtual ~migrator()
Virtual destructor.
Definition: migrator.h:64
static size_t rand_index(size_t n)
Static function to allow use of g_random function pointer in random_shuffle.
Definition: evocommon.h:119
A do-nothing migrator.
Definition: migrator.h:90
Defines migration between populations.
Definition: migrator.h:53
random_pool_migrator(size_t a_how_many)
Constructor.
Definition: migrator.h:118
A toolkit and framework for implementing evolutionary algorithms.
Definition: evocommon.h:60
A pool-based migrator.
Definition: migrator.h:111
Elements shared by all classes in Evocosm.
Definition: evocommon.h:115
virtual void migrate(vector< vector< OrganismType > > &a_populations)
Do-nothing emigration.
Definition: migrator.h:98
virtual void migrate(vector< vector< OrganismType > > &a_populations)
Emigrate organisms to pool.
Definition: migrator.h:129

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