Evocosm - A C++ Framework for Evolutionary Computing

Main Index

Created by Scott Robert Ladd at Coyote Gulch Productions.


evocosm.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_EVOCOSM_H)
54 #define LIBEVOCOSM_EVOCOSM_H
55 
56 #if defined(_MSC_VER)
57 #pragma warning (disable : 4786)
58 #endif
59 
60 #if defined(_OPENMP)
61 #include <omp.h>
62 #endif
63 
64 #include <unistd.h>
65 
66 // Standard C++ library
67 #include <vector>
68 
69 // libevocosm
70 #include "validator.h"
71 #include "listener.h"
72 #include "organism.h"
73 #include "landscape.h"
74 #include "mutator.h"
75 #include "reproducer.h"
76 #include "scaler.h"
77 #include "selector.h"
78 #include "analyzer.h"
79 
81 
90 namespace libevocosm
91 {
92  using std::vector;
93 
95 
101  template <class OrganismType>
102  class evocosm : protected globals
103  {
104  protected:
106  vector<OrganismType> & m_population;
107 
110 
113 
116 
119 
122 
125 
128 
130  size_t m_iteration;
131 
133  unsigned int m_sleep_time;
134 
135  public:
137 
152  evocosm(vector<OrganismType> & a_population,
153  landscape<OrganismType> & a_landscape,
154  mutator<OrganismType> & a_mutator,
155  reproducer<OrganismType> & a_reproducer,
156  scaler<OrganismType> & a_scaler,
157  selector<OrganismType> & a_selector,
158  analyzer<OrganismType> & a_analyzer,
159  listener<OrganismType> & a_listener);
160 
162 
166  evocosm(const evocosm<OrganismType> & a_source);
167 
169 
176  virtual ~evocosm();
177 
179 
184  evocosm & operator = (const evocosm<OrganismType> & a_source);
185 
187 
196  virtual bool run_generation();
197 
199 
204  vector<OrganismType> & get_population()
205  {
206  return m_population;
207  }
208 
210 
214  unsigned int get_sleep_time()
215  {
216  return m_sleep_time;
217  }
218 
220 
224  void set_sleep_time(unsigned int a_sleep_time)
225  {
226  m_sleep_time = a_sleep_time;
227  }
228 
229  protected:
231 
236  void yield()
237  {
238  if (m_sleep_time > 0)
239  {
240  #if defined(_MSC_VER)
241  Sleep(m_sleep_time);
242  #else
243  usleep((useconds_t)m_sleep_time);
244  #endif
245  }
246  }
247 
248  };
249 
250  // constructors
251  template <class OrganismType>
252  evocosm<OrganismType>::evocosm(vector<OrganismType> & a_population,
253  landscape<OrganismType> & a_landscape,
254  mutator<OrganismType> & a_mutator,
255  reproducer<OrganismType> & a_reproducer,
256  scaler<OrganismType> & a_scaler,
257  selector<OrganismType> & a_selector,
258  analyzer<OrganismType> & a_analyzer,
259  listener<OrganismType> & a_listener)
260  : m_population(a_population),
261  m_landscape(a_landscape),
262  m_mutator(a_mutator),
263  m_reproducer(a_reproducer),
264  m_scaler(a_scaler),
265  m_selector(a_selector),
266  m_analyzer(a_analyzer),
267  m_listener(a_listener),
268  m_iteration(0),
269  m_sleep_time(10000) // default to 10ms sleep time
270  {
271  // nada
272  }
273 
274  // copy constructor
275  template <class OrganismType>
277  : m_population(a_source.a_population),
278  m_landscape(a_source.m_landscape),
279  m_mutator(a_source.m_mutator),
280  m_reproducer(a_source.m_reproducer),
281  m_scaler(a_source.m_scaler),
282  m_selector(a_source.m_selector),
283  m_analyzer(a_source.m_analyzer),
284  m_listener(a_source.m_listener),
285  m_iteration(a_source.m_iteration),
286  m_sleep_time(a_source.m_sleep_time)
287  {
288  // nada
289  }
290 
291  // destructor
292  template <class OrganismType>
294  {
295  // nada
296  }
297 
298  // assignment operator
299  template <class OrganismType>
301  {
302  m_population = a_source.m_population;
303  m_landscape = a_source.m_landscape;
304  m_scaler = a_source.m_scaler;
305  m_analyzer = a_source.m_analyzer;
306  m_listener = a_source.m_analyzer;
307  m_iteration = a_source.m_iteration;
308  m_sleep_time = a_source.m_sleep_time;
309 
310  return *this;
311  }
312 
313  // compute next generation
314  template <class OrganismType>
316  {
317  bool keep_going = true;
318 
319  OrganismType * best = NULL;
320 
321  ++m_iteration;
322 
323  // announce beginning of new generation
324  m_listener.ping_generation_begin(m_population, m_iteration);
325 
326  // check population fitness
327  m_landscape.test(m_population);
328  yield();
329 
330  // we're done testing this generation
331  m_listener.ping_generation_end(m_population, m_iteration);
332  yield();
333 
334  // analyze the results of testing, and decide if we're going to stop or not
335  keep_going = m_analyzer.analyze(m_population, m_iteration);
336 
337  if (keep_going)
338  {
339  // fitness scaling
340  m_scaler.scale_fitness(m_population);
341  yield();
342 
343  // get survivors and number of chromosomes to add
344  vector<OrganismType> survivors = m_selector.select_survivors(m_population);
345  yield();
346 
347  // give birth to new chromosomes
348  vector<OrganismType> children = m_reproducer.breed(m_population, m_population.size() - survivors.size());
349  yield();
350 
351  // debugging only
352  //fitness_stats<OrganismType> s(survivors);
353  //fitness_stats<OrganismType> c(children);
354 
355  // mutate the child chromosomes
356  m_mutator.mutate(children);
357  yield();
358 
359  // append children to survivors and replace existing population form combined vector
360  survivors.insert(survivors.end(),children.begin(),children.end());
361  m_population = survivors;
362  yield();
363  }
364  else
365  {
366  m_listener.run_complete(m_population);
367  }
368 
369  return keep_going;
370  }
371 };
372 
373 #endif
listener< OrganismType > & m_listener
A listener for evocosm progress.
Definition: evocosm.h:127
void set_sleep_time(unsigned int a_sleep_time)
Set the sleep time property value.
Definition: evocosm.h:224
mutator< OrganismType > & m_mutator
A mutator to randomly influence genes.
Definition: evocosm.h:112
Mutates organisms.
Definition: mutator.h:72
scaler< OrganismType > & m_scaler
Scales the fitness of the evocosm.
Definition: evocosm.h:118
vector< OrganismType > & get_population()
Directly view population.
Definition: evocosm.h:204
An abstract interface defining a fitness landscape.
Definition: landscape.h:80
selector< OrganismType > & m_selector
Selects organisms that survive from one generation to the next.
Definition: evocosm.h:121
A toolkit and framework for implementing evolutionary algorithms.
Definition: analyzer.h:60
void yield()
Yield.
Definition: evocosm.h:236
Elements shared by all classes in Evocosm.
Definition: evocommon.h:117
Selects organisms that survive.
Definition: selector.h:79
An abstract interface defining a listener.
Definition: listener.h:80
virtual ~evocosm()
Virtual destructor.
Definition: evocosm.h:293
unsigned int m_sleep_time
Number microseconds for process to sleep on yield.
Definition: evocosm.h:133
evocosm & operator=(const evocosm< OrganismType > &a_source)
Assignment operator.
Definition: evocosm.h:300
reproducer< OrganismType > & m_reproducer
Creates new organisms.
Definition: evocosm.h:115
landscape< OrganismType > & m_landscape
Fitness landscapes common to all populations.
Definition: evocosm.h:109
Fitness scaling for a population.
Definition: scaler.h:72
Creates new organisms from an existing population.
Definition: reproducer.h:72
analyzer< OrganismType > & m_analyzer
Reports the a evocosm for analysis or display.
Definition: evocosm.h:124
unsigned int get_sleep_time()
Get the sleep time property value.
Definition: evocosm.h:214
size_t m_iteration
Count of iterations made.
Definition: evocosm.h:130
Associates organisms with the components of an evolutionary system.
Definition: evocosm.h:102
Reports on a given population.
Definition: analyzer.h:70
virtual bool run_generation()
Compute next generation.
Definition: evocosm.h:315
evocosm(vector< OrganismType > &a_population, landscape< OrganismType > &a_landscape, mutator< OrganismType > &a_mutator, reproducer< OrganismType > &a_reproducer, scaler< OrganismType > &a_scaler, selector< OrganismType > &a_selector, analyzer< OrganismType > &a_analyzer, listener< OrganismType > &a_listener)
Creation constructor.
Definition: evocosm.h:252
vector< OrganismType > & m_population
The populations of organisms.
Definition: evocosm.h:106

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