Modif sur les continuator + ILS

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1781 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
jhumeau 2010-05-05 09:44:21 +00:00
commit f580837dde
8 changed files with 114 additions and 85 deletions

View file

@ -40,6 +40,7 @@ Contact: paradiseo-help@lists.gforge.inria.fr
#include <eval/moEval.h>
#include <eoEvalFunc.h>
/**
* Iterated Local Search
*/
@ -52,7 +53,7 @@ public:
typedef moNeighborhood<Neighbor> Neighborhood ;
/**
* Simple constructor for Iterated Local Search
* Basic constructor for Iterated Local Search
* @param _ls the local search to iterates
* @param _fullEval the full evaluation function
* @param _op the operator used to perturb solution
@ -70,7 +71,7 @@ public:
* @param _ls the local search to iterates
* @param _fullEval the full evaluation function
* @param _op the operator used to perturb solution
* @param _nbIteration the time limit for search
* @param _cont a continuator
*/
moILS(moLocalSearch<Neighbor>& _ls, eoEvalFunc<EOT>& _fullEval, eoMonOp<EOT>& _op, moContinuator<moDummyNeighbor<EOT> >& _cont):
moLocalSearch<moDummyNeighbor<EOT> >(explorer, _cont, _fullEval),
@ -79,7 +80,28 @@ public:
explorer(_ls, defaultPerturb, defaultAccept)
{}
/**
* General constructor for Iterated Local Search
* @param _ls the local search to iterates
* @param _fullEval the full evaluation function
* @param _op the operator used to perturb solution
* @param _cont a continuator
* @param _perturb a perturbation operator
* @param _accept a acceptance criteria
*/
moILS(moLocalSearch<Neighbor>& _ls, eoEvalFunc<EOT>& _fullEval, eoMonOp<EOT>& _op, moContinuator<moDummyNeighbor<EOT> >& _cont, moMonOpPerturb<Neighbor>& _perturb, moAcceptanceCriterion<Neighbor>& _accept):
moLocalSearch<moDummyNeighbor<EOT> >(explorer, _cont, _fullEval),
iterCont(0),
defaultPerturb(dummyOp, _fullEval),
explorer(_ls, _perturb, _accept)
{}
private:
class dummmyMonOp: public eoMonOp<EOT>{
public:
bool operator()(EOT&){return false;}
}dummyOp;
moIterContinuator<moDummyNeighbor<EOT> > iterCont;
moMonOpPerturb<Neighbor> defaultPerturb;
moAlwaysAcceptCrit<Neighbor> defaultAccept;

View file

