Evocosm - A C++ Framework for Evolutionary Computing

Main Index

Created by Scott Robert Ladd at Coyote Gulch Productions.


scaler.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(LIBEVOCOSM_SCALER_H)
54 #define LIBEVOCOSM_SCALER_H
55 
56 // libevocosm
57 #include "organism.h"
58 #include "stats.h"
59 
60 namespace libevocosm
61 {
63 
71  template <class OrganismType>
72  class scaler : protected globals
73  {
74  public:
76 
83  virtual ~scaler()
84  {
85  // nada
86  }
87 
89 
95  virtual void scale_fitness(vector<OrganismType> & a_population) = 0;
96  };
97 
99 
104  template <class OrganismType>
105  class null_scaler : public scaler<OrganismType>
106  {
107  public:
109 
113  virtual void scale_fitness(vector<OrganismType> & a_population)
114  {
115  // nada
116  }
117  };
118 
120 
125  template <class OrganismType>
126  class linear_norm_scaler : public scaler<OrganismType>
127  {
128  public:
130 
133  linear_norm_scaler(double a_fitness_multiple = 2.0)
134  : m_fitness_multiple(a_fitness_multiple)
135  {
136  // nada
137  }
138 
140 
144  virtual void scale_fitness(vector<OrganismType> & a_population)
145  {
146  // calculate max, average, and minimum fitness for the population
147  fitness_stats<OrganismType> stats(a_population);
148 
149  // calculate coefficients for fitness scaling
150  double slope;
151  double intercept;
152  double delta;
153 
154  if (stats.getMin() > ((m_fitness_multiple * stats.getMean() - stats.getMax()) / (m_fitness_multiple - 1.0)))
155  {
156  // normal scaling
157  delta = stats.getMax() - stats.getMean();
158  slope = (m_fitness_multiple - 1.0) * stats.getMean() / delta;
159  intercept = stats.getMean() * (stats.getMax() - m_fitness_multiple * stats.getMean()) / delta;
160  }
161  else
162  {
163  // extreme scaling
164  delta = stats.getMean() - stats.getMin();
165  slope = stats.getMean() / delta;
166  intercept = -stats.getMin() * stats.getMean() / delta;
167  }
168 
169  // adjust fitness values
170  for (int n = 0; n < a_population.size(); ++n)
171  a_population[n].fitness = slope * a_population[n].fitness + intercept;
172  }
173 
174  private:
175  double m_fitness_multiple;
176  };
177 
179 
184  template <class OrganismType>
185  class windowed_scaler : public scaler<OrganismType>
186  {
187  public:
189 
193  {
194  // nada
195  }
196 
198 
202  virtual void scale_fitness(vector<OrganismType> & a_population)
203  {
204  fitness_stats<OrganismType> stats(a_population);
205 
206  // assign new fitness values
207  for (int n = 0; n < a_population.size(); ++n)
208  a_population[n].fitness = stats.getMin();
209  }
210  };
211 
213 
218  template <class OrganismType>
219  class exponential_scaler : public scaler<OrganismType>
220  {
221  public:
223 
230  exponential_scaler(double a_a = 1.0, double a_b = 1.0, double a_power = 2.0)
231  : m_a(a_a),
232  m_b(a_b),
233  m_power(a_power)
234  {
235  // nada
236  }
237 
239 
243  virtual void scale_fitness(vector<OrganismType> & a_population)
244  {
245  // assign new fitness values
246  for (int n = 0; n < a_population.size(); ++n)
247  a_population[n].fitness = pow((m_a * a_population[n].fitness + m_b),m_power);
248  }
249 
250  private:
251  double m_a;
252  double m_b;
253  double m_power;
254  };
255 
257 
261  template <class OrganismType>
262  class quadratic_scaler : public scaler<OrganismType>
263  {
264  public:
266 
269  quadratic_scaler(double a_a, double a_b, double a_c)
270  : m_a(a_a), m_b(a_b), m_c(a_c)
271  {
272  // nada
273  }
274 
276 
280  virtual void scale_fitness(vector<OrganismType> & a_population)
281  {
282  // adjust fitness values
283  for (int n = 0; n < a_population.size(); ++n)
284  {
285  double f = a_population[n].fitness;
286  a_population[n].fitness = m_a * pow(f,2.0) + m_b * f + m_c;
287  }
288  }
289 
290  private:
291  double m_a;
292  double m_b;
293  double m_c;
294  };
295 
297 
301  template <class OrganismType>
302  class sigma_scaler : public scaler<OrganismType>
303  {
304  public:
306 
310  {
311  }
312 
314 
321  virtual void scale_fitness(vector<OrganismType> & a_population)
322  {
323  fitness_stats<OrganismType> stats(a_population);
324 
325  // calculate 2 times the std. deviation (sigma)
326  double sigma2 = 2.0 * stats.getSigma();
327 
328  // now assign new fitness values
329  if (sigma2 == 0.0)
330  {
331  for (int n = 0; n < a_population.size(); ++n)
332  a_population[n].fitness = 1.0;
333  }
334  else
335  {
336  for (int n = 0; n < a_population.size(); ++n)
337  {
338  // change fitness
339  a_population[n].fitness = (1.0 + a_population[n].fitness / stats.mean) / sigma2;
340 
341  // avoid tiny or zero fitness value; everyone gets to reproduce
342  if (a_population[n].fitness < 0.1)
343  a_population[n].fitness = 0.1;
344  }
345  }
346  }
347  };
348 
349 };
350 
351 #endif
virtual void scale_fitness(vector< OrganismType > &a_population)=0
Scale a population's fitness values.
double getMin()
Get the minimum fitness value for analyzed population.
Definition: stats.h:162
virtual void scale_fitness(vector< OrganismType > &a_population)
Scaling function.
Definition: scaler.h:280
virtual void scale_fitness(vector< OrganismType > &a_population)
Scaling function.
Definition: scaler.h:243
A quadratic scaler.
Definition: scaler.h:262
virtual ~scaler()
Virtual destructor.
Definition: scaler.h:83
A sigma scaler.
Definition: scaler.h:302
virtual void scale_fitness(vector< OrganismType > &a_population)
Scaling function.
Definition: scaler.h:202
windowed_scaler()
Constructor.
Definition: scaler.h:192
double getMean()
Get the mean (average) fitness value for analyzed population.
Definition: stats.h:168
A windowed fitness scaler.
Definition: scaler.h:185
An exponential fitness scaler.
Definition: scaler.h:219
virtual void scale_fitness(vector< OrganismType > &a_population)
Do-nothing scaling function.
Definition: scaler.h:113
A toolkit and framework for implementing evolutionary algorithms.
Definition: analyzer.h:60
virtual void scale_fitness(vector< OrganismType > &a_population)
Scaling function.
Definition: scaler.h:144
Elements shared by all classes in Evocosm.
Definition: evocommon.h:117
virtual void scale_fitness(vector< OrganismType > &a_population)
Scaling function.
Definition: scaler.h:321
linear_norm_scaler(double a_fitness_multiple=2.0)
Constructor.
Definition: scaler.h:133
A do-nothing scaler.
Definition: scaler.h:105
double getSigma()
Get the standard deviation (sigma) value for fitness.
Definition: stats.h:174
exponential_scaler(double a_a=1.0, double a_b=1.0, double a_power=2.0)
Constructor.
Definition: scaler.h:230
quadratic_scaler(double a_a, double a_b, double a_c)
Constructor.
Definition: scaler.h:269
Fitness scaling for a population.
Definition: scaler.h:72
double getMax()
Get the maximum fitness value for analyzed population.
Definition: stats.h:165
A linear normalization scaler.
Definition: scaler.h:126
sigma_scaler()
Constructor.
Definition: scaler.h:309
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.