MyStructEA.cpp

00001 
00006 /*
00007 Template for creating a new representation in EO
00008 ================================================
00009 
00010 This is the template main file.
00011 It includes all other files that have been generated by the script create.sh
00012 so it is the only file to compile.
00013 
00014 In case you want to build up a separate library for your new Evolving Object,
00015 you'll need some work - follow what's done in the src/ga dir, used in the
00016 main file BitEA in tutorial/Lesson4 dir.
00017 Or you can wait until we do it :-)
00018 */
00019 
00020 // Miscilaneous include and declaration 
00021 #include <iostream>
00022 using namespace std;
00023 
00024 // eo general include
00025 #include "eo"
00026 // the real bounds (not yet in general eo include)
00027 #include "utils/eoRealVectorBounds.h"
00028 
00029 // include here whatever specific files for your representation
00030 // Basically, this should include at least the following
00031 
00035 #include "eoMyStruct.h"
00036 
00040 #include "eoMyStructInit.h"
00041 
00047 #include "eoMyStructEvalFunc.h"
00048 
00049 // GENOTYPE   eoMyStruct ***MUST*** be templatized over the fitness
00050 
00051 //*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
00052 // START fitness type: double or eoMaximizingFitness if you are maximizing
00053 //                     eoMinimizingFitness if you are minimizing
00054 typedef eoMinimizingFitness MyFitT ;    // type of fitness 
00055 // END fitness type
00056 //*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
00057 
00058 // Then define your EO objects using that fitness type
00059 typedef eoMyStruct<MyFitT> Indi;      // ***MUST*** derive from EO 
00060 
00061 // create an initializer
00062 #include "make_genotype_MyStruct.h"
00063 eoInit<Indi> & make_genotype(eoParser& _parser, eoState&_state, Indi _eo)
00064 {
00065   return do_make_genotype(_parser, _state, _eo);
00066 } 
00067 
00068 // and the variation operaotrs
00069 #include "make_op_MyStruct.h"
00070 eoGenOp<Indi>&  make_op(eoParser& _parser, eoState& _state, eoInit<Indi>& _init)
00071 {
00072   return do_make_op(_parser, _state, _init);
00073 }
00074 
00075 // Use existing modules to define representation independent routines
00076 // These are parser-based definitions of objects
00077 
00078 // how to initialize the population 
00079 // it IS representation independent if an eoInit is given
00080 #include <do/make_pop.h>
00081 eoPop<Indi >&  make_pop(eoParser& _parser, eoState& _state, eoInit<Indi> & _init)
00082 {
00083   return do_make_pop(_parser, _state, _init);
00084 }
00085 
00086 // the stopping criterion
00087 #include <do/make_continue.h>
00088 eoContinue<Indi>& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter<Indi> & _eval)
00089 {
00090   return do_make_continue(_parser, _state, _eval);
00091 }
00092 
00093 // outputs (stats, population dumps, ...)
00094 #include <do/make_checkpoint.h>
00095 eoCheckPoint<Indi>& make_checkpoint(eoParser& _parser, eoState& _state, eoEvalFuncCounter<Indi>& _eval, eoContinue<Indi>& _continue) 
00096 {
00097   return do_make_checkpoint(_parser, _state, _eval, _continue);
00098 }
00099 
00100 // evolution engine (selection and replacement)
00101 #include <do/make_algo_scalar.h>
00102 eoAlgo<Indi>&  make_algo_scalar(eoParser& _parser, eoState& _state, eoEvalFunc<Indi>& _eval, eoContinue<Indi>& _continue, eoGenOp<Indi>& _op)
00103 {
00104   return do_make_algo_scalar(_parser, _state, _eval, _continue, _op);
00105 }
00106 
00107 // simple call to the algo. stays there for consistency reasons 
00108 // no template for that one
00109 #include <do/make_run.h>
00110 // the instanciating fitnesses
00111 #include <eoScalarFitness.h>
00112 void run_ea(eoAlgo<Indi>& _ga, eoPop<Indi>& _pop)
00113 {
00114   do_run(_ga, _pop);
00115 }
00116 
00117 // checks for help demand, and writes the status file
00118 // and make_help; in libutils
00119 void make_help(eoParser & _parser);
00120 
00121 // now use all of the above, + representation dependent things
00122 int main(int argc, char* argv[])
00123 {
00124 
00125   try
00126   {
00127   eoParser parser(argc, argv);  // for user-parameter reading
00128 
00129   eoState state;    // keeps all things allocated
00130 
00131     // The fitness
00133    eoMyStructEvalFunc<Indi> plainEval/* (varType  _anyVariable) */;
00134    // turn that object into an evaluation counter
00135    eoEvalFuncCounter<Indi> eval(plainEval);
00136 
00137   // the genotype - through a genotype initializer
00138   eoInit<Indi>& init = make_genotype(parser, state, Indi());
00139 
00140   // Build the variation operator (any seq/prop construct)
00141   eoGenOp<Indi>& op = make_op(parser, state, init);
00142 
00143 
00145   //
00146   // YOU SHOULD NOT NEED TO MODIFY ANYTHING BEYOND THIS POINT
00147   // unless you want to add specific statistics to the checkpoint
00149 
00150   // initialize the population
00151   // yes, this is representation indepedent once you have an eoInit
00152   eoPop<Indi>& pop   = make_pop(parser, state, init);
00153 
00154   // stopping criteria
00155   eoContinue<Indi> & term = make_continue(parser, state, eval);
00156   // output
00157   eoCheckPoint<Indi> & checkpoint = make_checkpoint(parser, state, eval, term);
00158   // algorithm (need the operator!)
00159   eoAlgo<Indi>& ga = make_algo_scalar(parser, state, eval, checkpoint, op);
00160 
00162 
00164   // to be called AFTER all parameters have been read!!!
00165   make_help(parser);
00166 
00169   // evaluate intial population AFTER help and status in case it takes time
00170   apply<Indi>(eval, pop);
00171   // if you want to print it out
00172 //   cout << "Initial Population\n";
00173 //   pop.sortedPrintOn(cout);
00174 //   cout << endl;
00175 
00176   run_ea(ga, pop); // run the ga
00177 
00178   cout << "Final Population\n";
00179   pop.sortedPrintOn(cout);
00180   cout << endl;
00181 
00182   }
00183   catch(exception& e)
00184   {
00185     cout << e.what() << endl;
00186   }
00187   return 0;
00188 }

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