intermediate commit 4

This commit is contained in:
LPTK 2013-07-01 16:36:10 +02:00
commit 8c6610ec67
14 changed files with 149 additions and 607 deletions

View file

@ -50,6 +50,29 @@ public:
eoUpdater& addTo(eoCheckPoint<EOT>& cp) { cp.add(*this); return *this; } eoUpdater& addTo(eoCheckPoint<EOT>& cp) { cp.add(*this); return *this; }
}; };
/**
an eoUpdater that simply calls a function with no arguments
@ingroup Utilities
*/
class eoFunctionCaller : public eoUpdater
{public :
/** Default Ctor - requires a pointer to the function to call */
eoFunctionCaller(void (*_fct)())
: fct(_fct)
{ }
/** Simply increments */
virtual void operator()()
{
(*fct)();
}
virtual std::string className(void) const { return "eoFunctionCaller"; }
private:
void (*fct)();
};
/** /**
an eoUpdater that simply increments a counter an eoUpdater that simply increments a counter

View file

@ -1,110 +0,0 @@
/*
<moMetropolisHasting.h>
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010
Sebastien Verel, Arnaud Liefooghe, Jeremie Humeau
This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software. You can ue,
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".
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 _moMetropolisHasting_h
#define _moMetropolisHasting_h
#include <algo/moLocalSearch.h>
#include <explorer/moMetropolisHastingExplorer.h>
#include <continuator/moTrueContinuator.h>
#include <eval/moEval.h>
#include <eoEvalFunc.h>
/**
* Metropolis-Hasting local search
* Only the symetric case is considered when Q(x,y) = Q(y,x)
* Fitness must be > 0
*
* At each iteration,
* one of the random solution in the neighborhood is selected
* if the selected neighbor have higher or equal fitness than the current solution
* then the solution is replaced by the selected neighbor
* if a random number from [0,1] is lower than fitness(neighbor) / fitness(solution)
* then the solution is replaced by the selected neighbor
* the algorithm stops when the number of iterations is too large
*/
template<class Neighbor>
class moMetropolisHasting: 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
* @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)
{}
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;
};
#endif

View file

