From 2e23b837e467ce8809f95cfec9e48b14447d1232 Mon Sep 17 00:00:00 2001 From: nojhan Date: Sun, 3 May 2020 14:40:57 +0200 Subject: [PATCH] add mininal cmaes --- edo/test/CMakeLists.txt | 1 + edo/test/t-minimal-cmaes.cpp | 69 ++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 edo/test/t-minimal-cmaes.cpp diff --git a/edo/test/CMakeLists.txt b/edo/test/CMakeLists.txt index 294a8e7f5..691ca5a82 100644 --- a/edo/test/CMakeLists.txt +++ b/edo/test/CMakeLists.txt @@ -42,6 +42,7 @@ set(SOURCES t-repairer-modulo t-binomial t-binomialmulti + t-minimal-cmaes ) foreach(current ${SOURCES}) diff --git a/edo/test/t-minimal-cmaes.cpp b/edo/test/t-minimal-cmaes.cpp new file mode 100644 index 000000000..7bd2933cf --- /dev/null +++ b/edo/test/t-minimal-cmaes.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#include +#include + +using R = eoReal; +using CMA = edoNormalAdaptive; + +R::FitnessType sphere(const R& sol) { + double sum = 0; + for(auto x : sol) { sum += x * x; } + return sum; +} + +int main(int argc, char** argv) { + eoParser parser(argc, argv); + eoState state; + + size_t dim = parser.createParam(10, + "dimension", "Dimension", 'd', + "Problem").value(); + + size_t max_eval = parser.getORcreateParam(100 * dim, + "maxEval", "Maximum number of evaluations", 'E', + "Stopping criterion").value(); + + edoNormalAdaptive gaussian(dim); + + auto& obj_func = state.pack< eoEvalFuncPtr >(sphere); + auto& eval = state.pack< eoEvalCounterThrowException >(obj_func, max_eval); + auto& pop_eval = state.pack< eoPopLoopEval >(eval); + + auto& gen = state.pack< eoUniformGenerator >(-5, 5); + auto& init = state.pack< eoInitFixedLength >(dim, gen); + auto& pop = do_make_pop(parser, state, init); + pop_eval(pop,pop); + + auto& eo_continue = do_make_continue( parser, state, eval); + auto& pop_continue = do_make_checkpoint(parser, state, eval, eo_continue); + auto& best = state.pack< eoBestIndividualStat >(); + pop_continue.add( best ); + auto& distrib_continue = state.pack< edoContAdaptiveFinite >(); + + auto& selector = state.pack< eoRankMuSelect >(dim/2); + auto& estimator = state.pack< edoEstimatorNormalAdaptive >(gaussian); + auto& bounder = state.pack< edoBounderRng >(R(dim, -5), R(dim, 5), gen); + auto& sampler = state.pack< edoSamplerNormalAdaptive >(bounder); + auto& replacor = state.pack< eoCommaReplacement >(); + + make_verbose(parser); + make_help(parser); + + auto& algo = state.pack< edoAlgoAdaptive >( + gaussian , pop_eval, selector, + estimator, sampler , replacor, + pop_continue, distrib_continue); + + try { + algo(pop); + } catch (eoMaxEvalException& e) { + eo::log << eo::progress << "STOP" << std::endl; + } + + std::cout << best.value() << std::endl; + return 0; +}