Evocosm - A C++ Framework for Evolutionary Computing

Main Index

Created by Scott Robert Ladd at Coyote Gulch Productions.


evocommon.h

00001 //---------------------------------------------------------------------
00002 //  Algorithmic Conjurings @ http://www.coyotegulch.com
00003 //  Evocosm -- An Object-Oriented Framework for Evolutionary Algorithms
00004 //
00005 //  evocommon.h
00006 //---------------------------------------------------------------------
00007 //
00008 //  Copyright 1996, 1999, 2002, 2003, 2004, 2005, 2006, 2007 Scott Robert Ladd
00009 //
00010 //  This program is free software; you can redistribute it and/or modify
00011 //  it under the terms of the GNU General Public License as published by
00012 //  the Free Software Foundation; either version 2 of the License, or
00013 //  (at your option) any later version.
00014 //
00015 //  This program is distributed in the hope that it will be useful,
00016 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 //  GNU General Public License for more details.
00019 //
00020 //  You should have received a copy of the GNU General Public License
00021 //  along with this program; if not, write to the
00022 //      Free Software Foundation, Inc.
00023 //      59 Temple Place - Suite 330
00024 //      Boston, MA 02111-1307, USA.
00025 //
00026 //-----------------------------------------------------------------------
00027 //
00028 //  For more information on this software package, please visit
00029 //  Scott's web site, Coyote Gulch Productions, at:
00030 //
00031 //      http://www.coyotegulch.com
00032 //
00033 //-----------------------------------------------------------------------
00034 
00035 #if !defined(LIBEVOCOSM_EVOGLOBAL_H)
00036 #define LIBEVOCOSM_EVOGLOBAL_H
00037 
00038 // Brahe library
00039 #if defined(_MSC_VER)
00040 #include "prng.h"
00041 #else
00042 #include "libbrahe/prng.h"
00043 #endif
00044 
00045 // Standard C++ Library
00046 #include <string>
00047 #include <iostream>
00048 #include <iomanip>
00049 
00050 // Windows
00051 #if defined(_MSC_VER)
00052 #include "windows.h"
00053 #undef max
00054 #undef min
00055 #endif
00056 
00057 namespace libevocosm
00058 {
00060 
00064     class prng
00065     {
00066     private:
00067         brahe_prng_state_t m_random;
00068 
00069     public:
00071         prng()
00072         {
00073             brahe_prng_init(&m_random,BRAHE_PRNG_MWC1038,0);
00074         }
00075 
00077         ~prng()
00078         {
00079             brahe_prng_free(&m_random);
00080         }
00081 
00083         void set_seed(uint32_t a_seed)
00084         {
00085             brahe_prng_init(&m_random,BRAHE_PRNG_MWC1038,a_seed);
00086         }
00087 
00089         uint32_t get_seed()
00090         {
00091             return m_random.m_seed;
00092         }
00093 
00095         size_t get_index(size_t n)
00096         {
00097             return brahe_prng_index(&m_random,n);
00098         }
00099 
00101         double get_real()
00102         {
00103             return brahe_prng_real2(&m_random);
00104         }
00105     };
00106 
00108 
00112     class globals
00113     {
00114     protected:
00116         static size_t rand_index(size_t n)
00117         {
00118             return g_random.get_index(n);
00119         }
00120 
00122         static prng g_random;
00123 
00125         static std::string g_version;
00126 
00127     public:
00129         static void set_random_seed(uint32_t a_seed)
00130         {
00131             g_random.set_seed(a_seed);
00132         }
00133 
00135         static uint32_t get_seed()
00136         {
00137             return g_random.get_seed();
00138         }
00139 
00141         static std::string version()
00142         {
00143             return g_version;
00144         }
00145     };
00146 
00148 
00153     class listener
00154     {
00155         public:
00157 
00161             virtual void ping_generation_begin(size_t a_generation_number) = 0;
00162 
00164 
00168             virtual void ping_generation_end(size_t a_generation_number) = 0;
00169 
00171 
00175             virtual void ping_population_begin(size_t a_population_number) = 0;
00176 
00178 
00182             virtual void ping_population_end(size_t a_population_number) = 0;
00183 
00185 
00189             virtual void ping_fitness_test_begin(size_t a_organism_number) = 0;
00190 
00192 
00196             virtual void ping_fitness_test_end(size_t a_organism_number) = 0;
00197 
00199 
00207             virtual void report(const std::string & a_text) = 0;
00208 
00210 
00217             virtual void report_error(const std::string & a_text) = 0;
00218 
00220 
00224             virtual void run_complete() = 0;
00225 
00227 
00232             virtual void yield() = 0;
00233     };
00234 
00236 
00239     class null_listener : public listener
00240     {
00241         public:
00243 
00247             virtual void ping_generation_begin(size_t a_generation_number)
00248             {
00249                 // do nothing
00250             }
00251 
00253 
00257             virtual void ping_generation_end(size_t a_generation_number)
00258             {
00259                 // do nothing
00260             }
00261 
00263 
00267             virtual void ping_population_begin(size_t a_population_number)
00268             {
00269                 // do nothing
00270             }
00271 
00273 
00277             virtual void ping_population_end(size_t a_population_number)
00278             {
00279                 // do nothing
00280             }
00281 
00283 
00287             virtual void ping_fitness_test_begin(size_t a_organism_number)
00288             {
00289                 // do nothing
00290             }
00291 
00293 
00297             virtual void ping_fitness_test_end(size_t a_organism_number)
00298             {
00299                 // do nothing
00300             }
00301 
00303 
00311             virtual void report(const std::string & a_text)
00312             {
00313                 // do nothing
00314             }
00315 
00317 
00324             virtual void report_error(const std::string & a_text)
00325             {
00326                 // do nothing
00327             }
00328 
00330 
00334             virtual void run_complete()
00335             {
00336                 // do nothing
00337             }
00338 
00340 
00345             virtual void yield()
00346             {
00347                 // do nothing
00348             }
00349     };
00350 
00352 
00356     class listener_stdout : public listener
00357     {
00358         public:
00360 
00364             virtual void ping_generation_begin(size_t a_generation_number)
00365             {
00366                 std::cout << "------------------------------------------------------------\ngeneration "
00367                           << a_generation_number << " begins" << std::endl;
00368             }
00369 
00371 
00375             virtual void ping_generation_end(size_t a_generation_number)
00376             {
00377                 // nada
00378             }
00379 
00381 
00385             virtual void ping_population_begin(size_t a_population_number)
00386             {
00387                 std::cout << "\npopulation " << std::setw(2) << a_population_number << ": " << std::flush;
00388             }
00389 
00391 
00395             virtual void ping_population_end(size_t a_population_number)
00396             {
00397                 // nada
00398             }
00399 
00401 
00405             virtual void ping_fitness_test_begin(size_t a_organism_number)
00406             {
00407                 // nada
00408             }
00409 
00411 
00415             virtual void ping_fitness_test_end(size_t a_organism_number)
00416             {
00417                 std::cout << "." << std::flush;
00418             }
00419 
00421 
00429             virtual void report(const std::string & a_text)
00430             {
00431                 std::cout << a_text;
00432             }
00433 
00435 
00442             virtual void report_error(const std::string & a_text)
00443             {
00444                 std::cerr << a_text;
00445             }
00446 
00448 
00452             virtual void run_complete()
00453             {
00454                 // nada
00455             }
00456 
00458 
00463             virtual void yield()
00464             {
00465 #if defined(_MSC_VER)
00466                 Sleep(50000);
00467 #else
00468                 usleep(50000);
00469 #endif
00470             }
00471     };
00472 
00473 }
00474 
00475 #endif

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