@ -2,7 +2,7 @@
<moSA.h> <moSA.h>
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 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 This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software. You can ue, abiding by the rules of distribution of free software. You can ue,
@ -31,7 +31,7 @@ Contact: paradiseo-help@lists.gforge.inria.fr
#define _moSA_h #define _moSA_h
#include <algo/moLocalSearch.h> #include <algo/moLocalSearch.h>
#include <explorer/moSAexplorer.h> #include <explorer/moSAExplorer.h>
#include <coolingSchedule/moCoolingSchedule.h> #include <coolingSchedule/moCoolingSchedule.h>
#include <coolingSchedule/moSimpleCoolingSchedule.h> #include <coolingSchedule/moSimpleCoolingSchedule.h>
#include <continuator/moTrueContinuator.h> #include <continuator/moTrueContinuator.h>
@ -64,16 +64,16 @@ public:
/*moSA(Neighborhood& _neighborhood, eoEvalFunc<EOT>& _fullEval, moEval<Neighbor>& _eval, double _initT=10, double _alpha=0.9, unsigned _span=100, double _finalT=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)
: defaultCool(_initT, _alpha, _span, _finalT), : defaultCool(_initT, _alpha, _span, _finalT),
explorer(_neighborhood, _eval, defaultSolNeighborComp, defaultCool), explorer(_neighborhood, _eval, defaultSolNeighborComp, defaultCool),
moLocalSearch<Neighbor>(explorer, trueCont, _fullEval) moLocalSearch<Neighbor>(explorer, defaultContinuator, _fullEval)
{ }*/ { }*/
moSA(Neighborhood& _neighborhood, eoEvalFunc<EOT>& _fullEval, moEval<Neighbor>& _eval, double _initT=10, double _alpha=0.9, unsigned _span=100, double _finalT=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> ( : moLocalSearch<Neighbor> (
explorer_ptr = defaultExplorer = new moSAexplorer<Neighbor>(_neighborhood, _eval, NULL, *(defaultCool = new moSimpleCoolingSchedule<EOT>(_initT, _alpha, _span, _finalT))), explorer_ptr = defaultExplorer = new moSAExplorer<Neighbor>(_neighborhood, _eval, /*NULL,*/ *(defaultCool = new moSimpleCoolingSchedule<EOT>(_initT, _alpha, _span, _finalT)), NULL),
*(trueCont = new moTrueContinuator<Neighbor>()), *(defaultContinuator = new moTrueContinuator<Neighbor>()),
_fullEval ), _fullEval ),
explorer(*explorer_ptr), explorer(*explorer_ptr),
//defaultCool(), //defaultCool(),
default_eval(NULL) // removed in C++11 with unique_ptr defaultEval(NULL) // removed in C++11 with unique_ptr
//defaultSolNeighborComp(new moSolNeighborComparator<Neighbor>()) //defaultSolNeighborComp(new moSolNeighborComparator<Neighbor>())
//explorer(_neighborhood, _eval, *defaultSolNeighborComp, *defaultCool) //explorer(_neighborhood, _eval, *defaultSolNeighborComp, *defaultCool)
{ } { }
@ -86,7 +86,7 @@ public:
// * @param _cool a cooling schedule // * @param _cool a cooling schedule
// */ // */
// moSA(Neighborhood& _neighborhood, eoEvalFunc<EOT>& _fullEval, moEval<Neighbor>& _eval, moCoolingSchedule<EOT>& _cool): // moSA(Neighborhood& _neighborhood, eoEvalFunc<EOT>& _fullEval, moEval<Neighbor>& _eval, moCoolingSchedule<EOT>& _cool):
// moLocalSearch<Neighbor>(explorer, trueCont, _fullEval), // moLocalSearch<Neighbor>(explorer, defaultContinuator, _fullEval),
// defaultCool(0, 0, 0, 0), // defaultCool(0, 0, 0, 0),
// explorer(_neighborhood, _eval, defaultSolNeighborComp, _cool) // explorer(_neighborhood, _eval, defaultSolNeighborComp, _cool)
// {} // {}
@ -137,56 +137,59 @@ public:
eoOptional< moContinuator<Neighbor> > _cont = NULL, eoOptional< moContinuator<Neighbor> > _cont = NULL,
eoOptional< moSolNeighborComparator<Neighbor> > _comp = NULL eoOptional< moSolNeighborComparator<Neighbor> > _comp = NULL
) )
/*: moLocalSearch<Neighbor>(explorer, _cont.hasValue()? _cont.get(): *(trueCont = new moTrueContinuator<Neighbor>()), _fullEval), /*: moLocalSearch<Neighbor>(explorer, _cont.hasValue()? _cont.get(): *(defaultContinuator = new moTrueContinuator<Neighbor>()), _fullEval),
defaultCool(NULL), // removed in C++11 with unique_ptr defaultCool(NULL), // removed in C++11 with unique_ptr
default_eval(NULL), // removed in C++11 with unique_ptr defaultEval(NULL), // removed in C++11 with unique_ptr
defaultSolNeighborComp(NULL), // removed in C++11 with unique_ptr defaultSolNeighborComp(NULL), // removed in C++11 with unique_ptr
trueCont(NULL), // removed in C++11 with unique_ptr defaultContinuator(NULL), // removed in C++11 with unique_ptr
explorer ( explorer (
_neighborhood, _neighborhood,
_eval.hasValue()? _eval.get(): *(default_eval = new moFullEvalByCopy<Neighbor>(_fullEval)), _eval.hasValue()? _eval.get(): *(defaultEval = new moFullEvalByCopy<Neighbor>(_fullEval)),
// C++11: _eval.hasValue()? _eval.get(): default_eval = new moFullEvalByCopy<Neighbor>(), // C++11: _eval.hasValue()? _eval.get(): defaultEval = new moFullEvalByCopy<Neighbor>(),
_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator<Neighbor>()), _comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator<Neighbor>()),
_cool ) _cool )
{ }*/ { }*/
: moLocalSearch<Neighbor> ( : moLocalSearch<Neighbor> (
explorer_ptr = defaultExplorer = new moSAexplorer<Neighbor> ( explorer_ptr = defaultExplorer = new moSAExplorer<Neighbor> (
_neighborhood, _neighborhood,
//_eval, //_eval.hasValue()? _eval.get(): *(default_eval = new moFullEvalByCopy<Neighbor>(_fullEval)), //_eval, //_eval.hasValue()? _eval.get(): *(defaultEval = new moFullEvalByCopy<Neighbor>(_fullEval)),
_eval.hasValue()? default_eval = NULL, _eval.get(): *(default_eval = new moFullEvalByCopy<Neighbor>(_fullEval)), _eval.hasValue()? defaultEval = NULL, _eval.get(): *(defaultEval = new moFullEvalByCopy<Neighbor>(_fullEval)),
// C++11: _eval.hasValue()? _eval.get(): default_eval = new moFullEvalByCopy<Neighbor>(), // C++11: _eval.hasValue()? _eval.get(): defaultEval = new moFullEvalByCopy<Neighbor>(),
_comp, //_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator<Neighbor>()), //_comp, //_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator<Neighbor>()),
_cool ), //_cool ),
_cont.hasValue()? trueCont = NULL, _cont.get(): *(trueCont = new moTrueContinuator<Neighbor>()), _cool,
_comp ),
// ),
_cont.hasValue()? defaultContinuator = NULL, _cont.get(): *(defaultContinuator = new moTrueContinuator<Neighbor>()),
_fullEval ), _fullEval ),
explorer(*explorer_ptr), explorer(*explorer_ptr),
defaultCool(NULL) // removed in C++11 with unique_ptr defaultCool(NULL) // removed in C++11 with unique_ptr
//default_eval(NULL), // removed in C++11 with unique_ptr //defaultEval(NULL), // removed in C++11 with unique_ptr
//trueCont(NULL) // removed in C++11 with unique_ptr //defaultContinuator(NULL) // removed in C++11 with unique_ptr
//defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr //defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr
{ } { }
moSA ( moSA (
eoEvalFunc<EOT>& _fullEval, eoEvalFunc<EOT>& _fullEval,
moSAexplorer<Neighbor>& _explorer, moSAExplorer<Neighbor>& _explorer,
eoOptional< moContinuator<Neighbor> > _cont = NULL eoOptional< moContinuator<Neighbor> > _cont = NULL
) )
: moLocalSearch<Neighbor> ( : moLocalSearch<Neighbor> (
*(explorer_ptr = &_explorer), *(explorer_ptr = &_explorer),
_cont.hasValue()? _cont.get(): *(trueCont = new moTrueContinuator<Neighbor>()), _fullEval ), _cont.hasValue()? _cont.get(): *(defaultContinuator = new moTrueContinuator<Neighbor>()), _fullEval ),
defaultExplorer(NULL), // removed in C++11 with unique_ptr defaultExplorer(NULL), // removed in C++11 with unique_ptr
explorer(*explorer_ptr), explorer(*explorer_ptr),
defaultCool(NULL), // removed in C++11 with unique_ptr defaultCool(NULL), // removed in C++11 with unique_ptr
//default_eval(NULL), // removed in C++11 with unique_ptr //defaultEval(NULL), // removed in C++11 with unique_ptr
trueCont(NULL) // removed in C++11 with unique_ptr defaultContinuator(NULL) // removed in C++11 with unique_ptr
//defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr //defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr
{ } { }
/* /*
moSA ( moSA (
eoEvalFunc<EOT>& _fullEval, eoEvalFunc<EOT>& _fullEval,
moSAexplorer<Neighbor>& _explorer, moSAExplorer<Neighbor>& _explorer,
moContinuator<Neighbor> _cont moContinuator<Neighbor> _cont
) )
: moLocalSearch<Neighbor> ( : moLocalSearch<Neighbor> (
@ -194,8 +197,8 @@ public:
_cont, _fullEval ), _cont, _fullEval ),
defaultExplorer(NULL), // removed in C++11 with unique_ptr defaultExplorer(NULL), // removed in C++11 with unique_ptr
defaultCool(NULL), // removed in C++11 with unique_ptr defaultCool(NULL), // removed in C++11 with unique_ptr
//default_eval(NULL), // removed in C++11 with unique_ptr //defaultEval(NULL), // removed in C++11 with unique_ptr
trueCont(NULL) // removed in C++11 with unique_ptr defaultContinuator(NULL) // removed in C++11 with unique_ptr
//defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr //defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr
{ } { }
*/ */
@ -207,22 +210,22 @@ public:
virtual ~moSA () virtual ~moSA ()
{ {
// Note: using unique_ptr would allow us to remove this explicit destructor, but they were only introduced in C++11 // Note: using unique_ptr would allow us to remove this explicit destructor, but they were only introduced in C++11
if (trueCont != NULL) if (defaultContinuator != NULL)
delete trueCont; delete defaultContinuator;
if (defaultExplorer != NULL) if (defaultExplorer != NULL)
delete defaultExplorer; delete defaultExplorer;
if (default_eval != NULL) if (defaultEval != NULL)
delete default_eval; delete defaultEval;
} }
private: private:
moFullEvalByCopy<Neighbor>* default_eval; moFullEvalByCopy<Neighbor>* defaultEval;
moSAexplorer<Neighbor>* defaultExplorer; moSAExplorer<Neighbor>* defaultExplorer;
moSAexplorer<Neighbor>* explorer_ptr; // Not NULL moSAExplorer<Neighbor>* explorer_ptr; // Not NULL
moSAexplorer<Neighbor>& explorer; moSAExplorer<Neighbor>& explorer;
moSimpleCoolingSchedule<EOT>* defaultCool; // C++11: const std::unique_ptr<moSimpleCoolingSchedule<EOT>> moSimpleCoolingSchedule<EOT>* defaultCool; // C++11: const std::unique_ptr<moSimpleCoolingSchedule<EOT>>
//moFullEvalByCopy<Neighbor>* default_eval; //moFullEvalByCopy<Neighbor>* defaultEval;
moTrueContinuator<Neighbor>* trueCont; moTrueContinuator<Neighbor>* defaultContinuator;
//moSolNeighborComparator<Neighbor>* defaultSolNeighborComp; //moSolNeighborComparator<Neighbor>* defaultSolNeighborComp;
}; };

