Refactoring and sanitization of the Metropolis-Hastings-related algorithms for more modularity/flexibility

This commit is contained in:
LPTK 2013-07-18 15:21:13 +02:00
commit 78cbe4fd0b
5 changed files with 348 additions and 233 deletions

View file

@ -2,7 +2,7 @@
<moMetropolisHasting.h>
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 <algo/moLocalSearch.h>
#include <explorer/moMetropolisHastingExplorer.h>
#include <explorer/moMetropolisHastingsExplorer.h>
#include <explorer/moSimpleMetropolisHastingsExplorer.h>
#include <continuator/moTrueContinuator.h>
#include <eval/moEval.h>
#include <eoEvalFunc.h>
@ -50,61 +51,50 @@ Contact: paradiseo-help@lists.gforge.inria.fr
* the algorithm stops when the number of iterations is too large
*/
template<class Neighbor>
class moMetropolisHasting: public moLocalSearch<Neighbor>
class moMetropolisHastings: public moLocalSearch<Neighbor>
{
public:
typedef typename Neighbor::EOT EOT;
typedef moNeighborhood<Neighbor> 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<EOT>& _fullEval, moEval<Neighbor>& _eval, unsigned int _nbStep):
moLocalSearch<Neighbor>(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<EOT>& _fullEval, moEval<Neighbor>& _eval, unsigned int _nbStep, moContinuator<Neighbor>& _cont):
moLocalSearch<Neighbor>(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<EOT>& _fullEval, moEval<Neighbor>& _eval, unsigned int _nbStep, moContinuator<Neighbor>& _cont, moNeighborComparator<Neighbor>& _compN, moSolNeighborComparator<Neighbor>& _compSN):
moLocalSearch<Neighbor>(explorer, _cont, _fullEval),
explorer(_neighborhood, _eval, _compN, _compSN, _nbStep)
{}
moMetropolisHastings(
Neighborhood& _neighborhood
, eoEvalFunc<EOT>& _fullEval
, moEval<Neighbor>& _eval
, unsigned int _nbStep
, eoOptional< moContinuator<Neighbor> > _cont = NULL
, eoOptional< moSolNeighborComparator<Neighbor> > _compSN = NULL
)
: moLocalSearch<Neighbor>(explorer, _cont.getOr(trueCont), _fullEval)
, explorer(_neighborhood, _eval, _nbStep, _compSN)
{ }
private:
// always true continuator
moTrueContinuator<Neighbor> trueCont;
// compare the fitness values of neighbors
moNeighborComparator<Neighbor> defaultNeighborComp;
// compare the fitness values of the solution and the neighbor
moSolNeighborComparator<Neighbor> defaultSolNeighborComp;
// MetropolisHasting explorer
moMetropolisHastingExplorer<Neighbor> explorer;
// Default Metropolis-Hasting explorer
moSimpleMetropolisHastingsExplorer<Neighbor> explorer;
};
#endif

View file

