Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages | Search for

# t-MGE.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   VirusMutation<float> vm;
    00060   VirusTransmission<float> vt;
    00061   VirusBitFlip<float> vf;
    00062   eoUBitXover<Chrom> xover;
    00063   eoProportionalOp<Chrom> propSel;
    00064   eoGeneralBreeder<Chrom> breeder( lottery, propSel );
    00065   propSel.add(vm, 0.1);
    00066   propSel.add(vf, 0.05);
    00067   propSel.add(vt, 0.05);
    00068   propSel.add(xover, 0.8);
    00069
    00070   // Replace a single one
    00071   eoCommaReplacement<Chrom> replace;
    00072
    00073   // Terminators
    00074   eoGenContinue<Chrom> continuator1(10);
    00075   eoFitContinue<Chrom> continuator2(CHROM_SIZE);
    00076   eoCombinedContinue<Chrom> continuator(continuator1, continuator2);
    00077   eoCheckPoint<Chrom> checkpoint(continuator);
    00078   eoStdoutMonitor monitor;
    00079   checkpoint.add(monitor);
    00080   eoSecondMomentStats<Chrom> stats;
    00081   eoPopStat<Chrom> dumper( 10 );
    00082   monitor.add(stats);
    00083   checkpoint.add(dumper);
    00084   checkpoint.add(stats);
    00085
    00086   // GA generation
    00087   eoEasyEA<Chrom> ea(checkpoint, eval,  breeder, replace );
    00088
    00089   // evolution
    00090   try
    00091     {
    00092       ea(pop);
    00093     }
    00094   catch (std::exception& e)
    00095     {
    00096         std::cout << "exception: " << e.what() << std::endl;;
    00097         exit(EXIT_FAILURE);
    00098     }
    00099
    00100   std::cout << "pop" << std::endl;
    00101   for (i = 0; i < pop.size(); ++i)
    00102     std::cout << "\t" <<  pop[i] << " " << pop[i].fitness() << std::endl;
    00103
    00104   std::cout << "\n --> Number of Evaluations = " << eval.getValue() << std::endl;
    00105   return 0;
    00106 }
    00107
    00108 //-----------------------------------------------------------------------------