# t-eoESFull.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.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, eoRealVectorBounds& _bounds, eoValueParam<string> _load_name);
    00029
    00030 int main_function(int argc, char *argv[])
    00031 {
    00032   // Create the command-line parser
    00033   eoParser parser( argc, argv, "Basic EA for vector<float> with adaptive mutations");
    00034
    00035   // Define Parameters and load them
    00036   eoValueParam<uint32_t>& seed        = parser.createParam(static_cast<uint32_t>(time(0)),
    00037                                                            "seed", "Random number seed");
    00038   eoValueParam<string>& load_name   = parser.createParam(string(), "Load","Load a state file",'L');
    00039   eoValueParam<string>& save_name   = parser.createParam(string(), "Save","Saves a state file",'S');
    00040   eoValueParam<bool>&   stdevs      = parser.createParam(false, "Stdev", "Use adaptive mutation rates", 's');
    00041   eoValueParam<bool>&   corr        = parser.createParam(false, "Correl", "Use correlated mutations", 'c');
    00042   eoValueParam<unsigned>& chromSize = parser.createParam(unsigned(50), "ChromSize", "Number of chromosomes", 'n');
    00043   eoValueParam<double>& minimum     = parser.createParam(-1.0, "Min", "Minimum for Objective Variables", 'l');
    00044   eoValueParam<double>& maximum     = parser.createParam(1.0, "Max", "Maximum for Objective Variables", 'h');
    00045
    00046   eoState state;
    00047     state.registerObject(parser);
    00048     rng.reseed(seed.value());
    00049
    00050    if (!load_name.value().empty())
    00051    { // load the parser. This is only neccessary when the user wants to
    00052      // be able to change the parameters in the state file by hand
    00053      // Note that only parameters inserted in the parser at this point
    00054      // will be loaded!.
    00055        state.load(load_name.value()); // load the parser
    00056    }
    00057
    00058     state.registerObject(rng);
    00059
    00060     eoRealVectorBounds bounds(chromSize.value(), minimum.value(), maximum.value());
    00061
    00062     // Run the appropriate algorithm
    00063     if (stdevs.value() == false && corr.value() == false)
    00064     {
    00065         runAlgorithm(eoEsSimple<FitT>() ,parser, state, bounds, load_name);
    00066     }
    00067     else if (corr.value() == true)
    00068     {
    00069         runAlgorithm(eoEsFull<FitT>(),parser, state, bounds, load_name);
    00070     }
    00071     else
    00072     {
    00073         runAlgorithm(eoEsStdev<FitT>(), parser, state, bounds, load_name);
    00074     }
    00075
    00076     // and save
    00077     if (!save_name.value().empty())
    00078     {
    00079         string file_name = save_name.value();
    00080         save_name.value() = ""; // so that it does not appear in the parser section of the state file
    00081         state.save(file_name);
    00082     }
    00083
    00084         return 0;
    00085 }
    00086
    00087 // A main that catches the exceptions
    00088
    00089 int main(int argc, char **argv)
    00090 {
    00091 #ifdef _MSC_VER
    00092   //  rng.reseed(42);
    00093     int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
    00094      flag |= _CRTDBG_LEAK_CHECK_DF;
    00095     _CrtSetDbgFlag(flag);
    00096 //   _CrtSetBreakAlloc(100);
    00097 #endif
    00098
    00099     try
    00100     {
    00101         main_function(argc, argv);
    00102     }
    00103     catch(std::exception& e)
    00104     {
    00105         std::cout << "Exception: " << e.what() << '\n';
    00106     }
    00107
    00108     return 1;
    00109 }
    00110
    00111 template <class EOT>
    00112 void runAlgorithm(EOT, eoParser& _parser, eoState& _state, eoRealVectorBounds& _bounds, eoValueParam<string> _load_name)
    00113 {
    00114     // evaluation
    00115     eoEvalFuncPtr<EOT, double, const vector<double>&> eval(  real_value );
    00116
    00117     // population parameters, unfortunately these can not be altered in the state file
    00118     eoValueParam<unsigned> mu = _parser.createParam(unsigned(7), "mu","Size of the population");
    00119     eoValueParam<double>lambda_rate = _parser.createParam(double(7.0), "lambda_rate", "Factor of children to produce");
    00120
    00121     if (lambda_rate.value() < 1.0f)
    00122     {
    00123         throw logic_error("lambda_rate must be larger than 1 in a comma strategy");
    00124     }
    00125
    00126     // Initialization
    00127     eoEsChromInit<EOT> init(_bounds);
    00128
    00129     // State takes ownership of pop because it needs to save it in caller
    00130     eoPop<EOT>& pop = _state.takeOwnership(eoPop<EOT>(mu.value(), init));
    00131
    00132     _state.registerObject(pop);
    00133
    00134     if (!_load_name.value().empty())
    00135     { // The real loading happens here when all objects are registered
    00136        _state.load(_load_name.value()); // load all and everything
    00137     }
    00138     else
    00139     {
    00140         // evaluate initial population
    00141         apply<EOT>(eval, pop);
    00142     }
    00143
    00144     // Ok, time to set up the algorithm
    00145     // Proxy for the mutation parameters
    00146     eoEsMutationInit mutateInit(_parser);
    00147
    00148     eoEsMutate<EOT> mutate(mutateInit, _bounds);
    00149
    00150     // monitoring, statistics etc.
    00151     eoAverageStat<EOT> average;
    00152     eoStdoutMonitor monitor;
    00153
    00154     monitor.add(average);
    00155
    00156     eoGenContinue<EOT> cnt(100);
    00157     eoCheckPoint<EOT> checkpoint(cnt);
    00158     checkpoint.add(monitor);
    00159     checkpoint.add(average);
    00160
    00161     // only mutation (== with rate 1.0)
    00162     eoMonGenOp<EOT> op(mutate);
    00163
    00164     // the selection: sequential selection
    00165     eoSequentialSelect<EOT> select;
    00166     // the general breeder (lambda is a rate -> true)
    00167     eoGeneralBreeder<EOT> breed(select, op, lambda_rate.value(), true);
    00168
    00169     // the replacement - hard-coded Comma replacement
    00170     eoCommaReplacement<EOT> replace;
    00171
    00172     // now the eoEasyEA
    00173     eoEasyEA<EOT> es(checkpoint, eval, breed, replace);
    00174
    00175     es(pop);
    00176
    00177     pop.sort();
    00178     std::cout << "Final population\n" << pop << std::endl;
    00179
    00180 }