View file

@ -39,9 +39,8 @@
#include <utils/eoParam.h> #include <utils/eoParam.h>
/** /**
* Base class for to update what ever you want * Base class for updating whatever you want.
* similar to eoUpdater * Similar to eoUpdater, but there is an "init" method !
* But there is an init method !
*/ */
class moUpdater : public eoF<void> class moUpdater : public eoF<void>
{ {

View file

@ -132,6 +132,7 @@ public:
void update(double& _temp, bool _acceptedMove, EOT & _solution) { void update(double& _temp, bool _acceptedMove, EOT & _solution) {
//cout << _temp << " g " << generated << endl; //cout << _temp << " g " << generated << endl;
prevTemp = _temp;
generated++; generated++;
@ -147,9 +148,12 @@ public:
//cout << _solution.fitness() << " avgCost=" << momentStat.value().first << endl; //cout << _solution.fitness() << " avgCost=" << momentStat.value().first << endl;
} }
markovChainEnded = false;
if (accepted > max_accepted || generated > max_generated) { if (accepted > max_accepted || generated > max_generated) {
markovChainEnded = true;
if (accepted == 0) // ADDED! Otherwise the computed std dev is null; we're probably at equilibrium if (accepted == 0) // ADDED! Otherwise the computed std dev is null; we're probably at equilibrium
{ {
/// ///
@ -302,8 +306,23 @@ public:
&& !terminated ; // ADDED! because 'frozen' doesn't terminate anything && !terminated ; // ADDED! because 'frozen' doesn't terminate anything
} }
//private:
public://FIXME add friend bool markovChainJustEnded()
{
return markovChainEnded;
}
MarkovChainStats& getMarkovChainStats()
{
return markovChainStats;
}
private:
//public://FIXME add friend
//moNeighborhoodStat<Neighbor> nhStat; //moNeighborhoodStat<Neighbor> nhStat;
//moStdFitnessNeighborStat<Neighbor> stdDevStat; //moStdFitnessNeighborStat<Neighbor> stdDevStat;
const double const double
@ -325,7 +344,8 @@ public://FIXME add friend
stdDev, stdDev,
prevAvgCost, prevAvgCost,
expectedDecreaseInCost, // delta expectedDecreaseInCost, // delta
costs_sum costs_sum,
prevTemp
; ;
const int const int
max_accepted, max_accepted,
@ -339,7 +359,7 @@ public://FIXME add friend
negative_temp, negative_temp,
frozen frozen
; ;
bool reinitializing, terminated; bool reinitializing, terminated, markovChainEnded;
//moFitnessVarianceStat<EOT> varStat; //moFitnessVarianceStat<EOT> varStat;
moFitnessMomentsStat<EOT> momentStat; moFitnessMomentsStat<EOT> momentStat;
@ -347,7 +367,48 @@ public://FIXME add friend
ofstream outf; ofstream outf;
protected:
class Monitor {
public:
Monitor(moTrikiCoolingSchedule& _cooling)
: cooling(_cooling)
{ }
/*void setTemperatureOstream(ostream& os)
{
}*/
void getLatestTemperature()
{
return cooling.prevTemp;
}
void printCurrentStatus(ostream& os)
{
if (accepted >= max_accepted || generated >= max_generated)
{
os << "Markov chain finished. Temp was " << getLatestTemperature(); // chain number
}
}
private:
moTrikiCoolingSchedule& cooling;
};
}; };
#endif #endif

View file

@ -1,190 +0,0 @@
/*
<moMetropolisHastingExplorer.h>
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010
Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau
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 _moMetropolisHastingExplorer_h
#define _moMetropolisHastingExplorer_h
#include <cstdlib>
#include <explorer/moNeighborhoodExplorer.h>
#include <comparator/moNeighborComparator.h>
#include <comparator/moSolNeighborComparator.h>
#include <neighborhood/moNeighborhood.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
*/
template< class Neighbor >
class moMetropolisHastingExplorer : public moNeighborhoodExplorer<Neighbor>
{
public:
typedef typename Neighbor::EOT EOT ;
typedef moNeighborhood<Neighbor> Neighborhood ;
using moNeighborhoodExplorer<Neighbor>::neighborhood;
using moNeighborhoodExplorer<Neighbor>::eval;
using moNeighborhoodExplorer<Neighbor>::selectedNeighbor;
/**
* 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
*/
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;
if (!neighborhood.isRandom()) {
std::cout << "moMetropolisHastingExplorer::Warning -> the neighborhood used is not random" << std::endl;
}
}
/**
* Destructor
*/
~moMetropolisHastingExplorer() {
}
/**
* initialization of the number of step to be done
* @param _solution unused solution
*/
virtual void initParam(EOT & _solution) {
step = 0;
isAccept = true;
};
/**
* increase the number of step
* @param _solution unused solution
*/
virtual void updateParam(EOT & _solution) {
step++;
};
/**
* terminate: NOTHING TO DO
* @param _solution unused solution
*/
virtual void terminate(EOT & _solution) {};
/**
* Explore the neighborhood of a solution
* @param _solution
*/
virtual void operator()(EOT & _solution) {
//Test if _solution has a Neighbor
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(_solution, selectedNeighbor);
}
else {
//if _solution hasn't neighbor,
isAccept=false;
}
};
/**
* continue if there is a neighbor and it is remainds some steps to do
* @param _solution the solution
* @return true there is some steps to do
*/
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;
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;
}
}
}
return isAccept;
};
private:
// comparator betwenn solution and neighbor or between neighbors
moNeighborComparator<Neighbor>& neighborComparator;
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 ;
};
#endif

