intermediate commit 6'
This commit is contained in:
parent
483f815610
commit
9d0b83022d
10 changed files with 1306 additions and 0 deletions
90
eo/src/utils/eoGetterUpdater.h
Normal file
90
eo/src/utils/eoGetterUpdater.h
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
/*
|
||||||
|
|
||||||
|
(c) Thales group, 2010
|
||||||
|
|
||||||
|
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;
|
||||||
|
version 2 of the License.
|
||||||
|
|
||||||
|
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
Contact: http://eodev.sourceforge.net
|
||||||
|
|
||||||
|
Authors:
|
||||||
|
Lionel Parreaux <lionel.parreaux@gmail.com>
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _eoGetterUpdater_h
|
||||||
|
#define _eoGetterUpdater_h
|
||||||
|
|
||||||
|
#include <utils/eoUpdater.h>
|
||||||
|
|
||||||
|
template <class EOT> class eoCheckPoint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
eoGetterUpdater is an eoUpdater
|
||||||
|
TODO
|
||||||
|
|
||||||
|
@ingroup Utilities
|
||||||
|
*/
|
||||||
|
template <class T, class V = double>
|
||||||
|
class eoGetterUpdater : public eoUpdater, public eoValueParam<V>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using eoValueParam<V>::value;
|
||||||
|
|
||||||
|
virtual std::string className(void) const { return "eoGetterUpdater"; }
|
||||||
|
|
||||||
|
typedef V (T::*MethodType)();
|
||||||
|
|
||||||
|
// Overload to accept const getter methods; safely casts them to non-const
|
||||||
|
eoGetterUpdater(T& _instance, V (T::*_method)() const)
|
||||||
|
//eoGetterUpdater(T& _instance, V (T::*_method)()=&T::value)
|
||||||
|
//: instance(_instance), method(static_cast<V (T::*_method)()>(_method))
|
||||||
|
//: instance(_instance), method(const_cast<MethodType>(_method))
|
||||||
|
: instance(_instance), method((MethodType)_method)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
//eoGetterUpdater(T& _instance, V (T::*_method)())
|
||||||
|
eoGetterUpdater(T& _instance, MethodType _method)
|
||||||
|
//eoGetterUpdater(T& _instance, V (T::*_method)()=&T::value)
|
||||||
|
: instance(_instance), method(_method)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
virtual void operator()()
|
||||||
|
{
|
||||||
|
value() = (instance.*method)();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
T& instance;
|
||||||
|
V (T::*method)();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
112
mo/src/algo/moMetropolisHastings.h
Normal file
112
mo/src/algo/moMetropolisHastings.h
Normal file
|
|
@ -0,0 +1,112 @@
|
||||||
|
/*
|
||||||
|
<moMetropolisHasting.h>
|
||||||
|
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010
|
||||||
|
|
||||||
|
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,
|
||||||
|
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 _moMetropolisHastings_h
|
||||||
|
#define _moMetropolisHastings_h
|
||||||
|
|
||||||
|
#include <algo/moLocalSearch.h>
|
||||||
|
#include <explorer/moMetropolisHastingsExplorer.h>
|
||||||
|
#include <explorer/moSimpleMetropolisHastingsExplorer.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 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
|
||||||
|
*/
|
||||||
|
moMetropolisHastings(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
|
||||||
|
*/
|
||||||
|
moMetropolisHastings(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
|
||||||
|
*/
|
||||||
|
moMetropolisHastings(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
|
||||||
|
//moMetropolisHastingsExplorer<Neighbor> explorer;
|
||||||
|
moSimpleMetropolisHastingsExplorer<Neighbor> explorer;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
64
mo/src/continuator/moFunctionContinuator.h
Normal file
64
mo/src/continuator/moFunctionContinuator.h
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
/*
|
||||||
|
<moFunctionContinuator.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 _moFunctionContinuator_h
|
||||||
|
#define _moFunctionContinuator_h
|
||||||
|
|
||||||
|
#include <eoFunctor.h>
|
||||||
|
#include <neighborhood/moNeighborhood.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To make specific continuator from a solution
|
||||||
|
*/
|
||||||
|
template< class Neighbor >
|
||||||
|
class moFunctionContinuator : public moContinuator<Neighbor>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
typedef typename Neighbor::EOT EOT ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Init Continuator parameters
|
||||||
|
* @param _solution the related solution
|
||||||
|
*/
|
||||||
|
virtual void init(EOT& _solution) {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Last Call to terminate the checkpoint
|
||||||
|
* @param _solution the related solution
|
||||||
|
*/
|
||||||
|
virtual void lastCall(EOT& _solution) {};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
114
mo/src/coolingSchedule/moHuangCoolingSchedule.h
Normal file
114
mo/src/coolingSchedule/moHuangCoolingSchedule.h
Normal file
|
|
@ -0,0 +1,114 @@
|
||||||
|
/*
|
||||||
|
|
||||||
|
(c) Thales group, 2010
|
||||||
|
|
||||||
|
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;
|
||||||
|
version 2 of the License.
|
||||||
|
|
||||||
|
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
Contact: http://eodev.sourceforge.net
|
||||||
|
|
||||||
|
Authors:
|
||||||
|
Lionel Parreaux <lionel.parreaux@gmail.com>
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _moHuangCoolingSchedule_h
|
||||||
|
#define _moHuangCoolingSchedule_h
|
||||||
|
|
||||||
|
#include <coolingSchedule/moCoolingSchedule.h>
|
||||||
|
|
||||||
|
#include <continuator/moNeighborhoodStat.h>
|
||||||
|
#include <continuator/moStdFitnessNeighborStat.h>
|
||||||
|
#include <neighborhood/moNeighborhood.h>
|
||||||
|
#include <continuator/moStat.h>
|
||||||
|
#include <continuator/moFitnessMomentsStat.h>
|
||||||
|
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
//!
|
||||||
|
/*!
|
||||||
|
*/
|
||||||
|
template< class EOT >
|
||||||
|
class moHuangCoolingSchedule: public moCoolingSchedule< EOT >
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
//typedef typename Neighbor::EOT EOT ;
|
||||||
|
//typedef moNeighborhood<Neighbor> Neighborhood ;
|
||||||
|
|
||||||
|
//! Constructor
|
||||||
|
/*!
|
||||||
|
*/
|
||||||
|
|
||||||
|
moHuangCoolingSchedule (double _initTemp, double _stdDevEstimation, double _lambda = .7, double _finalTemp = .01)
|
||||||
|
: initTemp(_initTemp),
|
||||||
|
stdDevEstimation(_stdDevEstimation),
|
||||||
|
lambda(_lambda),
|
||||||
|
finalTemp(_finalTemp)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initial temperature
|
||||||
|
* @param _solution initial solution
|
||||||
|
*/
|
||||||
|
double init(EOT & _solution) {
|
||||||
|
|
||||||
|
return initTemp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* update the temperature by a factor
|
||||||
|
* @param _temp current temperature to update
|
||||||
|
* @param _acceptedMove true when the move is accepted, false otherwise
|
||||||
|
*/
|
||||||
|
void update(double& _temp, bool _acceptedMove, EOT & _solution) {
|
||||||
|
|
||||||
|
_temp *= exp ( -lambda*_temp / stdDevEstimation ) ;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Function which proceeds to the cooling
|
||||||
|
/*!
|
||||||
|
*/
|
||||||
|
bool operator() (double temperature)
|
||||||
|
{
|
||||||
|
return temperature > finalTemp;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
const double
|
||||||
|
// parameters of the algorithm
|
||||||
|
initTemp,
|
||||||
|
stdDevEstimation,
|
||||||
|
lambda,
|
||||||
|
finalTemp
|
||||||
|
;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
203
mo/src/explorer/moMetropolisHastingsExplorer.h
Normal file
203
mo/src/explorer/moMetropolisHastingsExplorer.h
Normal file
|
|
@ -0,0 +1,203 @@
|
||||||
|
/*
|
||||||
|
<moMetropolisHastingsExplorer.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 _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
|
||||||
|
*/
|
||||||
|
template< class Neighbor, class Derived /*TODO reqs on Derived*/>
|
||||||
|
class moMetropolisHastingsExplorer : 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
|
||||||
|
*/
|
||||||
|
moMetropolisHastingsExplorer(
|
||||||
|
Neighborhood& _neighborhood,
|
||||||
|
moEval<Neighbor>& _eval,
|
||||||
|
//moNeighborComparator<Neighbor>& _neighborComparator,
|
||||||
|
//moSolNeighborComparator<Neighbor>& _solNeighborComparator
|
||||||
|
eoOptional< moSolNeighborComparator<Neighbor> > _comp = NULL
|
||||||
|
)
|
||||||
|
: moNeighborhoodExplorer<Neighbor>(_neighborhood, _eval),
|
||||||
|
//neighborComparator(_neighborComparator),
|
||||||
|
//solNeighborComparator(_solNeighborComparator),
|
||||||
|
defaultSolNeighborComp(NULL),
|
||||||
|
solNeighborComparator(_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator<Neighbor>()))
|
||||||
|
{
|
||||||
|
//isMoveAccepted = false;
|
||||||
|
if (!neighborhood.isRandom()) {
|
||||||
|
//std::cout << "moMetropolisHastingsExplorer::Warning -> the neighborhood used is not random" << std::endl;
|
||||||
|
eo::log << eo::warnings << "moMetropolisHastingsExplorer::Warning -> the neighborhood used is not random" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destructor
|
||||||
|
*/
|
||||||
|
~moMetropolisHastingsExplorer() {
|
||||||
|
if (defaultSolNeighborComp != NULL)
|
||||||
|
delete defaultSolNeighborComp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * initialization of the number of step to be done
|
||||||
|
// * @param _solution unused solution
|
||||||
|
// */
|
||||||
|
// virtual void initParam(EOT & _solution) {
|
||||||
|
// step = 0;
|
||||||
|
// isMoveAccepted = true;
|
||||||
|
// };
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * terminate: NOTHING TO DO
|
||||||
|
// * @param _solution unused solution
|
||||||
|
// */
|
||||||
|
// virtual void terminate(EOT & _solution) {};
|
||||||
|
|
||||||
|
virtual void initParam(EOT & _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 stores the result in the neighbor
|
||||||
|
eval(_solution, selectedNeighbor);
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
else {
|
||||||
|
//if _solution hasn't neighbor,
|
||||||
|
isMoveAccepted = false;
|
||||||
|
}*/
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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))
|
||||||
|
isMoveAccepted = 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();
|
||||||
|
isMoveAccepted = (rng.uniform() < alpha) ;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if ( (double) selectedNeighbor.fitness() < (double) _solution.fitness()) // maximizing
|
||||||
|
isMoveAccepted = true;
|
||||||
|
else
|
||||||
|
isMoveAccepted = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
/*bool accepted = false;
|
||||||
|
if (neighborhood.hasNeighbor(_solution)) {
|
||||||
|
if (solNeighborComparator(_solution, selectedNeighbor)) // accept if the current neighbor is better than the solution
|
||||||
|
accepted = true;
|
||||||
|
//else accepted = static_cast<Derived*>(this)->doAccept(_solution);
|
||||||
|
else accepted = rng.uniform() <= static_cast<Derived*>(this)->getAlpha(_solution);
|
||||||
|
}
|
||||||
|
return isMoveAccepted = accepted;*/
|
||||||
|
bool isMoveAccepted = false;
|
||||||
|
if (neighborhood.hasNeighbor(_solution)) {
|
||||||
|
if (solNeighborComparator(_solution, selectedNeighbor)) // accept if the current neighbor is better than the solution
|
||||||
|
isMoveAccepted = true;
|
||||||
|
//else accepted = static_cast<Derived*>(this)->doAccept(_solution);
|
||||||
|
else isMoveAccepted = rng.uniform() < static_cast<Derived*>(this)->alpha(_solution);
|
||||||
|
}
|
||||||
|
return isMoveAccepted;
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
moSolNeighborComparator<Neighbor>* defaultSolNeighborComp;
|
||||||
|
|
||||||
|
// 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 isMoveAccepted ;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
243
mo/src/explorer/moSAExplorer.h
Normal file
243
mo/src/explorer/moSAExplorer.h
Normal file
|
|
@ -0,0 +1,243 @@
|
||||||
|
/*
|
||||||
|
<moSAExplorer.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 _moSAExplorer_h
|
||||||
|
#define _moSAExplorer_h
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
//#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>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Explorer for the Simulated Annealing
|
||||||
|
* Only the symetric case is considered when Q(x,y) = Q(y,x)
|
||||||
|
* Fitness must be > 0
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
template< class 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;
|
||||||
|
|
||||||
|
|
||||||
|
/*moSAExplorer(Neighborhood& _neighborhood, moEval<Neighbor>& _eval)
|
||||||
|
: moNeighborhoodExplorer<Neighbor>(_neighborhood, _eval)
|
||||||
|
{ }*/
|
||||||
|
|
||||||
|
moSAExplorer (
|
||||||
|
Neighborhood& _neighborhood,
|
||||||
|
//eoOptional< moEval<Neighbor> > _eval = NULL,
|
||||||
|
moEval<Neighbor>& _eval,
|
||||||
|
moCoolingSchedule<EOT>& _cool,
|
||||||
|
eoOptional< moSolNeighborComparator<Neighbor> > _comp = NULL
|
||||||
|
)
|
||||||
|
: moMetropolisHastingsExplorer< Neighbor, moSAExplorer<Neighbor> >(_neighborhood, _eval, _comp),
|
||||||
|
/*moNeighborhoodExplorer<Neighbor>(_neighborhood, _eval.hasValue()? _eval.get(): *(default_eval = new moFullEvalByCopy<Neighbor>(_fullEval))),
|
||||||
|
default_eval(NULL), // removed in C++11 with unique_ptr*/
|
||||||
|
//defaultSolNeighborComp(NULL), // removed in C++11 with unique_ptr
|
||||||
|
//solNeighborComparator(_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator<Neighbor>())),
|
||||||
|
//coolingSchedule(_coolingSchedule)
|
||||||
|
coolingSchedule(_cool)
|
||||||
|
{
|
||||||
|
/*isMoveAccepted = false;
|
||||||
|
if (!neighborhood.isRandom()) {
|
||||||
|
std::cout << "moSAexplorer::Warning -> the neighborhood used is not random" << std::endl;
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destructor
|
||||||
|
*/
|
||||||
|
~moSAExplorer() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* initialization of the initial temperature
|
||||||
|
* @param _solution the solution
|
||||||
|
*/
|
||||||
|
virtual void initParam(EOT & _solution) {
|
||||||
|
temperature = coolingSchedule.init(_solution);
|
||||||
|
//isMoveAccepted = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* decrease the temperature if necessary
|
||||||
|
* @param _solution unused solution
|
||||||
|
*/
|
||||||
|
virtual void updateParam(EOT & _solution) {
|
||||||
|
coolingSchedule.update(temperature, this->moveApplied(), _solution);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* terminate: NOTHING TO DO
|
||||||
|
* @param _solution unused solution
|
||||||
|
*/
|
||||||
|
virtual void terminate(EOT & _solution) {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Explore one random solution in the neighborhood
|
||||||
|
* @param _solution the solution
|
||||||
|
*/
|
||||||
|
virtual void operator()(EOT & _solution) {
|
||||||
|
//Test if _solution has a Neighbor
|
||||||
|
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,
|
||||||
|
//isMoveAccepted = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* continue if the temperature is not too low
|
||||||
|
* @param _solution the solution
|
||||||
|
* @return true if the criteria from the cooling schedule is true
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
// isMoveAccepted = 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 );
|
||||||
|
// isMoveAccepted = (rng.uniform() < alpha) ;*/
|
||||||
|
// /*
|
||||||
|
// double fit1 = (double)selectedNeighbor.fitness(),
|
||||||
|
// fit2 = (double)_solution.fitness(),
|
||||||
|
// alpha = fit1 < fit2 ? exp((fit1 - fit2) / temperature) : exp((fit2 - fit1) / temperature);
|
||||||
|
// //if (fit1 < fit2) // this is a maximization
|
||||||
|
// //else // this is a minimization
|
||||||
|
// isMoveAccepted = (rng.uniform() < alpha);*/
|
||||||
|
// /*double fit1 = (double) selectedNeighbor.fitness(),
|
||||||
|
// fit2 = (double) _solution.fitness(),
|
||||||
|
// alpha = fit1 < fit2 ? exp((fit1 - fit2) / temperature) : 1;
|
||||||
|
// isMoveAccepted = (rng.uniform() <= alpha);*/
|
||||||
|
// /*
|
||||||
|
// double fit1 = (double) selectedNeighbor.fitness(),
|
||||||
|
// fit2 = (double) _solution.fitness(),
|
||||||
|
// alpha = exp( - fabs(fit1 - fit2) / temperature );
|
||||||
|
// // (fit1 - fit2) positive or negative depending on whether we're maximizing or minimizing
|
||||||
|
// isMoveAccepted = (rng.uniform() <= alpha);*/
|
||||||
|
// //isMoveAccepted = Accepter::accept(_solution, selectedNeighbor);
|
||||||
|
// isMoveAccepted = static_cast<Derived*>(this)->accept(_solution);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return isMoveAccepted;
|
||||||
|
// };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter
|
||||||
|
* @return the temperature
|
||||||
|
*/
|
||||||
|
double getTemperature() const {
|
||||||
|
return temperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
virtual bool doAccept(EOT & _solution) {
|
||||||
|
double fit1 = (double) selectedNeighbor.fitness(),
|
||||||
|
fit2 = (double) _solution.fitness(),
|
||||||
|
alpha = exp( - fabs(fit1 - fit2) / temperature );
|
||||||
|
// (fit1 - fit2) positive or negative depending on whether we're maximizing or minimizing
|
||||||
|
return rng.uniform() <= alpha;
|
||||||
|
}*/
|
||||||
|
double alpha(EOT & _solution) {
|
||||||
|
//std::cout << "ok " << exp( - fabs((double) selectedNeighbor.fitness() - (double) _solution.fitness()) / temperature ) << " ";
|
||||||
|
return exp( - fabs((double) selectedNeighbor.fitness() - (double) _solution.fitness()) / temperature );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
//moSolNeighborComparator<Neighbor>* defaultSolNeighborComp;
|
||||||
|
|
||||||
|
public://FIXME add friend
|
||||||
|
// comparator betwenn solution and neighbor
|
||||||
|
//moSolNeighborComparator<Neighbor>& solNeighborComparator;
|
||||||
|
|
||||||
|
moCoolingSchedule<EOT>& coolingSchedule;
|
||||||
|
|
||||||
|
// temperatur of the process
|
||||||
|
double temperature;
|
||||||
|
|
||||||
|
// true if the move is accepted
|
||||||
|
//bool isMoveAccepted ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter
|
||||||
|
* @return the temperature
|
||||||
|
*/
|
||||||
|
//virtual double getTemperature() = 0;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
230
mo/src/explorer/moSimpleMetropolisHastingsExplorer.h
Normal file
230
mo/src/explorer/moSimpleMetropolisHastingsExplorer.h
Normal file
|
|
@ -0,0 +1,230 @@
|
||||||
|
/*
|
||||||
|
<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>::neighborhood;
|
||||||
|
using moNeighborhoodExplorer<Neighbor>::eval;
|
||||||
|
using moNeighborhoodExplorer<Neighbor>::selectedNeighbor;
|
||||||
|
*/
|
||||||
|
|
||||||
|
using moNeighborhoodExplorer<Neighbor>::selectedNeighbor;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
// * Constructor TODO
|
||||||
|
// * @param _neighborhood the neighborhood
|
||||||
|
// * @param _eval the evaluation function
|
||||||
|
// * @param _neighborComparator a neighbor comparator
|
||||||
|
// * @param _solNeighborComparator a solution vs neighbor comparator
|
||||||
|
// * @param _maxSteps maximum number of currentStepNb to do
|
||||||
|
// */
|
||||||
|
moSimpleMetropolisHastingsExplorer (
|
||||||
|
Neighborhood& _neighborhood,
|
||||||
|
moEval<Neighbor>& _eval,
|
||||||
|
unsigned int _maxSteps,
|
||||||
|
eoOptional< moSolNeighborComparator<Neighbor> > _comp = NULL
|
||||||
|
)
|
||||||
|
: moMetropolisHastingsExplorer< Neighbor, moSimpleMetropolisHastingsExplorer<Neighbor> >(_neighborhood, _eval, _comp),
|
||||||
|
maxSteps(_maxSteps)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
// * Constructor
|
||||||
|
// * @param _neighborhood the neighborhood
|
||||||
|
// * @param _eval the evaluation function
|
||||||
|
// * @param _neighborComparator a neighbor comparator
|
||||||
|
// * @param _solNeighborComparator a solution vs neighbor comparator
|
||||||
|
// * @param _maxSteps maximum number of currentStepNb to do
|
||||||
|
// */
|
||||||
|
// moSimpleMetropolisHastingsExplorer(
|
||||||
|
// Neighborhood& _neighborhood,
|
||||||
|
// moEval<Neighbor>& _eval,
|
||||||
|
// moNeighborComparator<Neighbor>& _neighborComparator,
|
||||||
|
// moSolNeighborComparator<Neighbor>& _solNeighborComparator,
|
||||||
|
// unsigned int _maxSteps
|
||||||
|
// ): moNeighborhoodExplorer<Neighbor>(_neighborhood, _eval),
|
||||||
|
// neighborComparator(_neighborComparator),
|
||||||
|
// solNeighborComparator(_solNeighborComparator),
|
||||||
|
// maxSteps(_maxSteps)
|
||||||
|
// {
|
||||||
|
// isAccept = false;
|
||||||
|
// if (!neighborhood.isRandom()) {
|
||||||
|
// std::cout << "moSimpleMetropolisHastingsExplorer::Warning -> the neighborhood used is not random" << std::endl;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * Destructor
|
||||||
|
// */
|
||||||
|
// ~moSimpleMetropolisHastingsExplorer() {
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
/**
|
||||||
|
* initialization of the number of currentStepNb to be done
|
||||||
|
* @param _solution unused solution
|
||||||
|
*/
|
||||||
|
virtual void initParam(EOT & _solution) {
|
||||||
|
currentStepNb = 0;
|
||||||
|
//isAccept = true;
|
||||||
|
};
|
||||||
|
//
|
||||||
|
/**
|
||||||
|
* increase the number of currentStepNb
|
||||||
|
* @param _solution unused solution
|
||||||
|
*/
|
||||||
|
virtual void updateParam(EOT & _solution) {
|
||||||
|
currentStepNb++;
|
||||||
|
};
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 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 currentStepNb < maxSteps;
|
||||||
|
};
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 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;
|
||||||
|
// };
|
||||||
|
|
||||||
|
double alpha(EOT & _solution) {
|
||||||
|
if (selectedNeighbor.fitness() == 0)
|
||||||
|
return selectedNeighbor.fitness() < (double) _solution.fitness() ? 1: 0;
|
||||||
|
return (double) _solution.fitness() / (double) selectedNeighbor.fitness();
|
||||||
|
}
|
||||||
|
//
|
||||||
|
//private:
|
||||||
|
// // comparator betwenn solution and neighbor or between neighbors
|
||||||
|
// moNeighborComparator<Neighbor>& neighborComparator;
|
||||||
|
// moSolNeighborComparator<Neighbor>& solNeighborComparator;
|
||||||
|
//
|
||||||
|
// // current number of currentStepNb
|
||||||
|
// unsigned int currentStepNb;
|
||||||
|
//
|
||||||
|
// // maximum number of steps to do
|
||||||
|
// unsigned int maxSteps;
|
||||||
|
//
|
||||||
|
// // true if the move is accepted
|
||||||
|
// bool isAccept ;
|
||||||
|
private:
|
||||||
|
// current number of steps
|
||||||
|
unsigned int currentStepNb;
|
||||||
|
|
||||||
|
// maximum number of steps to do
|
||||||
|
unsigned int maxSteps;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
65
mo/test/t-moMetropolisHastings.cpp
Normal file
65
mo/test/t-moMetropolisHastings.cpp
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
/*
|
||||||
|
<t-moMetropolisHastings.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/moMetropolisHastings.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-moMetropolisHastings] => 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
|
||||||
|
moMetropolisHastings<bitNeighbor> test1(nh, fullEval, eval, 3);
|
||||||
|
|
||||||
|
//test du 2eme constructeur
|
||||||
|
moMetropolisHastings<bitNeighbor> test2(nh, fullEval, eval, 3, cont);
|
||||||
|
|
||||||
|
//test du 3eme constructeur
|
||||||
|
moMetropolisHastings<bitNeighbor> test3(nh, fullEval, eval, 3, cont, ncomp, sncomp);
|
||||||
|
|
||||||
|
std::cout << "[t-moMetropolisHastings] => OK" << std::endl;
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
98
mo/test/t-moMetropolisHastingsExplorer.cpp
Normal file
98
mo/test/t-moMetropolisHastingsExplorer.cpp
Normal file
|
|
@ -0,0 +1,98 @@
|
||||||
|
/*
|
||||||
|
<t-moMetropolisHastingsExplorer.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/moSimpleMetropolisHastingsExplorer.h>
|
||||||
|
#include "moTestClass.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
std::cout << "[t-moMetropolisHastingsExplorer] => START" << std::endl;
|
||||||
|
|
||||||
|
//Instanciation
|
||||||
|
eoBit<eoMinimizingFitness> sol(4, true);
|
||||||
|
sol.fitness(4);
|
||||||
|
bitNeighborhood nh(4);
|
||||||
|
evalOneMax eval(4);
|
||||||
|
moNeighborComparator<bitNeighbor> ncomp;
|
||||||
|
moSolNeighborComparator<bitNeighbor> sncomp;
|
||||||
|
|
||||||
|
//moSimpleMetropolisHastingsExplorer<bitNeighbor> test(nh, eval, ncomp, sncomp, 3);
|
||||||
|
moSimpleMetropolisHastingsExplorer<bitNeighbor> test(nh, eval, 3, sncomp);
|
||||||
|
|
||||||
|
//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-moMetropolisHastingsExplorer] => OK" << std::endl;
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
87
mo/test/t-moSAExplorer.cpp
Normal file
87
mo/test/t-moSAExplorer.cpp
Normal file
|
|
@ -0,0 +1,87 @@
|
||||||
|
/*
|
||||||
|
<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, cool, sncomp);
|
||||||
|
moSAExplorer<bitNeighbor> test2(nh, eval, cool, sncomp);
|
||||||
|
|
||||||
|
//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;
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue