From 9d0b83022d469be106db07afef5cc49cb22ff4ce Mon Sep 17 00:00:00 2001 From: LPTK Date: Wed, 10 Jul 2013 17:50:01 +0200 Subject: [PATCH] intermediate commit 6' --- eo/src/utils/eoGetterUpdater.h | 90 +++++++ mo/src/algo/moMetropolisHastings.h | 112 ++++++++ mo/src/continuator/moFunctionContinuator.h | 64 +++++ .../coolingSchedule/moHuangCoolingSchedule.h | 114 ++++++++ .../explorer/moMetropolisHastingsExplorer.h | 203 +++++++++++++++ mo/src/explorer/moSAExplorer.h | 243 ++++++++++++++++++ .../moSimpleMetropolisHastingsExplorer.h | 230 +++++++++++++++++ mo/test/t-moMetropolisHastings.cpp | 65 +++++ mo/test/t-moMetropolisHastingsExplorer.cpp | 98 +++++++ mo/test/t-moSAExplorer.cpp | 87 +++++++ 10 files changed, 1306 insertions(+) create mode 100644 eo/src/utils/eoGetterUpdater.h create mode 100644 mo/src/algo/moMetropolisHastings.h create mode 100644 mo/src/continuator/moFunctionContinuator.h create mode 100644 mo/src/coolingSchedule/moHuangCoolingSchedule.h create mode 100644 mo/src/explorer/moMetropolisHastingsExplorer.h create mode 100644 mo/src/explorer/moSAExplorer.h create mode 100644 mo/src/explorer/moSimpleMetropolisHastingsExplorer.h create mode 100644 mo/test/t-moMetropolisHastings.cpp create mode 100644 mo/test/t-moMetropolisHastingsExplorer.cpp create mode 100644 mo/test/t-moSAExplorer.cpp diff --git a/eo/src/utils/eoGetterUpdater.h b/eo/src/utils/eoGetterUpdater.h new file mode 100644 index 000000000..99d2d8c54 --- /dev/null +++ b/eo/src/utils/eoGetterUpdater.h @@ -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 + +*/ + +#ifndef _eoGetterUpdater_h +#define _eoGetterUpdater_h + +#include + +template class eoCheckPoint; + +/** + eoGetterUpdater is an eoUpdater + TODO + + @ingroup Utilities +*/ +template +class eoGetterUpdater : public eoUpdater, public eoValueParam +{ +public: + using eoValueParam::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(_method)) + //: instance(_instance), method(const_cast(_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 + + + + + + + + + + + + + + + diff --git a/mo/src/algo/moMetropolisHastings.h b/mo/src/algo/moMetropolisHastings.h new file mode 100644 index 000000000..069451e9b --- /dev/null +++ b/mo/src/algo/moMetropolisHastings.h @@ -0,0 +1,112 @@ +/* + +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 +#include +#include +#include +#include +#include + +/** + * 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 moMetropolisHastings: public moLocalSearch +{ +public: + typedef typename Neighbor::EOT EOT; + typedef moNeighborhood Neighborhood ; + + /** + * Basic constructor of the Metropolis-Hasting + * @param _neighborhood the neighborhood + * @param _fullEval the full evaluation function + * @param _eval neighbor's evaluation function + * @param _nbStep maximum step to do + */ + moMetropolisHastings(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, unsigned int _nbStep): + moLocalSearch(explorer, trueCont, _fullEval), + explorer(_neighborhood, _eval, defaultNeighborComp, defaultSolNeighborComp, _nbStep) + {} + + /** + * Simple constructor of the Metropolis-Hasting + * @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& _fullEval, moEval& _eval, unsigned int _nbStep, moContinuator& _cont): + moLocalSearch(explorer, _cont, _fullEval), + explorer(_neighborhood, _eval, defaultNeighborComp, defaultSolNeighborComp, _nbStep) + {} + + /** + * General constructor of the Metropolis-Hasting + * @param _neighborhood the neighborhood + * @param _fullEval the full evaluation function + * @param _eval neighbor's evaluation function + * @param _nbStep maximum step to do + * @param _cont an external continuator + * @param _compN a neighbor vs neighbor comparator + * @param _compSN a solution vs neighbor comparator + */ + moMetropolisHastings(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, unsigned int _nbStep, moContinuator& _cont, moNeighborComparator& _compN, moSolNeighborComparator& _compSN): + moLocalSearch(explorer, _cont, _fullEval), + explorer(_neighborhood, _eval, _compN, _compSN, _nbStep) + {} + +private: + // always true continuator + moTrueContinuator trueCont; + // compare the fitness values of neighbors + moNeighborComparator defaultNeighborComp; + // compare the fitness values of the solution and the neighbor + moSolNeighborComparator defaultSolNeighborComp; + // MetropolisHasting explorer + //moMetropolisHastingsExplorer explorer; + moSimpleMetropolisHastingsExplorer explorer; +}; + +#endif diff --git a/mo/src/continuator/moFunctionContinuator.h b/mo/src/continuator/moFunctionContinuator.h new file mode 100644 index 000000000..f2cf7747e --- /dev/null +++ b/mo/src/continuator/moFunctionContinuator.h @@ -0,0 +1,64 @@ +/* + + 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 +#include + +/** + * To make specific continuator from a solution + */ +template< class Neighbor > +class moFunctionContinuator : public moContinuator +{ +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 diff --git a/mo/src/coolingSchedule/moHuangCoolingSchedule.h b/mo/src/coolingSchedule/moHuangCoolingSchedule.h new file mode 100644 index 000000000..6ac29d778 --- /dev/null +++ b/mo/src/coolingSchedule/moHuangCoolingSchedule.h @@ -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 + +*/ + +#ifndef _moHuangCoolingSchedule_h +#define _moHuangCoolingSchedule_h + +#include + +#include +#include +#include +#include +#include + + +#include +using namespace std; + +//! +/*! + */ +template< class EOT > +class moHuangCoolingSchedule: public moCoolingSchedule< EOT > +{ +public: + //typedef typename Neighbor::EOT EOT ; + //typedef moNeighborhood 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 + + + + + + + + + + diff --git a/mo/src/explorer/moMetropolisHastingsExplorer.h b/mo/src/explorer/moMetropolisHastingsExplorer.h new file mode 100644 index 000000000..caf44f49f --- /dev/null +++ b/mo/src/explorer/moMetropolisHastingsExplorer.h @@ -0,0 +1,203 @@ +/* + + 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 + +#include +#include +#include +#include +#include + +#include +#include + +/** + * Explorer for the Metropolis-Hasting Sampling. + * Only the symetric case is considered when Q(x,y) = Q(y,x) + * Fitness must be > 0 + */ +template< class Neighbor, class Derived /*TODO reqs on Derived*/> +class moMetropolisHastingsExplorer : public moNeighborhoodExplorer +{ +public: + typedef typename Neighbor::EOT EOT ; + typedef moNeighborhood Neighborhood ; + + using moNeighborhoodExplorer::neighborhood; + using moNeighborhoodExplorer::eval; + using moNeighborhoodExplorer::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& _eval, + //moNeighborComparator& _neighborComparator, + //moSolNeighborComparator& _solNeighborComparator + eoOptional< moSolNeighborComparator > _comp = NULL + ) + : moNeighborhoodExplorer(_neighborhood, _eval), + //neighborComparator(_neighborComparator), + //solNeighborComparator(_solNeighborComparator), + defaultSolNeighborComp(NULL), + solNeighborComparator(_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator())) + { + //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(this)->doAccept(_solution); + else accepted = rng.uniform() <= static_cast(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(this)->doAccept(_solution); + else isMoveAccepted = rng.uniform() < static_cast(this)->alpha(_solution); + } + return isMoveAccepted; + }; + +private: + moSolNeighborComparator* defaultSolNeighborComp; + + // comparator betwenn solution and neighbor or between neighbors + //moNeighborComparator& neighborComparator; + moSolNeighborComparator& solNeighborComparator; + /* + // current number of step + unsigned int step; + + // maximum number of steps to do + unsigned int nbStep; + */ + // true if the move is accepted + //bool isMoveAccepted ; +}; + + +#endif diff --git a/mo/src/explorer/moSAExplorer.h b/mo/src/explorer/moSAExplorer.h new file mode 100644 index 000000000..12afeac1f --- /dev/null +++ b/mo/src/explorer/moSAExplorer.h @@ -0,0 +1,243 @@ +/* + + 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 + +//#include +#include +#include +#include +#include +#include +#include + +#include + +/** + * 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 > +{ +public: + typedef typename Neighbor::EOT EOT ; + typedef moNeighborhood Neighborhood ; + + using moNeighborhoodExplorer::neighborhood; + using moNeighborhoodExplorer::eval; + using moNeighborhoodExplorer::selectedNeighbor; + + + /*moSAExplorer(Neighborhood& _neighborhood, moEval& _eval) + : moNeighborhoodExplorer(_neighborhood, _eval) + { }*/ + + moSAExplorer ( + Neighborhood& _neighborhood, + //eoOptional< moEval > _eval = NULL, + moEval& _eval, + moCoolingSchedule& _cool, + eoOptional< moSolNeighborComparator > _comp = NULL + ) + : moMetropolisHastingsExplorer< Neighbor, moSAExplorer >(_neighborhood, _eval, _comp), + /*moNeighborhoodExplorer(_neighborhood, _eval.hasValue()? _eval.get(): *(default_eval = new moFullEvalByCopy(_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())), + //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(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* defaultSolNeighborComp; + +public://FIXME add friend + // comparator betwenn solution and neighbor + //moSolNeighborComparator& solNeighborComparator; + + moCoolingSchedule& coolingSchedule; + + // temperatur of the process + double temperature; + + // true if the move is accepted + //bool isMoveAccepted ; + + /** + * Getter + * @return the temperature + */ + //virtual double getTemperature() = 0; + +}; + + +#endif + diff --git a/mo/src/explorer/moSimpleMetropolisHastingsExplorer.h b/mo/src/explorer/moSimpleMetropolisHastingsExplorer.h new file mode 100644 index 000000000..64fdc7bd9 --- /dev/null +++ b/mo/src/explorer/moSimpleMetropolisHastingsExplorer.h @@ -0,0 +1,230 @@ +/* + + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + + Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau, Lionel Parreaux + + This software is governed by the CeCILL license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited liability. + + In this respect, the user's attention is drawn to the risks associated + with loading, using, modifying and/or developing or reproducing the + software by the user in light of its specific status of free software, + that may mean that it is complicated to manipulate, and that also + therefore means that it is reserved for developers and experienced + professionals having in-depth computer knowledge. Users are therefore + encouraged to load and test the software's suitability as regards their + requirements in conditions enabling the security of their systems and/or + data to be ensured and, more generally, to use and operate it in the + same conditions as regards security. + The fact that you are presently reading this means that you have had + knowledge of the CeCILL license and that you accept its terms. + + ParadisEO WebSite : http://paradiseo.gforge.inria.fr + Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#ifndef _moSimpleMetropolisHastingsExplorer_h +#define _moSimpleMetropolisHastingsExplorer_h +/* +#include + +#include +#include +#include +#include + +#include */ +#include + + +/** + * Explorer for the Metropolis-Hasting Sampling. + * Only the symetric case is considered when Q(x,y) = Q(y,x) + * Fitness must be > 0 + */ +template< class Neighbor > +class moSimpleMetropolisHastingsExplorer : public moMetropolisHastingsExplorer< Neighbor, moSimpleMetropolisHastingsExplorer > +{ +public: + typedef typename Neighbor::EOT EOT ; + typedef moNeighborhood Neighborhood ; + /* + using moNeighborhoodExplorer::neighborhood; + using moNeighborhoodExplorer::eval; + using moNeighborhoodExplorer::selectedNeighbor; + */ + + using moNeighborhoodExplorer::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& _eval, + unsigned int _maxSteps, + eoOptional< moSolNeighborComparator > _comp = NULL + ) + : moMetropolisHastingsExplorer< Neighbor, moSimpleMetropolisHastingsExplorer >(_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& _eval, +// moNeighborComparator& _neighborComparator, +// moSolNeighborComparator& _solNeighborComparator, +// unsigned int _maxSteps +// ): moNeighborhoodExplorer(_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& neighborComparator; +// moSolNeighborComparator& 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 diff --git a/mo/test/t-moMetropolisHastings.cpp b/mo/test/t-moMetropolisHastings.cpp new file mode 100644 index 000000000..6bab6c332 --- /dev/null +++ b/mo/test/t-moMetropolisHastings.cpp @@ -0,0 +1,65 @@ +/* + +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 +#include +#include + +#include +#include "moTestClass.h" +#include +#include +#include +#include + +int main() { + + std::cout << "[t-moMetropolisHastings] => START" << std::endl; + + bitNeighborhood nh(4); + oneMaxEval fullEval; + evalOneMax eval(4); + moTrueContinuator cont; + moSolNeighborComparator sncomp; + moNeighborComparator ncomp; + + //test du 1er constructeur + moMetropolisHastings test1(nh, fullEval, eval, 3); + + //test du 2eme constructeur + moMetropolisHastings test2(nh, fullEval, eval, 3, cont); + + //test du 3eme constructeur + moMetropolisHastings test3(nh, fullEval, eval, 3, cont, ncomp, sncomp); + + std::cout << "[t-moMetropolisHastings] => OK" << std::endl; + + return EXIT_SUCCESS; +} + diff --git a/mo/test/t-moMetropolisHastingsExplorer.cpp b/mo/test/t-moMetropolisHastingsExplorer.cpp new file mode 100644 index 000000000..b7504486d --- /dev/null +++ b/mo/test/t-moMetropolisHastingsExplorer.cpp @@ -0,0 +1,98 @@ +/* + +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 +#include "moTestClass.h" + +#include +#include +#include + +int main() { + + std::cout << "[t-moMetropolisHastingsExplorer] => START" << std::endl; + + //Instanciation + eoBit sol(4, true); + sol.fitness(4); + bitNeighborhood nh(4); + evalOneMax eval(4); + moNeighborComparator ncomp; + moSolNeighborComparator sncomp; + + //moSimpleMetropolisHastingsExplorer test(nh, eval, ncomp, sncomp, 3); + moSimpleMetropolisHastingsExplorer 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; +} + diff --git a/mo/test/t-moSAExplorer.cpp b/mo/test/t-moSAExplorer.cpp new file mode 100644 index 000000000..873dbf080 --- /dev/null +++ b/mo/test/t-moSAExplorer.cpp @@ -0,0 +1,87 @@ +/* + +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 +#include +#include + +#include "moTestClass.h" +#include +#include + +int main() { + + std::cout << "[t-moSAExplorer] => START" << std::endl; + + eoBit sol(4, true); + sol.fitness(4); + bitNeighborhood nh(4); + bitNeighborhood emptyNH(0); + evalOneMax eval(4); + moSolNeighborComparator sncomp; + moSimpleCoolingSchedule cool(10,0.1,2,0.1); + + moSAExplorer test1(emptyNH, eval, cool, sncomp); + moSAExplorer 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; +} +