View file

@ -65,14 +65,18 @@ public:
typedef typename Neighbor::EOT EOT; typedef typename Neighbor::EOT EOT;
typedef typename EOT::Fitness Fitness ; typedef typename EOT::Fitness Fitness ;
moNeighborhoodExplorer():neighborhood(dummyNeighborhood), eval(dummyEval), isMoved(false) {} moNeighborhoodExplorer()
: neighborhood(dummyNeighborhood), eval(dummyEval), isMoved(false)
{ }
/** /**
* Constructor with a Neighborhood and evaluation function * Constructor with a Neighborhood and evaluation function
* @param _neighborhood the neighborhood * @param _neighborhood the neighborhood
* @param _eval the evaluation function * @param _eval the evaluation function
*/ */
moNeighborhoodExplorer(Neighborhood& _neighborhood, moEval<Neighbor>& _eval):neighborhood(_neighborhood), eval(_eval), isMoved(false) {} moNeighborhoodExplorer(Neighborhood& _neighborhood, moEval<Neighbor>& _eval)
: neighborhood(_neighborhood), eval(_eval), isMoved(false)
{ }
/** /**
* Init Search parameters * Init Search parameters

View file

@ -43,7 +43,7 @@
#include <algo/moFirstImprHC.h> #include <algo/moFirstImprHC.h>
#include <algo/moILS.h> #include <algo/moILS.h>
#include <algo/moLocalSearch.h> #include <algo/moLocalSearch.h>
#include <algo/moMetropolisHasting.h> #include <algo/moMetropolisHastings.h>
#include <algo/moNeutralHC.h> #include <algo/moNeutralHC.h>
#include <algo/moRandomBestHC.h> #include <algo/moRandomBestHC.h>
#include <algo/moRandomNeutralWalk.h> #include <algo/moRandomNeutralWalk.h>
@ -118,14 +118,15 @@
#include <explorer/moDummyExplorer.h> #include <explorer/moDummyExplorer.h>
#include <explorer/moFirstImprHCexplorer.h> #include <explorer/moFirstImprHCexplorer.h>
#include <explorer/moILSexplorer.h> #include <explorer/moILSexplorer.h>
#include <explorer/moMetropolisHastingExplorer.h> #include <explorer/moMetropolisHastingsExplorer.h>
#include <explorer/moNeighborhoodExplorer.h> #include <explorer/moNeighborhoodExplorer.h>
#include <explorer/moNeutralHCexplorer.h> #include <explorer/moNeutralHCexplorer.h>
#include <explorer/moRandomBestHCexplorer.h> #include <explorer/moRandomBestHCexplorer.h>
#include <explorer/moRandomNeutralWalkExplorer.h> #include <explorer/moRandomNeutralWalkExplorer.h>
#include <explorer/moRandomSearchExplorer.h> #include <explorer/moRandomSearchExplorer.h>
#include <explorer/moRandomWalkExplorer.h> #include <explorer/moRandomWalkExplorer.h>
#include <explorer/moSAexplorer.h> #include <explorer/moSAExplorer.h>
//#include <explorer/moMetropolisExplorer.h>
#include <explorer/moSimpleHCexplorer.h> #include <explorer/moSimpleHCexplorer.h>
#include <explorer/moTSexplorer.h> #include <explorer/moTSexplorer.h>
#include <explorer/moVNSexplorer.h> #include <explorer/moVNSexplorer.h>

View file

@ -39,7 +39,7 @@
#include <neighborhood/moNeighborhood.h> #include <neighborhood/moNeighborhood.h>
#include <eval/moEval.h> #include <eval/moEval.h>
#include <eoEvalFunc.h> #include <eoEvalFunc.h>
#include <algo/moMetropolisHasting.h> #include <algo/moMetropolisHastings.h>
#include <continuator/moNeighborBestStat.h> #include <continuator/moNeighborBestStat.h>
#include <sampling/moFitnessCloudSampling.h> #include <sampling/moFitnessCloudSampling.h>
@ -87,7 +87,7 @@ public:
delete localSearch; delete localSearch;
// Metropolis-Hasting sampling // Metropolis-Hasting sampling
localSearch = new moMetropolisHasting<Neighbor>(_neighborhood, _fullEval, _eval, _nbStep); localSearch = new moMetropolisHastings<Neighbor>(_neighborhood, _fullEval, _eval, _nbStep);
// delete the checkpoint with the wrong continuator // delete the checkpoint with the wrong continuator
delete checkpoint; delete checkpoint;

View file

@ -39,7 +39,7 @@
#include <neighborhood/moNeighborhood.h> #include <neighborhood/moNeighborhood.h>
#include <eval/moEval.h> #include <eval/moEval.h>
#include <eoEvalFunc.h> #include <eoEvalFunc.h>
#include <algo/moMetropolisHasting.h> #include <algo/moMetropolisHastings.h>
#include <continuator/moNeighborFitnessStat.h> #include <continuator/moNeighborFitnessStat.h>
#include <sampling/moFitnessCloudSampling.h> #include <sampling/moFitnessCloudSampling.h>
@ -87,7 +87,7 @@ public:
delete localSearch; delete localSearch;
// Metropolis-Hasting sampling // Metropolis-Hasting sampling
localSearch = new moMetropolisHasting<Neighbor>(_neighborhood, _fullEval, _eval, _nbStep); localSearch = new moMetropolisHastings<Neighbor>(_neighborhood, _fullEval, _eval, _nbStep);
// delete the checkpoint with the wrong continuator // delete the checkpoint with the wrong continuator
delete checkpoint; delete checkpoint;

View file

@ -38,7 +38,7 @@ set (TEST_LIST
t-moNeutralHCexplorer t-moNeutralHCexplorer
t-moFirstImprHCexplorer t-moFirstImprHCexplorer
t-moRandomWalkExplorer t-moRandomWalkExplorer
t-moMetropolisHastingExplorer t-moMetropolisHastingsExplorer
t-moRandomNeutralWalkExplorer t-moRandomNeutralWalkExplorer
t-moTSexplorer t-moTSexplorer
t-moSolComparator t-moSolComparator
@ -52,7 +52,7 @@ set (TEST_LIST
t-moMonOpPerturb t-moMonOpPerturb
t-moRestartPerturb t-moRestartPerturb
t-moNeighborhoodPerturb t-moNeighborhoodPerturb
t-moSAexplorer t-moSAExplorer
t-moSA t-moSA
t-moLocalSearch t-moLocalSearch
t-moILSexplorer t-moILSexplorer
@ -65,7 +65,7 @@ set (TEST_LIST
t-moILS t-moILS
t-moDummyLS t-moDummyLS
t-moRandomSearch t-moRandomSearch
t-moMetropolisHasting t-moMetropolisHastings
t-moNeutralHC t-moNeutralHC
t-moRandomWalk t-moRandomWalk
t-moRandomNeutralWalk t-moRandomNeutralWalk

View file

@ -1,65 +0,0 @@
/*
<t-moMetropolisHasting.cpp>
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010
Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau
This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software. You can ue,
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".
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
*/
#include <iostream>
#include <cstdlib>
#include <cassert>
#include <algo/moMetropolisHasting.h>
#include "moTestClass.h"
#include <eval/oneMaxEval.h>
#include <continuator/moTrueContinuator.h>
#include <comparator/moSolNeighborComparator.h>
#include <comparator/moNeighborComparator.h>
int main() {
std::cout << "[t-moMetropolisHasting] => START" << std::endl;
bitNeighborhood nh(4);
oneMaxEval<bitVector> fullEval;
evalOneMax eval(4);
moTrueContinuator<bitNeighbor> cont;
moSolNeighborComparator<bitNeighbor> sncomp;
moNeighborComparator<bitNeighbor> ncomp;
//test du 1er constructeur
moMetropolisHasting<bitNeighbor> test1(nh, fullEval, eval, 3);
//test du 2eme constructeur
moMetropolisHasting<bitNeighbor> test2(nh, fullEval, eval, 3, cont);
//test du 3eme constructeur
moMetropolisHasting<bitNeighbor> test3(nh, fullEval, eval, 3, cont, ncomp, sncomp);
std::cout << "[t-moMetropolisHasting] => OK" << std::endl;
return EXIT_SUCCESS;
}

