t-eoEasyEA.cpp

00001 //-----------------------------------------------------------------------------
00002 // t-eoEasyEA.cpp
00003 //-----------------------------------------------------------------------------
00004 
00005 #ifndef __GNUG__
00006 // to avoid long name warnings
00007 #pragma warning(disable:4786)
00008 #endif // __GNUG__
00009 
00010 #include <eo>
00011 
00012 #include "binary_value.h"
00013 
00014 //-----------------------------------------------------------------------------
00015 
00016 typedef eoBin<float> Chrom;
00017 
00018 //-----------------------------------------------------------------------------
00019 
00020 main()
00021 {
00022   const unsigned POP_SIZE = 8, CHROM_SIZE = 16;
00023   unsigned i;
00024 
00025 // a chromosome randomizer
00026   eoBinRandom<Chrom> random;
00027 // the populations: 
00028   eoPop<Chrom> pop; 
00029 
00030    // Evaluation
00031   eoEvalFuncPtr<Chrom> eval(  binary_value );
00032  
00033   for (i = 0; i < POP_SIZE; ++i)
00034     {
00035       Chrom chrom(CHROM_SIZE);
00036       random(chrom);
00037       eval(chrom);
00038       pop.push_back(chrom);
00039     }
00040   
00041   std::cout << "population:" << std::endl;
00042   for (i = 0; i < pop.size(); ++i)
00043     std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
00044 
00045   
00046   // selection
00047   eoLottery<Chrom> lottery;
00048 
00049   // breeder
00050   eoBinBitFlip<Chrom> bitflip;
00051   eoBinCrossover<Chrom> xover;
00052   eoProportionalOpSel<Chrom> propSel;
00053   eoBreeder<Chrom> breeder( propSel );
00054   propSel.addOp(bitflip, 0.25);
00055   propSel.addOp(xover, 0.75);
00056   
00057   // replacement
00058   eoInclusion<Chrom> inclusion;
00059 
00060   // Terminators
00061   eoFitTerm<Chrom> term( pow(2.0, CHROM_SIZE), 1 );
00062 
00063   // GA generation
00064   eoEasyEA<Chrom> ea(lottery, breeder, inclusion, eval, term);
00065 
00066   // evolution
00067   try
00068     {
00069       ea(pop);
00070     }
00071   catch (std::exception& e)
00072     {
00073         std::cout << "exception: " << e.what() << std::endl;;
00074         exit(EXIT_FAILURE);
00075     }
00076   
00077   std::cout << "pop" << std::endl;
00078   for (i = 0; i < pop.size(); ++i)
00079     std::cout << "\t" <<  pop[i] << " " << pop[i].fitness() << std::endl;
00080   
00081   return 0;
00082 }
00083 
00084 //-----------------------------------------------------------------------------

Generated on Thu Oct 19 05:06:43 2006 for EO by  doxygen 1.3.9.1