From 8120116c1b85bb7cad4fb2807ea9455b67367b0a Mon Sep 17 00:00:00 2001 From: Johann Dreo Date: Wed, 18 Jul 2012 10:41:17 +0200 Subject: [PATCH] Factorize algorithms code in a hierarchy of classes CMAES-like algorithm (edoAlgoAdaptive) main loop work on an extern distribution passed as a reference, which is updated at each iteration EDA-like algorithm (edoAlgoStateless) work on an intern distribution, that is replaced at each iteration edoAlgoStateless inherits from edoAlgoAdaptive, and embed a default member on which the work is done. --- edo/application/cmaes/main.cpp | 2 +- edo/application/eda/main.cpp | 2 +- edo/src/edo | 3 +- .../{edoAdaptiveAlgo.h => edoAlgoAdaptive.h} | 83 +----------- edo/src/edoAlgoStateless.h | 126 ++++++++++++++++++ 5 files changed, 136 insertions(+), 80 deletions(-) rename edo/src/{edoAdaptiveAlgo.h => edoAlgoAdaptive.h} (69%) create mode 100644 edo/src/edoAlgoStateless.h diff --git a/edo/application/cmaes/main.cpp b/edo/application/cmaes/main.cpp index b60348548..4da2fdbed 100644 --- a/edo/application/cmaes/main.cpp +++ b/edo/application/cmaes/main.cpp @@ -140,7 +140,7 @@ int main(int ac, char** av) eoPopLoopEval popEval( eval ); // EDA algorithm configuration - edoAlgo< Distrib >* algo = new edoAdaptiveAlgo< Distrib > + edoAlgo< Distrib >* algo = new edoAlgoAdaptive< Distrib > (distribution, popEval, *selector, *estimator, *sampler, *replacor, pop_continue, *distribution_continue ); diff --git a/edo/application/eda/main.cpp b/edo/application/eda/main.cpp index f6ce7b867..6e024545b 100644 --- a/edo/application/eda/main.cpp +++ b/edo/application/eda/main.cpp @@ -161,7 +161,7 @@ int main(int ac, char** av) eoPopLoopEval popEval( eval ); // EDA algorithm configuration - edoAlgo< Distrib >* algo = new edoAdaptiveAlgo< Distrib > + edoAlgo< Distrib >* algo = new edoAlgoStateless< Distrib > (popEval, *selector, *estimator, *sampler, *replacor, pop_continue, *distribution_continue ); diff --git a/edo/src/edo b/edo/src/edo index 70aff6eb0..0da9ed9d4 100644 --- a/edo/src/edo +++ b/edo/src/edo @@ -30,7 +30,8 @@ Authors: #include "edoAlgo.h" //#include "edoEDASA.h" -#include "edoAdaptiveAlgo.h" +#include "edoAlgoAdaptive.h" +#include "edoAlgoStateless.h" #include "edoDistrib.h" #include "edoUniform.h" diff --git a/edo/src/edoAdaptiveAlgo.h b/edo/src/edoAlgoAdaptive.h similarity index 69% rename from edo/src/edoAdaptiveAlgo.h rename to edo/src/edoAlgoAdaptive.h index c77039625..dbb8046fc 100644 --- a/edo/src/edoAdaptiveAlgo.h +++ b/edo/src/edoAlgoAdaptive.h @@ -25,8 +25,8 @@ Authors: Pierre Savéant */ -#ifndef _edoAdaptiveAlgo_h -#define _edoAdaptiveAlgo_h +#ifndef _edoAlgoAdaptive_h +#define _edoAlgoAdaptive_h #include @@ -43,7 +43,7 @@ Authors: /** A generic stochastic search template for algorithms that need a distribution parameter. */ template < typename EOD > -class edoAdaptiveAlgo : public edoAlgo< EOD > +class edoAlgoAdaptive : public edoAlgo< EOD > { public: //! Alias for the type EOT @@ -69,7 +69,7 @@ public: \param pop_continuator Stopping criterion based on the population features \param distribution_continuator Stopping criterion based on the distribution features */ - edoAdaptiveAlgo( + edoAlgoAdaptive( EOD & distrib, eoPopEvalFunc < EOType > & evaluator, eoSelect< EOType > & selector, @@ -79,7 +79,6 @@ public: eoContinue< EOType > & pop_continuator, edoContinue< EOD > & distribution_continuator ) : - _dummy_distrib(), _distrib(distrib), _evaluator(evaluator), _selector(selector), @@ -91,38 +90,6 @@ public: _distribution_continuator(distribution_continuator) {} - /*! - Without a distribution - - \param evaluation Evaluate a population - \param selector Selection of the best candidate solutions in the population - \param estimator Estimation of the distribution parameters - \param sampler Generate feasible solutions using the distribution - \param replacor Replace old solutions by new ones - \param pop_continuator Stopping criterion based on the population features - \param distribution_continuator Stopping criterion based on the distribution features - */ - edoAdaptiveAlgo( - eoPopEvalFunc < EOType > & evaluator, - eoSelect< EOType > & selector, - edoEstimator< EOD > & estimator, - edoSampler< EOD > & sampler, - eoReplacement< EOType > & replacor, - eoContinue< EOType > & pop_continuator, - edoContinue< EOD > & distribution_continuator - ) : - _dummy_distrib(), - _distrib( _dummy_distrib ), - _evaluator(evaluator), - _selector(selector), - _estimator(estimator), - _sampler(sampler), - _replacor(replacor), - _pop_continuator(pop_continuator), - _dummy_continue(), - _distribution_continuator(distribution_continuator) - {} - //! constructor without an edoContinue /*! @@ -136,7 +103,7 @@ public: \param replacor Replace old solutions by new ones \param pop_continuator Stopping criterion based on the population features */ - edoAdaptiveAlgo ( + edoAlgoAdaptive ( EOD & distrib, eoPopEvalFunc < EOType > & evaluator, eoSelect< EOType > & selector, @@ -145,7 +112,6 @@ public: eoReplacement< EOType > & replacor, eoContinue< EOType > & pop_continuator ) : - _dummy_distrib(), _distrib( distrib ), _evaluator(evaluator), _selector(selector), @@ -157,36 +123,6 @@ public: _distribution_continuator( _dummy_continue ) {} - //! constructor without an edoContinue nor a distribution - /*! - \param evaluation Evaluate a population - \param selector Selection of the best candidate solutions in the population - \param estimator Estimation of the distribution parameters - \param sampler Generate feasible solutions using the distribution - \param replacor Replace old solutions by new ones - \param pop_continuator Stopping criterion based on the population features - */ - edoAdaptiveAlgo ( - eoPopEvalFunc < EOType > & evaluator, - eoSelect< EOType > & selector, - edoEstimator< EOD > & estimator, - edoSampler< EOD > & sampler, - eoReplacement< EOType > & replacor, - eoContinue< EOType > & pop_continuator - ) : - _dummy_distrib(), - _distrib( _dummy_distrib ), - _evaluator(evaluator), - _selector(selector), - _estimator(estimator), - _sampler(sampler), - _replacor(replacor), - _pop_continuator(pop_continuator), - _dummy_continue(), - _distribution_continuator( _dummy_continue ) - {} - - /** Call the algorithm * * \param pop the population of candidate solutions @@ -235,13 +171,6 @@ public: protected: - /** A dummy distribution, for algorithms willing to replace it instead of updating - * - * Thus we can instanciate _distrib on this and replace it at the first iteration with an estimator. - * This is why an edoDistrib must have an empty constructor. - */ - EOD _dummy_distrib; - //! The distribution that you want to update EOD & _distrib; @@ -271,5 +200,5 @@ protected: }; -#endif // !_edoAdaptiveAlgo_h +#endif // !_edoAlgoAdaptive_h diff --git a/edo/src/edoAlgoStateless.h b/edo/src/edoAlgoStateless.h new file mode 100644 index 000000000..c2d4834d6 --- /dev/null +++ b/edo/src/edoAlgoStateless.h @@ -0,0 +1,126 @@ +/* +The Evolving Distribution Objects framework (EDO) is a template-based, +ANSI-C++ evolutionary computation library which helps you to write your +own estimation of distribution algorithms. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Copyright (C) 2010 Thales group +*/ +/* +Authors: + Johann Dréo + Pierre Savéant +*/ + +#ifndef _edoAlgoStateless_h +#define _edoAlgoStateless_h + +#include "edoAlgoAdaptive.h" + +//! edoEDA< D > + +/** A generic stochastic search template for algorithms that need a distribution parameter. + */ +template < typename EOD > +class edoAlgoStateless : public edoAlgoAdaptive< EOD > +{ +public: + //! Alias for the type EOT + typedef typename EOD::EOType EOType; + + //! Alias for the atom type + typedef typename EOType::AtomType AtomType; + + //! Alias for the fitness + typedef typename EOType::Fitness Fitness; + +public: + + + /*! + Without a distribution + + \param evaluation Evaluate a population + \param selector Selection of the best candidate solutions in the population + \param estimator Estimation of the distribution parameters + \param sampler Generate feasible solutions using the distribution + \param replacor Replace old solutions by new ones + \param pop_continuator Stopping criterion based on the population features + \param distribution_continuator Stopping criterion based on the distribution features + */ + edoAlgoStateless( + eoPopEvalFunc < EOType > & evaluator, + eoSelect< EOType > & selector, + edoEstimator< EOD > & estimator, + edoSampler< EOD > & sampler, + eoReplacement< EOType > & replacor, + eoContinue< EOType > & pop_continuator, + edoContinue< EOD > & distribution_continuator, + EOD* tmp_distrib = (new EOD()) + ) : + edoAlgoAdaptive( *tmp_distrib, evaluator, selector, estimator, sampler, replacor, pop_continuator, distribution_continuator), + _tmp_distrib( tmp_distrib ), + _dummy_distrib() + { + this->_distrib = _dummy_distrib; + } + + //! constructor without an edoContinue nor a distribution + /*! + \param evaluation Evaluate a population + \param selector Selection of the best candidate solutions in the population + \param estimator Estimation of the distribution parameters + \param sampler Generate feasible solutions using the distribution + \param replacor Replace old solutions by new ones + \param pop_continuator Stopping criterion based on the population features + */ + edoAlgoStateless ( + eoPopEvalFunc < EOType > & evaluator, + eoSelect< EOType > & selector, + edoEstimator< EOD > & estimator, + edoSampler< EOD > & sampler, + eoReplacement< EOType > & replacor, + eoContinue< EOType > & pop_continuator, + EOD* tmp_distrib = (new EOD()) + ) : + edoAlgoAdaptive( *tmp_distrib, evaluator, selector, estimator, sampler, replacor, pop_continuator), + _tmp_distrib( tmp_distrib ), + _dummy_distrib() + { + this->_distrib = _dummy_distrib; + } + + ~edoAlgoStateless() + { + delete _tmp_distrib; + } + + +protected: + + EOD* _tmp_distrib; + + /** A dummy distribution, for algorithms willing to replace it instead of updating + * + * Thus we can instanciate _distrib on this and replace it at the first iteration with an estimator. + * This is why an edoDistrib must have an empty constructor. + */ + EOD _dummy_distrib; + +}; + +#endif // !_edoAlgoStateless_h +