changed the moShiftNeighor

This commit is contained in:
verel 2013-02-07 12:05:54 +01:00
commit 1ef2a89dbf

View file

@ -2,7 +2,7 @@
<moShiftNeighbor.h>
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010
Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau
Sebastien Verel, Arnaud Liefooghe, Jeremie 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,
@ -30,85 +30,157 @@ Contact: paradiseo-help@lists.gforge.inria.fr
#ifndef _moShiftNeighbor_h
#define _moShiftNeighbor_h
#include <neighborhood/moBackableNeighbor.h>
#include <neighborhood/moIndexNeighbor.h>
/**
* Indexed Shift Neighbor
* Other name : insertion operator
*/
template <class EOT, class Fitness=typename EOT::Fitness>
class moShiftNeighbor: public moIndexNeighbor<EOT, Fitness>
class moShiftNeighbor: public moBackableNeighbor<EOT, Fitness>, public moIndexNeighbor<EOT>
{
public:
using moBackableNeighbor<EOT>::fitness;
using moIndexNeighbor<EOT, Fitness>::key;
using moIndexNeighbor<EOT, Fitness>::index;
/**
* Apply move on a solution regarding a key
* @param _sol the solution to move
* @param _solution the solution to move
*/
virtual void move(EOT & _sol) {
unsigned int tmp ;
size=_sol.size();
translate(key+1);
// keep the first component to change
tmp = _sol[first];
// shift
if (first < second) {
for (unsigned int i=first; i<second-1; i++)
_sol[i] = _sol[i+1];
// shift the first component
_sol[second-1] = tmp;
}
else { /* first > second*/
for (unsigned int i=first; i>second; i--)
_sol[i] = _sol[i-1];
// shift the first component
_sol[second] = tmp;
}
virtual void move(EOT & _solution) {
insertion(_solution, indices.first, indices.second);
_sol.invalidate();
}
/**
* fix two indexes regarding a key
* @param _key the key allowing to compute the two indexes for the shift
* apply the correct insertion to restore the solution (use by moFullEvalByModif)
* @param _solution the solution to move back
*/
void translate(unsigned int _key) {
int step;
int val = _key;
int tmpSize = size * (size-1) / 2;
// moves from left to right
if (val <= tmpSize) {
step = size - 1;
first = 0;
while ((val - step) > 0) {
val = val - step;
step--;
first++;
virtual void moveBack(EOT& _solution) {
if (indices.first < indices.second)
insertion(_solution, indices.second - 1, indices.first);
else
insertion(_solution, indices.second, indices.first + 1);
}
second = first + val + 1;
/**
* 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 );
indices.first = _key % _solution.size() ;
indices.second = _key / _solution.size() ;
if (indices.first <= indices.second)
indices.second += 2;
// =============== To kill :
// int step;
// int val = _key;
// int tmpSize = _solution.size() * (_solution.size() - 1) / 2;
// // moves from left to right
// if (val <= tmpSize) {
// step = _solution.size() - 1;
// indices.first = 0;
// while ((val - step) > 0) {
// val = val - step;
// step--;
// indices.first++;
// }
// indices.second = indices.first + val + 1;
// }
// // moves from right to left (equivalent moves are avoided)
// else { /* val > tmpSize */
// val = val - tmpSize;
// step = _solution.size() - 2;
// indices.second = 0;
// while ((val - step) > 0) {
// val = val - step;
// step--;
// indices.second++;
// }
// indices.first = indices.second + val + 1;
// }
}
// moves from right to left (equivalent moves are avoided)
else { /* val > tmpSize */
val = val - tmpSize;
step = size - 2;
second = 0;
while ((val - step) > 0) {
val = val - step;
step--;
second++;
/**
* Setter to fix the two indexes to swap
* @param _solution solution from which the neighborhood is visited
* @param _first first index
* @param _second second index
*/
void set(EOT & _solution, unsigned int _first, unsigned int _second) {
indices.first = _first;
indices.second = _second;
// set the index
if (_first < _second) {
index( (_second - 2) * _solution.size() + _first );
} else {
index( _second * _solution.size() + _first );
}
first = second + val + 1;
}
/**
* Getter of the firt location
* @return first indice
*/
unsigned int first() {
return indices.first;
}
/**
* Getter of the second location
* @return second indice
*/
unsigned int second() {
return indices.second;
}
void print() {
std::cout << key << ": [" << first << ", " << second << "] -> " << (*this).fitness() << std::endl;
std::cout << key << ": [" << indices.first << ", " << indices.second << "] -> " << (*this).fitness() << std::endl;
}
private:
unsigned int first;
unsigned int second;
unsigned int size;
std::pair<unsigned int, unsigned int> indices;
/**
* Apply insertion move on a solution regarding a key
* @param _sol the solution to move
* @param _first first position
* @param _second second position
*/
void insertion(EOT & _sol, unsigned int _first, unsigned int _second) {
unsigned int tmp ;
// keep the first component to change
tmp = _sol[_first];
// shift
if (_first < _second) {
for (unsigned int i = _first; i < _second - 1; i++)
_sol[i] = _sol[i+1];
// shift the first component
_sol[_second-1] = tmp;
}
else { /* first > second*/
for (unsigned int i = _first; i > _second; i--)
_sol[i] = _sol[i-1];
// shift the first component
_sol[_second] = tmp;
}
}
};