Evocosm - A C++ Framework for Evolutionary Computing

Main Index

Created by Scott Robert Ladd at Coyote Gulch Productions.


function_optimizer.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_FUNCTION_OPTIMIZER_H)
54 #define EVOCOSM_FUNCTION_OPTIMIZER_H
55 
56 #include <vector>
57 #include <stdexcept>
58 #include <limits>
59 
60 // other elements of Evocosm
61 #include "evocosm.h"
62 #include "evoreal.h"
63 
64 // OpenMP support, if requested
65 #if defined(_OPENMP)
66 #include <omp.h>
67 #endif
68 
69 namespace libevocosm
70 {
72 
78  {
79  protected:
81 
89  typedef vector<double> t_function(vector<double> a_args);
90 
93  };
94 
96 
105  class function_solution : public organism< vector<double> >, protected fopt_global
106  {
107  public:
114  double value;
115 
116  protected:
118  virtual void child_copy(const organism & a_source)
119  {
120  value = dynamic_cast<const function_solution &>(a_source).value;
121  }
122 
123  private:
124  double m_minarg;
125  double m_maxarg;
126  double m_extent;
127 
128  public:
130 
134  : organism< vector<double> >(),
135  value(0.0),
136  m_minarg(-1.0),
137  m_maxarg(1.0),
138  m_extent(2.0)
139  {
140  // nada
141  }
142 
144 
147  function_solution(int a_nargs, double a_minarg, double a_maxarg)
148  : organism< vector<double> >(), value(0.0)
149  {
150  double base;
151  double extent;
152 
153  if (a_maxarg < a_minarg)
154  {
155  double temp = a_maxarg;
156  a_maxarg = a_minarg;
157  a_minarg = a_maxarg;
158  }
159 
160  m_minarg = a_minarg;
161  m_maxarg = a_maxarg;
162  m_extent = a_maxarg - a_minarg;
163 
164  // values fall in the range [-1,1]
165  for (size_t n = 0; n < a_nargs; ++n)
166  genes.push_back(g_random.get_real() * m_extent + a_minarg);
167  }
168 
170 
173  function_solution(const vector<double> & a_source)
174  : organism< vector<double> >(a_source), value(0.0)
175  {
176  // nada
177  }
178 
180 
184  function_solution(const organism< vector<double> > & a_source)
185  : organism< vector<double> >(a_source), value(0.0)
186  {
187  // nada
188  }
189 
191 
196  : organism< vector<double> >(a_source),
197  value(a_source.value),
198  m_minarg(a_source.m_minarg),
199  m_maxarg(a_source.m_maxarg),
200  m_extent(a_source.m_extent)
201  {
202  // nada
203  }
204 
206 
210  {
211  // nada
212  }
213 
215 
221  {
222  organism< vector<double> >::operator = (a_source);
223  value = a_source.value;
224  m_minarg = a_source.m_minarg;
225  m_maxarg = a_source.m_maxarg;
226  m_extent = a_source.m_extent;
227  return *this;
228  }
229 
231 
238  virtual bool operator < (const organism< vector<double> > & a_right) const
239  {
240  return (fitness > a_right.fitness);
241  }
242  };
243 
245 
248  class function_mutator : public mutator<function_solution>, protected fopt_global
249  {
250  public:
252 
255  function_mutator(double a_mutation_rate)
256  : m_mutation_rate(a_mutation_rate)
257  {
258  // adjust mutation rate if necessary
259  if (m_mutation_rate > 1.0)
260  m_mutation_rate = 1.0;
261  else if (m_mutation_rate < 0.0)
262  m_mutation_rate = 0.0;
263  }
264 
266 
271  : m_mutation_rate(a_source.m_mutation_rate)
272  {
273  // nada
274  }
275 
277 
281  {
282  // nada
283  }
284 
286 
292  {
293  m_mutation_rate = a_source.m_mutation_rate;
294  return *this;
295  }
296 
298 
302  double mutation_rate() const
303  {
304  return m_mutation_rate;
305  }
306 
308 
312  void mutate(vector<function_solution> & a_population);
313 
314  private:
315  // rate of mutation
316  double m_mutation_rate;
317  };
318 
320 
323  class function_reproducer : public reproducer<function_solution>, protected fopt_global
324  {
325  public:
327 
330  function_reproducer(double p_crossover_rate = 1.0)
331  : m_crossover_rate(p_crossover_rate)
332  {
333  // adjust crossover rate if necessary
334  if (m_crossover_rate > 1.0)
335  m_crossover_rate = 1.0;
336  else if (m_crossover_rate < 0.0)
337  m_crossover_rate = 0.0;
338  }
339 
341 
346  : m_crossover_rate(a_source.m_crossover_rate)
347  {
348  // nada
349  }
350 
352 
356  {
357  // nada
358  }
359 
361 
367  {
368  m_crossover_rate = a_source.m_crossover_rate;
369  return *this;
370  }
371 
373 
377  double crossover_rate() const
378  {
379  return m_crossover_rate;
380  }
381 
383 
392  virtual vector<function_solution> breed(const vector<function_solution> & a_population, size_t p_limit);
393 
394  private:
395  // crossover chance
396  double m_crossover_rate;
397  };
398 
400 
405  class function_landscape : public landscape<function_solution>, protected fopt_global
406  {
407  public:
409 
415  : landscape<function_solution>(a_listener),
416  m_function(a_function)
417  {
418  // nada
419  }
420 
423  : landscape<function_solution>(a_source),
424  m_function(a_source.m_function)
425  {
426  // nada
427  }
428 
431  {
433  m_function = a_source.m_function;
434  return *this;
435  }
436 
438 
442  {
443  // nada
444  }
445 
447 
454  virtual double test(function_solution & a_organism, bool a_verbose = false) const
455  {
456  vector<double> z = m_function(a_organism.genes);
457  a_organism.value = z[0];
458  a_organism.fitness = z[1];
459  return a_organism.fitness;
460  }
461 
462  private:
463  // fitness function pointer
464  t_function * m_function;
465  };
466 
468 
472  class function_analyzer : public analyzer<function_solution>
473  {
474  private:
475  function_solution m_prev_best;
476  size_t m_count;
477 
478  public:
480 
484  function_analyzer(listener<function_solution> & a_listener, size_t max_iterations)
485  : analyzer<function_solution>(a_listener, max_iterations),
486  m_prev_best(function_solution()),
487  m_count(0)
488  {
489  // nada
490  }
491 
493 
503  virtual bool analyze(const vector<function_solution> & a_population,
504  size_t a_iteration,
505  double & a_fitness);
506  };
507 
509 
513  class function_listener : public null_listener<function_solution>
514  {
515  public:
517 
521  virtual void ping_generation_begin(size_t a_iteration);
522 
524 
529  virtual void ping_generation_end(const vector<function_solution> & a_population, size_t a_iteration);
530  };
531 
533 
538  class function_optimizer : protected fopt_global, protected function_listener
539  {
540  private:
541  // objects that define the characteristics of the genetic algorithm
542  vector<function_solution> m_population;
543  function_landscape m_landscape;
544  function_mutator m_mutator;
545  function_reproducer m_reproducer;
548  function_analyzer m_analyzer;
549 
550  // the evocosm binds it all together
551  evocosm<function_solution> * m_evocosm;
552 
553  // number of iterations to run
554  const size_t m_iterations;
555 
556  public:
558 
568  function_optimizer(t_function * a_function,
569  size_t a_nargs,
570  double a_minarg,
571  double a_maxarg,
572  size_t a_norgs,
573  double a_mutation_rate,
574  size_t a_iterations);
575 
577 
580  virtual ~function_optimizer();
581 
583 
588  void run();
589  };
590 
591 };
592 
593 #endif
An listener implementation that ignores all events.
Definition: listener.h:147
function_reproducer(double p_crossover_rate=1.0)
Creation constructor.
Definition: function_optimizer.h:330
vector< double > genes
Definition: organism.h:97
Defines the test for a population of solutions.
Definition: function_optimizer.h:405
double mutation_rate() const
Gets the mutation rate.
Definition: function_optimizer.h:302
An evolving organism.
Definition: organism.h:78
function_mutator(double a_mutation_rate)
Creation constructor.
Definition: function_optimizer.h:255
A potential solution to the problem at hand.
Definition: function_optimizer.h:105
function_solution(const vector< double > &a_source)
Construct from raw genes.
Definition: function_optimizer.h:173
Implements a elitism selector.
Definition: selector.h:159
void mutate(vector< function_solution > &a_population)
Performs mutations.
virtual void child_copy(const organism &a_source)
Definition: function_optimizer.h:118
function_optimizer(t_function *a_function, size_t a_nargs, double a_minarg, double a_maxarg, size_t a_norgs, double a_mutation_rate, size_t a_iterations)
Constructor.
function_solution(const function_solution &a_source)
Copy constructor.
Definition: function_optimizer.h:195
double fitness
Definition: organism.h:88
Mutates organisms.
Definition: mutator.h:72
~function_landscape()
Virtual destructor.
Definition: function_optimizer.h:441
virtual ~function_reproducer()
Virtual destructor.
Definition: function_optimizer.h:355
virtual ~function_solution()
Virtual destructor.
Definition: function_optimizer.h:209
An abstract interface defining a fitness landscape.
Definition: landscape.h:80
function_reproducer & operator=(const function_reproducer &a_source)
Assignment operator.
Definition: function_optimizer.h:366
function_reproducer(const function_reproducer &a_source)
Copy constructor.
Definition: function_optimizer.h:345
double crossover_rate() const
Gets the crossover rate.
Definition: function_optimizer.h:377
function_landscape(t_function *a_function, listener< function_solution > &a_listener)
Creation constructor.
Definition: function_optimizer.h:414
function_solution()
Default contructor.
Definition: function_optimizer.h:133
function_mutator & operator=(const function_mutator &a_source)
Assignment operator.
Definition: function_optimizer.h:291
Reports the state of a population of solutions.
Definition: function_optimizer.h:472
A toolkit and framework for implementing evolutionary algorithms.
Definition: analyzer.h:60
function_mutator(const function_mutator &a_source)
Copy constructor.
Definition: function_optimizer.h:270
virtual void ping_generation_begin(size_t a_iteration)
Ping that a generation begins.
function_analyzer(listener< function_solution > &a_listener, size_t max_iterations)
Constructor.
Definition: function_optimizer.h:484
Implements reproduction.
Definition: function_optimizer.h:323
landscape & operator=(const landscape &a_source)
Assignment operator.
Definition: landscape.h:102
Mutates solutions.
Definition: function_optimizer.h:248
virtual ~function_optimizer()
Destructor.
Tools for evolving real numbers.
Definition: evoreal.h:85
static prng g_random
A shared random number generator.
Definition: evocommon.h:127
vector< double > t_function(vector< double > a_args)
Definition of a function type.
Definition: function_optimizer.h:89
function_solution(int a_nargs, double a_minarg, double a_maxarg)
Creation constructor.
Definition: function_optimizer.h:147
virtual bool analyze(const vector< function_solution > &a_population, size_t a_iteration, double &a_fitness)
Reports on a population.
A generic function optimizer.
Definition: function_optimizer.h:538
double value
Definition: function_optimizer.h:114
An listener implementation that ignores all events.
Definition: function_optimizer.h:513
Global things used by all optimizer classes.
Definition: function_optimizer.h:77
void run()
Performs optimization.
Creates new organisms from an existing population.
Definition: reproducer.h:72
virtual void ping_generation_end(const vector< function_solution > &a_population, size_t a_iteration)
Ping that a generation ends.
function_solution & operator=(const function_solution &a_source)
Assignment operator.
Definition: function_optimizer.h:220
virtual ~function_mutator()
Virtual destructor.
Definition: function_optimizer.h:280
virtual double test(function_solution &a_organism, bool a_verbose=false) const
Performs fitness testing.
Definition: function_optimizer.h:454
function_landscape(const function_landscape &a_source)
Copy constructor.
Definition: function_optimizer.h:422
double get_real()
get the next value in the range [0,1)
Definition: evocommon.h:106
function_solution(const organism< vector< double > > &a_source)
Construct from base class.
Definition: function_optimizer.h:184
static evoreal g_evoreal
Provides mutation and crossover services for doubles.
Definition: function_optimizer.h:92
Associates organisms with the components of an evolutionary system.
Definition: evocosm.h:102
function_landscape & operator=(const function_landscape &a_source)
Assignment.
Definition: function_optimizer.h:430
virtual vector< function_solution > breed(const vector< function_solution > &a_population, size_t p_limit)
Reproduction for solutions.
Reports on a given population.
Definition: analyzer.h:70
A linear normalization scaler.
Definition: scaler.h:126

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