Test des Neighborhoods ajouté

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1665 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
jhumeau 2010-01-21 15:29:03 +00:00
commit 88afa96663
8 changed files with 164 additions and 14 deletions

View file

@ -49,18 +49,15 @@ public:
using moNeighbor<eoBit<Fitness>, Fitness>::fitness;
// describe the neighbor
unsigned bit ;
/**
* Default Constructor
*/
moBitNeighbor() : moBackableNeighbor<eoBit<Fitness> , Fitness>() { } ;
moBitNeighbor() : moBackableNeighbor<eoBit<Fitness> , Fitness>(), bit(0) { } ;
/**
* Copy Constructor
*/
moBitNeighbor(const moBitNeighbor& _n) : moNeighbor<eoBit<Fitness> , Fitness>(_n) {
moBitNeighbor(const moBitNeighbor& _n) : moBackableNeighbor<eoBit<Fitness> , Fitness>(_n) {
this->bit = _n.bit ;
} ;
@ -68,7 +65,7 @@ public:
* Constructor
* @param _b index
*/
moBitNeighbor(unsigned int _b) : moNeighbor<eoBit<Fitness> , Fitness>() , bit(_b) { } ;
moBitNeighbor(unsigned int _b) : moBackableNeighbor<eoBit<Fitness> , 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 ;
};

View file

@ -36,6 +36,7 @@
#define _bitNeighborhood_h
#include <neighborhood/moNeighborhood.h>
#include <neighborhood/moBitNeighbor.h>
/**
* Neighborhood related to a vector of Bit
@ -50,7 +51,7 @@ public:
/**
* Default Constructor
*/
moBitNeighborhood() : moNeighborhood<Neighbor>() { }
moBitNeighborhood() : moNeighborhood<Neighbor>(), 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;

View file

@ -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:

View file

@ -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")

View file

@ -7,7 +7,8 @@
typedef EO<int> Solution;
class moDummyNeighbor : public moNeighbor<Solution,int>{
public:
virtual void move(Solution & _solution){}
};
#endif

View file

@ -0,0 +1,54 @@
#include <neighborhood/moBitNeighbor.h>
#include <cstdlib>
#include <cassert>
int main(){
std::cout << "[t-moBitNeighbor] => START" << std::endl;
//init sol
eoBit<int> sol;
sol.push_back(true);
sol.push_back(false);
sol.push_back(true);
//verif du constructeur vide
moBitNeighbor<int> test1;
assert(test1.index()==0);
//verif du constructeur indiquant le bit
moBitNeighbor<int> hop(34);
assert(hop.index()==34);
//verif du setter d'index et du constructeur de copy
test1.index(6);
test1.fitness(2);
moBitNeighbor<int> 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;
}

View file

@ -0,0 +1,43 @@
#include <neighborhood/moBitNeighborhood.h>
#include <cstdlib>
#include <cassert>
int main(){
std::cout << "[t-moBitNeighborhood] => START" << std::endl;
//init sol
eoBit<int> sol;
sol.push_back(true);
sol.push_back(false);
sol.push_back(true);
moBitNeighbor<int> neighbor;
//verif du constructeur vide
moBitNeighborhood<moBitNeighbor<int> > 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;
}

View file

@ -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;
}