# t-MGE1bit.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 #include "RoyalRoad.h"
    00017
    00018 // Viri
    00019 #include "VirusOp.h"
    00020 #include "eoVirus.h"
    00021 #include "eoInitVirus.h"
    00022
    00023 //-----------------------------------------------------------------------------
    00024
    00025 typedef eoVirus<float> Chrom;
    00026
    00027 //-----------------------------------------------------------------------------
    00028
    00029 int main()
    00030 {
    00031   const unsigned POP_SIZE = 10, CHROM_SIZE = 12;
    00032   unsigned i;
    00033   eoBooleanGenerator gen;
    00034
    00035   // the populations:
    00036   eoPop<Chrom> pop;
    00037
    00038   // Evaluation
    00039   RoyalRoad<Chrom> rr( 8 );
    00040   eoEvalFuncCounter<Chrom> eval( rr );
    00041
    00042   eoInitVirus1bit<float> random(CHROM_SIZE, gen);
    00043   for (i = 0; i < POP_SIZE; ++i) {
    00044       Chrom chrom;
    00045       random(chrom);
    00046       eval(chrom);
    00047       pop.push_back(chrom);
    00048   }
    00049
    00050   std::cout << "population:" << std::endl;
    00051   for (i = 0; i < pop.size(); ++i)
    00052     std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
    00053
    00054   // selection
    00055   eoStochTournamentSelect<Chrom> lottery(0.9 );
    00056
    00057   // breeder
    00058   VirusShiftMutation<float> vm;
    00059   VirusTransmission<float> vt;
    00060   VirusBitFlip<float> vf;
    00061   eoUBitXover<Chrom> xover;
    00062   eoProportionalOp<Chrom> propSel;
    00063   eoGeneralBreeder<Chrom> breeder( lottery, propSel );
    00064   propSel.add(vm, 0.8);
    00065   propSel.add(vf, 0.05);
    00066   propSel.add(vt, 0.05);
    00067   propSel.add(xover, 0.1);
    00068
    00069   // Replace a single one
    00070   eoCommaReplacement<Chrom> replace;
    00071
    00072   // Terminators
    00073   eoGenContinue<Chrom> continuator1(10);
    00074   eoFitContinue<Chrom> continuator2(CHROM_SIZE);
    00075   eoCombinedContinue<Chrom> continuator(continuator1, continuator2);
    00076   eoCheckPoint<Chrom> checkpoint(continuator);
    00077   eoStdoutMonitor monitor;
    00078   checkpoint.add(monitor);
    00079   eoSecondMomentStats<Chrom> stats;
    00080   eoPopStat<Chrom> dumper( 10 );
    00081   monitor.add(stats);
    00082   checkpoint.add(dumper);
    00083   checkpoint.add(stats);
    00084
    00085   // GA generation
    00086   eoEasyEA<Chrom> ea(checkpoint, eval, breeder, replace);
    00087
    00088   // evolution
    00089   try {
    00090       ea(pop);
    00091   } catch (std::exception& e) {
    00092       std::cerr << "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 EXIT_SUCCESS;
    00102 }
    00103
    00104 //-----------------------------------------------------------------------------
    00105
    00106
    00107
    00108 // Local Variables:
    00109 // mode: C++
    00110 // c-file-style: "Stroustrup"
    00111 // End: