# OneMaxLibEA.cpp

    00001
    00006 /*
    00007 Template for creating a new representation in EO
    00008 ================================================
    00009
    00010 This is the template main file for compiling after creating a
    00011 library.
    00012 See make_OneMax.cpp file.
    00013 */
    00014
    00015 // Miscilaneous include and declaration
    00016 #include <iostream>
    00017 using namespace std;
    00018
    00019 // eo general include
    00020 #include "eo"
    00021 // the real bounds (not yet in general eo include)
    00022 #include "utils/eoRealVectorBounds.h"
    00023
    00024 // include here whatever specific files for your representation
    00025 // Basically, this should include at least the following
    00026
    00030 #include "eoOneMax.h"
    00031
    00035 #include "eoOneMaxInit.h"
    00036
    00042 #include "eoOneMaxEvalFunc.h"
    00043
    00044 // GENOTYPE   eoOneMax ***MUST*** be templatized over the fitness
    00045
    00046 //
    00047 // START fitness type: double or eoMaximizingFitness if you are maximizing
    00048 //                     eoMinimizingFitness if you are minimizing
    00049 typedef eoMinimizingFitness MyFitT ;    // type of fitness
    00050 // END fitness type
    00051 //
    00052
    00053 // Then define your EO objects using that fitness type
    00054 typedef eoOneMax<MyFitT> Indi;      // ***MUST*** derive from EO
    00055
    00056 // create an initializer - done here and NOT in make_OneMax.cpp
    00057 // because it is NOT representation independent
    00058 #include "make_genotype_OneMax.h"
    00059 eoInit<Indi> & make_genotype(eoParser& _parser, eoState&_state, Indi _eo)
    00060 {
    00061   return do_make_genotype(_parser, _state, _eo);
    00062 }
    00063
    00064 // same thing for the variation operaotrs
    00065 #include "make_op_OneMax.h"
    00066 eoGenOp<Indi>&  make_op(eoParser& _parser, eoState& _state, eoInit<Indi>& _init)
    00067 {
    00068   return do_make_op(_parser, _state, _init);
    00069 }
    00070
    00071 // The representation independent routines are simply declared here
    00072
    00073 // how to initialize the population
    00074 // it IS representation independent if an eoInit is given
    00075 eoPop<Indi >&  make_pop(eoParser& _parser, eoState& _state, eoInit<Indi> & _init);
    00076
    00077 // the stopping criterion
    00078 eoContinue<Indi>& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter<Indi> & _eval);
    00079
    00080 // outputs (stats, population dumps, ...)
    00081 eoCheckPoint<Indi>& make_checkpoint(eoParser& _parser, eoState& _state, eoEvalFuncCounter<Indi>& _eval, eoContinue<Indi>& _continue);
    00082
    00083 // evolution engine (selection and replacement)
    00084 eoAlgo<Indi>&  make_algo_scalar(eoParser& _parser, eoState& _state, eoEvalFunc<Indi>& _eval, eoContinue<Indi>& _continue, eoGenOp<Indi>& _op);
    00085
    00086 // simple call to the algo. stays there for consistency reasons
    00087 // no template for that one
    00088 void run_ea(eoAlgo<Indi>& _ga, eoPop<Indi>& _pop);
    00089
    00090 // checks for help demand, and writes the status file
    00091 // and make_help; in libutils - just a declaration, code in libeoutils.a
    00092 void make_help(eoParser & _parser);
    00093
    00094 // now use all of the above, + representation dependent things
    00095 // from here on, no difference with eoOneMax.cpp
    00096 int main(int argc, char* argv[])
    00097 {
    00098
    00099   try
    00100   {
    00101   eoParser parser(argc, argv);  // for user-parameter reading
    00102
    00103   eoState state;    // keeps all things allocated
    00104
    00105     // The fitness
    00107    eoOneMaxEvalFunc<Indi> plainEval/* (varType  _anyVariable) */;
    00108    // turn that object into an evaluation counter
    00109    eoEvalFuncCounter<Indi> eval(plainEval);
    00110
    00111   // the genotype - through a genotype initializer
    00112   eoInit<Indi>& init = make_genotype(parser, state, Indi());
    00113
    00114   // Build the variation operator (any seq/prop construct)
    00115   eoGenOp<Indi>& op = make_op(parser, state, init);
    00116
    00117
    00119   //
    00120   // YOU SHOULD NOT NEED TO MODIFY ANYTHING BEYOND THIS POINT
    00121   // unless you want to add specific statistics to the checkpoint
    00123
    00124   // initialize the population
    00125   // yes, this is representation indepedent once you have an eoInit
    00126   eoPop<Indi>& pop   = make_pop(parser, state, init);
    00127
    00128   // stopping criteria
    00129   eoContinue<Indi> & term = make_continue(parser, state, eval);
    00130   // output
    00131   eoCheckPoint<Indi> & checkpoint = make_checkpoint(parser, state, eval, term);
    00132   // algorithm (need the operator!)
    00133   eoAlgo<Indi>& ga = make_algo_scalar(parser, state, eval, checkpoint, op);
    00134
    00136
    00138   // to be called AFTER all parameters have been read!!!
    00139   make_help(parser);
    00140
    00143   // evaluate intial population AFTER help and status in case it takes time
    00144   apply<Indi>(eval, pop);
    00145   // if you want to print it out
    00146    cout << "Initial Population\n";
    00147    pop.sortedPrintOn(cout);
    00148    cout << endl;
    00149
    00150   run_ea(ga, pop); // run the ga
    00151
    00152   cout << "Final Population\n";
    00153   pop.sortedPrintOn(cout);
    00154   cout << endl;
    00155
    00156   }
    00157   catch(exception& e)
    00158   {
    00159     cout << e.what() << endl;
    00160   }
    00161   return 0;
    00162 }