From 88afa96663e76b411de2a787564818a8188fbe7a Mon Sep 17 00:00:00 2001 From: jhumeau Date: Thu, 21 Jan 2010 15:29:03 +0000 Subject: [PATCH] =?UTF-8?q?Test=20des=20Neighborhoods=20ajout=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1665 331e1502-861f-0410-8da2-ba01fb791d7f --- .../newMo/src/neighborhood/moBitNeighbor.h | 28 +++++++--- .../src/neighborhood/moBitNeighborhood.h | 23 ++++++-- branches/newMo/src/neighborhood/moNeighbor.h | 4 +- branches/newMo/test/CMakeLists.txt | 5 +- branches/newMo/test/moTestClass.h | 3 +- branches/newMo/test/t-moBitNeighbor.cpp | 54 +++++++++++++++++++ branches/newMo/test/t-moBitNeighborhood.cpp | 43 +++++++++++++++ branches/newMo/test/t-moNeighbor.cpp | 18 +++++++ 8 files changed, 164 insertions(+), 14 deletions(-) create mode 100644 branches/newMo/test/t-moBitNeighbor.cpp create mode 100644 branches/newMo/test/t-moBitNeighborhood.cpp diff --git a/branches/newMo/src/neighborhood/moBitNeighbor.h b/branches/newMo/src/neighborhood/moBitNeighbor.h index 91248d18b..0480f5daa 100644 --- a/branches/newMo/src/neighborhood/moBitNeighbor.h +++ b/branches/newMo/src/neighborhood/moBitNeighbor.h @@ -49,18 +49,15 @@ public: using moNeighbor, Fitness>::fitness; - // describe the neighbor - unsigned bit ; - /** * Default Constructor */ - moBitNeighbor() : moBackableNeighbor , Fitness>() { } ; + moBitNeighbor() : moBackableNeighbor , Fitness>(), bit(0) { } ; /** * Copy Constructor */ - moBitNeighbor(const moBitNeighbor& _n) : moNeighbor , Fitness>(_n) { + moBitNeighbor(const moBitNeighbor& _n) : moBackableNeighbor , Fitness>(_n) { this->bit = _n.bit ; } ; @@ -68,7 +65,7 @@ public: * Constructor * @param _b index */ - moBitNeighbor(unsigned int _b) : moNeighbor , Fitness>() , bit(_b) { } ; + moBitNeighbor(unsigned int _b) : moBackableNeighbor , Fitness>() , bit(_b) { } ; /** * Assignment operator @@ -135,6 +132,25 @@ public: _os << fitness() << ' ' << bit << std::endl; } + /** + * Getter + * @return index of the bitNeighbor + */ + unsigned int index(){ + return bit; + } + + /** + * Setter + * @param index of the bitNeighbor + */ + void index(unsigned int _bit){ + bit=_bit; + } + +private: + // describe the neighbor + unsigned int bit ; }; diff --git a/branches/newMo/src/neighborhood/moBitNeighborhood.h b/branches/newMo/src/neighborhood/moBitNeighborhood.h index 175cbf7a2..a6a16c839 100644 --- a/branches/newMo/src/neighborhood/moBitNeighborhood.h +++ b/branches/newMo/src/neighborhood/moBitNeighborhood.h @@ -36,6 +36,7 @@ #define _bitNeighborhood_h #include +#include /** * Neighborhood related to a vector of Bit @@ -50,7 +51,7 @@ public: /** * Default Constructor */ - moBitNeighborhood() : moNeighborhood() { } + moBitNeighborhood() : moNeighborhood(), currentBit(0) { } /** * Test if it exist a neighbor @@ -68,7 +69,7 @@ public: */ virtual void init(EOT & _solution, Neighbor & _neighbor) { currentBit = 0 ; - _neighbor.bit = currentBit ; + _neighbor.index(currentBit) ; } /** @@ -78,7 +79,7 @@ public: */ virtual void next(EOT & _solution, Neighbor & _neighbor) { currentBit++ ; - _neighbor.bit = currentBit ; + _neighbor.index(currentBit); } /** @@ -88,8 +89,22 @@ public: */ virtual bool cont(EOT & _solution) { return (currentBit < _solution.size()) ; - } + } + + /** + * Getter + * @return the position in the Neighborhood + */ + unsigned int position(){ + return currentBit; + } + /** + * Return the class id. + * @return the class name as a std::string + */ + virtual std::string className() const { return "moBitNeighborhood"; } + private: //Position in the neighborhood unsigned int currentBit; diff --git a/branches/newMo/src/neighborhood/moNeighbor.h b/branches/newMo/src/neighborhood/moNeighbor.h index 5abcc0b10..6fadb2263 100644 --- a/branches/newMo/src/neighborhood/moNeighbor.h +++ b/branches/newMo/src/neighborhood/moNeighbor.h @@ -81,7 +81,7 @@ public: * Get the fitness of the neighbor * @return fitness of the neighbor */ - const Fitness& fitness() const { + const Fitness fitness() const { return repFitness; } @@ -124,7 +124,7 @@ public: * @param _os A std::ostream. */ virtual void printOn(std::ostream& _os) const { - _os << repFitness << ' ' ; + _os << repFitness << std::endl; } protected: diff --git a/branches/newMo/test/CMakeLists.txt b/branches/newMo/test/CMakeLists.txt index 9c9802334..09860566a 100644 --- a/branches/newMo/test/CMakeLists.txt +++ b/branches/newMo/test/CMakeLists.txt @@ -28,7 +28,10 @@ LINK_DIRECTORIES(${PARADISEO_EO_BIN_DIR}/lib) ### 3) Define your targets and link the librairies ###################################################################################### -SET (TEST_LIST t-moNeighbor) +SET (TEST_LIST + t-moNeighbor + t-moBitNeighbor + t-moBitNeighborhood) FOREACH (test ${TEST_LIST}) SET ("T_${test}_SOURCES" "${test}.cpp") diff --git a/branches/newMo/test/moTestClass.h b/branches/newMo/test/moTestClass.h index 7bcbfc9c3..1a1299ca9 100644 --- a/branches/newMo/test/moTestClass.h +++ b/branches/newMo/test/moTestClass.h @@ -7,7 +7,8 @@ typedef EO Solution; class moDummyNeighbor : public moNeighbor{ - +public: + virtual void move(Solution & _solution){} }; #endif diff --git a/branches/newMo/test/t-moBitNeighbor.cpp b/branches/newMo/test/t-moBitNeighbor.cpp new file mode 100644 index 000000000..363bc6ce6 --- /dev/null +++ b/branches/newMo/test/t-moBitNeighbor.cpp @@ -0,0 +1,54 @@ +#include + +#include +#include + +int main(){ + + std::cout << "[t-moBitNeighbor] => START" << std::endl; + + //init sol + eoBit sol; + sol.push_back(true); + sol.push_back(false); + sol.push_back(true); + + //verif du constructeur vide + moBitNeighbor test1; + assert(test1.index()==0); + + //verif du constructeur indiquant le bit + moBitNeighbor hop(34); + assert(hop.index()==34); + + //verif du setter d'index et du constructeur de copy + test1.index(6); + test1.fitness(2); + moBitNeighbor test2(test1); + assert(test2.index()==6); + assert(test2.fitness()==2); + + //verif du getter + assert(test1.index()==6); + + //verif de l'operateur= + test1.fitness(8); + test1.index(2); + test2=test1; + assert(test2.fitness()==8); + assert(test2.index()==2); + + //verif de move + test2.move(sol); + assert(!sol[2]); + + //verif de moveBack + test2.moveBack(sol); + assert(sol[2]); + + test1.printOn(std::cout); + test2.printOn(std::cout); + + std::cout << "[t-moBitNeighbor] => OK" << std::endl; + return EXIT_SUCCESS; +} diff --git a/branches/newMo/test/t-moBitNeighborhood.cpp b/branches/newMo/test/t-moBitNeighborhood.cpp new file mode 100644 index 000000000..ff6282c39 --- /dev/null +++ b/branches/newMo/test/t-moBitNeighborhood.cpp @@ -0,0 +1,43 @@ +#include + +#include +#include + +int main(){ + + std::cout << "[t-moBitNeighborhood] => START" << std::endl; + + //init sol + eoBit sol; + sol.push_back(true); + sol.push_back(false); + sol.push_back(true); + + moBitNeighbor neighbor; + + //verif du constructeur vide + moBitNeighborhood > test; + assert(test.position()==0); + + //verif du hasneighbor + assert(test.hasNeighbor(sol)); + + //verif de init + test.init(sol, neighbor); + assert(neighbor.index()==0); + assert(test.position()==0); + + //verif du next + test.next(sol, neighbor); + assert(neighbor.index()==1); + assert(test.position()==1); + + //verif du cont + test.next(sol, neighbor); + assert(test.cont(sol)); + test.next(sol, neighbor); + assert(!test.cont(sol)); + + std::cout << "[t-moBitNeighborhood] => OK" << std::endl; + return EXIT_SUCCESS; +} diff --git a/branches/newMo/test/t-moNeighbor.cpp b/branches/newMo/test/t-moNeighbor.cpp index c504ffa66..8f2e69961 100644 --- a/branches/newMo/test/t-moNeighbor.cpp +++ b/branches/newMo/test/t-moNeighbor.cpp @@ -5,6 +5,24 @@ int main(){ + std::cout << "[t-moNeighbor] => START" << std::endl; + + moDummyNeighbor test1, test2; + + test1.fitness(3); + test2=test1; + + assert(test1.fitness()==test2.fitness()); + + moDummyNeighbor test3(test1); + + assert(test1.fitness()==test3.fitness()); + + test1.printOn(std::cout); + test2.printOn(std::cout); + test3.printOn(std::cout); + + std::cout << "[t-moNeighbor] => OK" << std::endl; return EXIT_SUCCESS; }