add method index(sol, index) to moIndexNeighbor

This commit is contained in:
verel 2012-10-24 23:07:05 +02:00
commit 72ffb89999
6 changed files with 70 additions and 34 deletions

View file

@ -86,17 +86,32 @@ public:
* Getter
* @return index of the IndexNeighbor
*/
unsigned int index() {
inline unsigned int index() const {
return key;
}
/**
* Setter
* Setter :
* Only set the index which not depends on the current solution
*
* @param _key index of the IndexNeighbor
*/
void index(unsigned int _key) {
key = _key;
void index(unsigned int _key) {
key = _key;
}
/**
* Setter
* The "parameters" of the neighbor is a function of key and the current solution
* for example, for variable length solution
*
* @param _solution solution from which the neighborhood is visited
* @param _key index of the IndexNeighbor
*/
virtual void index(EOT & _solution, unsigned int _key) {
key = _key;
}
/**
* @param _neighbor a neighbor

View file

@ -49,7 +49,14 @@ public:
*/
typedef typename Neighbor::EOT EOT;
using moIndexNeighborhood<Neighbor>::neighborhoodSize;
using moIndexNeighborhood<Neighbor>::getNeighborhoodSize;
/**
* Empty constructor
*/
moOrderNeighborhood() :
moIndexNeighborhood<Neighbor>(0), currentIndex(0) {
}
/**
* Constructor
@ -65,7 +72,7 @@ public:
* @return true if the neighborhood was not empty
*/
virtual bool hasNeighbor(EOT& _solution) {
return neighborhoodSize > 0;
return getNeighborhoodSize() > 0;
}
/**
@ -75,7 +82,7 @@ public:
*/
virtual void init(EOT & _solution, Neighbor & _neighbor) {
currentIndex = 0;
_neighbor.index(currentIndex);
_neighbor.index(_solution, currentIndex);
}
/**
@ -85,7 +92,7 @@ public:
*/
virtual void next(EOT & _solution, Neighbor & _neighbor) {
currentIndex++;
_neighbor.index(currentIndex);
_neighbor.index(_solution, currentIndex);
}
/**
@ -96,14 +103,14 @@ public:
* @return true if there is again a neighbor to explore
*/
virtual bool cont(EOT & _solution) {
return (currentIndex < neighborhoodSize - 1);
return (currentIndex < getNeighborhoodSize() - 1);
}
/**
* Getter
* @return the position in the Neighborhood
*/
unsigned int position() {
inline unsigned int position() const {
return currentIndex;
}

View file

@ -75,7 +75,7 @@ public:
* @param _neighbor the first neighbor
*/
virtual void init(EOT & _solution, Neighbor & _neighbor) {
_neighbor.index(rng.random(neighborhoodSize));
_neighbor.index(_solution, rng.random(neighborhoodSize));
}
/**
@ -84,7 +84,7 @@ public:
* @param _neighbor the next neighbor
*/
virtual void next(EOT & _solution, Neighbor & _neighbor) {
_neighbor.index(rng.random(neighborhoodSize));
_neighbor.index(_solution, rng.random(neighborhoodSize));
}
/**

View file

@ -82,7 +82,7 @@ public:
unsigned int i, tmp;
maxIndex = neighborhoodSize ;
i = rng.random(maxIndex);
_neighbor.index(indexVector[i]);
_neighbor.index(_solution, indexVector[i]);
tmp=indexVector[i];
indexVector[i]=indexVector[maxIndex-1];
indexVector[maxIndex-1]=tmp;
@ -97,7 +97,7 @@ public:
virtual void next(EOT & _solution, Neighbor & _neighbor) {
unsigned int i, tmp;
i = rng.random(maxIndex);
_neighbor.index(indexVector[i]);
_neighbor.index(_solution, indexVector[i]);
tmp=indexVector[i];
indexVector[i]=indexVector[maxIndex-1];
indexVector[maxIndex-1]=tmp;

View file

@ -57,7 +57,7 @@ public:
*/
virtual void move(EOT & _solution) {
_solution[key] = !_solution[key];
_solution.invalidate();
_solution.invalidate(); // only there for eoEvalFuncCounter !
}
/**

View file

@ -49,15 +49,12 @@ public:
* @param _solution the solution to move
*/
virtual void move(EOT& _solution) {
unsigned int tmp;
unsigned i, j;
this->getIndices(_solution.size(), i, j);
tmp = _solution[i];
_solution[i] = _solution[j];
_solution[j] = tmp;
// bof utiliser plutot le template du vector : to do
EOT tmp(1);
tmp[0] = _solution[indices.first];
_solution[indices.first] = _solution[indices.second];
_solution[indices.second] = tmp[0];
_solution.invalidate();
}
@ -74,25 +71,42 @@ public:
* @param _solution the solution to move back
*/
virtual void moveBack(EOT& _solution) {
move(_solution);
move(_solution);
}
/**
* Get the two indexes of the swap
* @param N size of the permutation
* Setter
* The "parameters" of the neighbor is a function of key and the current solution
* for example, for variable length solution
*
* @param _solution solution from which the neighborhood is visited
* @param _key index of the IndexNeighbor
*/
virtual void index(EOT & _solution, unsigned int _key) {
index( _key );
unsigned int n = (unsigned int) ( (1 + sqrt(1 + 8 * _key)) / 2);
indices.first = _key - (n - 1) * n / 2;
indices.second = N - 1 - (n - 1 - _first);
}
/**
* Setter to fix the two indexes to swap
* @param _first first index
* @param _second second index
*/
void getIndices(unsigned N, unsigned int & _first, unsigned int & _second) {
unsigned int n = (unsigned int) ( (1 + sqrt(1 + 8 * key)) / 2);
void set(unsigned int _first, unsigned int _second) {
indices.first = _first;
indices.second = _second;
_first = key - (n - 1) * n / 2;
_second = N - 1 - (n - 1 - _first);
// set the index
unsigned n = _solution.nbClientsWithin() + _first - _second;
index( _first + n * (n - 1) / 2 );
}
private:
std::pair<unsigned int, unsigned int> indices;
};