@ -72,7 +72,7 @@ public:
// initialization of the external continuator (for example the time, or the number of generations)
cont->init(_solution);
bool b= (*cont)(_solution);
bool b;
do {
// explore the neighborhood of the solution

View file

@ -96,6 +96,13 @@ public :
* @param _sol the corresponding solution
*/
virtual void init(EOT& _sol) {
for (unsigned i = 0; i < stats.size(); ++i)
stats[i]->init(_sol);
counter=1;
for (unsigned int i = 0; i < monitors.size(); ++i)
(*monitors[i])();
for (unsigned i = 0; i < continuators.size(); ++i)
continuators[i]->init(_sol);
}

View file

@ -51,8 +51,13 @@ public:
*@return true if counter < maxIter
*/
virtual bool operator()(EOT & _solution) {
bool res;
cpt++;
return (cpt < maxIter);
res = (cpt < maxIter);
if(!res){
std::cout << "STOP in moIterContinuator: Reached maximum number of iterations [" << cpt << "/" << maxIter << "]" << std::endl;
}
return res;
}
/**

View file

@ -54,6 +54,11 @@ public:
*/
virtual void lastCall(EOT &) {}
/**
* first call of a statistical operator
*/
virtual void init(EOT &){}
/**
* @return name of the class
*/

View file

@ -66,6 +66,14 @@ public:
return true;
}
/**
* reset the start time
* @param _solution a solution
*/
virtual void init(EOT & _solution) {
start = time(NULL);
}
/**
* Class name

View file

@ -34,7 +34,6 @@ using namespace std;
//Neighbors and Neighborhoods
#include <problems/permutation/moShiftNeighbor.h>
#include <neighborhood/moRndWithReplNeighborhood.h>
#include <neighborhood/moOrderNeighborhood.h>
//Algorithm and its components

View file

@ -2,6 +2,7 @@
/** testILS.cpp
*
* SV - 12/01/10
* JH - 04/05/10
*
*/
//-----------------------------------------------------------------------------
@ -23,38 +24,44 @@
using namespace std;
//-----------------------------------------------------------------------------
// fitness function
#include <eval/oneMaxEval.h>
#include <problems/bitString/moBitNeighbor.h>
//Representation and initializer
#include <eoInt.h>
#include <neighborhood/moOrderNeighborhood.h>
#include <neighborhood/moRndWithoutReplNeighborhood.h>
#include <neighborhood/moDummyNeighbor.h>
#include <eoInit.h>
#include <eoScalarFitness.h>
// fitness function
#include <eval/queenEval.h>
#include <eval/moFullEvalByModif.h>
#include <eval/moFullEvalByCopy.h>
#include <comparator/moNeighborComparator.h>
#include <comparator/moSolNeighborComparator.h>
#include <continuator/moTrueContinuator.h>
#include <algo/moLocalSearch.h>
#include <explorer/moSimpleHCexplorer.h>
#include <explorer/moILSexplorer.h>
//Neighbors and Neighborhoods
#include <problems/permutation/moShiftNeighbor.h>
#include <neighborhood/moOrderNeighborhood.h>
//Mutation
#include <eoSwapMutation.h>
//Algorithm and its components
#include <algo/moTS.h>
#include <algo/moILS.h>
//mo eval
#include <eval/moFullEvalByCopy.h>
#include <perturb/moMonOpPerturb.h>
#include <perturb/moRestartPerturb.h>
#include <perturb/moNeighborhoodPerturb.h>
#include <acceptCrit/moAlwaysAcceptCrit.h>
#include <acceptCrit/moBetterAcceptCrit.h>
#include <continuator/moIterContinuator.h>
// REPRESENTATION
//-----------------------------------------------------------------------------
typedef eoBit<unsigned int> Indi;
typedef moBitNeighbor<unsigned int> Neighbor ; // incremental evaluation
typedef moOrderNeighborhood<Neighbor> Neighborhood ;
typedef moRndWithoutReplNeighborhood<Neighbor> Neighborhood2 ;
typedef moSimpleHCexplorer<Neighborhood> NHE;
typedef eoInt<eoMinimizingFitness> Queen; //Permutation (Queen's problem representation)
typedef moShiftNeighbor<Queen> shiftNeighbor; //shift Neighbor
typedef moOrderNeighborhood<shiftNeighbor> orderShiftNeighborhood; //order shift Neighborhood (Indexed)
void main_function(int argc, char **argv)
{
@ -108,46 +115,49 @@ void main_function(int argc, char **argv)
/* =========================================================
*
* Eval fitness function
* Full evaluation fitness function
*
* ========================================================= */
oneMaxEval<Indi> eval;
queenEval<Queen> fullEval;
//FuncNK<Indi> eval(vecSize, 2);
/* =========================================================
*
* Initilisation of the solution
* Initializer of a solution
*
* ========================================================= */
// a Indi random initializer
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
eoInitPermutation<Queen> init(vecSize);
/* =========================================================
*
* Declare and init solutions
*
* ========================================================= */
Queen sol1;
Queen sol2;
Queen sol3;
//random initialization
init(sol1);
init(sol2);
init(sol3);
//evaluation
fullEval(sol1);
fullEval(sol2);
fullEval(sol3);
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
moFullEvalByModif<Neighbor> fulleval(eval);
//An eval by copy can be used instead of the eval by modif
//moFullEvalByCopy<Neighbor> fulleval(eval);
/* =========================================================
*
* Comparator of neighbors
*
* ========================================================= */
moNeighborComparator<Neighbor> comparator;
moSolNeighborComparator<Neighbor> solComparator;
moFullEvalByCopy<shiftNeighbor> shiftEval(fullEval);
/* =========================================================
*
@ -155,67 +165,40 @@ void main_function(int argc, char **argv)
*
* ========================================================= */
Neighborhood neighborhood(vecSize);
Neighborhood2 neighborhood2(vecSize);
orderShiftNeighborhood orderShiftNH(pow(vecSize-1, 2));
/* =========================================================
*
* a neighborhood explorer solution
* the local search algorithms
*
* ========================================================= */
moSimpleHCexplorer<Neighbor> explorer(neighborhood, fulleval, comparator, solComparator);
//Basic Constructor of the Tabu Search
moTS<shiftNeighbor> ts(orderShiftNH, fullEval, shiftEval, 5, 7);
eoSwapMutation<Queen> mut;
//Basic Constructor of the Iterated Local Search
moILS<shiftNeighbor> localSearch1(ts, fullEval, mut, 3);
/* =========================================================
*
* the local search algorithm
*
* ========================================================= */
//Simple Constructor of the Iterated Local Search
//Be carefull, template of the continuator must be a dummyNeighbor!!!
moIterContinuator<moDummyNeighbor<Queen> > cont(4);
moILS<shiftNeighbor> localSearch2(ts, fullEval, mut, cont);
moTrueContinuator<Neighbor> continuator;//always continue
std::cout << "Iterated Local Search 1:" << std::endl;
std::cout << "--------------" << std::endl;
std::cout << "initial: " << sol1 << std::endl ;
localSearch1(sol1);
std::cout << "final: " << sol1 << std::endl << std::endl;
moLocalSearch< Neighbor > hc(explorer, continuator, eval);
eoBitMutation<Indi> monOp(1.0/vecSize);
moMonOpPerturb<Neighbor> perturb(monOp, eval);
//moRestartPerturb<Neighbor> perturb(random, eval, 5);
//moNeighborhoodPerturb<Neighbor, Neighborhood2> perturb(neighborhood2, fulleval);
moSolComparator<Indi> comp;
//moAlwaysAcceptCrit<Neighbor> accept;
moBetterAcceptCrit<Neighbor> accept(comp);
moILSexplorer< Neighbor > explorerILS(hc, perturb, accept);
moIterContinuator<moDummyNeighbor<Indi> > continuatorILS(100);
moLocalSearch<moDummyNeighbor<Indi> >localSearch(explorerILS, continuatorILS, eval);
/* =========================================================
*
* execute the local search from random sollution
*
* ========================================================= */
Indi solution;
random(solution);
//Can be eval here, else it will be done at the beginning of the localSearch
//eval(solution);
std::cout << "initial: " << solution << std::endl ;
localSearch(solution);
std::cout << "final: " << solution << std::endl ;
std::cout << "Iterated Local Search 2:" << std::endl;
std::cout << "--------------" << std::endl;
std::cout << "initial: " << sol2 << std::endl ;
localSearch2(sol2);
std::cout << "final: " << sol2 << std::endl << std::endl;
}