diff --git a/mo/src/neighborhood/moIndexNeighbor.h b/mo/src/neighborhood/moIndexNeighbor.h index 52c9ea1b0..5ce17cdda 100644 --- a/mo/src/neighborhood/moIndexNeighbor.h +++ b/mo/src/neighborhood/moIndexNeighbor.h @@ -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 diff --git a/mo/src/neighborhood/moOrderNeighborhood.h b/mo/src/neighborhood/moOrderNeighborhood.h index 7561d143b..384d0dda7 100644 --- a/mo/src/neighborhood/moOrderNeighborhood.h +++ b/mo/src/neighborhood/moOrderNeighborhood.h @@ -49,7 +49,14 @@ public: */ typedef typename Neighbor::EOT EOT; - using moIndexNeighborhood::neighborhoodSize; + using moIndexNeighborhood::getNeighborhoodSize; + + /** + * Empty constructor + */ + moOrderNeighborhood() : + moIndexNeighborhood(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; } diff --git a/mo/src/neighborhood/moRndWithReplNeighborhood.h b/mo/src/neighborhood/moRndWithReplNeighborhood.h index 16e9b0211..126e56af5 100644 --- a/mo/src/neighborhood/moRndWithReplNeighborhood.h +++ b/mo/src/neighborhood/moRndWithReplNeighborhood.h @@ -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)); } /** diff --git a/mo/src/neighborhood/moRndWithoutReplNeighborhood.h b/mo/src/neighborhood/moRndWithoutReplNeighborhood.h index 8c66ee3f9..52113cc07 100644 --- a/mo/src/neighborhood/moRndWithoutReplNeighborhood.h +++ b/mo/src/neighborhood/moRndWithoutReplNeighborhood.h @@ -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; diff --git a/mo/src/problems/bitString/moBitNeighbor.h b/mo/src/problems/bitString/moBitNeighbor.h index f112fe180..28cda0093 100644 --- a/mo/src/problems/bitString/moBitNeighbor.h +++ b/mo/src/problems/bitString/moBitNeighbor.h @@ -57,7 +57,7 @@ public: */ virtual void move(EOT & _solution) { _solution[key] = !_solution[key]; - _solution.invalidate(); + _solution.invalidate(); // only there for eoEvalFuncCounter ! } /** diff --git a/mo/src/problems/permutation/moIndexedSwapNeighbor.h b/mo/src/problems/permutation/moIndexedSwapNeighbor.h index 0b503ca35..cff62ffd7 100644 --- a/mo/src/problems/permutation/moIndexedSwapNeighbor.h +++ b/mo/src/problems/permutation/moIndexedSwapNeighbor.h @@ -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 indices; };