From f580837ddecfe825bc8376cc3f7c4a076aa40051 Mon Sep 17 00:00:00 2001 From: jhumeau Date: Wed, 5 May 2010 09:44:21 +0000 Subject: [PATCH] Modif sur les continuator + ILS git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1781 331e1502-861f-0410-8da2-ba01fb791d7f --- trunk/paradiseo-mo/src/algo/moILS.h | 26 ++- trunk/paradiseo-mo/src/algo/moLocalSearch.h | 2 +- .../src/continuator/moCheckpoint.h | 7 + .../src/continuator/moIterContinuator.h | 7 +- .../paradiseo-mo/src/continuator/moStatBase.h | 5 + .../src/continuator/moTimeContinuator.h | 8 + .../tutorial/Lesson4/testSimpleTS.cpp | 1 - .../paradiseo-mo/tutorial/Lesson5/testILS.cpp | 163 ++++++++---------- 8 files changed, 124 insertions(+), 95 deletions(-) diff --git a/trunk/paradiseo-mo/src/algo/moILS.h b/trunk/paradiseo-mo/src/algo/moILS.h index 53a23a377..446de3ce2 100644 --- a/trunk/paradiseo-mo/src/algo/moILS.h +++ b/trunk/paradiseo-mo/src/algo/moILS.h @@ -40,6 +40,7 @@ Contact: paradiseo-help@lists.gforge.inria.fr #include #include + /** * Iterated Local Search */ @@ -52,7 +53,7 @@ public: typedef moNeighborhood 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& _ls, eoEvalFunc& _fullEval, eoMonOp& _op, moContinuator >& _cont): moLocalSearch >(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& _ls, eoEvalFunc& _fullEval, eoMonOp& _op, moContinuator >& _cont, moMonOpPerturb& _perturb, moAcceptanceCriterion& _accept): + moLocalSearch >(explorer, _cont, _fullEval), + iterCont(0), + defaultPerturb(dummyOp, _fullEval), + explorer(_ls, _perturb, _accept) + {} + private: + + class dummmyMonOp: public eoMonOp{ + public: + bool operator()(EOT&){return false;} + }dummyOp; moIterContinuator > iterCont; moMonOpPerturb defaultPerturb; moAlwaysAcceptCrit defaultAccept; diff --git a/trunk/paradiseo-mo/src/algo/moLocalSearch.h b/trunk/paradiseo-mo/src/algo/moLocalSearch.h index 971f28347..c74cf6943 100644 --- a/trunk/paradiseo-mo/src/algo/moLocalSearch.h +++ b/trunk/paradiseo-mo/src/algo/moLocalSearch.h @@ -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 diff --git a/trunk/paradiseo-mo/src/continuator/moCheckpoint.h b/trunk/paradiseo-mo/src/continuator/moCheckpoint.h index f081d636e..f6fd2c53b 100644 --- a/trunk/paradiseo-mo/src/continuator/moCheckpoint.h +++ b/trunk/paradiseo-mo/src/continuator/moCheckpoint.h @@ -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); } diff --git a/trunk/paradiseo-mo/src/continuator/moIterContinuator.h b/trunk/paradiseo-mo/src/continuator/moIterContinuator.h index f8392a75a..c04ca502a 100644 --- a/trunk/paradiseo-mo/src/continuator/moIterContinuator.h +++ b/trunk/paradiseo-mo/src/continuator/moIterContinuator.h @@ -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; } /** diff --git a/trunk/paradiseo-mo/src/continuator/moStatBase.h b/trunk/paradiseo-mo/src/continuator/moStatBase.h index 965b16c9e..8984c5add 100644 --- a/trunk/paradiseo-mo/src/continuator/moStatBase.h +++ b/trunk/paradiseo-mo/src/continuator/moStatBase.h @@ -54,6 +54,11 @@ public: */ virtual void lastCall(EOT &) {} + /** + * first call of a statistical operator + */ + virtual void init(EOT &){} + /** * @return name of the class */ diff --git a/trunk/paradiseo-mo/src/continuator/moTimeContinuator.h b/trunk/paradiseo-mo/src/continuator/moTimeContinuator.h index 563b786e7..48ec1fe1b 100644 --- a/trunk/paradiseo-mo/src/continuator/moTimeContinuator.h +++ b/trunk/paradiseo-mo/src/continuator/moTimeContinuator.h @@ -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 diff --git a/trunk/paradiseo-mo/tutorial/Lesson4/testSimpleTS.cpp b/trunk/paradiseo-mo/tutorial/Lesson4/testSimpleTS.cpp index 6a39018dd..c6831f296 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson4/testSimpleTS.cpp +++ b/trunk/paradiseo-mo/tutorial/Lesson4/testSimpleTS.cpp @@ -34,7 +34,6 @@ using namespace std; //Neighbors and Neighborhoods #include -#include #include //Algorithm and its components diff --git a/trunk/paradiseo-mo/tutorial/Lesson5/testILS.cpp b/trunk/paradiseo-mo/tutorial/Lesson5/testILS.cpp index c3f8bafa7..b10ea7cbd 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson5/testILS.cpp +++ b/trunk/paradiseo-mo/tutorial/Lesson5/testILS.cpp @@ -2,6 +2,7 @@ /** testILS.cpp * * SV - 12/01/10 + * JH - 04/05/10 * */ //----------------------------------------------------------------------------- @@ -23,38 +24,44 @@ using namespace std; //----------------------------------------------------------------------------- -// fitness function -#include -#include +//Representation and initializer #include -#include -#include - -#include +#include +#include +// fitness function +#include #include #include -#include -#include -#include -#include -#include -#include + +//Neighbors and Neighborhoods +#include +#include + +//Mutation +#include + +//Algorithm and its components +#include +#include + +//mo eval +#include #include #include #include #include #include + #include // REPRESENTATION //----------------------------------------------------------------------------- -typedef eoBit Indi; -typedef moBitNeighbor Neighbor ; // incremental evaluation -typedef moOrderNeighborhood Neighborhood ; -typedef moRndWithoutReplNeighborhood Neighborhood2 ; -typedef moSimpleHCexplorer NHE; +typedef eoInt Queen; //Permutation (Queen's problem representation) + +typedef moShiftNeighbor shiftNeighbor; //shift Neighbor +typedef moOrderNeighborhood 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 eval; + queenEval fullEval; - //FuncNK eval(vecSize, 2); /* ========================================================= * - * Initilisation of the solution + * Initializer of a solution * * ========================================================= */ - // a Indi random initializer - eoUniformGenerator uGen; - eoInitFixedLength random(vecSize, uGen); + eoInitPermutation 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 fulleval(eval); - - //An eval by copy can be used instead of the eval by modif - //moFullEvalByCopy fulleval(eval); - - - /* ========================================================= - * - * Comparator of neighbors - * - * ========================================================= */ - - moNeighborComparator comparator; - moSolNeighborComparator solComparator; - + moFullEvalByCopy 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 explorer(neighborhood, fulleval, comparator, solComparator); + //Basic Constructor of the Tabu Search + moTS ts(orderShiftNH, fullEval, shiftEval, 5, 7); + + eoSwapMutation mut; + + //Basic Constructor of the Iterated Local Search + moILS 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 > cont(4); + moILS localSearch2(ts, fullEval, mut, cont); - moTrueContinuator 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 monOp(1.0/vecSize); - - moMonOpPerturb perturb(monOp, eval); - - //moRestartPerturb perturb(random, eval, 5); - - //moNeighborhoodPerturb perturb(neighborhood2, fulleval); - - moSolComparator comp; - - //moAlwaysAcceptCrit accept; - moBetterAcceptCrit accept(comp); - - moILSexplorer< Neighbor > explorerILS(hc, perturb, accept); - - moIterContinuator > continuatorILS(100); - - moLocalSearch >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; }