Lesson 2 added
git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1738 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
parent
ecbc80f577
commit
d9674919b3
7 changed files with 614 additions and 1 deletions
|
|
@ -30,8 +30,10 @@ Contact: paradiseo-help@lists.gforge.inria.fr
|
||||||
#ifndef _oneMaxFullEval_h
|
#ifndef _oneMaxFullEval_h
|
||||||
#define _oneMaxFullEval_h
|
#define _oneMaxFullEval_h
|
||||||
|
|
||||||
|
#include <eoEvalFunc.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Full evalution Function for OneMax problem
|
* Full evaluation Function for OneMax problem
|
||||||
*/
|
*/
|
||||||
template< class EOT >
|
template< class EOT >
|
||||||
class oneMaxFullEval : public eoEvalFunc<EOT>
|
class oneMaxFullEval : public eoEvalFunc<EOT>
|
||||||
|
|
|
||||||
57
trunk/paradiseo-mo/src/problems/eval/queenFullEval.h
Normal file
57
trunk/paradiseo-mo/src/problems/eval/queenFullEval.h
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
/*
|
||||||
|
<queenFullEval.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 _queenFullEval_h
|
||||||
|
#define _queenFullEval_h
|
||||||
|
|
||||||
|
#include <eoEvalFunc.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Full evaluation function for QUEEN problem
|
||||||
|
*/
|
||||||
|
template< class EOT >
|
||||||
|
class queenFullEval : public eoEvalFunc<EOT>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Count number of threat
|
||||||
|
* @param _queen a solution
|
||||||
|
*/
|
||||||
|
void operator()(EOT& _queen){
|
||||||
|
unsigned int fit=0;
|
||||||
|
for(unsigned int i=0; i<_queen.size()-1; i++)
|
||||||
|
for(unsigned int j=i+1; j< _queen.size(); j++)
|
||||||
|
if((_queen[i]+j-i == _queen[j]) || (_queen[i]+i-j == _queen[j]))
|
||||||
|
fit++;
|
||||||
|
_queen.fitness(fit);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
115
trunk/paradiseo-mo/src/problems/permutation/moShiftNeighbor.h
Normal file
115
trunk/paradiseo-mo/src/problems/permutation/moShiftNeighbor.h
Normal 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
|
||||||
90
trunk/paradiseo-mo/src/problems/permutation/moSwapNeighbor.h
Normal file
90
trunk/paradiseo-mo/src/problems/permutation/moSwapNeighbor.h
Normal 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
|
||||||
101
trunk/paradiseo-mo/src/problems/permutation/moSwapNeighborhood.h
Normal file
101
trunk/paradiseo-mo/src/problems/permutation/moSwapNeighborhood.h
Normal 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
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
INCLUDE_DIRECTORIES(${EO_SRC_DIR}/src
|
||||||
|
${MO_SRC_DIR}/src
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/../src)
|
||||||
|
|
||||||
|
LINK_DIRECTORIES(${EO_BIN_DIR}/lib)
|
||||||
|
|
||||||
|
ADD_EXECUTABLE(testNeighborhood testNeighborhood.cpp)
|
||||||
|
|
||||||
|
TARGET_LINK_LIBRARIES(testNeighborhood eoutils ga eo)
|
||||||
239
trunk/paradiseo-mo/tutorial/Lesson2/testNeighborhood.cpp
Normal file
239
trunk/paradiseo-mo/tutorial/Lesson2/testNeighborhood.cpp
Normal file
|
|
@ -0,0 +1,239 @@
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
/** testNeighborhood.cpp
|
||||||
|
*
|
||||||
|
* JH - 09/04/10
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// standard includes
|
||||||
|
#define HAVE_SSTREAM
|
||||||
|
|
||||||
|
#include <stdexcept> // runtime_error
|
||||||
|
#include <iostream> // cout
|
||||||
|
#include <sstream> // ostrstream, istrstream
|
||||||
|
#include <fstream>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// the general include for eo
|
||||||
|
#include <eo>
|
||||||
|
#include <ga.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
//Representation and initializer
|
||||||
|
#include <eoInt.h>
|
||||||
|
#include <eoInit.h>
|
||||||
|
|
||||||
|
// fitness function
|
||||||
|
#include <problems/eval/queenFullEval.h>
|
||||||
|
#include <eval/moFullEvalByModif.h>
|
||||||
|
#include <eval/moFullEvalByCopy.h>
|
||||||
|
|
||||||
|
//Neighbors and Neighborhoods
|
||||||
|
#include <problems/permutation/moShiftNeighbor.h>
|
||||||
|
#include <problems/permutation/moSwapNeighbor.h>
|
||||||
|
#include <problems/permutation/moSwapNeighborhood.h>
|
||||||
|
#include <neighborhood/moRndWithReplNeighborhood.h>
|
||||||
|
#include <neighborhood/moRndWithoutReplNeighborhood.h>
|
||||||
|
#include <neighborhood/moOrderNeighborhood.h>
|
||||||
|
|
||||||
|
|
||||||
|
// Define types of the representation solution, different neighbors and neighborhoods
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
typedef eoInt<unsigned int> Queen; //Permutation (Queen's problem representation)
|
||||||
|
typedef moSwapNeighbor<Queen> swapNeighbor ; //swap Neighbor
|
||||||
|
typedef moShiftNeighbor<Queen> shiftNeighbor; //shift Neighbor
|
||||||
|
typedef moSwapNeighborhood<Queen> swapNeighborhood; //classical swap Neighborhood
|
||||||
|
typedef moOrderNeighborhood<shiftNeighbor> orderShiftNeighborhood; //order shift Neighborhood (Indexed)
|
||||||
|
typedef moRndWithoutReplNeighborhood<shiftNeighbor> rndWithoutReplShiftNeighborhood; //random without replacement shift Neighborhood (Indexed)
|
||||||
|
typedef moRndWithReplNeighborhood<shiftNeighbor> rndWithReplShiftNeighborhood; //random with replacement shift Neighborhood (Indexed)
|
||||||
|
|
||||||
|
void main_function(int argc, char **argv)
|
||||||
|
{
|
||||||
|
/* =========================================================
|
||||||
|
*
|
||||||
|
* Parameters
|
||||||
|
*
|
||||||
|
* ========================================================= */
|
||||||
|
|
||||||
|
// First define a parser from the command-line arguments
|
||||||
|
eoParser parser(argc, argv);
|
||||||
|
|
||||||
|
// For each parameter, define Parameter, read it through the parser,
|
||||||
|
// and assign the value to the variable
|
||||||
|
|
||||||
|
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
|
||||||
|
parser.processParam( seedParam );
|
||||||
|
unsigned seed = seedParam.value();
|
||||||
|
|
||||||
|
// description of genotype
|
||||||
|
eoValueParam<unsigned int> vecSizeParam(8, "vecSize", "Genotype size", 'V');
|
||||||
|
parser.processParam( vecSizeParam, "Representation" );
|
||||||
|
unsigned vecSize = vecSizeParam.value();
|
||||||
|
|
||||||
|
// the name of the "status" file where all actual parameter values will be saved
|
||||||
|
string str_status = parser.ProgramName() + ".status"; // default value
|
||||||
|
eoValueParam<string> statusParam(str_status.c_str(), "status", "Status file");
|
||||||
|
parser.processParam( statusParam, "Persistence" );
|
||||||
|
|
||||||
|
// do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED
|
||||||
|
// i.e. in case you need parameters somewhere else, postpone these
|
||||||
|
if (parser.userNeedsHelp()) {
|
||||||
|
parser.printHelp(cout);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
if (statusParam.value() != "") {
|
||||||
|
ofstream os(statusParam.value().c_str());
|
||||||
|
os << parser;// and you can use that file as parameter file
|
||||||
|
}
|
||||||
|
|
||||||
|
/* =========================================================
|
||||||
|
*
|
||||||
|
* Random seed
|
||||||
|
*
|
||||||
|
* ========================================================= */
|
||||||
|
|
||||||
|
//reproducible random seed: if you don't change SEED above,
|
||||||
|
// you'll aways get the same result, NOT a random run
|
||||||
|
rng.reseed(seed);
|
||||||
|
|
||||||
|
|
||||||
|
/* =========================================================
|
||||||
|
*
|
||||||
|
* Eval fitness function
|
||||||
|
*
|
||||||
|
* ========================================================= */
|
||||||
|
|
||||||
|
queenFullEval<Queen> fullEval;
|
||||||
|
|
||||||
|
|
||||||
|
/* =========================================================
|
||||||
|
*
|
||||||
|
* Initializer of the solution
|
||||||
|
*
|
||||||
|
* ========================================================= */
|
||||||
|
|
||||||
|
eoInitPermutation<Queen> init(vecSize);
|
||||||
|
|
||||||
|
|
||||||
|
/* =========================================================
|
||||||
|
*
|
||||||
|
* evaluation operators of a neighbor solution
|
||||||
|
*
|
||||||
|
* ========================================================= */
|
||||||
|
|
||||||
|
moFullEvalByModif<swapNeighbor> swapEval(fullEval);
|
||||||
|
moFullEvalByCopy<shiftNeighbor> shiftEval(fullEval);
|
||||||
|
|
||||||
|
|
||||||
|
/* =========================================================
|
||||||
|
*
|
||||||
|
* Neighbors and Neighborhoods
|
||||||
|
*
|
||||||
|
* ========================================================= */
|
||||||
|
|
||||||
|
swapNeighborhood swapNH;
|
||||||
|
orderShiftNeighborhood orderShiftNH(pow(vecSize-1, 2));
|
||||||
|
rndWithoutReplShiftNeighborhood rndNoReplShiftNH(pow(vecSize-1, 2));
|
||||||
|
rndWithReplShiftNeighborhood rndReplShiftNH(pow(vecSize-1, 2));
|
||||||
|
|
||||||
|
|
||||||
|
/* =========================================================
|
||||||
|
*
|
||||||
|
* Init and eval a Queen
|
||||||
|
*
|
||||||
|
* ========================================================= */
|
||||||
|
|
||||||
|
Queen solution;
|
||||||
|
|
||||||
|
init(solution);
|
||||||
|
|
||||||
|
fullEval(solution);
|
||||||
|
|
||||||
|
std::cout << "Initial Solution:" << std::endl;
|
||||||
|
std::cout << solution << std::endl << std::endl;
|
||||||
|
|
||||||
|
/* =========================================================
|
||||||
|
*
|
||||||
|
* Use classical Neighbor and Neighborhood (swap)
|
||||||
|
*
|
||||||
|
* ========================================================= */
|
||||||
|
|
||||||
|
std::cout << "SWAP NEIGHBORHOOD" << std::endl;
|
||||||
|
std::cout << "-----------------" << std::endl;
|
||||||
|
std::cout << "Neighbors List: (Neighbor -> fitness)" << std::endl;
|
||||||
|
|
||||||
|
swapNeighbor n1;
|
||||||
|
swapNH.init(solution, n1);
|
||||||
|
swapEval(solution,n1);
|
||||||
|
n1.print();
|
||||||
|
while(swapNH.cont(solution)){
|
||||||
|
swapNH.next(solution, n1);
|
||||||
|
swapEval(solution,n1);
|
||||||
|
n1.print();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* =========================================================
|
||||||
|
*
|
||||||
|
* Use indexed Neighborhood with shift operator
|
||||||
|
*
|
||||||
|
* ========================================================= */
|
||||||
|
|
||||||
|
std::cout << "\nSHIFT ORDER NEIGHBORHOOD" << std::endl;
|
||||||
|
std::cout << "------------------------" << std::endl;
|
||||||
|
std::cout << "Neighbors List: (key: Neighbor -> fitness)" << std::endl;
|
||||||
|
|
||||||
|
shiftNeighbor n2;
|
||||||
|
|
||||||
|
orderShiftNH.init(solution, n2);
|
||||||
|
shiftEval(solution,n2);
|
||||||
|
n2.print();
|
||||||
|
while(orderShiftNH.cont(solution)){
|
||||||
|
orderShiftNH.next(solution, n2);
|
||||||
|
shiftEval(solution,n2);
|
||||||
|
n2.print();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "\nSHIFT RANDOM WITHOUT REPLACEMENT NEIGHBORHOOD" << std::endl;
|
||||||
|
std::cout << "---------------------------------------------" << std::endl;
|
||||||
|
std::cout << "Neighbors List: (key: Neighbor -> fitness)" << std::endl;
|
||||||
|
|
||||||
|
rndNoReplShiftNH.init(solution, n2);
|
||||||
|
shiftEval(solution,n2);
|
||||||
|
n2.print();
|
||||||
|
while(rndNoReplShiftNH.cont(solution)){
|
||||||
|
rndNoReplShiftNH.next(solution, n2);
|
||||||
|
shiftEval(solution,n2);
|
||||||
|
n2.print();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "\nSHIFT RANDOM WITH REPLACEMENT NEIGHBORHOOD" << std::endl;
|
||||||
|
std::cout << "---------------------------------------------" << std::endl;
|
||||||
|
std::cout << "Neighbors List: (key: Neighbor -> fitness)" << std::endl;
|
||||||
|
|
||||||
|
rndReplShiftNH.init(solution, n2);
|
||||||
|
shiftEval(solution,n2);
|
||||||
|
n2.print();
|
||||||
|
for(unsigned int i=0; i<100; i++){
|
||||||
|
rndReplShiftNH.next(solution, n2);
|
||||||
|
shiftEval(solution,n2);
|
||||||
|
n2.print();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// A main that catches the exceptions
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
main_function(argc, argv);
|
||||||
|
}
|
||||||
|
catch (exception& e) {
|
||||||
|
cout << "Exception: " << e.what() << '\n';
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue