Evocosm - A C++ Framework for Evolutionary Computing

Main Index

Created by Scott Robert Ladd at Coyote Gulch Productions.


evocosm.h
1 //---------------------------------------------------------------------
2 // Algorithmic Conjurings @ http://www.coyotegulch.com
3 // Evocosm -- An Object-Oriented Framework for Evolutionary Algorithms
4 //
5 // evocosm.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(LIBEVOCOSM_EVOCOSM_H)
36 #define LIBEVOCOSM_EVOCOSM_H
37 
38 #if defined(_MSC_VER)
39 #pragma warning (disable : 4786)
40 #endif
41 
42 #if defined(_OPENMP)
43 #include <omp.h>
44 #endif
45 
46 // Standard C++ library
47 #include <vector>
48 
49 // libevocosm
50 #include "validator.h"
51 #include "organism.h"
52 #include "landscape.h"
53 #include "mutator.h"
54 #include "reproducer.h"
55 #include "scaler.h"
56 #include "migrator.h"
57 #include "selector.h"
58 #include "reporter.h"
59 
61 
70 namespace libevocosm
71 {
72  using std::vector;
73 
75 
79  template <class OrganismType>
81  {
82  public:
84 
87  virtual OrganismType create() = 0;
88 
90 
93  virtual void append(vector<OrganismType> & a_population, size_t a_size) = 0;
94  };
95 
97 
101  template <class LandscapeType>
103  {
104  public:
106 
110  virtual LandscapeType generate() = 0;
111  };
112 
114 
121  template <class OrganismType, class LandscapeType>
122  class evocosm : protected globals
123  {
124  protected:
127 
130 
132  vector< vector<OrganismType> > m_populations;
133 
136 
139 
142 
144  vector< vector<LandscapeType> > m_unique_landscapes;
145 
147  vector<LandscapeType> m_common_landscapes;
148 
151 
154 
157 
160 
163 
166 
168  size_t m_iteration;
169 
172 
174  bool m_running;
175 
176  public:
178 
199  evocosm(listener & a_listener,
200  size_t a_population_size,
201  size_t a_number_of_populations,
202  size_t a_number_of_unique_landscapes,
203  size_t a_number_of_common_landscapes,
204  mutator<OrganismType> & a_mutator,
205  reproducer<OrganismType> & a_reproducer,
206  scaler<OrganismType> & a_scaler,
207  migrator<OrganismType> & a_migrator,
208  selector<OrganismType> & a_selector,
210  organism_factory<OrganismType> & a_organism_factory,
211  landscape_factory<LandscapeType> & a_landscape_factory,
212  bool a_minimizing = false);
213 
215 
220 
222 
229  virtual ~evocosm();
230 
232 
238 
240 
250  virtual bool run_generation(bool a_finished, double & a_fitness);
251 
253 
259  vector<OrganismType, LandscapeType> & population(size_t a_index = 0)
260  {
261  return m_populations[a_index];
262  }
263 
265 
268  void terminate()
269  {
270  m_running = false;
271  }
272  };
273 
274  // constructors
275  template <class OrganismType, class LandscapeType>
277  size_t a_population_size,
278  size_t a_number_of_populations,
279  size_t a_number_of_unique_landscapes,
280  size_t a_number_of_common_landscapes,
281  mutator<OrganismType> & a_mutator,
282  reproducer<OrganismType> & a_reproducer,
283  scaler<OrganismType> & a_scaler,
284  migrator<OrganismType> & a_migrator,
285  selector<OrganismType> & a_selector,
287  organism_factory<OrganismType> & a_organism_factory,
288  landscape_factory<LandscapeType> & a_landscape_factory,
289  bool a_minimizing)
290  : m_listener(a_listener),
291  m_population_size(a_population_size),
292  m_populations(),
293  m_number_of_populations(a_number_of_populations),
294  m_number_of_unique_landscapes(a_number_of_unique_landscapes),
295  m_number_of_common_landscapes(a_number_of_common_landscapes),
296  m_unique_landscapes(),
297  m_common_landscapes(),
298  m_mutator(a_mutator),
299  m_reproducer(a_reproducer),
300  m_scaler(a_scaler),
301  m_migrator(a_migrator),
302  m_selector(a_selector),
303  m_reporter(a_reporter),
304  m_iteration(0),
305  m_minimizing(a_minimizing),
306  m_running(true)
307  {
308  // adjust evocosm size if necessary
311 
312  // calculate number of common and unique landscapes
315 
316  a_landscape_factory.generate();
317 
318  // allocate vectors of landscapes
319  for (size_t n = 0; n < m_number_of_common_landscapes; ++n)
320  m_common_landscapes.push_back(a_landscape_factory.generate());
321 
322  // create initial populations
325 
326  for (size_t p = 0; p < m_number_of_populations; ++p)
327  {
328  // add organisms to populations
329  a_organism_factory.append(m_populations[p], m_population_size);
330 
331  // create unique landscapes for this population
332  for (size_t n = 0; n < m_number_of_unique_landscapes; ++n)
333  m_unique_landscapes[p].push_back(a_landscape_factory.generate());
334  }
335  }
336 
337  // copy constructor
338  template <class OrganismType, class LandscapeType>
340  : m_population_size(a_source.m_population_size),
341  m_populations(a_source.m_populations),
342  m_number_of_populations(a_source.m_number_of_populations),
343  m_number_of_common_landscapes(a_source.m_number_of_common_landscapes),
344  m_number_of_unique_landscapes(a_source.m_number_of_unique_landscapes),
345  m_common_landscapes(a_source.m_common_landscapes),
346  m_unique_landscapes(a_source.m_unique_landscapes),
347  m_mutator(a_source.m_mutator),
348  m_reproducer(a_source.m_reproducer),
349  m_scaler(a_source.m_scaler),
350  m_migrator(a_source.m_migrator),
351  m_selector(a_source.m_selector),
352  m_iteration(a_source.m_iteration)
353  {
354  // nada
355  }
356 
357  // destructor
358  template <class OrganismType, class LandscapeType>
360  {
361  // nada
362  }
363 
364  // assignment operator
365  template <class OrganismType, class LandscapeType>
367  {
368  m_population_size = a_source.m_population_size;
369  m_populations = a_source.m_populations;
370  m_number_of_populations = a_source.m_number_of_populations;
371 
372  m_number_of_common_landscapes = a_source.m_number_of_common_landscapes;
373  m_number_of_unique_landscapes = a_source.m_number_of_unique_landscapes;
374  m_common_landscapes = a_source.m_common_landscapes;
375  m_unique_landscapes = a_source.m_unique_landscapes;
376 
377  m_mutator = a_source.m_mutator;
378  m_reproducer = a_source.m_reproducer;
379  m_scaler = a_source.m_scaler;
380  m_migrator = a_source.m_migrator;
381  m_selector = a_source.m_selector;
382 
383  m_iteration = a_source.m_iteration;
384 
385  return *this;
386  }
387 
388  // compute next generation
389  template <class OrganismType, class LandscapeType>
390  bool evocosm<OrganismType, LandscapeType>::run_generation(bool a_finished, double & a_fitness)
391  {
392  int n, p;
393 
394  ++m_iteration;
395 
396  // announce beginning of new generation
397  m_listener.ping_generation_begin(m_iteration);
398 
399  // test fitness for each chromosome
400  for (p = 0; p < (int)m_number_of_populations; p++)
401  {
402  // announce beginning of population processing
403  m_listener.ping_population_begin(p + 1);
404 
405  // clear fitness
406  // using an iterator here crashes MSVC++ 13.0 (.Net) with an internal error
407  for (n = 0; n < (int)m_population_size; ++n) // vector<OrganismType>::iterator organism = m_populations[p].begin(); organism != m_populations[p].end; ++organism)
408  m_populations[p][n].reset_all();
409 
410  // let other processes do something
411  m_listener.yield();
412 
413  // accumulate fitness for each population common to all populations
414  if (m_number_of_common_landscapes > 0)
415  {
416  for (n = 0; n < (int)m_number_of_common_landscapes; ++n)
417  m_common_landscapes[n].test_pop(m_populations[p]);
418  }
419 
420  // let other processes do something
421  m_listener.yield();
422 
423  // accumulate fitness for each landscape unique to this population
424  if (m_number_of_unique_landscapes > 0)
425  {
426  for (n = 0; n < (int)m_number_of_unique_landscapes; ++n)
427  m_unique_landscapes[p][n].test_pop(m_populations[p]);
428  }
429 
430  // announce completion of population processing
431  m_listener.ping_population_end(p + 1);
432  }
433 
434  // report on new generation
435  bool keep_going = m_reporter.report(m_populations,m_iteration,a_fitness,a_finished);
436 
437  // let other processes do something
438  m_listener.yield();
439 
440  if (keep_going && m_running)
441  {
442  // create next generation
443  for (p = 0; p < (int)m_number_of_populations; ++p)
444  {
445  // reverse the sense of fitness when minimizing (i.e., best fitness is smallest value)
446  if (m_minimizing)
447  m_scaler.invert(m_populations[p]);
448 
449  // let other processes do something
450  m_listener.yield();
451 
452  // scale the population's fitness
453  m_scaler.scale_fitness(m_populations[p]);
454 
455  // let other processes do something
456  m_listener.yield();
457 
458  // get survivors and number of chromosomes to add
459  vector<OrganismType> survivors = m_selector.select_survivors(m_populations[p]);
460 
461  // let other processes do something
462  m_listener.yield();
463 
464  // give birth to new chromosomes
465  vector<OrganismType> children = m_reproducer.breed(m_populations[p], m_population_size - survivors.size());
466 
467  // let other processes do something
468  m_listener.yield();
469 
470  // mutate the child chromosomes
471  m_mutator.mutate(children);
472 
473  // let other processes do something
474  m_listener.yield();
475 
476  // replace old evocosm with new one
477  m_populations[p] = survivors;
478  m_populations[p].insert(m_populations[p].end(),children.begin(),children.end());
479  }
480 
481  if (m_number_of_populations > 1)
482  m_migrator.migrate(m_populations);
483  }
484 
485  // we're done with this generation
486  m_listener.ping_generation_end(m_iteration);
487 
488  // let other processes do something
489  m_listener.yield();
490 
491  return keep_going & m_running;
492  }
493 };
494 
495 #endif
virtual void append(vector< OrganismType > &a_population, size_t a_size)=0
Organism population factory.
Interface for an organism factory.
Definition: evocosm.h:80
Interface for a landscape factory.
Definition: evocosm.h:102
vector< OrganismType, LandscapeType > & population(size_t a_index=0)
Directly view population.
Definition: evocosm.h:259
Creates new organisms from an existing population.
Definition: reproducer.h:54
void terminate()
Terminate this run.
Definition: evocosm.h:268
bool m_running
termination flag
Definition: evocosm.h:174
Selects organisms that survive.
Definition: selector.h:61
selector< OrganismType > & m_selector
Selects organisms that survive from one generation to the next.
Definition: evocosm.h:162
vector< LandscapeType > m_common_landscapes
Fitness landscapes common to all populations.
Definition: evocosm.h:147
size_t m_number_of_common_landscapes
The number of fitness landscapes common to all populations.
Definition: evocosm.h:141
scaler< OrganismType > & m_scaler
Scales the fitness of the evocosm.
Definition: evocosm.h:156
reproducer< OrganismType > & m_reproducer
Creates new organisms.
Definition: evocosm.h:153
mutator< OrganismType > & m_mutator
A mutator to randomly influence genes.
Definition: evocosm.h:150
Reports on a given population.
Definition: reporter.h:52
Defines migration between populations.
Definition: migrator.h:53
evocosm & operator=(const evocosm< OrganismType, LandscapeType > &a_source)
Assignment operator.
Definition: evocosm.h:366
bool m_minimizing
Set true when minimizing; i.e., best fitness < worst fitness.
Definition: evocosm.h:171
An abstract interface defining a listener.
Definition: evocommon.h:156
migrator< OrganismType > & m_migrator
Handles emigration and immigration.
Definition: evocosm.h:159
Fitness scaling for a population.
Definition: scaler.h:60
size_t m_population_size
The initial evocosm size.
Definition: evocosm.h:129
virtual bool run_generation(bool a_finished, double &a_fitness)
Compute next generation.
Definition: evocosm.h:390
size_t m_iteration
Count of iterations made.
Definition: evocosm.h:168
A toolkit and framework for implementing evolutionary algorithms.
Definition: evocommon.h:60
virtual LandscapeType generate()=0
Landscape factory.
virtual OrganismType create()=0
Organism factory.
virtual ~evocosm()
Virtual destructor.
Definition: evocosm.h:359
evocosm(listener &a_listener, size_t a_population_size, size_t a_number_of_populations, size_t a_number_of_unique_landscapes, size_t a_number_of_common_landscapes, mutator< OrganismType > &a_mutator, reproducer< OrganismType > &a_reproducer, scaler< OrganismType > &a_scaler, migrator< OrganismType > &a_migrator, selector< OrganismType > &a_selector, reporter< OrganismType, LandscapeType > &a_reporter, organism_factory< OrganismType > &a_organism_factory, landscape_factory< LandscapeType > &a_landscape_factory, bool a_minimizing=false)
Creation constructor.
Definition: evocosm.h:276
Elements shared by all classes in Evocosm.
Definition: evocommon.h:115
reporter< OrganismType, LandscapeType > & m_reporter
Reports the a evocosm for analysis or display.
Definition: evocosm.h:165
listener & m_listener
A listener for evocosm progress.
Definition: evocosm.h:126
Mutates organisms.
Definition: mutator.h:54
size_t m_number_of_populations
The number of populations.
Definition: evocosm.h:135
Associates organisms with the components of an evolutionary system.
Definition: evocosm.h:122
vector< vector< OrganismType > > m_populations
The populations of organisms.
Definition: evocosm.h:132
void enforce_lower_limit(Type &object, const Type &low_value)
Enforce a lower limit on the value of an object.
Definition: validator.h:287
vector< vector< LandscapeType > > m_unique_landscapes
Fitness landscapes unique to individual populations.
Definition: evocosm.h:144
size_t m_number_of_unique_landscapes
The number of fitness landscapes unique to individual populations.
Definition: evocosm.h:138

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