# t-eobreeder.cpp

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 //-----------------------------------------------------------------------------
    00004 // t-eobreeder.cpp
    00005 //   This program test the breeder object
    00006 // (c) GeNeura Team, 1998
    00007 /*
    00008     This library is free software; you can redistribute it and/or
    00009     modify it under the terms of the GNU General Public
    00010     License as published by the Free Software Foundation; either
    00011     version 2 of the License, or (at your option) any later version.
    00012
    00013     This library 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 GNU
    00016     Lesser General Public License for more details.
    00017
    00018     You should have received a copy of the GNU Lesser General Public
    00019     License along with this library; 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 #ifndef __GNUG__
    00028 // to avoid long name warnings
    00029 #pragma warning(disable:4786)
    00030 #endif // __GNUG__
    00031
    00032 #include <ga/eoBin.h>  // eoBin, eoPop, eoBreeder
    00033 #include <eoPop.h>
    00034 #include <ga/eoBitOp.h>
    00035 #include <eoProportionalOpSel.h>
    00036 #include <eoBreeder.h>
    00037
    00038 //-----------------------------------------------------------------------------
    00039
    00040 typedef eoBin<float> Chrom;
    00041
    00042 #include "binary_value.h"
    00043
    00044 //-----------------------------------------------------------------------------
    00045
    00046 main()
    00047 {
    00048   const unsigned POP_SIZE = 8, CHROM_SIZE = 4;
    00049   unsigned i;
    00050
    00051   eoBinRandom<Chrom> random;
    00052   eoPop<Chrom> pop;
    00053
    00054   BinaryValue eval;
    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 << pop[i] << " " << pop[i].fitness() << std::endl;
    00067
    00068   eoBinBitFlip<Chrom> bitflip;
    00069   eoBinCrossover<Chrom> xover;
    00070   eoProportionalOpSel<Chrom> propSel;
    00071   eoBreeder<Chrom> breeder( propSel );
    00072   propSel.addOp(bitflip, 0.25);
    00073   propSel.addOp(xover, 0.75);
    00074
    00075   breeder(pop);
    00076
    00077   // reevaluation of fitness
    00078   for_each(pop.begin(), pop.end(), BinaryValue());
    00079
    00080   std::cout << "new population:" << std::endl;
    00081   for (i = 0; i < pop.size(); ++i)
    00082     std::cout << pop[i] << " " << pop[i].fitness() << std::endl;
    00083
    00084   return 0;
    00085 }
    00086
    00087 //-----------------------------------------------------------------------------