@ -2,7 +2,7 @@
<moSA.h>
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 <algo/moLocalSearch.h>
#include <explorer/moSAexplorer.h>
#include <explorer/moSAExplorer.h>
#include <coolingSchedule/moCoolingSchedule.h>
#include <coolingSchedule/moSimpleCoolingSchedule.h>
#include <continuator/moTrueContinuator.h>
#include <eval/moEval.h>
#include <eoEvalFunc.h>
#include <eoOptional.h>
/**
* 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<EOT>& _fullEval, moEval<Neighbor>& _eval, double _initT=10, double _alpha=0.9, unsigned _span=100, double _finalT=0.01):
moLocalSearch<Neighbor>(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<EOT>& _fullEval, moEval<Neighbor>& _eval, moCoolingSchedule<EOT>& _cool):
moLocalSearch<Neighbor>(explorer, trueCont, _fullEval),
defaultCool(0, 0, 0, 0),
explorer(_neighborhood, _eval, defaultSolNeighborComp, _cool)
{}
moSA(
Neighborhood& _neighborhood,
eoEvalFunc<EOT>& _fullEval,
moEval<Neighbor>& _eval,
double _initT=10,
double _alpha=0.9,
unsigned _span=100,
double _finalT=0.01
)
: moLocalSearch<Neighbor> (
*(explorer_ptr = defaultExplorer = new moSAExplorer<Neighbor>(_neighborhood, _eval, *(defaultCool = new moSimpleCoolingSchedule<EOT>(_initT, _alpha, _span, _finalT)), NULL)),
*(defaultContinuator = new moTrueContinuator<Neighbor>()),
_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<EOT>& _fullEval, moEval<Neighbor>& _eval, moCoolingSchedule<EOT>& _cool, moContinuator<Neighbor>& _cont):
moLocalSearch<Neighbor>(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<EOT>& _fullEval,
moCoolingSchedule<EOT>& _cool,
eoOptional< moEval<Neighbor> > _eval = NULL,
eoOptional< moContinuator<Neighbor> > _cont = NULL,
eoOptional< moSolNeighborComparator<Neighbor> > _comp = NULL
)
: moLocalSearch<Neighbor> (
*(explorer_ptr = defaultExplorer = new moSAExplorer<Neighbor> (
_neighborhood,
_eval.hasValue()? defaultEval = NULL, _eval.get(): *(defaultEval = new moFullEvalByCopy<Neighbor>(_fullEval)),
// C++11: _eval.hasValue()? _eval.get(): *(defaultEval = new moFullEvalByCopy<Neighbor>(_fullEval)),
_cool,
_comp )),
// ),
_cont.hasValue()? defaultContinuator = NULL, _cont.get(): *(defaultContinuator = new moTrueContinuator<Neighbor>()),
_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<EOT>& _fullEval, moEval<Neighbor>& _eval, moCoolingSchedule<EOT>& _cool, moSolNeighborComparator<Neighbor>& _comp, moContinuator<Neighbor>& _cont):
moLocalSearch<Neighbor>(explorer, _cont, _fullEval),
defaultCool(0, 0, 0, 0),
explorer(_neighborhood, _eval, _comp, _cool)
{}
moSA (
eoEvalFunc<EOT>& _fullEval,
moSAExplorer<Neighbor>& _explorer,
eoOptional< moContinuator<Neighbor> > _cont = NULL
)
: moLocalSearch<Neighbor> (
*(explorer_ptr = &_explorer),
_cont.hasValue()? defaultContinuator = NULL, _cont.get(): *(defaultContinuator = new moTrueContinuator<Neighbor>()), _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<Neighbor> trueCont;
moSimpleCoolingSchedule<EOT> defaultCool;
moSolNeighborComparator<Neighbor> defaultSolNeighborComp;
moSAexplorer<Neighbor> explorer;
moFullEvalByCopy<Neighbor>* defaultEval;
moSAExplorer<Neighbor>* defaultExplorer;
moSAExplorer<Neighbor>* explorer_ptr; // Should never be NULL
moSAExplorer<Neighbor>& explorer; // Not very useful field (not used yet)
moSimpleCoolingSchedule<EOT>* defaultCool; // C++11: const std::unique_ptr<moSimpleCoolingSchedule<EOT>> defaultCool;
moTrueContinuator<Neighbor>* defaultContinuator;
};
#endif

View file

@ -1,8 +1,8 @@
/*
<moMetropolisHastingExplorer.h>
<moMetropolisHastingsExplorer.h>
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 <cstdlib>
#include <eoOptional.h>
#include <explorer/moNeighborhoodExplorer.h>
#include <comparator/moNeighborComparator.h>
#include <comparator/moSolNeighborComparator.h>
#include <neighborhood/moNeighborhood.h>
#include <utils/eoLogger.h>
#include <utils/eoRNG.h>
/**
* 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<Neighbor>
template< class Neighbor, class Derived >
class moMetropolisHastingsExplorer : public moNeighborhoodExplorer<Neighbor>
{
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<Neighbor>& _eval, moNeighborComparator<Neighbor>& _neighborComparator, moSolNeighborComparator<Neighbor>& _solNeighborComparator, unsigned int _nbStep) : moNeighborhoodExplorer<Neighbor>(_neighborhood, _eval), neighborComparator(_neighborComparator), solNeighborComparator(_solNeighborComparator), nbStep(_nbStep) {
isAccept = false;
moMetropolisHastingsExplorer(
Neighborhood& _neighborhood,
moEval<Neighbor>& _eval,
eoOptional< moSolNeighborComparator<Neighbor> > _comp = NULL
)
: moNeighborhoodExplorer<Neighbor>(_neighborhood, _eval)
, defaultSolNeighborComp(NULL)
, solNeighborComparator(_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator<Neighbor>()))
{
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<Derived*>(this)->alpha(_solution);
}
return isAccept;
return isMoveAccepted;
};
private:
// comparator betwenn solution and neighbor or between neighbors
moNeighborComparator<Neighbor>& neighborComparator;
// default comparator betwenn solution and neighbor
moSolNeighborComparator<Neighbor>* defaultSolNeighborComp;
// comparator betwenn solution and neighbor
moSolNeighborComparator<Neighbor>& 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 ;
};

View file

@ -1,8 +1,8 @@
/*
<moSAexplorer.h>
<moSAExplorer.h>
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 <cstdlib>
#include <explorer/moNeighborhoodExplorer.h>
//#include <explorer/moNeighborhoodExplorer.h>
#include <explorer/moMetropolisHastingsExplorer.h>
#include <comparator/moSolNeighborComparator.h>
#include <coolingSchedule/moCoolingSchedule.h>
#include <neighborhood/moNeighborhood.h>
#include <eoOptional.h>
#include <eval/moFullEvalByCopy.h>
#include <utils/eoRNG.h>
@ -51,35 +54,37 @@
*
*/
template< class Neighbor >
class moSAexplorer : public moNeighborhoodExplorer<Neighbor>
class moSAExplorer : public moMetropolisHastingsExplorer< Neighbor, moSAExplorer<Neighbor> >
{
public:
typedef typename Neighbor::EOT EOT ;
typedef moNeighborhood<Neighbor> Neighborhood ;
using moNeighborhoodExplorer<Neighbor>::neighborhood;
using moNeighborhoodExplorer<Neighbor>::eval;
using moNeighborhoodExplorer<Neighbor>::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<Neighbor>& _eval, moSolNeighborComparator<Neighbor>& _solNeighborComparator, moCoolingSchedule<EOT>& _coolingSchedule) : moNeighborhoodExplorer<Neighbor>(_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<Neighbor>& _eval,
moCoolingSchedule<EOT>& _cool,
eoOptional< moSolNeighborComparator<Neighbor> > _comp = NULL
)
: moMetropolisHastingsExplorer< Neighbor, moSAExplorer<Neighbor> >(_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<Neighbor>& solNeighborComparator;
// The cooling schedule
moCoolingSchedule<EOT>& coolingSchedule;
// temperatur of the process
// current temperatur of the process
double temperature;
// true if the move is accepted
bool isAccept ;
};
#endif

View file

@ -0,0 +1,129 @@
/*
<moSimpleMetropolisHastingsExplorer.h>
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 <cstdlib>
#include <explorer/moNeighborhoodExplorer.h>
#include <comparator/moNeighborComparator.h>
#include <comparator/moSolNeighborComparator.h>
#include <neighborhood/moNeighborhood.h>
#include <utils/eoRNG.h>*/
#include <explorer/moMetropolisHastingsExplorer.h>
/**
* 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<Neighbor> >
{
public:
typedef typename Neighbor::EOT EOT ;
typedef moNeighborhood<Neighbor> Neighborhood ;
using moNeighborhoodExplorer<Neighbor>::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<Neighbor>& _eval,
unsigned int _maxSteps,
eoOptional< moSolNeighborComparator<Neighbor> > _comp = NULL
)
: moMetropolisHastingsExplorer< Neighbor, moSimpleMetropolisHastingsExplorer<Neighbor> >(_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