main.cpp
00001 /*
00002 The Evolving Distribution Objects framework (EDO) is a template-based,
00003 ANSI-C++ evolutionary computation library which helps you to write your
00004 own estimation of distribution algorithms.
00005 
00006 This library is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Lesser General Public
00008 License as published by the Free Software Foundation; either
00009 version 2.1 of the License, or (at your option) any later version.
00010 
00011 This library is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public
00017 License along with this library; if not, write to the Free Software
00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 
00020 Copyright (C) 2010 Thales group
00021 */
00022 /*
00023 Authors:
00024     Johann Dréo <johann.dreo@thalesgroup.com>
00025     Caner Candan <caner.candan@thalesgroup.com>
00026 */
00027 
00028 #include <eo>
00029 //#include <mo>
00030 
00031 #include <eoEvalFuncCounterBounder.h>
00032 
00033 #include <do/make_pop.h>
00034 #include <do/make_run.h>
00035 #include <do/make_continue.h>
00036 #include <do/make_checkpoint.h>
00037 
00038 #include <edo>
00039 
00040 #include "Rosenbrock.h"
00041 #include "Sphere.h"
00042 
00043 
00044 typedef eoReal<eoMinimizingFitness> RealVec;
00045 typedef edoNormalAdaptive< RealVec > Distrib;
00046 
00047 
00048 int main(int ac, char** av)
00049 {
00050     eoParser parser(ac, av);
00051 
00052     // Letters used by the following declarations:
00053     // a d i p t
00054 
00055     std::string    section("Algorithm parameters");
00056 
00057     eoState state;
00058 
00059     // Instantiate all needed parameters for EDA algorithm
00060     //double selection_rate = parser.createParam((double)0.5, "selection_rate", "Selection Rate", 'R', section).value(); // R
00061 
00062     unsigned long max_eval = parser.getORcreateParam((unsigned long)0, "maxEval", "Maximum number of evaluations (0 = none)", 'E', "Stopping criterion").value(); // E
00063 
00064     unsigned int dim = parser.createParam((unsigned int)10, "dimension-size", "Dimension size", 'd', section).value(); // d
00065 
00066 
00067     double mu = dim / 2;
00068 
00069 
00070     edoNormalAdaptive<RealVec> distribution(dim);
00071 
00072     eoSelect< RealVec >* selector = new eoRankMuSelect< RealVec >( mu );
00073     state.storeFunctor(selector);
00074 
00075     edoEstimator< Distrib >* estimator = new edoEstimatorNormalAdaptive<RealVec>( distribution );
00076     state.storeFunctor(estimator);
00077 
00078     eoEvalFunc< RealVec >* plainEval = new Rosenbrock< RealVec >();
00079     state.storeFunctor(plainEval);
00080 
00081     eoEvalFuncCounterBounder< RealVec > eval(*plainEval, max_eval);
00082 
00083     eoRndGenerator< double >* gen = new eoUniformGenerator< double >(-5, 5);
00084     state.storeFunctor(gen);
00085 
00086 
00087     eoInitFixedLength< RealVec >* init = new eoInitFixedLength< RealVec >( dim, *gen );
00088     state.storeFunctor(init);
00089 
00090 
00091     // (1) Population init and sampler
00092     // Generation of population from do_make_pop (creates parameters, manages persistance and so on...)
00093     // ... and creates the parameters: L P r S
00094     // this first sampler creates a uniform distribution independently from our distribution (it does not use edoUniform).
00095     eoPop< RealVec >& pop = do_make_pop(parser, state, *init);
00096 
00097     // (2) First evaluation before starting the research algorithm
00098     apply(eval, pop);
00099 
00100     // Prepare bounder class to set bounds of sampling.
00101     // This is used by edoSampler.
00102     edoBounder< RealVec >* bounder = 
00103         new edoBounderRng< RealVec >( RealVec(dim, -5), RealVec(dim, 5), *gen); // FIXME do not use hard-coded bounds
00104     state.storeFunctor(bounder);
00105 
00106     // Prepare sampler class with a specific distribution
00107     edoSampler< Distrib >* sampler = new edoSamplerNormalAdaptive< RealVec >( *bounder );
00108     state.storeFunctor(sampler);
00109 
00110     // stopping criteria
00111     // ... and creates the parameter letters: C E g G s T
00112     eoContinue< RealVec >& eo_continue = do_make_continue(parser, state, eval);
00113 
00114     // population output
00115     eoCheckPoint< RealVec >& pop_continue = do_make_checkpoint(parser, state, eval, eo_continue);
00116 
00117     // distribution output
00118     edoDummyContinue< Distrib >* dummy_continue = new edoDummyContinue< Distrib >();
00119     state.storeFunctor(dummy_continue);
00120 
00121     edoCheckPoint< Distrib >* distribution_continue = new edoCheckPoint< Distrib >( *dummy_continue );
00122     state.storeFunctor(distribution_continue);
00123 
00124     // eoEPRemplacement causes the using of the current and previous
00125     // sample for sampling.
00126     eoReplacement< RealVec >* replacor = new eoEPReplacement< RealVec >(pop.size());
00127     state.storeFunctor(replacor);
00128 
00129     // Some stuff to display helper when we are using -h option
00130     if (parser.userNeedsHelp())
00131     {
00132         parser.printHelp(std::cout);
00133         exit(1);
00134     }
00135 
00136     // Help + Verbose routines
00137     make_verbose(parser);
00138     make_help(parser);
00139 
00140     eoPopLoopEval<RealVec> popEval( eval );
00141 
00142     // EDA algorithm configuration
00143     edoAlgo< Distrib >* algo = new edoAlgoAdaptive< Distrib >
00144         (distribution, popEval, *selector, *estimator, *sampler, *replacor,
00145          pop_continue, *distribution_continue );
00146 
00147 
00148     // Beginning of the algorithm call
00149     try {
00150         do_run(*algo, pop);
00151 
00152     } catch (eoEvalFuncCounterBounderException& e) {
00153             eo::log << eo::warnings << "warning: " << e.what() << std::endl;
00154 
00155     } catch (std::exception& e) {
00156         eo::log << eo::errors << "error: " << e.what() << std::endl;
00157         exit(EXIT_FAILURE);
00158     }
00159     return 0;
00160 }
 All Classes Functions Variables Typedefs