t-eoFitnessAssembledEA.cpp

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002  
00003 //-----------------------------------------------------------------------------
00004 // t-eoFitnessAssembledEA.cpp
00005 // Marc Wintermantel & Oliver Koenig
00006 // IMES-ST@ETHZ.CH
00007 // March 2003
00008 
00009 /*
00010     This library is free software; you can redistribute it and/or
00011     modify it under the terms of the GNU Lesser General Public
00012     License as published by the Free Software Foundation; either
00013     version 2 of the License, or (at your option) any later version.
00014  
00015     This library is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018     Lesser General Public License for more details.
00019  
00020     You should have received a copy of the GNU Lesser General Public
00021     License along with this library; if not, write to the Free Software
00022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  
00024     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
00025              Marc.Schoenauer@inria.fr
00026              mak@dhi.dk
00027 */
00028 //-----------------------------------------------------------------------------
00029 #ifdef HAVE_CONFIG_H
00030 #include <config.h>
00031 #endif
00032 
00033 #include <iostream>
00034 #include <cmath>
00035 
00036 // General eo includes
00037 #include <eo>
00038 #include <utils/eoRealVectorBounds.h>   // The real bounds (not yet in general eo include)
00039 
00040 // Representation dependent includes and typedefs
00041 #include <es/eoReal.h>                  // Definition of representation
00042 #include <es/eoRealInitBounded.h>       // Uniformly initializes real vector in bounds
00043 #include <es/make_genotype_real.h>              // Initialization of a genotype
00044 #include <eoEvalFunc.h>                 // Base class for fitness evaluation 
00045 #include <es/make_op_real.h>            // Variation operators using standard Real operators
00046 #include <eoScalarFitnessAssembled.h>     // The fitness class
00047 typedef eoReal<eoAssembledMinimizingFitness> Indi;
00048 
00049 // Representation independent modules
00050 #include <do/make_pop.h>                // Initialization of population
00051 #include <do/make_continue.h>           // The stopping criterion
00052 #include <do/make_checkpoint_assembled.h>       // Outputs (stats, population dumps, ...)
00053 #include <do/make_algo_scalar.h>        // Evolution engine (selection and replacement)
00054 #include <do/make_run.h>                // simple call to the algo.stays there for consistency reasons 
00055 
00056 // Define a fitness class
00057 template <class EOT>
00058 class eoAssembledEvalFunc : public eoEvalFunc<EOT>{
00059 public:
00060   // Constructor defining number and descriptions of fitness terms
00061   eoAssembledEvalFunc() {
00062     
00063     // Define a temporary fitness object to have access to its static traits
00064     typename EOT::Fitness tmpfit(3, 0.0);    
00065     tmpfit.setDescription(0,"Fitness");
00066     tmpfit.setDescription(1,"Some Value");
00067     tmpfit.setDescription(2,"Other Value");
00068 
00069   }
00070 
00071   void operator()(EOT& _eo){
00072 
00073     // Define temporary fitness object 
00074     // (automatically gets initialized with size given in constructor)
00075     typename EOT::Fitness tmpfit;
00076     
00077     // Eval some dummy fitness
00078     double sum1=0.0, sum2=0.0;
00079     for (unsigned i=0; i < _eo.size(); ++i){
00080       sum1 += _eo[i]*_eo[i];
00081       sum2 += fabs(_eo[i]) + fabs(_eo[i]);
00082     }
00083     
00084     // Store some fitness terms
00085     tmpfit[1]= sum1;
00086     tmpfit[2]= sum2;
00087     
00088     // Store the fitness
00089     tmpfit = (sum1 + sum2)/_eo.size();
00090 
00091     // Pass it
00092     _eo.fitness( tmpfit );
00093   
00094   }
00095 };
00096 
00097 // checks for help demand, and writes the status file and make_help; in libutils
00098 void make_help(eoParser & _parser);
00099 
00100 // now use all of the above, + representation dependent things
00101 int main(int argc, char* argv[]){
00102   
00103   std::cout << "-----------------------------------" << std::endl;
00104   std::cout << "START t-eoFitnessAssembledEA" << std::endl;
00105 
00106   try{
00107 
00108     // Parser & State
00109     eoParser parser(argc, argv);  // for user-parameter reading    
00110     eoState state;    // keeps all things allocated
00111 
00113     // A) Representation dependent stuff
00115 
00116     // The fitness
00117     eoAssembledEvalFunc<Indi> plainEval;
00118     // turn that object into an evaluation counter
00119     eoEvalFuncCounter<Indi> eval(plainEval);
00120 
00121     // The genotype
00122     eoRealInitBounded<Indi>& init = do_make_genotype(parser, state, Indi() );
00123     
00124     // The variation operators
00125     eoGenOp<Indi>& op = do_make_op(parser, state, init);
00126 
00128     // B) Create representation independent stuff
00130 
00131     // initialize the population
00132     // yes, this is representation indepedent once you have an eoInit
00133     eoPop<Indi>& pop   = do_make_pop(parser, state, init);
00134 
00135     // stopping criteria
00136     eoContinue<Indi> & term = do_make_continue(parser, state, eval);
00137     // output
00138     eoCheckPoint<Indi> & checkpoint = do_make_checkpoint_assembled(parser, state, eval, term);
00139     // algorithm (need the operator!)
00140     eoAlgo<Indi>& ga = do_make_algo_scalar(parser, state, eval, checkpoint, op);
00141 
00142 
00143     make_help(parser);  // To be called after all parameters have been read !
00144 
00146     // C) Run the algorithm
00148 
00149     // evaluate intial population AFTER help and status in case it takes time
00150     apply<Indi>(eval, pop);
00151     // if you want to print it out
00152     std::cout << "Initial Population\n";
00153     pop.sortedPrintOn(std::cout);
00154     std::cout << std::endl;
00155 
00156     do_run(ga, pop); // run the ga
00157 
00158     std::cout << "Final Population\n";
00159     pop.sortedPrintOn(std::cout);
00160     std::cout << std::endl;
00161     
00162   }
00163   catch(std::exception& e)
00164     {
00165       std::cout << e.what() << std::endl;
00166       return 1;
00167     }
00168 
00169   std::cout << "-----------------------------------" << std::endl;
00170   std::cout << "END t-eoFitnessAssembledEA" << std::endl;
00171 
00172   return 0;
00173 
00174 }

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