t-eoGOpSel.cpp

00001 /* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003     t-eoGOpSel.cpp
00004       Testing proportional operator selectors
00005 
00006     (c) Maarten Keijzer and GeNeura Team, 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 #ifndef __GNUG__
00029 // to avoid long name warnings
00030 #pragma warning(disable:4786)
00031 #endif // __GNUG__
00032 
00033 #include <iostream>
00034 
00035 #include <ga/eoBin.h>  // eoBin, eoPop, eoBreeder
00036 #include <eoPop.h>
00037 #include <ga/eoBitOp.h>
00038 #include <eoProportionalGOpSel.h>
00039 #include <eoSequentialGOpSelector.h>
00040 #include <eoRandomIndiSelector.h>
00041 
00042 #include <eoDetTournamentIndiSelector.h>
00043 #include <eoDetTournamentInserter.h>
00044 #include <eoStochTournamentInserter.h>
00045 
00046 #include <eoGOpBreeder.h>
00047 
00048 #include <utils/eoRNG.h>
00049 #include <utils/eoState.h>
00050 
00051 // Fitness evaluation
00052 #include "binary_value.h"
00053 
00054 //-----------------------------------------------------------------------------
00055 
00056 typedef eoBin<float> Chrom;
00057 
00058 //-----------------------------------------------------------------------------
00059 
00060 main()
00061 {
00062   rng.reseed(42); // reproducible random seed
00063 
00064   const unsigned POP_SIZE = 8, CHROM_SIZE = 4;
00065   unsigned i;
00066 
00067   eoBinRandom<Chrom> random;
00068   eoPop<Chrom> pop; 
00069   
00070   for (i = 0; i < POP_SIZE; ++i)
00071     {
00072       Chrom chrom(CHROM_SIZE);
00073       random(chrom);
00074       chrom.fitness(binary_value(chrom));
00075       pop.push_back(chrom);
00076     }
00077   
00078   std::cout << "population:" << std::endl;
00079   for (i = 0; i < pop.size(); ++i)
00080     std::cout << pop[i] << " " << pop[i].fitness() << std::endl;
00081 
00082   eoBinBitFlip<Chrom> bitflip;
00083   eoBinCrossover<Chrom> xover;
00084   
00085   eoEvalFuncPtr<Chrom> eval(binary_value);
00086 
00087   //Create the proportional operator selector and add the 
00088   // two operators creatd above to it.
00089 
00090   eoProportionalGOpSel<Chrom > propSel;
00091   eoSequentialGOpSel<Chrom>    seqSel;
00092 
00093   propSel.addOp(bitflip, 0.5);
00094   propSel.addOp(xover, 0.5);
00095   
00096   // seqSel selects operator in sequence, creating a combined operator
00097   // add a bitflip, an xover and another bitflip
00098   seqSel.addOp(bitflip, 0.25);
00099   seqSel.addOp(xover, 0.5);
00100   seqSel.addOp(bitflip, 0.25);
00101 
00102 
00103   eoRandomIndiSelector<Chrom>         selector1;
00104   eoDetTournamentIndiSelector<Chrom>  selector2(2);
00105 
00106   eoBackInserter<Chrom> inserter1;
00107   eoDetTournamentInserter<Chrom> inserter2(eval, 2);
00108   eoStochTournamentInserter<Chrom> inserter3(eval, 0.9f);
00109 
00110   eoGOpBreeder<Chrom> breeder1(propSel, selector1);
00111   eoGOpBreeder<Chrom> breeder2(seqSel, selector1);
00112   eoGOpBreeder<Chrom> breeder3(propSel, selector2);
00113   eoGOpBreeder<Chrom> breeder4(seqSel, selector2);
00114 
00115   // test the breeders
00116 
00117   breeder1(pop);
00118   breeder2(pop);
00119   breeder3(pop);
00120   breeder4(pop);
00121 
00122   eoState state;
00123 
00124   state.registerObject(pop);
00125 
00126   state.save(std::std::cout);
00127     
00128   return 0;
00129 }
00130 
00131 //-----------------------------------------------------------------------------

Generated on Thu Apr 19 11:02:30 2007 for EO by  doxygen 1.4.7