changed the moShiftNeighor
This commit is contained in:
parent
12ee2c5471
commit
1ef2a89dbf
1 changed files with 163 additions and 91 deletions
|
|
@ -2,7 +2,7 @@
|
||||||
<moShiftNeighbor.h>
|
<moShiftNeighbor.h>
|
||||||
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010
|
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
|
This software is governed by the CeCILL license under French law and
|
||||||
abiding by the rules of distribution of free software. You can ue,
|
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
|
#ifndef _moShiftNeighbor_h
|
||||||
#define _moShiftNeighbor_h
|
#define _moShiftNeighbor_h
|
||||||
|
|
||||||
|
#include <neighborhood/moBackableNeighbor.h>
|
||||||
#include <neighborhood/moIndexNeighbor.h>
|
#include <neighborhood/moIndexNeighbor.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indexed Shift Neighbor
|
* Indexed Shift Neighbor
|
||||||
|
* Other name : insertion operator
|
||||||
*/
|
*/
|
||||||
template <class EOT, class Fitness=typename EOT::Fitness>
|
template <class EOT, class Fitness=typename EOT::Fitness>
|
||||||
class moShiftNeighbor: public moIndexNeighbor<EOT, Fitness>
|
class moShiftNeighbor: public moBackableNeighbor<EOT, Fitness>, public moIndexNeighbor<EOT>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using moBackableNeighbor<EOT>::fitness;
|
||||||
using moIndexNeighbor<EOT, Fitness>::key;
|
using moIndexNeighbor<EOT, Fitness>::key;
|
||||||
|
using moIndexNeighbor<EOT, Fitness>::index;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply move on a solution regarding a key
|
* 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) {
|
virtual void move(EOT & _solution) {
|
||||||
unsigned int tmp ;
|
insertion(_solution, indices.first, indices.second);
|
||||||
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;
|
|
||||||
}
|
|
||||||
_sol.invalidate();
|
_sol.invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* fix two indexes regarding a key
|
* apply the correct insertion to restore the solution (use by moFullEvalByModif)
|
||||||
* @param _key the key allowing to compute the two indexes for the shift
|
* @param _solution the solution to move back
|
||||||
*/
|
*/
|
||||||
void translate(unsigned int _key) {
|
virtual void moveBack(EOT& _solution) {
|
||||||
int step;
|
if (indices.first < indices.second)
|
||||||
int val = _key;
|
insertion(_solution, indices.second - 1, indices.first);
|
||||||
int tmpSize = size * (size-1) / 2;
|
else
|
||||||
// moves from left to right
|
insertion(_solution, indices.second, indices.first + 1);
|
||||||
if (val <= tmpSize) {
|
|
||||||
step = size - 1;
|
|
||||||
first = 0;
|
|
||||||
while ((val - step) > 0) {
|
|
||||||
val = val - step;
|
|
||||||
step--;
|
|
||||||
first++;
|
|
||||||
}
|
}
|
||||||
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;
|
* Setter to fix the two indexes to swap
|
||||||
step = size - 2;
|
* @param _solution solution from which the neighborhood is visited
|
||||||
second = 0;
|
* @param _first first index
|
||||||
while ((val - step) > 0) {
|
* @param _second second index
|
||||||
val = val - step;
|
*/
|
||||||
step--;
|
void set(EOT & _solution, unsigned int _first, unsigned int _second) {
|
||||||
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() {
|
void print() {
|
||||||
std::cout << key << ": [" << first << ", " << second << "] -> " << (*this).fitness() << std::endl;
|
std::cout << key << ": [" << indices.first << ", " << indices.second << "] -> " << (*this).fitness() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned int first;
|
std::pair<unsigned int, unsigned int> indices;
|
||||||
unsigned int second;
|
|
||||||
unsigned int size;
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue