Evocosm - A C++ Framework for Evolutionary Computing

Main Index

Created by Scott Robert Ladd at Coyote Gulch Productions.


selector.h
1 /*
2  Evocosm is a C++ framework for implementing evolutionary algorithms.
3 
4  Copyright 2011 Scott Robert Ladd. All rights reserved.
5 
6  Evocosm is user-supported open source software. Its continued development is dependent
7  on financial support from the community. You can provide funding by visiting the Evocosm
8  website at:
9 
10  http://www.coyotegulch.com
11 
12  You may license Evocosm in one of two fashions:
13 
14  1) Simplified BSD License (FreeBSD License)
15 
16  Redistribution and use in source and binary forms, with or without modification, are
17  permitted provided that the following conditions are met:
18 
19  1. Redistributions of source code must retain the above copyright notice, this list of
20  conditions and the following disclaimer.
21 
22  2. Redistributions in binary form must reproduce the above copyright notice, this list
23  of conditions and the following disclaimer in the documentation and/or other materials
24  provided with the distribution.
25 
26  THIS SOFTWARE IS PROVIDED BY SCOTT ROBERT LADD ``AS IS'' AND ANY EXPRESS OR IMPLIED
27  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
28  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SCOTT ROBERT LADD OR
29  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
32  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 
36  The views and conclusions contained in the software and documentation are those of the
37  authors and should not be interpreted as representing official policies, either expressed
38  or implied, of Scott Robert Ladd.
39 
40  2) Closed-Source Proprietary License
41 
42  If your project is a closed-source or proprietary project, the Simplified BSD License may
43  not be appropriate or desirable. In such cases, contact the Evocosm copyright holder to
44  arrange your purchase of an appropriate license.
45 
46  The author can be contacted at:
47 
48  scott.ladd@coyotegulch.com
49  scott.ladd@gmail.com
50  http:www.coyotegulch.com
51 */
52 
53 #if !defined(EVOCOSM_SELECTOR_H)
54 #define EVOCOSM_SELECTOR_H
55 
56 // Standard C++ Library
57 #include <algorithm>
58 
59 // libevocosm
60 #include "organism.h"
61 
62 namespace libevocosm
63 {
65 
78  template <class OrganismType>
79  class selector : protected globals
80  {
81  public:
83 
90  virtual ~selector()
91  {
92  // nada
93  }
94 
96 
102  virtual vector<OrganismType> select_survivors(vector<OrganismType> & a_population) = 0;
103  };
104 
106 
111  template <class OrganismType>
112  class null_selector : public selector<OrganismType>
113  {
114  public:
115  // Do-nothing selection function
120  virtual vector<OrganismType> select_survivors(vector<OrganismType> & a_population)
121  {
122  return vector<OrganismType>(); // an empty vector
123  }
124  };
125 
127 
132  template <class OrganismType>
133  class all_selector : public selector<OrganismType>
134  {
135  public:
136  // Do-nothing selection function
141  virtual vector<OrganismType> select_survivors(vector<OrganismType> & a_population)
142  {
143  vector<OrganismType> result;
144 
145  for (int n = 0; n < a_population.size(); ++n)
146  result.push_back(a_population[n]);
147 
148  return result;
149  }
150  };
151 
153 
158  template <class OrganismType>
159  class elitism_selector : public selector<OrganismType>
160  {
161  public:
163 
168  elitism_selector(double a_factor = 0.9)
169  : m_factor(a_factor)
170  {
171  // nada
172  }
173 
175 
180  : m_factor(a_source.m_factor)
181  {
182  // nada
183  }
184 
186 
191  {
192  m_factor = a_source.m_factor;
193  }
194 
196 
202  virtual vector<OrganismType> select_survivors(vector<OrganismType> & a_population);
203 
204  private:
205  // number of organisms to keep
206  double m_factor;
207  };
208 
209  template <class OrganismType>
210  vector<OrganismType> elitism_selector<OrganismType>::select_survivors(vector<OrganismType> & a_population)
211  {
212  // create a new vector
213  vector<OrganismType> chosen_ones;
214 
215  // get population stats
216  fitness_stats<OrganismType> stats(a_population);
217 
218  // calculate survival based on percentage of best fitness
219  double threshold = m_factor * stats.getBest().fitness;
220 
221  // pick survivors
222  for (int n = 0; n < a_population.size(); ++n)
223  {
224  if (a_population[n].fitness > threshold)
225  chosen_ones.push_back(a_population[n]);
226  }
227 
228  // return result
229  return chosen_ones;
230  }
231 
232 };
233 
234 #endif
Implements a elitism selector.
Definition: selector.h:159
virtual ~selector()
Virtual destructor.
Definition: selector.h:90
A do-nothing selector.
Definition: selector.h:133
virtual vector< OrganismType > select_survivors(vector< OrganismType > &a_population)
Definition: selector.h:141
elitism_selector(double a_factor=0.9)
Constructor.
Definition: selector.h:168
virtual vector< OrganismType > select_survivors(vector< OrganismType > &a_population)
Definition: selector.h:120
virtual vector< OrganismType > select_survivors(vector< OrganismType > &a_population)
Select individuals that survive.
Definition: selector.h:210
A toolkit and framework for implementing evolutionary algorithms.
Definition: analyzer.h:60
A do-nothing selector.
Definition: selector.h:112
Elements shared by all classes in Evocosm.
Definition: evocommon.h:117
Selects organisms that survive.
Definition: selector.h:79
elitism_selector(const elitism_selector< OrganismType > &a_source)
Copy constructor.
Definition: selector.h:179
virtual vector< OrganismType > select_survivors(vector< OrganismType > &a_population)=0
Select individuals that survive.
OrganismType getBest()
Get the organism with the highest fitness for analyzed population.
Definition: stats.h:177
elitism_selector & operator=(const elitism_selector< OrganismType > &a_source)
Assignment operator.
Definition: selector.h:190
Population fitness statistics.
Definition: stats.h:74

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