# t-eoESAll.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 <ctime>
    00013
    00014 #ifdef _MSC_VER
    00015   #include <crtdbg.h>
    00016 #endif
    00017
    00018 using namespace std;
    00019
    00020 #include <eo>
    00021
    00022 // representation specific
    00023 #include <es/make_es.h>
    00024
    00025 #include "real_value.h"         // the sphere fitness
    00026
    00027 // Now the main
    00029 typedef eoMinimizingFitness  FitT;
    00030
    00031 template <class EOT>
    00032 void runAlgorithm(EOT, eoParser& _parser, eoState& _state);
    00033
    00034 int main_function(int argc, char *argv[])
    00035 {
    00036     // Create the command-line parser
    00037     eoParser parser(argc, argv);  // for user-parameter reading
    00038     eoState state;    // keeps all things allocated
    00039     eoValueParam<bool>& simpleParam = parser.getORcreateParam(true, "Isotropic",
    00040                                                               "Isotropic self-adaptive mutation",
    00041                                                               'i', "ES mutation");
    00042     eoValueParam<bool>& stdevsParam = parser.getORcreateParam(false, "Stdev",
    00043                                                               "One self-adaptive stDev per variable",
    00044                                                               's', "ES mutation");
    00045     eoValueParam<bool>& corrParam = parser.getORcreateParam(false, "Correl",
    00046                                                             "Use correlated mutations",
    00047                                                             'c', "ES mutation");
    00048     // Run the appropriate algorithm
    00049     if (simpleParam.value() == false)
    00050     {
    00051         std::cout << "Using eoReal" << std::endl;
    00052         runAlgorithm(eoReal<FitT>(), parser, state);
    00053     }
    00054     else if (stdevsParam.value() == false)
    00055     {
    00056         std::cout << "Using eoEsSimple" << std::endl;
    00057         runAlgorithm(eoEsSimple<FitT>(), parser, state);
    00058     }
    00059     else if (corrParam.value() == false)
    00060     {
    00061         std::cout << "Using eoEsStdev" << std::endl;
    00062         runAlgorithm(eoEsStdev<FitT>(), parser, state);
    00063     }
    00064     else
    00065     {
    00066         std::cout << "Using eoEsFull" << std::endl;
    00067         runAlgorithm(eoEsFull<FitT>(), parser, state);
    00068     }
    00069     return 0;
    00070 }
    00071
    00072
    00073
    00074 // A main that catches the exceptions
    00075 int main(int argc, char **argv)
    00076 {
    00077 #ifdef _MSC_VER
    00078     // rng.reseed(42);
    00079     int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
    00080     flag |= _CRTDBG_LEAK_CHECK_DF;
    00081     _CrtSetDbgFlag(flag);
    00082     // _CrtSetBreakAlloc(100);
    00083 #endif
    00084     try
    00085     {
    00086         main_function(argc, argv);
    00087     }
    00088     catch(std::exception& e)
    00089     {
    00090         std::cout << "Exception: " << e.what() << '\n';
    00091     }
    00092 }
    00093
    00094
    00095
    00101 template <class EOT>
    00102 void runAlgorithm(EOT, eoParser& _parser, eoState& _state)
    00103 {
    00104     typedef typename EOT::Fitness FitT;
    00105
    00108
    00109     // The evaluation fn - encapsulated into an eval counter for output
    00110     eoEvalFuncPtr<EOT, double, const std::vector<double>&> mainEval( real_value );
    00111     eoEvalFuncCounter<EOT> eval(mainEval);
    00112
    00113     // the genotype - through a genotype initializer
    00114     eoRealInitBounded<EOT>& init = make_genotype(_parser, _state, EOT());
    00115
    00116     // Build the variation operator (any seq/prop construct)
    00117     eoGenOp<EOT>& op = make_op(_parser, _state, init);
    00118
    00121
    00122     // initialize the population - and evaluate
    00123     // yes, this is representation indepedent once you have an eoInit
    00124     eoPop<EOT>& pop = make_pop(_parser, _state, init);
    00125     apply<EOT>(eval, pop);
    00126
    00127     // stopping criteria
    00128     eoContinue<EOT> & term = make_continue(_parser, _state, eval);
    00129     // output
    00130     eoCheckPoint<EOT> & checkpoint = make_checkpoint(_parser, _state, eval, term);
    00131     // algorithm (need the operator!)
    00132     eoAlgo<EOT>& ga = make_algo_scalar(_parser, _state, eval, checkpoint, op);
    00133
    00136     // to be called AFTER all parameters have been read!!!
    00137     make_help(_parser);
    00138
    00141     std::cout << "Initial Population\n";
    00142     pop.sortedPrintOn(std::cout);
    00143     std::cout << std::endl;
    00144
    00145     run_ea(ga, pop); // run the ga
    00146
    00147     std::cout << "Final Population\n";
    00148     pop.sortedPrintOn(std::cout);
    00149     std::cout << std::endl;
    00150 }