diff --git a/trunk/paradiseo-mo/src/algo/moILS.h b/trunk/paradiseo-mo/src/algo/moILS.h index 446de3ce2..9da7e4123 100644 --- a/trunk/paradiseo-mo/src/algo/moILS.h +++ b/trunk/paradiseo-mo/src/algo/moILS.h @@ -84,12 +84,11 @@ public: * 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): + moILS(moLocalSearch& _ls, eoEvalFunc& _fullEval, moContinuator >& _cont, moMonOpPerturb& _perturb, moAcceptanceCriterion& _accept): moLocalSearch >(explorer, _cont, _fullEval), iterCont(0), defaultPerturb(dummyOp, _fullEval), diff --git a/trunk/paradiseo-mo/src/continuator/moIterContinuator.h b/trunk/paradiseo-mo/src/continuator/moIterContinuator.h index c04ca502a..7eb90ec78 100644 --- a/trunk/paradiseo-mo/src/continuator/moIterContinuator.h +++ b/trunk/paradiseo-mo/src/continuator/moIterContinuator.h @@ -44,7 +44,7 @@ public: /** * @param _maxIter number maximum of iterations */ - moIterContinuator(unsigned int _maxIter): maxIter(_maxIter){} + moIterContinuator(unsigned int _maxIter, bool _verbose=true): maxIter(_maxIter), verbose(_verbose){} /** *@param _solution a solution @@ -54,9 +54,8 @@ public: bool res; cpt++; res = (cpt < maxIter); - if(!res){ + if(!res && verbose) std::cout << "STOP in moIterContinuator: Reached maximum number of iterations [" << cpt << "/" << maxIter << "]" << std::endl; - } return res; } @@ -79,6 +78,7 @@ public: private: unsigned int maxIter; unsigned int cpt; + bool verbose; }; #endif diff --git a/trunk/paradiseo-mo/src/continuator/moTimeContinuator.h b/trunk/paradiseo-mo/src/continuator/moTimeContinuator.h index 48ec1fe1b..8add92ffa 100644 --- a/trunk/paradiseo-mo/src/continuator/moTimeContinuator.h +++ b/trunk/paradiseo-mo/src/continuator/moTimeContinuator.h @@ -46,7 +46,7 @@ public: * Ctor. * @param _max maximum running time */ - moTimeContinuator(time_t _max): max(_max){ + moTimeContinuator(time_t _max, bool _verbose=true): max(_max), verbose(_verbose){ start = time(NULL); } @@ -57,13 +57,12 @@ public: */ virtual bool operator() (EOT& _sol) { + bool res; time_t elapsed = (time_t) difftime(time(NULL), start); - if (elapsed >= max) - { - std::cout << "STOP in moTimeContinuator: Reached maximum time [" << elapsed << "/" << max << "]" << std::endl; - return false; - } - return true; + res = (elapsed < max); + if(!res && verbose) + std::cout << "STOP in moTimeContinuator: Reached maximum time [" << elapsed << "/" << max << "]" << std::endl; + return res; } /** @@ -90,6 +89,8 @@ private: time_t max; /** starting time */ time_t start; + /** verbose mode */ + bool verbose; }; diff --git a/trunk/paradiseo-mo/test/CMakeLists.txt b/trunk/paradiseo-mo/test/CMakeLists.txt index 51a3904f9..aa0e00872 100644 --- a/trunk/paradiseo-mo/test/CMakeLists.txt +++ b/trunk/paradiseo-mo/test/CMakeLists.txt @@ -81,6 +81,7 @@ SET (TEST_LIST t-moNeighborVectorTabuList t-moMonOpDiversification t-moTS + t-moILS ) FOREACH (test ${TEST_LIST}) diff --git a/trunk/paradiseo-mo/test/t-moCheckpoint.cpp b/trunk/paradiseo-mo/test/t-moCheckpoint.cpp index 2792dd5e5..e080b9a7b 100644 --- a/trunk/paradiseo-mo/test/t-moCheckpoint.cpp +++ b/trunk/paradiseo-mo/test/t-moCheckpoint.cpp @@ -78,14 +78,14 @@ int main() { test1.init(s); test1(s); - assert(a==3 && b==16 && c==11 && d==48); + assert(a==3 && b==16 && c==12 && d==49); assert(stat.value()[0]); assert(stat.value()[1]); assert(!stat.value()[2]); assert(stat.value().fitness()==17); test1(s); - assert(a==4 && b==17 && c==12 && d==49); + assert(a==4 && b==17 && c==13 && d==50); assert(stat.value()[0]); assert(stat.value()[1]); assert(!stat.value()[2]); @@ -95,7 +95,7 @@ int main() { test2.init(s); test2(s); - assert(a==5 && b==18 && c==13 && d==50); + assert(a==5 && b==18 && c==15 && d==52); assert(stat.value()[0]); assert(stat.value()[1]); assert(!stat.value()[2]); @@ -105,14 +105,14 @@ int main() { test2(s); assert(stat.value().fitness()==4); test2(s); - assert(stat.value().fitness()==4); + assert(stat.value().fitness()==6); test2(s); assert(stat.value().fitness()==6); test1.lastCall(s); - assert(a==9 && b==22 && c==17 && d==54); + assert(a==9 && b==22 && c==19 && d==56); test2.lastCall(s); - assert(a==10 && b==23 && c==18 && d==55); + assert(a==10 && b==23 && c==20 && d==57); assert(test1.className()=="moCheckpoint"); std::cout << "[t-moCheckpoint] => OK" << std::endl; diff --git a/trunk/paradiseo-mo/test/t-moILS.cpp b/trunk/paradiseo-mo/test/t-moILS.cpp new file mode 100644 index 000000000..25643d48d --- /dev/null +++ b/trunk/paradiseo-mo/test/t-moILS.cpp @@ -0,0 +1,77 @@ +/* + +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 +*/ + +#include +#include +#include + +#include +#include +#include "moTestClass.h" +#include +#include +#include +#include +#include +#include + +class dummyMonOp: public eoMonOp{ + public: + bool operator()(bitVector&){return false;} +}; + +int main(){ + + std::cout << "[t-moILS] => START" << std::endl; + + bitNeighborhood nh(4); + oneMaxEval fullEval; + evalOneMax eval(4); + + moTS ts(nh, fullEval, eval, 1, 7); + + dummyMonOp op; + + //basic constructor + moILS test1(ts, fullEval, op, 3); + + //simple constructor + moTrueContinuator > cont; + moILS test2(ts, fullEval, op, cont); + + //general constructor + moMonOpPerturb perturb(op, fullEval); + moAlwaysAcceptCrit accept; + moILS test3(ts, fullEval, cont, perturb, accept); + + std::cout << "[t-moILS] => OK" << std::endl; + + return EXIT_SUCCESS; +} + diff --git a/trunk/paradiseo-mo/tutorial/Lesson5/testILS.cpp b/trunk/paradiseo-mo/tutorial/Lesson5/testILS.cpp index b10ea7cbd..b43232261 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson5/testILS.cpp +++ b/trunk/paradiseo-mo/tutorial/Lesson5/testILS.cpp @@ -175,7 +175,7 @@ void main_function(int argc, char **argv) * ========================================================= */ //Basic Constructor of the Tabu Search - moTS ts(orderShiftNH, fullEval, shiftEval, 5, 7); + moTS ts(orderShiftNH, fullEval, shiftEval, 1, 7); eoSwapMutation mut; @@ -185,9 +185,17 @@ void main_function(int argc, char **argv) //Simple Constructor of the Iterated Local Search //Be carefull, template of the continuator must be a dummyNeighbor!!! - moIterContinuator > cont(4); + moIterContinuator > cont(4, false); moILS localSearch2(ts, fullEval, mut, cont); + //General Constructor of the Iterated Local Search + moMonOpPerturb perturb(mut, fullEval); + + moSolComparator solComp; + moBetterAcceptCrit accept(solComp); + + moILS localSearch3(ts, fullEval, cont, perturb, accept); + std::cout << "Iterated Local Search 1:" << std::endl; std::cout << "--------------" << std::endl; std::cout << "initial: " << sol1 << std::endl ; @@ -200,6 +208,12 @@ void main_function(int argc, char **argv) localSearch2(sol2); std::cout << "final: " << sol2 << std::endl << std::endl; + std::cout << "Iterated Local Search 3:" << std::endl; + std::cout << "--------------" << std::endl; + std::cout << "initial: " << sol3 << std::endl ; + localSearch3(sol3); + std::cout << "final: " << sol3 << std::endl << std::endl; + } // A main that catches the exceptions diff --git a/trunk/paradiseo-mo/tutorial/Lesson7/CMakeLists.txt b/trunk/paradiseo-mo/tutorial/Lesson7/CMakeLists.txt index e69de29bb..96ed21db4 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson7/CMakeLists.txt +++ b/trunk/paradiseo-mo/tutorial/Lesson7/CMakeLists.txt @@ -0,0 +1,10 @@ +INCLUDE_DIRECTORIES(${EO_SRC_DIR}/src + ${MO_SRC_DIR}/src + ${PROBLEMS_SRC_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/../src) + +LINK_DIRECTORIES(${EO_BIN_DIR}/lib) + +ADD_EXECUTABLE(hybridAlgo hybridAlgo.cpp) + +TARGET_LINK_LIBRARIES(hybridAlgo eoutils ga eo) diff --git a/trunk/paradiseo-mo/tutorial/Lesson7/hybridAlgo.cpp b/trunk/paradiseo-mo/tutorial/Lesson7/hybridAlgo.cpp new file mode 100644 index 000000000..685f64b38 --- /dev/null +++ b/trunk/paradiseo-mo/tutorial/Lesson7/hybridAlgo.cpp @@ -0,0 +1,222 @@ +//----------------------------------------------------------------------------- +/** testILS.cpp + * + * SV - 12/01/10 + * JH - 06/05/10 + * + */ +//----------------------------------------------------------------------------- + +// standard includes +#define HAVE_SSTREAM + +#include // runtime_error +#include // cout +#include // ostrstream, istrstream +#include +#include + +// the general include for eo +#include +#include +#include + +using namespace std; + +//----------------------------------------------------------------------------- +//Representation and initializer +#include +#include +#include + +// fitness function +#include +#include +#include + +//Neighbors and Neighborhoods +#include +#include + +//Mutation +#include +#include + +//Algorithm and its components +#include + +//mo eval +#include + +#include + +// REPRESENTATION +//----------------------------------------------------------------------------- +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) +{ + /* ========================================================= + * + * 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 seedParam(time(0), "seed", "Random number seed", 'S'); + parser.processParam( seedParam ); + unsigned seed = seedParam.value(); + + // description of genotype + eoValueParam 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 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); + + /* ========================================================= + * + * Full evaluation fitness function + * + * ========================================================= */ + + queenEval fullEval; + + + /* ========================================================= + * + * Initializer of a solution + * + * ========================================================= */ + + eoInitPermutation init(vecSize); + + /* ========================================================= + * + * Declare and init a population + * + * ========================================================= */ + + eoPop pop; + + Queen tmp; + + for(unsigned int i=0; i<20; i++){ + init(tmp); + fullEval(tmp); + pop.push_back(tmp); + } + + /* ========================================================= + * + * evaluation of a neighbor solution + * + * ========================================================= */ + + moFullEvalByCopy shiftEval(fullEval); + + /* ========================================================= + * + * the neighborhood of a solution + * + * ========================================================= */ + + orderShiftNeighborhood orderShiftNH(pow(vecSize-1, 2)); + + /* ========================================================= + * + * the local search algorithm + * + * ========================================================= */ + + //Basic Constructor a first improvement hill climber + moFirstImprHC hc(orderShiftNH, fullEval, shiftEval); + + /* ========================================================= + * + * the evolutionary algorithm + * + * ========================================================= */ + + //continuator + eoGenContinue EAcont(50); + + //selection + eoDetTournamentSelect selectOne(2); + eoSelectMany select(selectOne, 1); + + //crossover + eoOrderXover cross; + + //transform operator (the hill climber replace the mutation operator) + eoSGATransform transform(cross, 0.3, hc, 0.7); + + //replacement + eoGenerationalReplacement repl; + + //easyEA + eoEasyEA hybridAlgo(EAcont, fullEval, select, transform, repl); + + + std::cout << "INITIAL POPULATION:" << std::endl; + std::cout << "-------------------" << std::endl; + + for(unsigned int i=0; i