# t-eoCMAES.cpp

    00001
    00002 #include <iostream>
    00003
    00004 #include <eoScalarFitness.h>
    00005 #include <eoVector.h>
    00006 #include <eoPop.h>
    00007 #include <utils/eoParser.h>
    00008 #include <utils/eoCheckPoint.h>
    00009 #include <eoEvalFuncPtr.h>
    00010
    00011 #include <eoGenContinue.h>
    00012 #include <eoFitContinue.h>
    00013 #include <utils/eoStdoutMonitor.h>
    00014 #include <utils/eoStat.h>
    00015 #include <utils/eoTimedMonitor.h>
    00016
    00017 #include <eoMergeReduce.h>
    00018 #include <eoEasyEA.h>
    00019
    00020 #include <es/CMAState.h>
    00021 #include <es/CMAParams.h>
    00022 #include <es/eoCMAInit.h>
    00023 #include <es/eoCMABreed.h>
    00024
    00025 using namespace eo;
    00026 using namespace std;
    00027
    00028 typedef eoMinimizingFitness FitT;
    00029 typedef eoVector<FitT, double> EoType;
    00030
    00031 double sqr(double x) { return x*x; }
    00032
    00033 eoValueParam<int> evals(0,"Function Evals","Number of Evaluations");
    00034
    00035 double f_sphere(const vector<double>& values) {
    00036     double sum = 0.0;
    00037     for (unsigned i = 0; i < values.size(); ++i) {
    00038         sum += values[i] * values[i];
    00039     }
    00040     ++evals.value();
    00041     return sum;
    00042 }
    00043
    00044 double f_rosen(const vector<double>& x) {
    00045     double sum =0.0;
    00046
    00047     for (int i = 0; i < x.size()-1; ++i) {
    00048         sum += 100 * sqr(sqr(x[i])-x[i+1]) + sqr(1.-x[i]);
    00049     }
    00050     ++evals.value();
    00051     return sum;
    00052 }
    00053
    00054
    00055
    00056 int main(int argc, char* argv[]) {
    00057
    00058     // make sure we have a dimensionality parameter (for testing)
    00059     char** rargv = new char*[argc+1];
    00060     rargv[0] = argv[0];
    00061     rargv[1] = "-N10";
    00062     for (int i = 2; i < argc; ++i) {
    00063         rargv[i] = argv[i-1];
    00064     }
    00065
    00066     eoParser parser(argc+1, rargv);
    00067
    00068     CMAParams params(parser);
    00069
    00070     vector<double> initial_point(params.n, 0.0);
    00071
    00072     CMAState state(params, initial_point);
    00073
    00074     if (parser.userNeedsHelp())
    00075     {
    00076         parser.printHelp(std::cout);
    00077         return 1;
    00078     }
    00079
    00080     eoCMAInit<FitT> init(state);
    00081
    00082     eoPop<EoType> pop(params.mu, init);
    00083
    00084     eoEvalFuncPtr<EoType, double, const vector<double>&> eval(  f_rosen );
    00085
    00086     eoCMABreed<FitT> breed(state, params.lambda);
    00087
    00088     for (unsigned i = 0; i < pop.size(); ++i) {
    00089         eval(pop[i]);
    00090     }
    00091
    00092     eoCommaReplacement<EoType> comma;
    00093
    00094     eoGenContinue<EoType> gen(params.maxgen);
    00095     eoFitContinue<EoType> fit(1e-10);
    00096
    00097     eoCheckPoint<EoType> checkpoint(gen);
    00098     checkpoint.add(fit);
    00099
    00100     eoBestFitnessStat<EoType> stat;
    00101
    00102     eoStdoutMonitor mon;
    00103     mon.add(stat);
    00104     mon.add(evals);
    00105
    00106     eoTimedMonitor timed(1);// 1 seconds
    00107     timed.add(mon); // wrap it
    00108
    00109     checkpoint.add(timed);
    00110     checkpoint.add(stat);
    00111
    00112     eoEasyEA<EoType> algo(
    00113             checkpoint,
    00114             eval,
    00115             breed,
    00116             comma);
    00117
    00118
    00119     algo(pop);
    00120     pop.sort();
    00121
    00122     cout << pop[0] << endl;
    00123     cout << "Fitness achieved = " << pop[0].fitness() << endl;
    00124     cout << "Function evaluations = " << evals.value() << endl;
    00125 }
    00126
    00127
    00128