# t-eogeneration.cpp

    00001 /* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003     t-eogeneration.cpp
    00004       Testing the eoGeneration classes, and classes related to it
    00005
    00006     (c) GeNeura Team, 1999, 2000
    00007
    00008     This program is free software; you can redistribute it and/or modify
    00009     it under the terms of the GNU General Public License as published by
    00010     the Free Software Foundation; either version 2 of the License, or
    00011     (at your option) any later version.
    00012
    00013     This program is distributed in the hope that it will be useful,
    00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00016     GNU General Public License for more details.
    00017
    00018     You should have received a copy of the GNU General Public License
    00019     along with this program; if not, write to the Free Software
    00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00021
    00022     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
    00023
    00024 */
    00025
    00026 //-----------------------------------------------------------------------------//
    00027
    00028
    00029 // to avoid long name warnings
    00030 #ifdef _MSC_VER
    00031 #pragma warning(disable:4786)
    00032 #endif
    00033
    00034 #include <eoGeneration.h>
    00035 #include <eoEvalFuncPtrCnt.h>
    00036
    00037 #include "binary_value.h"
    00038
    00039 //-----------------------------------------------------------------------------
    00040
    00041 typedef eoBin<float> Chrom;
    00042
    00043 //-----------------------------------------------------------------------------
    00044
    00045 main()
    00046 {
    00047   const unsigned POP_SIZE = 8, CHROM_SIZE = 16;
    00048   unsigned i;
    00049
    00050   eoBinRandom<Chrom> random;
    00051   eoPop<Chrom> pop;
    00052
    00053   // Evaluation
    00054   eoEvalFuncPtr<Chrom> eval( binary_value );
    00055
    00056   for (i = 0; i < POP_SIZE; ++i)
    00057     {
    00058       Chrom chrom(CHROM_SIZE);
    00059       random(chrom);
    00060       eval(chrom);
    00061       pop.push_back(chrom);
    00062     }
    00063
    00064   std::cout << "population:" << std::endl;
    00065   for (i = 0; i < pop.size(); ++i)
    00066     std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
    00067
    00068
    00069   // selection
    00070   eoLottery<Chrom> lottery;
    00071
    00072   // breeder
    00073   eoBinBitFlip<Chrom> bitflip;
    00074   eoBinCrossover<Chrom> xover;
    00075   eoProportionalOpSel<Chrom> propSel;
    00076   eoBreeder<Chrom> breeder( propSel );
    00077   propSel.addOp(bitflip, 0.25);
    00078   propSel.addOp(xover, 0.75);
    00079
    00080   // replacement
    00081   eoInclusion<Chrom> inclusion;
    00082
    00083
    00084
    00085   // GA generation
    00086   eoGeneration<Chrom> generation(lottery, breeder, inclusion, eval);
    00087
    00088   // evolution
    00089   unsigned g = 0;
    00090   do {
    00091     try
    00092       {
    00093         generation(pop);
    00094       }
    00095     catch (std::exception& e)
    00096       {
    00097         std::cout << "exception: " << e.what() << std::endl;;
    00098         exit(EXIT_FAILURE);
    00099       }
    00100
    00101     std::cout << "pop[" << ++g << "]" << std::endl;
    00102     for (i = 0; i < pop.size(); ++i)
    00103       std::cout << "\t" <<  pop[i] << " " << pop[i].fitness() << std::endl;
    00104
    00105   } while (pop[0].fitness() < pow(2.0, CHROM_SIZE) - 1);
    00106
    00107   // Try again, with a "counted" evaluation function
    00108   // GA generation
    00109   // Evaluation
    00110   eoEvalFuncPtrCnt<Chrom> eval2( binary_value );
    00111   eoPop<Chrom> pop2;
    00112
    00113   for (i = 0; i < POP_SIZE; ++i)
    00114     {
    00115       Chrom chrom(CHROM_SIZE);
    00116       random(chrom);
    00117       binary_value(chrom);
    00118       eval2(chrom);
    00119       pop2.push_back(chrom);
    00120     }
    00121   eoGeneration<Chrom> generation2(lottery, breeder, inclusion, eval2);
    00122
    00123   // evolution
    00124   do {
    00125     try
    00126       {
    00127         generation2(pop2);
    00128       }
    00129     catch (std::exception& e)
    00130       {
    00131         std::cout << "exception: " << e.what() << std::endl;;
    00132         exit(EXIT_FAILURE);
    00133       }
    00134
    00135     std::cout << "pop[" << ++g << "]" << std::endl;
    00136     for (i = 0; i < pop2.size(); ++i)
    00137       std::cout << "\t" <<  pop2[i] << " " << pop[i].fitness() << std::endl;
    00138
    00139   } while (pop2[0].fitness() < pow(2.0, CHROM_SIZE) - 1);
    00140
    00141   std::cout << "Number of evaluations " << eval2.getNumOfEvaluations() << std::endl;
    00142   return 0;
    00143 }
    00144
    00145 //-----------------------------------------------------------------------------