Evocosm - A C++ Framework for Evolutionary Computing

Main Index

Created by Scott Robert Ladd at Coyote Gulch Productions.


evocommon.h
1 //---------------------------------------------------------------------
2 // Algorithmic Conjurings @ http://www.coyotegulch.com
3 // Evocosm -- An Object-Oriented Framework for Evolutionary Algorithms
4 //
5 // evocommon.h
6 //---------------------------------------------------------------------
7 //
8 // Copyright 1996, 1999, 2002, 2003, 2004, 2005, 2006, 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_EVOGLOBAL_H)
36 #define LIBEVOCOSM_EVOGLOBAL_H
37 
38 // Brahe library
39 #if defined(_MSC_VER)
40 #include "prng.h"
41 #else
42 #include "libbrahe/prng.h"
43 #endif
44 
45 // Standard C++ Library
46 #include <string>
47 #include <iostream>
48 #include <iomanip>
49 
50 #if !defined(_MSC_VER)
51 #include <unistd.h>
52 #endif
53 // Windows
54 #if defined(_MSC_VER)
55 #include "windows.h"
56 #undef max
57 #undef min
58 #endif
59 
60 namespace libevocosm
61 {
63 
67  class prng
68  {
69  private:
70  brahe_prng_state_t m_random;
71 
72  public:
74  prng()
75  {
76  brahe_prng_init(&m_random,BRAHE_PRNG_MWC1038,0);
77  }
78 
81  {
82  brahe_prng_free(&m_random);
83  }
84 
86  void set_seed(uint32_t a_seed)
87  {
88  brahe_prng_init(&m_random,BRAHE_PRNG_MWC1038,a_seed);
89  }
90 
92  uint32_t get_seed()
93  {
94  return m_random.m_seed;
95  }
96 
98  size_t get_index(size_t n)
99  {
100  return brahe_prng_index(&m_random,n);
101  }
102 
104  double get_real()
105  {
106  return brahe_prng_real2(&m_random);
107  }
108  };
109 
111 
115  class globals
116  {
117  protected:
119  static size_t rand_index(size_t n)
120  {
121  return g_random.get_index(n);
122  }
123 
125  static prng g_random;
126 
128  static std::string g_version;
129 
130  public:
132  static void set_random_seed(uint32_t a_seed)
133  {
134  g_random.set_seed(a_seed);
135  }
136 
138  static uint32_t get_seed()
139  {
140  return g_random.get_seed();
141  }
142 
144  static std::string version()
145  {
146  return g_version;
147  }
148  };
149 
151 
156  class listener
157  {
158  public:
160 
164  virtual void ping_generation_begin(size_t a_generation_number) = 0;
165 
167 
171  virtual void ping_generation_end(size_t a_generation_number) = 0;
172 
174 
178  virtual void ping_population_begin(size_t a_population_number) = 0;
179 
181 
185  virtual void ping_population_end(size_t a_population_number) = 0;
186 
188 
192  virtual void ping_fitness_test_begin(size_t a_organism_number) = 0;
193 
195 
199  virtual void ping_fitness_test_end(size_t a_organism_number) = 0;
200 
202 
210  virtual void report(const std::string & a_text) = 0;
211 
213 
220  virtual void report_error(const std::string & a_text) = 0;
221 
223 
227  virtual void run_complete() = 0;
228 
230 
235  virtual void yield() = 0;
236  };
237 
239 
242  class null_listener : public listener
243  {
244  public:
246 
250  virtual void ping_generation_begin(size_t a_generation_number)
251  {
252  // do nothing
253  }
254 
256 
260  virtual void ping_generation_end(size_t a_generation_number)
261  {
262  // do nothing
263  }
264 
266 
270  virtual void ping_population_begin(size_t a_population_number)
271  {
272  // do nothing
273  }
274 
276 
280  virtual void ping_population_end(size_t a_population_number)
281  {
282  // do nothing
283  }
284 
286 
290  virtual void ping_fitness_test_begin(size_t a_organism_number)
291  {
292  // do nothing
293  }
294 
296 
300  virtual void ping_fitness_test_end(size_t a_organism_number)
301  {
302  // do nothing
303  }
304 
306 
314  virtual void report(const std::string & a_text)
315  {
316  // do nothing
317  }
318 
320 
327  virtual void report_error(const std::string & a_text)
328  {
329  // do nothing
330  }
331 
333 
337  virtual void run_complete()
338  {
339  // do nothing
340  }
341 
343 
348  virtual void yield()
349  {
350  // do nothing
351  }
352  };
353 
355 
359  class listener_stdout : public listener
360  {
361  public:
363 
367  virtual void ping_generation_begin(size_t a_generation_number)
368  {
369  std::cout << "------------------------------------------------------------\ngeneration "
370  << a_generation_number << " begins" << std::endl;
371  }
372 
374 
378  virtual void ping_generation_end(size_t a_generation_number)
379  {
380  // nada
381  }
382 
384 
388  virtual void ping_population_begin(size_t a_population_number)
389  {
390  std::cout << "\npopulation " << std::setw(2) << a_population_number << ": " << std::flush;
391  }
392 
394 
398  virtual void ping_population_end(size_t a_population_number)
399  {
400  // nada
401  }
402 
404 
408  virtual void ping_fitness_test_begin(size_t a_organism_number)
409  {
410  // nada
411  }
412 
414 
418  virtual void ping_fitness_test_end(size_t a_organism_number)
419  {
420  std::cout << "." << std::flush;
421  }
422 
424 
432  virtual void report(const std::string & a_text)
433  {
434  std::cout << a_text;
435  }
436 
438 
445  virtual void report_error(const std::string & a_text)
446  {
447  std::cerr << a_text;
448  }
449 
451 
455  virtual void run_complete()
456  {
457  // nada
458  }
459 
461 
466  virtual void yield()
467  {
468 #if defined(_MSC_VER)
469  Sleep(50000);
470 #else
471  usleep(50000);
472 #endif
473  }
474  };
475 
476 }
477 
478 #endif
virtual void run_complete()=0
Evocosm is finished.
virtual void ping_population_end(size_t a_population_number)
Ping that a population ends.
Definition: evocommon.h:398
static uint32_t get_seed()
Set the seed for the random number generator.
Definition: evocommon.h:138
virtual void ping_population_end(size_t a_population_number)
Ping that a population ends.
Definition: evocommon.h:280
virtual void ping_fitness_test_end(size_t a_organism_number)
Ping that a test run ends.
Definition: evocommon.h:300
virtual void ping_population_begin(size_t a_population_number)
Ping that a population begins.
Definition: evocommon.h:388
virtual void ping_population_end(size_t a_population_number)=0
Ping that a population ends.
virtual void ping_population_begin(size_t a_population_number)=0
Ping that a population begins.
virtual void ping_generation_begin(size_t a_generation_number)=0
Ping that a generation begins.
The random number generator used by Evocosm.
Definition: evocommon.h:67
static size_t rand_index(size_t n)
Static function to allow use of g_random function pointer in random_shuffle.
Definition: evocommon.h:119
static std::string g_version
Version number.
Definition: evocommon.h:128
virtual void ping_generation_end(size_t a_generation_number)=0
Ping that a generation ends.
virtual void ping_fitness_test_begin(size_t a_organism_number)
Ping that a test run begins.
Definition: evocommon.h:290
size_t get_index(size_t n)
get a random index value
Definition: evocommon.h:98
double get_real()
get the next value in the range [0,1)
Definition: evocommon.h:104
virtual void yield()
Yield.
Definition: evocommon.h:348
virtual void report(const std::string &a_text)
Report non-specific text.
Definition: evocommon.h:432
virtual void run_complete()
Evocosm is finished.
Definition: evocommon.h:337
virtual void report(const std::string &a_text)
Report non-specific text.
Definition: evocommon.h:314
virtual void report_error(const std::string &a_text)=0
Send error message.
virtual void report_error(const std::string &a_text)
Send error message.
Definition: evocommon.h:327
An abstract interface defining a listener.
Definition: evocommon.h:156
prng()
Constructor.
Definition: evocommon.h:74
virtual void ping_fitness_test_begin(size_t a_organism_number)=0
Ping that a test run begins.
virtual void ping_generation_begin(size_t a_generation_number)
Ping that a generation begins.
Definition: evocommon.h:367
static prng g_random
A shared random number generator.
Definition: evocommon.h:125
An listener implementation that ignores all events.
Definition: evocommon.h:242
virtual void ping_fitness_test_end(size_t a_organism_number)
Ping that a test run ends.
Definition: evocommon.h:418
virtual void run_complete()
Evocosm is finished.
Definition: evocommon.h:455
virtual void ping_fitness_test_end(size_t a_organism_number)=0
Ping that a test run ends.
virtual void ping_population_begin(size_t a_population_number)
Ping that a population begins.
Definition: evocommon.h:270
virtual void ping_generation_end(size_t a_generation_number)
Ping that a generation ends.
Definition: evocommon.h:378
virtual void report(const std::string &a_text)=0
Report non-specific text.
A toolkit and framework for implementing evolutionary algorithms.
Definition: evocommon.h:60
An listener implementation that ignores all events.
Definition: evocommon.h:359
void set_seed(uint32_t a_seed)
Set the seed for the random number generator.
Definition: evocommon.h:86
virtual void report_error(const std::string &a_text)
Send error message.
Definition: evocommon.h:445
Elements shared by all classes in Evocosm.
Definition: evocommon.h:115
virtual void ping_fitness_test_begin(size_t a_organism_number)
Ping that a test run begins.
Definition: evocommon.h:408
virtual void yield()=0
Yield.
uint32_t get_seed()
get seed value
Definition: evocommon.h:92
~prng()
Destructor.
Definition: evocommon.h:80
virtual void ping_generation_end(size_t a_generation_number)
Ping that a generation ends.
Definition: evocommon.h:260
virtual void ping_generation_begin(size_t a_generation_number)
Ping that a generation begins.
Definition: evocommon.h:250
static std::string version()
Get version number.
Definition: evocommon.h:144
virtual void yield()
Yield.
Definition: evocommon.h:466
static void set_random_seed(uint32_t a_seed)
Set the seed for the random number generator.
Definition: evocommon.h:132

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