diff --git a/branches/newMo/src/explorer/moMetropolisHastingExplorer.h b/branches/newMo/src/explorer/moMetropolisHastingExplorer.h index f343b3b4b..7e47cd9f8 100644 --- a/branches/newMo/src/explorer/moMetropolisHastingExplorer.h +++ b/branches/newMo/src/explorer/moMetropolisHastingExplorer.h @@ -44,6 +44,7 @@ /** * 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 Neighborhood > class moMetropolisHastingExplorer : public moNeighborhoodExplorer @@ -103,8 +104,6 @@ public: * @param _solution */ virtual void operator()(EOT & _solution){ - - //est qu'on peut initializer //Test if _solution has a Neighbor if(neighborhood.hasNeighbor(_solution)){ //init the first neighbor @@ -151,11 +150,18 @@ public: isAccept = true; else{ if(_solution.fitness() != 0){ - alpha = (double) current->fitness() / (double) _solution.fitness(); + if( (double)current->fitness() < (double)_solution.fitness()) // maximizing + alpha = (double) current->fitness() / (double) _solution.fitness(); + else //minimizing + alpha = (double) _solution.fitness() / (double) current->fitness(); isAccept = (rng.uniform() < alpha) ; } - else - isAccept = false; + else{ + if( (double)current->fitness() < (double)_solution.fitness()) // maximizing + isAccept = true; + else + isAccept = false; + } } } return isAccept; diff --git a/branches/newMo/src/explorer/moRandomNeutralWalkExplorer.h b/branches/newMo/src/explorer/moRandomNeutralWalkExplorer.h index 33260e3c9..c6f46909c 100644 --- a/branches/newMo/src/explorer/moRandomNeutralWalkExplorer.h +++ b/branches/newMo/src/explorer/moRandomNeutralWalkExplorer.h @@ -41,6 +41,7 @@ /** * Explorer for a random neutral walk * accept the movement when the neighbor has the same fitnes + * To sample the neutral networks by random walk, there is no memory * neighborhood must be explored in random order */ template< class Neighborhood > diff --git a/branches/newMo/src/neighborhood/moIndexNeighborhood.h b/branches/newMo/src/neighborhood/moIndexNeighborhood.h index 3817b0e30..12c8b4246 100644 --- a/branches/newMo/src/neighborhood/moIndexNeighborhood.h +++ b/branches/newMo/src/neighborhood/moIndexNeighborhood.h @@ -41,7 +41,7 @@ * A Indexed Neighborhood */ template< class Neighbor > -class moIndexNeighborhood : public moNeighborhood +class moIndexNeighborhood : virtual public moNeighborhood { public: /** diff --git a/branches/newMo/src/neighborhood/moRndNeighborhood.h b/branches/newMo/src/neighborhood/moRndNeighborhood.h new file mode 100644 index 000000000..501f37696 --- /dev/null +++ b/branches/newMo/src/neighborhood/moRndNeighborhood.h @@ -0,0 +1,38 @@ +/* + +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 +*/ + +#ifndef _moRndNeighborhood_h +#define _moRndNeighborhood_h + +#include + +template< class Neighbor > +class moRndNeighborhood : virtual public moNeighborhood{}; + +#endif diff --git a/branches/newMo/src/neighborhood/moRndWithReplNeighborhood.h b/branches/newMo/src/neighborhood/moRndWithReplNeighborhood.h index 9fb27ea86..773138696 100644 --- a/branches/newMo/src/neighborhood/moRndWithReplNeighborhood.h +++ b/branches/newMo/src/neighborhood/moRndWithReplNeighborhood.h @@ -36,13 +36,14 @@ #define _moRndWithReplNeighborhood_h #include +#include #include /** * A Random With replacement Neighborhood */ template< class N > -class moRndWithReplNeighborhood : public moIndexNeighborhood +class moRndWithReplNeighborhood : public moIndexNeighborhood, public moRndNeighborhood { public: diff --git a/branches/newMo/src/neighborhood/moRndWithoutReplNeighborhood.h b/branches/newMo/src/neighborhood/moRndWithoutReplNeighborhood.h index 94337ee71..3766deafb 100644 --- a/branches/newMo/src/neighborhood/moRndWithoutReplNeighborhood.h +++ b/branches/newMo/src/neighborhood/moRndWithoutReplNeighborhood.h @@ -36,13 +36,14 @@ #define _moRndWithoutReplNeighborhood_h #include +#include #include /** * A Random without replacement Neighborhood */ template< class N > -class moRndWithoutReplNeighborhood : public moIndexNeighborhood +class moRndWithoutReplNeighborhood : public moIndexNeighborhood, public moRndNeighborhood { public: diff --git a/branches/newMo/src/newmo.h b/branches/newMo/src/newmo.h index 3d3961008..a275a4466 100755 --- a/branches/newMo/src/newmo.h +++ b/branches/newMo/src/newmo.h @@ -90,6 +90,7 @@ #include #include #include +#include #include #include diff --git a/branches/newMo/test/CMakeLists.txt b/branches/newMo/test/CMakeLists.txt index 7bcabceaf..65c8e8b64 100644 --- a/branches/newMo/test/CMakeLists.txt +++ b/branches/newMo/test/CMakeLists.txt @@ -53,6 +53,8 @@ SET (TEST_LIST t-moHCneutralExplorer t-moFirstImprExplorer t-moRandomWalkExplorer + t-moMetropolisHastingExplorer + t-moRandomNeutralWalkExplorer ) diff --git a/branches/newMo/test/moTestClass.h b/branches/newMo/test/moTestClass.h index a18d15952..0087e4f45 100644 --- a/branches/newMo/test/moTestClass.h +++ b/branches/newMo/test/moTestClass.h @@ -40,6 +40,7 @@ #include #include #include +#include #include #include @@ -50,9 +51,17 @@ #include #include + + typedef eoBit bitVector; typedef moBitNeighbor bitNeighbor ; -typedef moOrderNeighborhood bitNeighborhood ; + +class moDummyRndNeighborhood: public moOrderNeighborhood, public moRndNeighborhood { +public: + moDummyRndNeighborhood(unsigned int a): moOrderNeighborhood(a){} +}; + +typedef moDummyRndNeighborhood bitNeighborhood ; typedef EO Solution; @@ -123,6 +132,22 @@ public: } }; +class dummyEvalOneMax : public moEval< bitNeighbor > +{ +private: + unsigned size; + +public: + dummyEvalOneMax(unsigned _size) : size(_size) {}; + + ~dummyEvalOneMax(void) {} ; + + void operator() (bitVector& _sol, bitNeighbor& _n) { + unsigned int fit = _sol.fitness(); + _n.fitness(fit); + } +}; + class monitor1 : public eoMonitor { public: diff --git a/branches/newMo/test/t-moMetropolisHastingExplorer.cpp b/branches/newMo/test/t-moMetropolisHastingExplorer.cpp new file mode 100644 index 000000000..bd4f99efa --- /dev/null +++ b/branches/newMo/test/t-moMetropolisHastingExplorer.cpp @@ -0,0 +1,92 @@ +/* + +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-moMetropolisHastingExplorer] => START" << std::endl; + + eoBit sol(4, true); + sol.fitness(4); + bitNeighborhood nh(4); + evalOneMax eval(4); + moNeighborComparator ncomp; + moSolNeighborComparator sncomp; + + moMetropolisHastingExplorer test(nh, eval, ncomp, sncomp, 3); + + 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; + + 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.updateParam(sol); + assert(test.isContinue(sol)); + test.updateParam(sol); + assert(!test.isContinue(sol)); + + sol[0]=false; + sol[1]=false; + sol[2]=false; + sol[3]=false; + sol.fitness(0); + + test.initParam(sol); + test(sol); + assert(!test.accept(sol)); + + std::cout << "[t-moMetropolisHastingExplorer] => OK" << std::endl; + + return EXIT_SUCCESS; +} + diff --git a/branches/newMo/test/t-moRandomNeutralWalkExplorer.cpp b/branches/newMo/test/t-moRandomNeutralWalkExplorer.cpp new file mode 100644 index 000000000..3b7b18d0a --- /dev/null +++ b/branches/newMo/test/t-moRandomNeutralWalkExplorer.cpp @@ -0,0 +1,84 @@ +/* + +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-moRandomNeutralWalkExplorer] => START" << std::endl; + + eoBit sol(4, true); + sol.fitness(4); + bitNeighborhood nh(4); + evalOneMax eval(4); + dummyEvalOneMax eval2(4); + moSolNeighborComparator sncomp; + + moRandomNeutralWalkExplorer test(nh, eval, sncomp, 3); + + test.initParam(sol); + test(sol); + assert(!test.accept(sol)); + assert(!test.isContinue(sol)); + + moRandomNeutralWalkExplorer test2(nh, eval2, sncomp, 3); + + sol.fitness(2); + test2.initParam(sol); + test2(sol); + assert(test2.accept(sol)); + test2.move(sol); + assert(sol.fitness()==2); + test2.updateParam(sol); + assert(test2.isContinue(sol)); + + test2(sol); + assert(test2.accept(sol)); + test2.move(sol); + assert(sol.fitness()==2); + test2.updateParam(sol); + assert(test2.isContinue(sol)); + + test2(sol); + assert(test2.accept(sol)); + test2.move(sol); + assert(sol.fitness()==2); + test2.updateParam(sol); + assert(!test2.isContinue(sol)); + + std::cout << "[t-moRandomNeutralWalkExplorer] => OK" << std::endl; + + return EXIT_SUCCESS; +} +