Evocosm - A C++ Framework for Evolutionary Computing

Main Index

Created by Scott Robert Ladd at Coyote Gulch Productions.


selector.h
1 //---------------------------------------------------------------------
2 // Algorithmic Conjurings @ http://www.coyotegulch.com
3 // Evocosm -- An Object-Oriented Framework for Evolutionary Algorithms
4 //
5 // selector.h
6 //---------------------------------------------------------------------
7 //
8 // Copyright 1996, 1999, 2002, 2003, 2004, 2005, 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(EVOCOSM_SELECTOR_H)
36 #define EVOCOSM_SELECTOR_H
37 
38 // Standard C++ Library
39 #include <algorithm>
40 
41 // libevocosm
42 #include "organism.h"
43 
44 namespace libevocosm
45 {
47 
60  template <class OrganismType>
61  class selector : protected globals
62  {
63  public:
65 
72  virtual ~selector()
73  {
74  // nada
75  }
76 
78 
84  virtual vector<OrganismType> select_survivors(vector<OrganismType> & a_population) = 0;
85  };
86 
88 
93  template <class OrganismType>
94  class null_selector : public selector<OrganismType>
95  {
96  public:
97  // Do-nothing selection function
102  virtual vector<OrganismType> select_survivors(vector<OrganismType> & a_population)
103  {
104  return vector<OrganismType>(); // an empty vector
105  }
106  };
107 
109 
114  template <class OrganismType>
115  class elitism_selector : public selector<OrganismType>
116  {
117  public:
119 
124  elitism_selector(size_t a_how_many = 1)
125  : m_how_many(a_how_many)
126  {
127  // nada
128  }
129 
131 
136  : m_how_many(a_source.how_many)
137  {
138  // nada
139  }
140 
142 
147  {
148  m_how_many = a_source.m_how_many;
149  }
150 
152 
158  virtual vector<OrganismType> select_survivors(vector<OrganismType> & a_population);
159 
160  private:
161  // number of organisms to keep
162  size_t m_how_many;
163  };
164 
165  template <class OrganismType>
166  vector<OrganismType> elitism_selector<OrganismType>::select_survivors(vector<OrganismType> & a_population)
167  {
168  // create a new vector
169  vector<OrganismType> chosen_ones;
170 
171  if (m_how_many > 0)
172  {
173  for (typename vector<OrganismType>::iterator i = a_population.begin(); i != a_population.end(); ++i)
174  {
175  typename vector<OrganismType>::iterator c = chosen_ones.begin();
176  size_t n = 0;
177 
178  while ((c != chosen_ones.end()) && (n < m_how_many))
179  {
180  if (i->fitness() > c->fitness())
181  break;
182 
183  ++c;
184  ++n;
185  }
186 
187  if (n < m_how_many)
188  chosen_ones.insert(c,*i);
189 
190  if (chosen_ones.size() > m_how_many)
191  chosen_ones.pop_back();
192  }
193 
194  }
195 
196  // return result
197  return chosen_ones;
198  }
199 
200 };
201 
202 #endif
Selects organisms that survive.
Definition: selector.h:61
Implements a elitism selector.
Definition: selector.h:115
virtual vector< OrganismType > select_survivors(vector< OrganismType > &a_population)
Select individuals that survive.
Definition: selector.h:166
virtual ~selector()
Virtual destructor.
Definition: selector.h:72
virtual vector< OrganismType > select_survivors(vector< OrganismType > &a_population)
Definition: selector.h:102
virtual vector< OrganismType > select_survivors(vector< OrganismType > &a_population)=0
Select individuals that survive.
elitism_selector & operator=(const elitism_selector< OrganismType > &a_source)
Assignment operator.
Definition: selector.h:146
A do-nothing selector.
Definition: selector.h:94
A toolkit and framework for implementing evolutionary algorithms.
Definition: evocommon.h:60
elitism_selector(const elitism_selector< OrganismType > &a_source)
Copy constructor.
Definition: selector.h:135
Elements shared by all classes in Evocosm.
Definition: evocommon.h:115
elitism_selector(size_t a_how_many=1)
Constructor.
Definition: selector.h:124

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