From 78cbe4fd0b08d3f0574c33ef541e04464a776b5d Mon Sep 17 00:00:00 2001 From: LPTK Date: Thu, 18 Jul 2013 15:21:13 +0200 Subject: [PATCH] Refactoring and sanitization of the Metropolis-Hastings-related algorithms for more modularity/flexibility --- mo/src/algo/moMetropolisHastings.h | 78 +++++----- mo/src/algo/moSA.h | 128 ++++++++++------ .../explorer/moMetropolisHastingsExplorer.h | 142 +++++++----------- mo/src/explorer/moSAExplorer.h | 108 ++++++------- .../moSimpleMetropolisHastingsExplorer.h | 129 ++++++++++++++++ 5 files changed, 350 insertions(+), 235 deletions(-) create mode 100644 mo/src/explorer/moSimpleMetropolisHastingsExplorer.h diff --git a/mo/src/algo/moMetropolisHastings.h b/mo/src/algo/moMetropolisHastings.h index 5b4292459..dc7ec2f30 100644 --- a/mo/src/algo/moMetropolisHastings.h +++ b/mo/src/algo/moMetropolisHastings.h @@ -2,7 +2,7 @@ Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 -Sebastien Verel, Arnaud Liefooghe, Jeremie Humeau +Sebastien Verel, Arnaud Liefooghe, Jeremie Humeau, Lionel Parreaux This software is governed by the CeCILL license under French law and abiding by the rules of distribution of free software. You can ue, @@ -27,11 +27,12 @@ ParadisEO WebSite : http://paradiseo.gforge.inria.fr Contact: paradiseo-help@lists.gforge.inria.fr */ -#ifndef _moMetropolisHasting_h -#define _moMetropolisHasting_h +#ifndef _moMetropolisHastings_h +#define _moMetropolisHastings_h #include -#include +#include +#include #include #include #include @@ -50,61 +51,50 @@ Contact: paradiseo-help@lists.gforge.inria.fr * the algorithm stops when the number of iterations is too large */ template -class moMetropolisHasting: public moLocalSearch +class moMetropolisHastings: public moLocalSearch { public: typedef typename Neighbor::EOT EOT; typedef moNeighborhood Neighborhood ; - + /** - * Basic constructor of the Metropolis-Hasting - * @param _neighborhood the neighborhood - * @param _fullEval the full evaluation function - * @param _eval neighbor's evaluation function - * @param _nbStep maximum step to do - */ - moMetropolisHasting(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, unsigned int _nbStep): - moLocalSearch(explorer, trueCont, _fullEval), - explorer(_neighborhood, _eval, defaultNeighborComp, defaultSolNeighborComp, _nbStep) - {} - - /** - * Simple constructor of the Metropolis-Hasting + * General constructor of the Metropolis-Hastings algotrithm * @param _neighborhood the neighborhood * @param _fullEval the full evaluation function * @param _eval neighbor's evaluation function * @param _nbStep maximum step to do * @param _cont an external continuator - */ - moMetropolisHasting(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, unsigned int _nbStep, moContinuator& _cont): - moLocalSearch(explorer, _cont, _fullEval), - explorer(_neighborhood, _eval, defaultNeighborComp, defaultSolNeighborComp, _nbStep) - {} - - /** - * General constructor of the Metropolis-Hasting - * @param _neighborhood the neighborhood - * @param _fullEval the full evaluation function - * @param _eval neighbor's evaluation function - * @param _nbStep maximum step to do - * @param _cont an external continuator - * @param _compN a neighbor vs neighbor comparator * @param _compSN a solution vs neighbor comparator */ - moMetropolisHasting(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, unsigned int _nbStep, moContinuator& _cont, moNeighborComparator& _compN, moSolNeighborComparator& _compSN): - moLocalSearch(explorer, _cont, _fullEval), - explorer(_neighborhood, _eval, _compN, _compSN, _nbStep) - {} - + moMetropolisHastings( + Neighborhood& _neighborhood + , eoEvalFunc& _fullEval + , moEval& _eval + , unsigned int _nbStep + , eoOptional< moContinuator > _cont = NULL + , eoOptional< moSolNeighborComparator > _compSN = NULL + ) + : moLocalSearch(explorer, _cont.getOr(trueCont), _fullEval) + , explorer(_neighborhood, _eval, _nbStep, _compSN) + { } + private: + // always true continuator moTrueContinuator trueCont; - // compare the fitness values of neighbors - moNeighborComparator defaultNeighborComp; - // compare the fitness values of the solution and the neighbor - moSolNeighborComparator defaultSolNeighborComp; - // MetropolisHasting explorer - moMetropolisHastingExplorer explorer; + + // Default Metropolis-Hasting explorer + moSimpleMetropolisHastingsExplorer explorer; + }; #endif + + + + + + + + + diff --git a/mo/src/algo/moSA.h b/mo/src/algo/moSA.h index c126f31fc..8bc434233 100644 --- a/mo/src/algo/moSA.h +++ b/mo/src/algo/moSA.h @@ -2,7 +2,7 @@ Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 -Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau +Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau, Lionel Parreaux This software is governed by the CeCILL license under French law and abiding by the rules of distribution of free software. You can ue, @@ -31,12 +31,13 @@ Contact: paradiseo-help@lists.gforge.inria.fr #define _moSA_h #include -#include +#include #include #include #include #include #include +#include /** * Simulated Annealing @@ -60,61 +61,96 @@ public: * @param _span number of iteration with equal temperature for cooling schedule (default = 100) * @param _finalT final temperature, threshold of the stopping criteria for cooling schedule (default = 0.01) */ - moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, double _initT=10, double _alpha=0.9, unsigned _span=100, double _finalT=0.01): - moLocalSearch(explorer, trueCont, _fullEval), - defaultCool(_initT, _alpha, _span, _finalT), - explorer(_neighborhood, _eval, defaultSolNeighborComp, defaultCool) - {} - - /** - * Simple constructor for a simulated annealing - * @param _neighborhood the neighborhood - * @param _fullEval the full evaluation function - * @param _eval neighbor's evaluation function - * @param _cool a cooling schedule - */ - moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, moCoolingSchedule& _cool): - moLocalSearch(explorer, trueCont, _fullEval), - defaultCool(0, 0, 0, 0), - explorer(_neighborhood, _eval, defaultSolNeighborComp, _cool) - {} - + moSA( + Neighborhood& _neighborhood, + eoEvalFunc& _fullEval, + moEval& _eval, + double _initT=10, + double _alpha=0.9, + unsigned _span=100, + double _finalT=0.01 + ) + : moLocalSearch ( + *(explorer_ptr = defaultExplorer = new moSAExplorer(_neighborhood, _eval, *(defaultCool = new moSimpleCoolingSchedule(_initT, _alpha, _span, _finalT)), NULL)), + *(defaultContinuator = new moTrueContinuator()), + _fullEval ), + explorer(*explorer_ptr), + defaultEval(NULL) // removed in C++11 with unique_ptr + { } + /** * General constructor for a simulated annealing * @param _neighborhood the neighborhood * @param _fullEval the full evaluation function - * @param _eval neighbor's evaluation function * @param _cool a cooling schedule + * @param _eval neighbor's evaluation function * @param _cont an external continuator - */ - moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, moCoolingSchedule& _cool, moContinuator& _cont): - moLocalSearch(explorer, _cont, _fullEval), - defaultCool(0, 0, 0, 0), - explorer(_neighborhood, _eval, defaultSolNeighborComp, _cool) - {} - - /** - * General constructor for a simulated annealing - * @param _neighborhood the neighborhood - * @param _fullEval the full evaluation function - * @param _eval neighbor's evaluation function - * @param _cool a cooling schedule * @param _comp a solution vs neighbor comparator + */ + moSA ( + Neighborhood& _neighborhood, + eoEvalFunc& _fullEval, + moCoolingSchedule& _cool, + eoOptional< moEval > _eval = NULL, + eoOptional< moContinuator > _cont = NULL, + eoOptional< moSolNeighborComparator > _comp = NULL + ) + : moLocalSearch ( + *(explorer_ptr = defaultExplorer = new moSAExplorer ( + _neighborhood, + _eval.hasValue()? defaultEval = NULL, _eval.get(): *(defaultEval = new moFullEvalByCopy(_fullEval)), + // C++11: _eval.hasValue()? _eval.get(): *(defaultEval = new moFullEvalByCopy(_fullEval)), + _cool, + _comp )), + // ), + _cont.hasValue()? defaultContinuator = NULL, _cont.get(): *(defaultContinuator = new moTrueContinuator()), + _fullEval ), + explorer(*explorer_ptr), + defaultCool(NULL) // removed in C++11 with unique_ptr + { } + + /** + * More general constructor for a simulated annealing, giving the explorer explicitly + * @param _fullEval the full evaluation function + * @param _explorer the SA explorer * @param _cont an external continuator */ - moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, moCoolingSchedule& _cool, moSolNeighborComparator& _comp, moContinuator& _cont): - moLocalSearch(explorer, _cont, _fullEval), - defaultCool(0, 0, 0, 0), - explorer(_neighborhood, _eval, _comp, _cool) - {} - - + moSA ( + eoEvalFunc& _fullEval, + moSAExplorer& _explorer, + eoOptional< moContinuator > _cont = NULL + ) + : moLocalSearch ( + *(explorer_ptr = &_explorer), + _cont.hasValue()? defaultContinuator = NULL, _cont.get(): *(defaultContinuator = new moTrueContinuator()), _fullEval ), + defaultEval(NULL), // removed in C++11 with unique_ptr + defaultExplorer(NULL), // removed in C++11 with unique_ptr + explorer(*explorer_ptr), + defaultCool(NULL) // removed in C++11 with unique_ptr + { } + + virtual ~moSA () + { + // Note: using unique_ptr would allow us to remove this explicit destructor, but they were only introduced in C++11 + if (defaultContinuator != NULL) + delete defaultContinuator; + if (defaultCool != NULL) + delete defaultCool; + if (defaultExplorer != NULL) + delete defaultExplorer; + if (defaultEval != NULL) + delete defaultEval; + } private: - moTrueContinuator trueCont; - moSimpleCoolingSchedule defaultCool; - moSolNeighborComparator defaultSolNeighborComp; - moSAexplorer explorer; + moFullEvalByCopy* defaultEval; + moSAExplorer* defaultExplorer; + moSAExplorer* explorer_ptr; // Should never be NULL + moSAExplorer& explorer; // Not very useful field (not used yet) + moSimpleCoolingSchedule* defaultCool; // C++11: const std::unique_ptr> defaultCool; + moTrueContinuator* defaultContinuator; }; #endif + + diff --git a/mo/src/explorer/moMetropolisHastingsExplorer.h b/mo/src/explorer/moMetropolisHastingsExplorer.h index 3123c4914..37bcebbdc 100644 --- a/mo/src/explorer/moMetropolisHastingsExplorer.h +++ b/mo/src/explorer/moMetropolisHastingsExplorer.h @@ -1,8 +1,8 @@ /* - + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 - Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau + Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau, Lionel Parreaux This software is governed by the CeCILL license under French law and abiding by the rules of distribution of free software. You can use, @@ -32,25 +32,31 @@ Contact: paradiseo-help@lists.gforge.inria.fr */ -#ifndef _moMetropolisHastingExplorer_h -#define _moMetropolisHastingExplorer_h +#ifndef _moMetropolisHastingsExplorer_h +#define _moMetropolisHastingsExplorer_h #include +#include #include #include #include #include +#include #include /** * Explorer for the Metropolis-Hasting Sampling. * Only the symetric case is considered when Q(x,y) = Q(y,x) * Fitness must be > 0 + * + * Class Derived passed in template parameter should define an "alpha" function + * returning a real between 0 and 1 representing the probability of accepting + * a worse solution */ -template< class Neighbor > -class moMetropolisHastingExplorer : public moNeighborhoodExplorer +template< class Neighbor, class Derived > +class moMetropolisHastingsExplorer : public moNeighborhoodExplorer { public: typedef typename Neighbor::EOT EOT ; @@ -64,116 +70,84 @@ public: * Constructor * @param _neighborhood the neighborhood * @param _eval the evaluation function - * @param _neighborComparator a neighbor comparator - * @param _solNeighborComparator a solution vs neighbor comparator - * @param _nbStep maximum number of step to do + * @param _comp a neighbor comparator */ - moMetropolisHastingExplorer(Neighborhood& _neighborhood, moEval& _eval, moNeighborComparator& _neighborComparator, moSolNeighborComparator& _solNeighborComparator, unsigned int _nbStep) : moNeighborhoodExplorer(_neighborhood, _eval), neighborComparator(_neighborComparator), solNeighborComparator(_solNeighborComparator), nbStep(_nbStep) { - isAccept = false; + moMetropolisHastingsExplorer( + Neighborhood& _neighborhood, + moEval& _eval, + eoOptional< moSolNeighborComparator > _comp = NULL + ) + : moNeighborhoodExplorer(_neighborhood, _eval) + , defaultSolNeighborComp(NULL) + , solNeighborComparator(_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator())) + { if (!neighborhood.isRandom()) { - std::cout << "moMetropolisHastingExplorer::Warning -> the neighborhood used is not random" << std::endl; + eo::log << eo::warnings << "moMetropolisHastingsExplorer::Warning -> the neighborhood used is not random" << std::endl; } } /** * Destructor */ - ~moMetropolisHastingExplorer() { + ~moMetropolisHastingsExplorer() { + if (defaultSolNeighborComp != NULL) + delete defaultSolNeighborComp; } - + /** - * initialization of the number of step to be done - * @param _solution unused solution + * Init Search parameters: Nothing to do + * @param _solution the solution to explore */ - virtual void initParam(EOT & _solution) { - step = 0; - isAccept = true; - }; - + virtual void initParam(EOT & _solution) {}; + /** - * increase the number of step - * @param _solution unused solution - */ - virtual void updateParam(EOT & _solution) { - step++; - }; - - /** - * terminate: NOTHING TO DO - * @param _solution unused solution + * terminate: Nothing to do + * @param _solution the solution to explore */ virtual void terminate(EOT & _solution) {}; - + /** * Explore the neighborhood of a solution * @param _solution */ - virtual void operator()(EOT & _solution) { + virtual void operator()(EOT & _solution) + { //Test if _solution has a Neighbor - if (neighborhood.hasNeighbor(_solution)) { + if (neighborhood.hasNeighbor(_solution)) + { //init the first neighbor neighborhood.init(_solution, selectedNeighbor); - - //eval the _solution moved with the neighbor and stock the result in the neighbor + + //eval the _solution moved with the neighbor and stores the result in the neighbor eval(_solution, selectedNeighbor); } - else { - //if _solution hasn't neighbor, - isAccept=false; - } }; - + /** - * continue if there is a neighbor and it is remainds some steps to do + * accept chooses whether to accept or reject the selected neighbor * @param _solution the solution - * @return true there is some steps to do + * @return true if the selected neighbor is accepted */ - virtual bool isContinue(EOT & _solution) { - return (step < nbStep) ; - }; - - /** - * accept test if an ameliorated neighbor was found - * @param _solution the solution - * @return true if the best neighbor ameliorate the fitness - */ - virtual bool accept(EOT & _solution) { - double alpha=0.0; + virtual bool accept(EOT & _solution) + { + bool isMoveAccepted = false; if (neighborhood.hasNeighbor(_solution)) { - if (solNeighborComparator(_solution, selectedNeighbor)) - isAccept = true; - else { - if (_solution.fitness() != 0) { - if ( (double)selectedNeighbor.fitness() < (double)_solution.fitness()) // maximizing - alpha = (double) selectedNeighbor.fitness() / (double) _solution.fitness(); - else //minimizing - alpha = (double) _solution.fitness() / (double) selectedNeighbor.fitness(); - isAccept = (rng.uniform() < alpha) ; - } - else { - if ( (double) selectedNeighbor.fitness() < (double) _solution.fitness()) // maximizing - isAccept = true; - else - isAccept = false; - } - } + if (solNeighborComparator(_solution, selectedNeighbor)) + // accept if the current neighbor is better than the solution + isMoveAccepted = true; + else isMoveAccepted = rng.uniform() < static_cast(this)->alpha(_solution); } - return isAccept; + return isMoveAccepted; }; - + private: - // comparator betwenn solution and neighbor or between neighbors - moNeighborComparator& neighborComparator; + + // default comparator betwenn solution and neighbor + moSolNeighborComparator* defaultSolNeighborComp; + + // comparator betwenn solution and neighbor moSolNeighborComparator& solNeighborComparator; - - // current number of step - unsigned int step; - - // maximum number of steps to do - unsigned int nbStep; - - // true if the move is accepted - bool isAccept ; + }; diff --git a/mo/src/explorer/moSAExplorer.h b/mo/src/explorer/moSAExplorer.h index 37fa57955..89ae128d0 100644 --- a/mo/src/explorer/moSAExplorer.h +++ b/mo/src/explorer/moSAExplorer.h @@ -1,8 +1,8 @@ /* - + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 - Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau + Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau, Lionel Parreaux This software is governed by the CeCILL license under French law and abiding by the rules of distribution of free software. You can use, @@ -32,15 +32,18 @@ Contact: paradiseo-help@lists.gforge.inria.fr */ -#ifndef _moSAexplorer_h -#define _moSAexplorer_h +#ifndef _moSAExplorer_h +#define _moSAExplorer_h #include -#include +//#include +#include #include #include #include +#include +#include #include @@ -51,35 +54,37 @@ * */ template< class Neighbor > -class moSAexplorer : public moNeighborhoodExplorer +class moSAExplorer : public moMetropolisHastingsExplorer< Neighbor, moSAExplorer > { public: typedef typename Neighbor::EOT EOT ; typedef moNeighborhood Neighborhood ; - + using moNeighborhoodExplorer::neighborhood; using moNeighborhoodExplorer::eval; using moNeighborhoodExplorer::selectedNeighbor; /** - * Constructor + * Constructor for the simple MH explorer * @param _neighborhood the neighborhood * @param _eval the evaluation function + * @param _cool the cooling schedule * @param _solNeighborComparator a solution vs neighbor comparator - * @param _coolingSchedule the cooling schedule */ - moSAexplorer(Neighborhood& _neighborhood, moEval& _eval, moSolNeighborComparator& _solNeighborComparator, moCoolingSchedule& _coolingSchedule) : moNeighborhoodExplorer(_neighborhood, _eval), solNeighborComparator(_solNeighborComparator), coolingSchedule(_coolingSchedule) { - isAccept = false; - - if (!neighborhood.isRandom()) { - std::cout << "moSAexplorer::Warning -> the neighborhood used is not random" << std::endl; - } - } - + moSAExplorer ( + Neighborhood& _neighborhood, + moEval& _eval, + moCoolingSchedule& _cool, + eoOptional< moSolNeighborComparator > _comp = NULL + ) + : moMetropolisHastingsExplorer< Neighbor, moSAExplorer >(_neighborhood, _eval, _comp) + , coolingSchedule(_cool) + { } + /** * Destructor */ - ~moSAexplorer() { + ~moSAExplorer() { } /** @@ -88,7 +93,7 @@ public: */ virtual void initParam(EOT & _solution) { temperature = coolingSchedule.init(_solution); - isAccept = false; + //isMoveAccepted = false; }; /** @@ -100,7 +105,7 @@ public: }; /** - * terminate: NOTHING TO DO + * terminate: Nothing to do * @param _solution unused solution */ virtual void terminate(EOT & _solution) {}; @@ -111,17 +116,14 @@ public: */ virtual void operator()(EOT & _solution) { //Test if _solution has a Neighbor - if (neighborhood.hasNeighbor(_solution)) { + if (neighborhood.hasNeighbor(_solution)) + { //init on the first neighbor: supposed to be random solution in the neighborhood neighborhood.init(_solution, selectedNeighbor); //eval the _solution moved with the neighbor and stock the result in the neighbor eval(_solution, selectedNeighbor); } - else { - //if _solution hasn't neighbor, - isAccept=false; - } }; /** @@ -132,51 +134,35 @@ public: virtual bool isContinue(EOT & _solution) { return coolingSchedule(temperature); }; - + /** - * acceptance criterion according to the boltzmann criterion - * @param _solution the solution - * @return true if better neighbor or rnd < exp(delta f / T) - */ - virtual bool accept(EOT & _solution) { - if (neighborhood.hasNeighbor(_solution)) { - if (solNeighborComparator(_solution, selectedNeighbor)) // accept if the current neighbor is better than the solution - isAccept = true; - else { - double alpha=0.0; - double fit1, fit2; - fit1=(double)selectedNeighbor.fitness(); - fit2=(double)_solution.fitness(); - if (fit1 < fit2) // this is a maximization - alpha = exp((fit1 - fit2) / temperature ); - else // this is a minimization - alpha = exp((fit2 - fit1) / temperature ); - isAccept = (rng.uniform() < alpha) ; - } - } - return isAccept; - }; - - /** - * Getter + * getTemperature getter function * @return the temperature */ - double getTemperature() { + double getTemperature() const { return temperature; } - + + /** + * alpha required by moMetropolisHastingsExplorer, using the Boltzman distribution e^(-delta/T) + * @param _solution the solution + * @return a real between 0 and 1 representing the probability of accepting a worse solution + */ + double alpha(EOT & _solution) { + return exp( - fabs((double) selectedNeighbor.fitness() - (double) _solution.fitness()) / temperature ); + } + + private: - // comparator betwenn solution and neighbor - moSolNeighborComparator& solNeighborComparator; - + + // The cooling schedule moCoolingSchedule& coolingSchedule; - - // temperatur of the process + + // current temperatur of the process double temperature; - - // true if the move is accepted - bool isAccept ; + }; #endif + diff --git a/mo/src/explorer/moSimpleMetropolisHastingsExplorer.h b/mo/src/explorer/moSimpleMetropolisHastingsExplorer.h new file mode 100644 index 000000000..341b4e70c --- /dev/null +++ b/mo/src/explorer/moSimpleMetropolisHastingsExplorer.h @@ -0,0 +1,129 @@ +/* + + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + + Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau, Lionel Parreaux + + This software is governed by the CeCILL license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited liability. + + In this respect, the user's attention is drawn to the risks associated + with loading, using, modifying and/or developing or reproducing the + software by the user in light of its specific status of free software, + that may mean that it is complicated to manipulate, and that also + therefore means that it is reserved for developers and experienced + professionals having in-depth computer knowledge. Users are therefore + encouraged to load and test the software's suitability as regards their + requirements in conditions enabling the security of their systems and/or + data to be ensured and, more generally, to use and operate it in the + same conditions as regards security. + The fact that you are presently reading this means that you have had + knowledge of the CeCILL license and that you accept its terms. + + ParadisEO WebSite : http://paradiseo.gforge.inria.fr + Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#ifndef _moSimpleMetropolisHastingsExplorer_h +#define _moSimpleMetropolisHastingsExplorer_h +/* +#include + +#include +#include +#include +#include + +#include */ +#include + + +/** + * Explorer for the Metropolis-Hasting Sampling. + * Only the symetric case is considered when Q(x,y) = Q(y,x) + * Fitness must be > 0 + */ +template< class Neighbor > +class moSimpleMetropolisHastingsExplorer +: public moMetropolisHastingsExplorer< Neighbor, moSimpleMetropolisHastingsExplorer > +{ +public: + typedef typename Neighbor::EOT EOT ; + typedef moNeighborhood Neighborhood ; + + using moNeighborhoodExplorer::selectedNeighbor; + + /** + * Constructor for the simple MH explorer + * @param _neighborhood the neighborhood + * @param _eval the evaluation function + * @param _maxSteps maximum number of currentStepNb to do + * @param _solNeighborComparator a solution vs neighbor comparator + */ + moSimpleMetropolisHastingsExplorer ( + Neighborhood& _neighborhood, + moEval& _eval, + unsigned int _maxSteps, + eoOptional< moSolNeighborComparator > _comp = NULL + ) + : moMetropolisHastingsExplorer< Neighbor, moSimpleMetropolisHastingsExplorer >(_neighborhood, _eval, _comp) + , maxSteps(_maxSteps) + { } + + /** + * initialization of currentStepNb to be done here + * @param _solution unused + */ + virtual void initParam(EOT & _solution) { + currentStepNb = 0; + //isAccept = true; + }; + + /** + * increment currentStepNb + * @param _solution unused + */ + virtual void updateParam(EOT & _solution) { + currentStepNb++; + }; + + /** + * continue if there is a neighbor and it is remainds some steps to do + * @param _solution the solution + * @return true if there is still some steps to perform + */ + virtual bool isContinue(EOT & _solution) { + return currentStepNb < maxSteps; + }; + + /** + * alpha required by moMetropolisHastingsExplorer + * @param _solution the solution + * @return a real between 0 and 1 representing the probability of accepting a worse solution + */ + double alpha(EOT & _solution) { + if (selectedNeighbor.fitness() == 0) + return selectedNeighbor.fitness() < (double) _solution.fitness() ? 1: 0; + return (double) _solution.fitness() / (double) selectedNeighbor.fitness(); + } + +private: + + // current number of steps + unsigned int currentStepNb; + + // maximum number of steps to perform + unsigned int maxSteps; + +}; + + +#endif