t-MGE-control.cpp

00001 //-----------------------------------------------------------------------------
00002 // t-eoMGE.cpp
00003 //-----------------------------------------------------------------------------
00004 
00005 #ifdef HAVE_CONFIG_H
00006 #include <config.h>
00007 #endif
00008 
00009 #ifndef __GNUG__
00010 // to avoid long name warnings
00011 #pragma warning(disable:4786)
00012 #endif // __GNUG__
00013 
00014 #include "eo"
00015 #include "ga/eoBitOp.h"
00016 
00017 #include "RoyalRoad.h"
00018 
00019 // Viri
00020 #include "VirusOp.h"
00021 #include "eoVirus.h"
00022 #include "eoInitVirus.h"
00023 
00024 //-----------------------------------------------------------------------------
00025 
00026 typedef eoVirus<float> Chrom;
00027 
00028 //-----------------------------------------------------------------------------
00029 
00030 int main()
00031 {
00032   const unsigned POP_SIZE = 10, CHROM_SIZE = 12;
00033   unsigned i;
00034   eoBooleanGenerator gen;
00035 
00036   // the populations:
00037   eoPop<Chrom> pop;
00038 
00039   // Evaluation
00040   RoyalRoad<Chrom> rr( 8 );
00041   eoEvalFuncCounter<Chrom> eval( rr );
00042 
00043   eoInitVirus<float> random(CHROM_SIZE, gen);
00044   for (i = 0; i < POP_SIZE; ++i) {
00045       Chrom chrom;
00046       random(chrom);
00047       eval(chrom);
00048       pop.push_back(chrom);
00049   }
00050 
00051   std::cout << "population:" << std::endl;
00052   for (i = 0; i < pop.size(); ++i)
00053     std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
00054 
00055   // selection
00056   eoStochTournamentSelect<Chrom> lottery(0.9 );
00057 
00058   // breeder
00059   eoOneBitFlip<Chrom> vm;
00060   eoUBitXover<Chrom> xover;
00061   eoProportionalOp<Chrom> propSel;
00062   eoGeneralBreeder<Chrom> breeder( lottery, propSel );
00063   propSel.add(vm, 0.2);
00064   propSel.add(xover, 0.8);
00065 
00066   // Replace a single one
00067   eoCommaReplacement<Chrom> replace;
00068 
00069   // Terminators
00070   eoGenContinue<Chrom> continuator1(10);
00071   eoFitContinue<Chrom> continuator2(CHROM_SIZE);
00072   eoCombinedContinue<Chrom> continuator(continuator1, continuator2);
00073   eoCheckPoint<Chrom> checkpoint(continuator);
00074   eoStdoutMonitor monitor;
00075   checkpoint.add(monitor);
00076   eoSecondMomentStats<Chrom> stats;
00077   eoPopStat<Chrom> dumper( 10 );
00078   monitor.add(stats);
00079   checkpoint.add(dumper);
00080   checkpoint.add(stats);
00081 
00082   // GA generation
00083   eoEasyEA<Chrom> ea(checkpoint, eval,  breeder, replace );
00084 
00085   // evolution
00086   try
00087     {
00088       ea(pop);
00089     }
00090   catch (std::exception& e)
00091     {
00092         std::cout << "exception: " << e.what() << std::endl;;
00093         exit(EXIT_FAILURE);
00094     }
00095 
00096   std::cout << "pop" << std::endl;
00097   for (i = 0; i < pop.size(); ++i)
00098     std::cout << "\t" <<  pop[i] << " " << pop[i].fitness() << std::endl;
00099 
00100   std::cout << "\n --> Number of Evaluations = " << eval.getValue() << std::endl;
00101   return 0;
00102 }
00103 
00104 //-----------------------------------------------------------------------------

Generated on Thu Apr 19 11:02:30 2007 for EO by  doxygen 1.4.7