Lesson 2 added

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1738 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
jhumeau 2010-04-09 15:39:30 +00:00
commit d9674919b3
7 changed files with 614 additions and 1 deletions

View file

@ -0,0 +1,115 @@
/*
<moShiftNeighbor.h>
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010
Sébastien Verel, Arnaud Liefooghe, Jérémie 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,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
therefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.
ParadisEO WebSite : http://paradiseo.gforge.inria.fr
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef _moShiftNeighbor_h
#define _moShiftNeighbor_h
#include <neighborhood/moIndexNeighbor.h>
/**
* Indexed Shift Neighbor
*/
template <class EOT>
class moShiftNeighbor: public moIndexNeighbor<EOT>
{
public:
using moIndexNeighbor<EOT>::key;
/**
* Apply move on a solution regarding a key
* @param _sol 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;
}
_sol.invalidate();
}
/**
* fix two indexes regarding a key
* @param _key the key allowing to compute the two indexes for the shift
*/
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++;
}
second = first + 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++;
}
first = second + val + 1;
}
}
void print(){
std::cout << key << ": [" << first << ", " << second << "] -> " << (*this).fitness() << std::endl;
}
private:
unsigned int first;
unsigned int second;
unsigned int size;
};
#endif

View file

@ -0,0 +1,90 @@
/*
<moSwapNeighbor.h>
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010
Sébastien Verel, Arnaud Liefooghe, Jérémie 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,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
therefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.
ParadisEO WebSite : http://paradiseo.gforge.inria.fr
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef _moSwapNeighbor_h
#define _moSwapNeighbor_h
#include <neighborhood/moBackableNeighbor.h>
/**
* Swap Neighbor
*/
template <class EOT>
class moSwapNeighbor: public moBackableNeighbor<EOT>
{
public:
/**
* Apply the swap
* @param _solution the solution to move
*/
virtual void move(EOT& _solution){
unsigned int tmp;
tmp=_solution[indices.first];
_solution[indices.first]=_solution[indices.second];
_solution[indices.second]=tmp;
_solution.invalidate();
}
/**
* apply the swap to restore the solution (use by moFullEvalByModif)
* @param _solution the solution to move
*/
virtual void moveBack(EOT& _solution){
unsigned int tmp;
tmp=_solution[indices.first];
_solution[indices.first]=_solution[indices.second];
_solution[indices.second]=tmp;
_solution.invalidate();
}
/**
* Setter to fix the two indexes to swap
* @param _first first index
* @param _second second index
*/
void setIndices(unsigned int _first, unsigned int _second){
indices.first = _first;
indices.second = _second;
}
/**
* Print the Neighbor
*/
void print(){
std::cout << "[" << indices.first << ", " << indices.second << "] -> " << (*this).fitness() << std::endl;
}
private:
std::pair<unsigned int, unsigned int> indices;
};
#endif

View file

@ -0,0 +1,101 @@
/*
<moSwapNeighborhood.h>
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010
Sébastien Verel, Arnaud Liefooghe, Jérémie 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,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
therefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.
ParadisEO WebSite : http://paradiseo.gforge.inria.fr
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef _moSwapNeighborhood_h
#define _moSwapNeighborhood_h
#include <problems/permutation/moSwapNeighbor.h>
#include <neighborhood/moNeighborhood.h>
/**
* Swap Neighborhood
*/
template <class EOT>
class moSwapNeighborhood : public moNeighborhood<moSwapNeighbor<EOT> >
{
public:
typedef moSwapNeighbor<EOT> Neighbor;
/**
* @return if there are available Neighbor
*/
virtual bool hasNeighbor(EOT& _solution){
return (_solution.size() > 1);
};
/**
* Initialization of the neighborhood
* @param _solution the solution to explore
* @param _current the first neighbor
*/
virtual void init(EOT& _solution, Neighbor& _current){
indices.first=0;
indices.second=1;
size=_solution.size();
_current.setIndices(0,1);
}
/**
* Give the next neighbor
* @param _solution the solution to explore
* @param _current the next neighbor
*/
virtual void next(EOT& _solution, Neighbor& _current){
if(indices.second==size-1){
indices.first++;
indices.second=indices.first+1;
}
else
indices.second++;
_current.setIndices(indices.first, indices.second);
}
/**
* Test if there is again a neighbor
* @param _solution the solution to explore
* @return if there is again a neighbor not explored
*/
virtual bool cont(EOT& _solution){
return !((indices.first == (size-2)) && (indices.second == (size-1)));
}
/**
* Return the class id.
* @return the class name as a std::string
*/
virtual std::string className() const {
return "moSwapNeighborhood";
}
private:
std::pair<unsigned int, unsigned int> indices;
unsigned int size;
};
#endif