From 5c3260d712aa8c9195e9df3a569597e10fd14f88 Mon Sep 17 00:00:00 2001 From: jhumeau Date: Thu, 18 Mar 2010 15:01:16 +0000 Subject: [PATCH] tests added git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1700 331e1502-861f-0410-8da2-ba01fb791d7f --- .../newMo/src/explorer/moFirstImprExplorer.h | 3 +- .../newMo/src/explorer/moHCneutralExplorer.h | 24 ++--- .../src/explorer/moSimpleHCneutralExplorer.h | 74 +++++++------- branches/newMo/test/CMakeLists.txt | 6 +- branches/newMo/test/t-moFirstImprExplorer.cpp | 90 +++++++++++++++++ branches/newMo/test/t-moHCneutralExplorer.cpp | 89 +++++++++++++++++ branches/newMo/test/t-moSimpleHCexplorer.cpp | 2 +- .../test/t-moSimpleHCneutralExplorer.cpp | 98 +++++++++++++++++++ 8 files changed, 331 insertions(+), 55 deletions(-) create mode 100644 branches/newMo/test/t-moFirstImprExplorer.cpp create mode 100644 branches/newMo/test/t-moHCneutralExplorer.cpp create mode 100644 branches/newMo/test/t-moSimpleHCneutralExplorer.cpp diff --git a/branches/newMo/src/explorer/moFirstImprExplorer.h b/branches/newMo/src/explorer/moFirstImprExplorer.h index c784ecd02..15710861c 100644 --- a/branches/newMo/src/explorer/moFirstImprExplorer.h +++ b/branches/newMo/src/explorer/moFirstImprExplorer.h @@ -59,7 +59,7 @@ public: * @param _neighborComparator a neighbor comparator * @param _solNeighborComparator a solution vs neighbor comparator */ - moFirstImprExplorer(Neighborhood& _neighborhood, moEval& _eval, moNeighborComparator& _neighborComparator, moSolNeighborComparator& _solNeighborComparator) : moNeighborhoodExplorer(_neighborhood, _eval), neighborComparator(_neighborComparator), solNeighborComparator(_solNeighborComparator) { + moFirstImprExplorer(Neighborhood& _neighborhood, moEval& _eval, moNeighborComparator& _neighborComparator, moSolNeighborComparator& _solNeighborComparator) : moNeighborhoodExplorer(_neighborhood, _eval), neighborComparator(_neighborComparator), solNeighborComparator(_solNeighborComparator) { isAccept = false; current=new Neighbor(); } @@ -92,7 +92,6 @@ public: */ virtual void operator()(EOT & _solution){ - //est qu'on peut initializer //Test if _solution has a Neighbor if(neighborhood.hasNeighbor(_solution)){ //init the first neighbor diff --git a/branches/newMo/src/explorer/moHCneutralExplorer.h b/branches/newMo/src/explorer/moHCneutralExplorer.h index 1a8f4933c..f6a5abce4 100644 --- a/branches/newMo/src/explorer/moHCneutralExplorer.h +++ b/branches/newMo/src/explorer/moHCneutralExplorer.h @@ -40,7 +40,7 @@ #include /** - * Explorer for a simple Hill-climbing + * Explorer for a neutral Hill-climbing */ template< class Neighborhood > class moHCneutralExplorer : public moSimpleHCneutralExplorer @@ -64,13 +64,13 @@ public: * @param _solNeighborComparator a solution vs neighbor comparator * @param _nbStep maximum step to do */ - moHCneutralExplorer(Neighborhood& _neighborhood, + moHCneutralExplorer(Neighborhood& _neighborhood, moEval& _eval, moNeighborComparator& _neighborComparator, - moSolNeighborComparator& _solNeighborComparator, - unsigned _nbStep) : - moSimpleHCneutralExplorer(_neighborhood, _eval, _neighborComparator, _solNeighborComparator), nbStep(_nbStep) { - } + moSolNeighborComparator& _solNeighborComparator, + unsigned _nbStep) : + moSimpleHCneutralExplorer(_neighborhood, _eval, _neighborComparator, _solNeighborComparator), + nbStep(_nbStep){} /** * Destructor @@ -102,7 +102,7 @@ public: * @return true there is some steps to do */ virtual bool isContinue(EOT & _solution) { - return (step < nbStep) && isAccept ; + return (step < nbStep) && isAccept ; }; /** @@ -111,14 +111,8 @@ public: * @return true if the best neighbor ameliorate the fitness or is equals */ virtual bool accept(EOT & _solution) { - if(neighborhood.hasNeighbor(_solution)){ - isAccept = solNeighborComparator(_solution, bestVector[0]) || solNeighborComparator.equals(_solution, bestVector[0]) ; - std::cout << bestVector.size() << " " << bestVector[0] << " " ; - if (isAccept) - std::cout << "true" << std::endl; - else - std::cout << "false" << std::endl; - } + if(neighborhood.hasNeighbor(_solution)) + isAccept = solNeighborComparator(_solution, bestVector[0]) || solNeighborComparator.equals(_solution, bestVector[0]) ; return isAccept; }; diff --git a/branches/newMo/src/explorer/moSimpleHCneutralExplorer.h b/branches/newMo/src/explorer/moSimpleHCneutralExplorer.h index 0d7d11e93..6df1b3959 100644 --- a/branches/newMo/src/explorer/moSimpleHCneutralExplorer.h +++ b/branches/newMo/src/explorer/moSimpleHCneutralExplorer.h @@ -38,9 +38,11 @@ #include #include #include +#include +#include /** - * Explorer for a simple Hill-climbing + * Explorer for a simple neutral Hill-climbing */ template< class Neighborhood > class moSimpleHCneutralExplorer : public moNeighborhoodExplorer @@ -59,12 +61,13 @@ public: * @param _neighborComparator a neighbor comparator * @param _solNeighborComparator solution vs neighbor comparator */ - moSimpleHCneutralExplorer(Neighborhood& _neighborhood, + moSimpleHCneutralExplorer(Neighborhood& _neighborhood, moEval& _eval, moNeighborComparator& _neighborComparator, moSolNeighborComparator& _solNeighborComparator) : - moNeighborhoodExplorer(_neighborhood, _eval), - neighborComparator(_neighborComparator), solNeighborComparator(_solNeighborComparator) { + moNeighborhoodExplorer(_neighborhood, _eval), + neighborComparator(_neighborComparator), + solNeighborComparator(_solNeighborComparator) { isAccept = false; current=new Neighbor(); } @@ -103,39 +106,39 @@ public: */ virtual void operator()(EOT & _solution){ - //est qu'on peut initializer - //Test if _solution has a Neighbor - if(neighborhood.hasNeighbor(_solution)){ - //init the first neighbor - neighborhood.init(_solution, (*current)); + //Test if _solution has a Neighbor + if(neighborhood.hasNeighbor(_solution)){ + //init the first neighbor + neighborhood.init(_solution, (*current)); - //eval the _solution moved with the neighbor and stock the result in the neighbor - eval(_solution, (*current)); + //eval the _solution moved with the neighbor and stock the result in the neighbor + eval(_solution, (*current)); - //initialize the best neighbor - bestVector.push_back(*current); + //initialize the best neighbor + bestVector.push_back(*current); - //test all others neighbors - while (neighborhood.cont(_solution)) { - //next neighbor - neighborhood.next(_solution, (*current)); + //test all others neighbors + while (neighborhood.cont(_solution)) { + //next neighbor + neighborhood.next(_solution, (*current)); - //eval - eval(_solution, (*current)); + //eval + eval(_solution, (*current)); - //if we found a better neighbor, update the best - if (neighborComparator(bestVector[0], (*current))) { - bestVector.clear(); - bestVector.push_back(*current); - } else //if the current is equals to previous best solutions then update vector of the best solution - if (neighborComparator.equals((*current), bestVector[0])) - bestVector.push_back(*current); - } - } else { - //if _solution hasn't neighbor, - isAccept=false; - } - }; + //if we found a better neighbor, update the best + if (neighborComparator(bestVector[0], (*current))) { + bestVector.clear(); + bestVector.push_back(*current); + } + else if (neighborComparator.equals((*current), bestVector[0])) //if the current is equals to previous best solutions then update vector of the best solution + bestVector.push_back(*current); + } + } + else { + //if _solution hasn't neighbor, + isAccept=false; + } + }; /** * continue if a move is accepted @@ -167,10 +170,9 @@ public: * @return true if the best neighbor ameliorate the fitness */ virtual bool accept(EOT & _solution) { - if(neighborhood.hasNeighbor(_solution)){ - isAccept = solNeighborComparator(_solution, bestVector[0]) ; - } - return isAccept; + if(neighborhood.hasNeighbor(_solution)) + isAccept = solNeighborComparator(_solution, bestVector[0]) ; + return isAccept; }; protected: diff --git a/branches/newMo/test/CMakeLists.txt b/branches/newMo/test/CMakeLists.txt index 10c2c7bd1..09401702b 100644 --- a/branches/newMo/test/CMakeLists.txt +++ b/branches/newMo/test/CMakeLists.txt @@ -36,7 +36,6 @@ SET (TEST_LIST t-moOrderNeighborhood t-moFullEvalByCopy t-moFullEvalByModif - t-moSimpleHCexplorer t-moNeighborComparator t-moSolNeighborComparator t-moTrueContinuator @@ -48,6 +47,11 @@ SET (TEST_LIST t-moCounterMonitorSaver t-moSolutionStat t-moCheckpoint + t-moSimpleHCexplorer + t-moSimpleHCneutralExplorer + t-moHCneutralExplorer + t-moFirstImprExplorer + ) FOREACH (test ${TEST_LIST}) diff --git a/branches/newMo/test/t-moFirstImprExplorer.cpp b/branches/newMo/test/t-moFirstImprExplorer.cpp new file mode 100644 index 000000000..4cbca875b --- /dev/null +++ b/branches/newMo/test/t-moFirstImprExplorer.cpp @@ -0,0 +1,90 @@ +/* + +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 +#include + +int main(){ + + std::cout << "[t-moFirstImprExplorer] => START" << std::endl; + + eoBit sol(4, true); + sol.fitness(4); + bitNeighborhood nh(4); + evalOneMax eval(4); + moNeighborComparator ncomp; + moSolNeighborComparator sncomp; + + moFirstImprExplorer test(nh, eval, ncomp, sncomp); + + test(sol); + assert(test.accept(sol)); + test.move(sol); + assert(sol.fitness()==3); + assert(!sol[0]); + assert(test.isContinue(sol)); + + + test(sol); + assert(test.accept(sol)); + test.move(sol); + assert(sol.fitness()==2); + assert(!sol[1]); + assert(test.isContinue(sol)); + + test(sol); + assert(test.accept(sol)); + test.move(sol); + assert(sol.fitness()==1); + assert(!sol[2]); + assert(test.isContinue(sol)); + + test(sol); + assert(test.accept(sol)); + test.move(sol); + assert(sol.fitness()==0); + assert(!sol[3]); + assert(test.isContinue(sol)); + + test(sol); + assert(!test.accept(sol)); + assert(sol.fitness()==0); + assert(!test.isContinue(sol)); + + std::cout << "[t-moFirstImprExplorer] => OK" << std::endl; + + return EXIT_SUCCESS; +} + diff --git a/branches/newMo/test/t-moHCneutralExplorer.cpp b/branches/newMo/test/t-moHCneutralExplorer.cpp new file mode 100644 index 000000000..83ecd5346 --- /dev/null +++ b/branches/newMo/test/t-moHCneutralExplorer.cpp @@ -0,0 +1,89 @@ +/* + +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 +#include + +int main(){ + + std::cout << "[t-moHCneutralExplorer] => START" << std::endl; + + eoBit sol(4, true); + sol.fitness(4); + bitNeighborhood nh(4); + evalOneMax eval(4); + moNeighborComparator ncomp; + moSolNeighborComparator sncomp; + + moHCneutralExplorer 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)); + + + std::cout << sol << std::endl; + + test(sol); + assert(test.accept(sol)); + test.move(sol); + assert(sol.fitness()==2); + test.updateParam(sol); + assert(test.isContinue(sol)); + + + std::cout << sol << std::endl; + + test(sol); + assert(test.accept(sol)); + test.move(sol); + assert(sol.fitness()==1); + test.updateParam(sol); + assert(!test.isContinue(sol)); + + + std::cout << sol << std::endl; + + + + std::cout << "[t-moHCneutralExplorer] => OK" << std::endl; + + return EXIT_SUCCESS; +} + diff --git a/branches/newMo/test/t-moSimpleHCexplorer.cpp b/branches/newMo/test/t-moSimpleHCexplorer.cpp index 8c91c7fc2..90c7a66fe 100644 --- a/branches/newMo/test/t-moSimpleHCexplorer.cpp +++ b/branches/newMo/test/t-moSimpleHCexplorer.cpp @@ -78,7 +78,7 @@ int main(){ //verif de move -> on affecte la fitness du best d'avant test.move(sol); - //hasNeighbor() retourne vrai et on ameliore la pas fitness donc on doit pas continuer + //hasNeighbor() retourne vrai et on ameliore pas la fitness donc on doit pas continuer test(sol); test(sol); test.accept(sol); diff --git a/branches/newMo/test/t-moSimpleHCneutralExplorer.cpp b/branches/newMo/test/t-moSimpleHCneutralExplorer.cpp new file mode 100644 index 000000000..1abc956a8 --- /dev/null +++ b/branches/newMo/test/t-moSimpleHCneutralExplorer.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 +#include +#include "moTestClass.h" + +#include +#include +#include + +int main(){ + + std::cout << "[t-moSimpleHCneutralExplorer] => START" << std::endl; + + eoBit sol(4, true); + sol.fitness(4); + bitNeighborhood nh(4); + evalOneMax eval(4); + moNeighborComparator ncomp; + moSolNeighborComparator sncomp; + + moSimpleHCneutralExplorer test(nh, eval, ncomp, sncomp); + + test.initParam(sol); + test(sol); + assert(test.accept(sol)); + test.move(sol); + assert(sol.fitness()==3); + assert(test.isContinue(sol)); + test.updateParam(sol); + + std::cout << sol << std::endl; + + test(sol); + assert(test.accept(sol)); + test.move(sol); + assert(sol.fitness()==2); + assert(test.isContinue(sol)); + test.updateParam(sol); + + std::cout << sol << std::endl; + + test(sol); + assert(test.accept(sol)); + test.move(sol); + assert(sol.fitness()==1); + assert(test.isContinue(sol)); + test.updateParam(sol); + + std::cout << sol << std::endl; + + test(sol); + assert(test.accept(sol)); + test.move(sol); + assert(sol.fitness()==0); + assert(test.isContinue(sol)); + test.updateParam(sol); + + test(sol); + assert(!test.accept(sol)); + assert(sol.fitness()==0); + assert(!test.isContinue(sol)); + test.updateParam(sol); + + + std::cout << "[t-moSimpleHCneutralExplorer] => OK" << std::endl; + + return EXIT_SUCCESS; +} +