View file

@ -1,97 +0,0 @@
/*
<t-moMetropolisHastingExplorer.cpp>
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010
Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau
This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software. You can ue,
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".
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
*/
#include <explorer/moMetropolisHastingExplorer.h>
#include "moTestClass.h"
#include <iostream>
#include <cstdlib>
#include <cassert>
int main() {
std::cout << "[t-moMetropolisHastingExplorer] => START" << std::endl;
//Instanciation
eoBit<eoMinimizingFitness> sol(4, true);
sol.fitness(4);
bitNeighborhood nh(4);
evalOneMax eval(4);
moNeighborComparator<bitNeighbor> ncomp;
moSolNeighborComparator<bitNeighbor> sncomp;
moMetropolisHastingExplorer<bitNeighbor> test(nh, eval, ncomp, sncomp, 3);
//test de l'acceptation d'un voisin améliorant
test.initParam(sol);
test(sol);
assert(test.accept(sol));
test.move(sol);
assert(sol.fitness()==3);
test.updateParam(sol);
assert(test.isContinue(sol));
unsigned int oui=0, non=0;
//test de l'acceptation d'un voisin non améliorant
for (unsigned int i=0; i<1000; i++) {
test(sol);
if (test.accept(sol))
oui++;
else
non++;
}
std::cout << "Attention test en fonction d'une proba \"p\" uniforme dans [0,1] , oui si p < 3/4, non sinon -> resultat sur 1000 essai" << std::endl;
std::cout << "oui: " << oui << std::endl;
std::cout << "non: " << non << std::endl;
assert(oui > 700 && oui < 800); //verification grossiere
//test du critere d'arret
test.updateParam(sol);
assert(test.isContinue(sol));
test.updateParam(sol);
assert(!test.isContinue(sol));
//test de l'acceptation d'un voisin
sol[0]=false;
sol[1]=false;
sol[2]=false;
sol[3]=false;
sol.fitness(0);
test.initParam(sol);
test(sol);
assert(!test.accept(sol));
std::cout << "[t-moMetropolisHastingExplorer] => OK" << std::endl;
return EXIT_SUCCESS;
}

