main.cpp
00001 #include <eo>
00002 #include <mo>
00003 
00004 #include <eoEvalFuncCounterBounder.h>
00005 
00006 #include <do/make_pop.h>
00007 #include <do/make_run.h>
00008 #include <do/make_continue.h>
00009 #include <do/make_checkpoint.h>
00010 
00011 #include <edo>
00012 
00013 #include "Rosenbrock.h"
00014 #include "Sphere.h"
00015 
00016 
00017 typedef eoReal<eoMinimizingFitness> EOT;
00018 typedef edoNormalMulti< EOT > Distrib;
00019 
00020 
00021 int main(int ac, char** av)
00022 {
00023     eoParser parser(ac, av);
00024 
00025     // Letters used by the following declarations:
00026     // a d i p t
00027 
00028     std::string section("Algorithm parameters");
00029 
00030     // FIXME: default value to check
00031     double initial_temperature = parser.createParam((double)10e5, "temperature", "Initial temperature", 'i', section).value(); // i
00032 
00033     eoState state;
00034 
00035 
00036     //-----------------------------------------------------------------------------
00037     // Instantiate all needed parameters for EDASA algorithm
00038     //-----------------------------------------------------------------------------
00039 
00040     double selection_rate = parser.createParam((double)0.5, "selection_rate", "Selection Rate", 'R', section).value(); // R
00041 
00042     eoSelect< EOT >* selector = new eoDetSelect< EOT >( selection_rate );
00043     state.storeFunctor(selector);
00044 
00045     edoEstimator< Distrib >* estimator =        new edoEstimatorNormalMulti< EOT >();
00046     state.storeFunctor(estimator);
00047 
00048     eoSelectOne< EOT >* selectone = new eoDetTournamentSelect< EOT >( 2 );
00049     state.storeFunctor(selectone);
00050 
00051     edoModifierMass< Distrib >* modifier = new edoNormalMultiCenter< EOT >();
00052     state.storeFunctor(modifier);
00053 
00054     eoEvalFunc< EOT >* plainEval = new Rosenbrock< EOT >();
00055     state.storeFunctor(plainEval);
00056 
00057     unsigned long max_eval = parser.getORcreateParam((unsigned long)0, "maxEval", "Maximum number of evaluations (0 = none)", 'E', "Stopping criterion").value(); // E
00058     eoEvalFuncCounterBounder< EOT > eval(*plainEval, max_eval);
00059 
00060     eoRndGenerator< double >* gen = new eoUniformGenerator< double >(-5, 5);
00061     state.storeFunctor(gen);
00062 
00063 
00064     unsigned int dimension_size = parser.createParam((unsigned int)10, "dimension-size", "Dimension size", 'd', section).value(); // d
00065 
00066     eoInitFixedLength< EOT >* init = new eoInitFixedLength< EOT >( dimension_size, *gen );
00067     state.storeFunctor(init);
00068 
00069     //-----------------------------------------------------------------------------
00070 
00071 
00072     //-----------------------------------------------------------------------------
00073     // (1) Population init and sampler
00074     //-----------------------------------------------------------------------------
00075 
00076     // Generation of population from do_make_pop (creates parameters, manages persistance and so on...)
00077     // ... and creates the parameters: L P r S
00078 
00079     // this first sampler creates a uniform distribution independently from our distribution (it does not use doUniform).
00080 
00081     eoPop< EOT >& pop = do_make_pop(parser, state, *init);
00082 
00083     //-----------------------------------------------------------------------------
00084 
00085 
00086     //-----------------------------------------------------------------------------
00087     // (2) First evaluation before starting the research algorithm
00088     //-----------------------------------------------------------------------------
00089 
00090     apply(eval, pop);
00091 
00092     //-----------------------------------------------------------------------------
00093 
00094 
00095     //-----------------------------------------------------------------------------
00096     // Prepare bounder class to set bounds of sampling.
00097     // This is used by doSampler.
00098     //-----------------------------------------------------------------------------
00099 
00100     edoBounder< EOT >* bounder = new edoBounderRng< EOT >(EOT(pop[0].size(), -5),
00101                                                         EOT(pop[0].size(), 5),
00102                                                         *gen);
00103     state.storeFunctor(bounder);
00104 
00105     //-----------------------------------------------------------------------------
00106 
00107 
00108     //-----------------------------------------------------------------------------
00109     // Prepare sampler class with a specific distribution
00110     //-----------------------------------------------------------------------------
00111 
00112     edoSampler< Distrib >* sampler = new edoSamplerNormalMulti< EOT >( *bounder );
00113     state.storeFunctor(sampler);
00114 
00115     //-----------------------------------------------------------------------------
00116 
00117 
00118     //-----------------------------------------------------------------------------
00119     // Metropolis sample parameters
00120     //-----------------------------------------------------------------------------
00121 
00122     unsigned int popSize = parser.getORcreateParam((unsigned int)20, "popSize", "Population Size", 'P', "Evolution Engine").value();
00123 
00124     moContinuator< moDummyNeighbor<EOT> >* sa_continue = new moIterContinuator< moDummyNeighbor<EOT> >( popSize );
00125     state.storeFunctor(sa_continue);
00126 
00127     //-----------------------------------------------------------------------------
00128 
00129 
00130     //-----------------------------------------------------------------------------
00131     // SA parameters
00132     //-----------------------------------------------------------------------------
00133 
00134     double threshold_temperature = parser.createParam((double)0.1, "threshold", "Minimal temperature at which stop", 't', section).value(); // t
00135     double alpha = parser.createParam((double)0.1, "alpha", "Temperature decrease rate", 'a', section).value(); // a
00136 
00137     moCoolingSchedule<EOT>* cooling_schedule = new moSimpleCoolingSchedule<EOT>(initial_temperature, alpha, 0, threshold_temperature);
00138     state.storeFunctor(cooling_schedule);
00139 
00140     //-----------------------------------------------------------------------------
00141 
00142 
00143     //-----------------------------------------------------------------------------
00144     // stopping criteria
00145     // ... and creates the parameter letters: C E g G s T
00146     //-----------------------------------------------------------------------------
00147 
00148     eoContinue< EOT >& eo_continue = do_make_continue(parser, state, eval);
00149 
00150     //-----------------------------------------------------------------------------
00151 
00152 
00153     //-----------------------------------------------------------------------------
00154     // population output
00155     //-----------------------------------------------------------------------------
00156 
00157     eoCheckPoint< EOT >& pop_continue = do_make_checkpoint(parser, state, eval, eo_continue);
00158 
00159     //-----------------------------------------------------------------------------
00160 
00161 
00162     //-----------------------------------------------------------------------------
00163     // distribution output
00164     //-----------------------------------------------------------------------------
00165 
00166     edoDummyContinue< Distrib >* dummy_continue = new edoDummyContinue< Distrib >();
00167     state.storeFunctor(dummy_continue);
00168 
00169     edoCheckPoint< Distrib >* distribution_continue = new edoCheckPoint< Distrib >( *dummy_continue );
00170     state.storeFunctor(distribution_continue);
00171 
00172     //-----------------------------------------------------------------------------
00173 
00174 
00175     //-----------------------------------------------------------------------------
00176     // eoEPRemplacement causes the using of the current and previous
00177     // sample for sampling.
00178     //-----------------------------------------------------------------------------
00179 
00180     eoReplacement< EOT >* replacor = new eoEPReplacement< EOT >(pop.size());
00181 
00182     // Below, use eoGenerationalReplacement to sample only on the current sample
00183 
00184     //eoReplacement< EOT >* replacor = new eoGenerationalReplacement< EOT >(); // FIXME: to define the size
00185 
00186     state.storeFunctor(replacor);
00187 
00188     //-----------------------------------------------------------------------------
00189 
00190 
00191     //-----------------------------------------------------------------------------
00192     // Some stuff to display helper when we are using -h option
00193     //-----------------------------------------------------------------------------
00194 
00195     if (parser.userNeedsHelp())
00196         {
00197             parser.printHelp(std::cout);
00198             exit(1);
00199         }
00200 
00201     // Help + Verbose routines
00202 
00203     make_verbose(parser);
00204     make_help(parser);
00205 
00206     //-----------------------------------------------------------------------------
00207 
00208 
00209     //-----------------------------------------------------------------------------
00210     // population output (after helper)
00211     //
00212     // FIXME: theses objects are instanciate there in order to avoid a folder
00213     // removing as edoFileSnapshot does within ctor.
00214     //-----------------------------------------------------------------------------
00215 
00216     edoPopStat< EOT >* popStat = new edoPopStat<EOT>;
00217     state.storeFunctor(popStat);
00218     pop_continue.add(*popStat);
00219 
00220     edoFileSnapshot* fileSnapshot = new edoFileSnapshot("EDASA_ResPop");
00221     state.storeFunctor(fileSnapshot);
00222     fileSnapshot->add(*popStat);
00223     pop_continue.add(*fileSnapshot);
00224 
00225     //-----------------------------------------------------------------------------
00226 
00227 
00228     //-----------------------------------------------------------------------------
00229     // distribution output (after helper)
00230     //-----------------------------------------------------------------------------
00231 
00232     edoDistribStat< Distrib >* distrib_stat = new edoStatNormalMulti< EOT >();
00233     state.storeFunctor(distrib_stat);
00234 
00235     distribution_continue->add( *distrib_stat );
00236 
00237     // eoMonitor* stdout_monitor = new eoStdoutMonitor();
00238     // state.storeFunctor(stdout_monitor);
00239     // stdout_monitor->add(*distrib_stat);
00240     // distribution_continue->add( *stdout_monitor );
00241 
00242     eoFileMonitor* file_monitor = new eoFileMonitor("eda_sa_distribution_bounds.txt");
00243     state.storeFunctor(file_monitor);
00244     file_monitor->add(*distrib_stat);
00245     distribution_continue->add( *file_monitor );
00246 
00247     //-----------------------------------------------------------------------------
00248 
00249 
00250     //-----------------------------------------------------------------------------
00251     // EDASA algorithm configuration
00252     //-----------------------------------------------------------------------------
00253 
00254     edoAlgo< Distrib >* algo = new edoEDASA< Distrib >
00255         (*selector, *estimator, *selectone, *modifier, *sampler,
00256          pop_continue, *distribution_continue,
00257          eval, *sa_continue, *cooling_schedule,
00258          initial_temperature, *replacor);
00259 
00260     //-----------------------------------------------------------------------------
00261 
00262 
00263     //-----------------------------------------------------------------------------
00264     // Beginning of the algorithm call
00265     //-----------------------------------------------------------------------------
00266 
00267     try
00268         {
00269             do_run(*algo, pop);
00270         }
00271     catch (eoEvalFuncCounterBounderException& e)
00272         {
00273             eo::log << eo::warnings << "warning: " << e.what() << std::endl;
00274         }
00275     catch (std::exception& e)
00276         {
00277             eo::log << eo::errors << "error: " << e.what() << std::endl;
00278             exit(EXIT_FAILURE);
00279         }
00280 
00281     //-----------------------------------------------------------------------------
00282 
00283     return 0;
00284 }
 All Classes Functions Variables Typedefs