CMA-ES is non-monotonic, thus keep the best individual found so far with a stat; clean the code

This commit is contained in:
Johann Dreo 2012-09-06 16:06:16 +02:00
commit 6b95172600

View file

@ -49,24 +49,13 @@ int main(int ac, char** av)
{ {
eoParser parser(ac, av); eoParser parser(ac, av);
// Letters used by the following declarations:
// a d i p t
std::string section("Algorithm parameters");
eoState state; eoState state;
// Letters used by the following declarations:
// Instantiate all needed parameters for EDA algorithm
//double selection_rate = parser.createParam((double)0.5, "selection_rate", "Selection Rate", 'R', section).value(); // R
unsigned long max_eval = parser.getORcreateParam((unsigned long)0, "maxEval", "Maximum number of evaluations (0 = none)", 'E', "Stopping criterion").value(); // E unsigned long max_eval = parser.getORcreateParam((unsigned long)0, "maxEval", "Maximum number of evaluations (0 = none)", 'E', "Stopping criterion").value(); // E
unsigned int dim = parser.createParam((unsigned int)10, "dimension-size", "Dimension size", 'd', "Problem").value(); // d
unsigned int dim = parser.createParam((unsigned int)10, "dimension-size", "Dimension size", 'd', section).value(); // d
double mu = dim / 2; double mu = dim / 2;
edoNormalAdaptive<RealVec> distribution(dim); edoNormalAdaptive<RealVec> distribution(dim);
eoSelect< RealVec >* selector = new eoRankMuSelect< RealVec >( mu ); eoSelect< RealVec >* selector = new eoRankMuSelect< RealVec >( mu );
@ -113,6 +102,11 @@ int main(int ac, char** av)
// population output // population output
eoCheckPoint< RealVec >& pop_continue = do_make_checkpoint(parser, state, eval, eo_continue); eoCheckPoint< RealVec >& pop_continue = do_make_checkpoint(parser, state, eval, eo_continue);
// keep the best solution found so far in an eoStat
// thus, if the population's best individual fitness decreases during the search, we could
// still keep the best found since the beginning, while avoiding the bias of elitism on the sample
eoBestIndividualStat<RealVec> best_indiv;
pop_continue.add( best_indiv );
// distribution output // distribution output
edoDummyContinue< Distrib >* dummy_continue = new edoDummyContinue< Distrib >(); edoDummyContinue< Distrib >* dummy_continue = new edoDummyContinue< Distrib >();
@ -121,11 +115,13 @@ int main(int ac, char** av)
edoCheckPoint< Distrib >* distribution_continue = new edoCheckPoint< Distrib >( *dummy_continue ); edoCheckPoint< Distrib >* distribution_continue = new edoCheckPoint< Distrib >( *dummy_continue );
state.storeFunctor(distribution_continue); state.storeFunctor(distribution_continue);
// eoEPRemplacement causes the using of the current and previous eoReplacement< RealVec >* replacor = new eoCommaReplacement< RealVec >();
// sample for sampling.
eoReplacement< RealVec >* replacor = new eoEPReplacement< RealVec >(pop.size());
state.storeFunctor(replacor); state.storeFunctor(replacor);
// Help + Verbose routines
make_verbose(parser);
make_help(parser);
// Some stuff to display helper when we are using -h option // Some stuff to display helper when we are using -h option
if (parser.userNeedsHelp()) if (parser.userNeedsHelp())
{ {
@ -133,28 +129,28 @@ int main(int ac, char** av)
exit(1); exit(1);
} }
// Help + Verbose routines
make_verbose(parser);
make_help(parser);
eoPopLoopEval<RealVec> popEval( eval ); eoPopLoopEval<RealVec> popEval( eval );
// EDA algorithm configuration // CMA-ES algorithm configuration
edoAlgo< Distrib >* algo = new edoAlgoAdaptive< Distrib > edoAlgo< Distrib >* algo = new edoAlgoAdaptive< Distrib >
(distribution, popEval, *selector, *estimator, *sampler, *replacor, (distribution, popEval, *selector, *estimator, *sampler, *replacor,
pop_continue, *distribution_continue ); pop_continue, *distribution_continue );
// Use the best solution of the random first pop to start the search
// That is, center the distribution's mean on it.
distribution.mean( pop.best_element() );
// Beginning of the algorithm call // Beginning of the algorithm call
try { try {
eo::log << eo::progress << "Best solution after random init: " << pop.best_element().fitness() << std::endl;
do_run(*algo, pop); do_run(*algo, pop);
} catch (eoEvalFuncCounterBounderException& e) { } catch (eoEvalFuncCounterBounderException& e) {
eo::log << eo::warnings << "warning: " << e.what() << std::endl; eo::log << eo::warnings << "warning: " << e.what() << std::endl;
} catch (std::exception& e) {
eo::log << eo::errors << "error: " << e.what() << std::endl;
exit(EXIT_FAILURE);
} }
// use the stat instead of the pop, to get the best solution of the whole search
std::cout << best_indiv.value() << std::endl;
return 0; return 0;
} }