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> EOT;
00045 typedef edoNormalMulti< EOT > 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     eoSelect< EOT >* selector = new eoDetSelect< EOT >( selection_rate );
00063     state.storeFunctor(selector);
00064 
00065     edoEstimator< Distrib >* estimator =    new edoEstimatorNormalMulti< EOT >();
00066     state.storeFunctor(estimator);
00067 
00068     eoEvalFunc< EOT >* plainEval = new Rosenbrock< EOT >();
00069     state.storeFunctor(plainEval);
00070 
00071     unsigned long max_eval = parser.getORcreateParam((unsigned long)0, "maxEval", "Maximum number of evaluations (0 = none)", 'E', "Stopping criterion").value(); // E
00072     eoEvalFuncCounterBounder< EOT > eval(*plainEval, max_eval);
00073 
00074     eoRndGenerator< double >* gen = new eoUniformGenerator< double >(-5, 5);
00075     state.storeFunctor(gen);
00076 
00077     unsigned int dimension_size = parser.createParam((unsigned int)10, "dimension-size", "Dimension size", 'd', section).value(); // d
00078 
00079     eoInitFixedLength< EOT >* init = new eoInitFixedLength< EOT >( dimension_size, *gen );
00080     state.storeFunctor(init);
00081 
00082 
00083     // (1) Population init and sampler
00084     // Generation of population from do_make_pop (creates parameters, manages persistance and so on...)
00085     // ... and creates the parameters: L P r S
00086     // this first sampler creates a uniform distribution independently from our distribution (it does not use edoUniform).
00087     eoPop< EOT >& pop = do_make_pop(parser, state, *init);
00088 
00089     // (2) First evaluation before starting the research algorithm
00090     apply(eval, pop);
00091 
00092     // Prepare bounder class to set bounds of sampling.
00093     // This is used by edoSampler.
00094     edoBounder< EOT >* bounder = 
00095         new edoBounderRng< EOT >( EOT(dimension_size, -5), EOT(dimension_size, 5), *gen); // FIXME do not use hard-coded bounds
00096     state.storeFunctor(bounder);
00097 
00098     // Prepare sampler class with a specific distribution
00099     edoSampler< Distrib >* sampler = new edoSamplerNormalMulti< EOT >( *bounder );
00100     state.storeFunctor(sampler);
00101     
00102     // stopping criteria
00103     // ... and creates the parameter letters: C E g G s T
00104     eoContinue< EOT >& eo_continue = do_make_continue(parser, state, eval);
00105     
00106     // population output
00107     eoCheckPoint< EOT >& pop_continue = do_make_checkpoint(parser, state, eval, eo_continue);
00108     
00109     // distribution output
00110     edoDummyContinue< Distrib >* dummy_continue = new edoDummyContinue< Distrib >();
00111     state.storeFunctor(dummy_continue);
00112 
00113     edoCheckPoint< Distrib >* distribution_continue = new edoCheckPoint< Distrib >( *dummy_continue );
00114     state.storeFunctor(distribution_continue);
00115 
00116     // eoEPRemplacement causes the using of the current and previous
00117     // sample for sampling.
00118     eoReplacement< EOT >* replacor = new eoEPReplacement< EOT >(pop.size());
00119     state.storeFunctor(replacor);
00120 
00121     // Help + Verbose routines
00122     make_verbose(parser);
00123     make_help(parser);
00124 
00125     // Some stuff to display helper when we are using -h option
00126     if (parser.userNeedsHelp())
00127     {
00128         parser.printHelp(std::cout);
00129         exit(1);
00130     }
00131 
00132     // population output (after helper)
00133     //
00134     // FIXME: theses objects are instanciated there in order to avoid a folder
00135     // removing as edoFileSnapshot does within ctor.
00136     edoPopStat< EOT >* popStat = new edoPopStat<EOT>;
00137     state.storeFunctor(popStat);
00138     pop_continue.add(*popStat);
00139 
00140     edoFileSnapshot* fileSnapshot = new edoFileSnapshot("EDA_ResPop");
00141     state.storeFunctor(fileSnapshot);
00142     fileSnapshot->add(*popStat);
00143     pop_continue.add(*fileSnapshot);
00144 
00145     // distribution output (after helper)
00146     edoDistribStat< Distrib >* distrib_stat = new edoStatNormalMulti< EOT >();
00147     state.storeFunctor(distrib_stat);
00148 
00149     distribution_continue->add( *distrib_stat );
00150 
00151     // eoMonitor* stdout_monitor = new eoStdoutMonitor();
00152     // state.storeFunctor(stdout_monitor);
00153     // stdout_monitor->add(*distrib_stat);
00154     // distribution_continue->add( *stdout_monitor );
00155 
00156     eoFileMonitor* file_monitor = new eoFileMonitor("eda_distribution_bounds.txt");
00157     state.storeFunctor(file_monitor);
00158     file_monitor->add(*distrib_stat);
00159     distribution_continue->add( *file_monitor );
00160 
00161     eoPopLoopEval<EOT> popEval( eval );
00162 
00163     // EDA algorithm configuration
00164     edoAlgo< Distrib >* algo = new edoAlgoStateless< Distrib >
00165         (popEval, *selector, *estimator, *sampler, *replacor,
00166          pop_continue, *distribution_continue );
00167 
00168     // Beginning of the algorithm call
00169     try {
00170         do_run(*algo, pop);
00171 
00172     } catch (eoEvalFuncCounterBounderException& e) {
00173             eo::log << eo::warnings << "warning: " << e.what() << std::endl;
00174 
00175     } catch (std::exception& e) {
00176         eo::log << eo::errors << "error: " << e.what() << std::endl;
00177         exit(EXIT_FAILURE);
00178     }
00179     return 0;
00180 }
 All Classes Functions Variables Typedefs