# ESEA.cpp

    00001 // Program to test several EO-ES features
    00002
    00003 #ifdef _MSC_VER
    00004 #pragma warning(disable:4786)
    00005 #endif
    00006
    00007 #include <algorithm>
    00008 #include <string>
    00009 #include <iostream>
    00010 #include <iterator>
    00011 #include <stdexcept>
    00012 #include <time.h>
    00013
    00014 using namespace std;
    00015
    00016 #include <eo>
    00017
    00018 // representation specific
    00019 #include <es/make_es.h>
    00020
    00021 #include "real_value.h"         // the sphere fitness
    00022
    00023 // Now the main
    00025 typedef eoMinimizingFitness  FitT;
    00026
    00027 template <class EOT>
    00028 void runAlgorithm(EOT, eoParser& _parser, eoState& _state);
    00029
    00030 int main_function(int argc, char *argv[])
    00031 {
    00032   // Create the command-line parser
    00033   eoParser parser(argc, argv);  // for user-parameter reading
    00034
    00035   eoState state;    // keeps all things allocated
    00036
    00037
    00038   eoValueParam<bool>& simpleParam = parser.createParam(true, "Isotropic", "Isotropic self-adaptive mutation", 'i', "ES mutation");
    00039   eoValueParam<bool>& stdevsParam = parser.createParam(false, "Stdev", "One self-adaptive stDev per variable", 's', "ES mutation");
    00040   eoValueParam<bool>& corrParam = parser.createParam(false, "Correl", "Use correlated mutations", 'c', "ES mutation");
    00041
    00042     // Run the appropriate algorithm
    00043     if (simpleParam.value() == false)
    00044     {
    00045       cout << "Using eoReal" << endl;
    00046       runAlgorithm(eoReal<FitT>(), parser, state);
    00047     }
    00048     else if (stdevsParam.value() == false)
    00049     {
    00050       cout << "Using eoEsSimple" << endl;
    00051       runAlgorithm(eoEsSimple<FitT>(), parser, state);
    00052     }
    00053     else if (corrParam.value() == false)
    00054     {
    00055       cout << "Using eoEsStdev" << endl;
    00056       runAlgorithm(eoEsStdev<FitT>(), parser, state);
    00057     }
    00058     else
    00059     {
    00060       cout << "Using eoEsFull" << endl;
    00061       runAlgorithm(eoEsFull<FitT>(), parser, state);
    00062     }
    00063
    00064         return 0;
    00065 }
    00066
    00067 // A main that catches the exceptions
    00068
    00069 int main(int argc, char **argv)
    00070 {
    00071     try
    00072     {
    00073         main_function(argc, argv);
    00074     }
    00075     catch(exception& e)
    00076     {
    00077         cout << "Exception: " << e.what() << '\n';
    00078     }
    00079
    00080     return 1;
    00081 }
    00082
    00087 template <class EOT>
    00088 void runAlgorithm(EOT, eoParser& _parser, eoState& _state)
    00089 {
    00090   typedef typename EOT::Fitness FitT;
    00091
    00094
    00095   // The evaluation fn - encapsulated into an eval counter for output
    00096   eoEvalFuncPtr<EOT, double, const std::vector<double>&>
    00097                mainEval( real_value );
    00098   eoEvalFuncCounter<EOT> eval(mainEval);
    00099
    00100   // the genotype - through a genotype initializer
    00101   eoRealInitBounded<EOT>& init = make_genotype(_parser, _state, EOT());
    00102
    00103   // Build the variation operator (any seq/prop construct)
    00104   eoGenOp<EOT>& op = make_op(_parser, _state, init);
    00105
    00108
    00109   // initialize the population - and evaluate
    00110   // yes, this is representation indepedent once you have an eoInit
    00111   eoPop<EOT>& pop   = make_pop(_parser, _state, init);
    00112   apply<EOT>(eval, pop);
    00113
    00114   // stopping criteria
    00115   eoContinue<EOT> & term = make_continue(_parser, _state, eval);
    00116   // output
    00117   eoCheckPoint<EOT> & checkpoint = make_checkpoint(_parser, _state, eval, term);
    00118   // algorithm (need the operator!)
    00119   eoAlgo<EOT>& ga = make_algo_scalar(_parser, _state, eval, checkpoint, op);
    00120
    00123   // to be called AFTER all parameters have been read!!!
    00124   make_help(_parser);
    00125
    00128   cout << "Initial Population\n";
    00129   pop.sortedPrintOn(cout);
    00130   cout << endl;
    00131
    00132   run_ea(ga, pop); // run the ga
    00133
    00134   cout << "Final Population\n";
    00135   pop.sortedPrintOn(cout);
    00136   cout << endl;
    00137 }