View file

@ -1,87 +0,0 @@
/*
<t-moSAexplorer.cpp>
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010
Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau
This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software. You can ue,
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".
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
*/
#include <iostream>
#include <cstdlib>
#include <cassert>
#include "moTestClass.h"
#include <explorer/moSAexplorer.h>
#include <coolingSchedule/moSimpleCoolingSchedule.h>
int main() {
std::cout << "[t-moSAexplorer] => START" << std::endl;
eoBit<eoMinimizingFitness> sol(4, true);
sol.fitness(4);
bitNeighborhood nh(4);
bitNeighborhood emptyNH(0);
evalOneMax eval(4);
moSolNeighborComparator<bitNeighbor> sncomp;
moSimpleCoolingSchedule<bitVector> cool(10,0.1,2,0.1);
moSAexplorer<bitNeighbor> test1(emptyNH, eval, sncomp, cool);
moSAexplorer<bitNeighbor> test2(nh, eval, sncomp, cool);
//test d'un voisinage vide
test1.initParam(sol);
test1(sol);
assert(!test1.accept(sol));
assert(test1.getTemperature()==10.0);
//test d'un voisinage "normal"
test2.initParam(sol);
test2(sol);
assert(test2.accept(sol));
test2.updateParam(sol);
assert(test2.isContinue(sol));
test2.move(sol);
assert(sol.fitness()==3);
unsigned int ok=0;
unsigned int ko=0;
for (unsigned int i=0; i<1000; i++) {
test2(sol);
if (test2.isContinue(sol))
test2.updateParam(sol);
if (test2.accept(sol))
ok++;
else
ko++;
test2.move(sol);
}
assert((ok>0) && (ko>0));
std::cout << "[t-moSAexplorer] => OK" << std::endl;
return EXIT_SUCCESS;
}