From d71d58bd63925c6f19e9764f838366f8b6948592 Mon Sep 17 00:00:00 2001 From: verel Date: Wed, 28 Apr 2010 15:24:35 +0000 Subject: [PATCH] update de la lesson 1 de tout git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1752 331e1502-861f-0410-8da2-ba01fb791d7f --- .../src/explorer/moRandomBestHCExplorer.h | 5 +- trunk/paradiseo-mo/src/mo.h | 2 + .../tutorial/Lesson1/CMakeLists.txt | 4 +- .../tutorial/Lesson1/lesson1_firstImprHC.cpp | 196 +++++++++++++ .../tutorial/Lesson1/lesson1_randomBestHC.cpp | 275 +++++++++--------- .../tutorial/Lesson1/lesson1_simpleHC.cpp | 20 +- 6 files changed, 350 insertions(+), 152 deletions(-) create mode 100644 trunk/paradiseo-mo/tutorial/Lesson1/lesson1_firstImprHC.cpp diff --git a/trunk/paradiseo-mo/src/explorer/moRandomBestHCExplorer.h b/trunk/paradiseo-mo/src/explorer/moRandomBestHCExplorer.h index d393debf5..ac929669b 100644 --- a/trunk/paradiseo-mo/src/explorer/moRandomBestHCExplorer.h +++ b/trunk/paradiseo-mo/src/explorer/moRandomBestHCExplorer.h @@ -2,7 +2,7 @@ Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 - Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau + Sebastien Verel, Arnaud Liefooghe, Jeremie Humeau This software is governed by the CeCILL license under French law and abiding by the rules of distribution of free software. You can use, @@ -43,7 +43,8 @@ #include /** - * Explorer for a simple neutral Hill-climbing + * Explorer for Hill-Climbing + * which choose randomly one of the best solution in the neighborhood at each iteration */ template< class Neighbor > class moRandomBestHCExplorer : public moNeighborhoodExplorer diff --git a/trunk/paradiseo-mo/src/mo.h b/trunk/paradiseo-mo/src/mo.h index efda63965..c3521d554 100755 --- a/trunk/paradiseo-mo/src/mo.h +++ b/trunk/paradiseo-mo/src/mo.h @@ -38,6 +38,8 @@ #include #include #include +#include +#include #include #include diff --git a/trunk/paradiseo-mo/tutorial/Lesson1/CMakeLists.txt b/trunk/paradiseo-mo/tutorial/Lesson1/CMakeLists.txt index 80ea32f43..8da7be7b1 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson1/CMakeLists.txt +++ b/trunk/paradiseo-mo/tutorial/Lesson1/CMakeLists.txt @@ -5,11 +5,11 @@ INCLUDE_DIRECTORIES(${EO_SRC_DIR}/src LINK_DIRECTORIES(${EO_BIN_DIR}/lib) ADD_EXECUTABLE(lesson1_simpleHC lesson1_simpleHC.cpp) -ADD_EXECUTABLE(lesson1_firstImpr lesson1_firstImpr.cpp) +ADD_EXECUTABLE(lesson1_firstImprHC lesson1_firstImprHC.cpp) ADD_EXECUTABLE(lesson1_randomBestHC lesson1_randomBestHC.cpp) ADD_EXECUTABLE(lesson1_HCneutral lesson1_HCneutral.cpp) TARGET_LINK_LIBRARIES(lesson1_simpleHC eoutils ga eo) -TARGET_LINK_LIBRARIES(lesson1_firstImpr eoutils ga eo) +TARGET_LINK_LIBRARIES(lesson1_firstImprHC eoutils ga eo) TARGET_LINK_LIBRARIES(lesson1_randomBestHC eoutils ga eo) TARGET_LINK_LIBRARIES(lesson1_HCneutral eoutils ga eo) diff --git a/trunk/paradiseo-mo/tutorial/Lesson1/lesson1_firstImprHC.cpp b/trunk/paradiseo-mo/tutorial/Lesson1/lesson1_firstImprHC.cpp new file mode 100644 index 000000000..d8db8d519 --- /dev/null +++ b/trunk/paradiseo-mo/tutorial/Lesson1/lesson1_firstImprHC.cpp @@ -0,0 +1,196 @@ +//----------------------------------------------------------------------------- +/** lesson1_firstImprHC.cpp + * + * SV - 27/04/10 - version 1 + * + */ +//----------------------------------------------------------------------------- + +// standard includes +#define HAVE_SSTREAM + +#include // runtime_error +#include // cout +#include // ostrstream, istrstream +#include +#include + +// the general include for eo +#include + +// declaration of the namespace +using namespace std; + +//----------------------------------------------------------------------------- +// representation of solutions, and neighbors +#include // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp +#include // neighbor of bit string + +//----------------------------------------------------------------------------- +// fitness function, and evaluation of neighbors +#include +#include +#include + +//----------------------------------------------------------------------------- +// neighborhood description +#include // visit all neighbors in random order without repeating any neighbor + +//----------------------------------------------------------------------------- +// the first improvement Hill-Climbing local search +#include + +// Declaration of types +//----------------------------------------------------------------------------- +// Indi is the typedef of the solution type like in paradisEO-eo +typedef eoBit Indi; // bit string with unsigned fitness type +// Neighbor is the typedef of the neighbor type, +// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness) +// all classes from paradisEO-mo use this template type +typedef moBitNeighbor Neighbor ; // bit string neighbor with unsigned fitness type + + +// Main function +//----------------------------------------------------------------------------- +void main_function(int argc, char **argv) +{ + /* ========================================================= + * + * Parameters from parser + * + * ========================================================= */ + // more information on the input parameters: see EO tutorial lesson 3 + // but don't care at first it just read the parameters of the bit string size and the random seed. + + // 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 + + // random seed parameter + eoValueParam seedParam(time(0), "seed", "Random number seed", 'S'); + parser.processParam( seedParam ); + unsigned seed = seedParam.value(); + + // length of the bit string + eoValueParam vecSizeParam(20, "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 + // more information: see EO tutorial lesson 1 (FirstBitGA.cpp) + rng.reseed(seed); + + /* ========================================================= + * + * Initialization of the solution + * + * ========================================================= */ + + // a Indi random initializer: each bit is random + // more information: see EO tutorial lesson 1 (FirstBitGA.cpp) + eoUniformGenerator uGen; + eoInitFixedLength random(vecSize, uGen); + + /* ========================================================= + * + * Eval fitness function (full evaluation) + * + * ========================================================= */ + + // the fitness function is just the number of 1 in the bit string + oneMaxFullEval fullEval; + + /* ========================================================= + * + * evaluation of a neighbor solution + * + * ========================================================= */ + + // Use it if there is no incremental evaluation: a neighbor is evaluated by the full evaluation of a solution + // moFullEvalByModif neighborEval(fullEval); + + // Incremental evaluation of the neighbor: fitness is modified by +/- 1 + moOneMaxIncrEval neighborEval; + + /* ========================================================= + * + * the neighborhood of a solution + * + * ========================================================= */ + + // Exploration of the neighborhood in random order of the neigbor's index: + // each neighbor is visited only once + moRndWithoutReplNeighborhood neighborhood(vecSize); + + /* ========================================================= + * + * the local search algorithm + * + * ========================================================= */ + + moFirstImprHC hc(neighborhood, fullEval, neighborEval); + + /* ========================================================= + * + * executes the local search from a random solution + * + * ========================================================= */ + + // The current solution + Indi solution; + + // Apply random initialization + random(solution); + + // Evaluation of the initial solution: + // can be evaluated here, or else it will be done at the beginning of the local search + fullEval(solution); + + // Output: the intial solution + std::cout << "initial: " << solution << std::endl ; + + // Apply the local search on the solution ! + hc(solution); + + // Output: the final solution + std::cout << "final: " << solution << std::endl ; + +} + +// 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; +} diff --git a/trunk/paradiseo-mo/tutorial/Lesson1/lesson1_randomBestHC.cpp b/trunk/paradiseo-mo/tutorial/Lesson1/lesson1_randomBestHC.cpp index 67e0f94df..5ab554378 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson1/lesson1_randomBestHC.cpp +++ b/trunk/paradiseo-mo/tutorial/Lesson1/lesson1_randomBestHC.cpp @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -/** testRandomBestHC.cpp +/** first_randomBestHC.cpp * - * SV - 24/01/10 + * SV - 27/04/10 - version 1 * */ //----------------------------------------------------------------------------- @@ -11,175 +11,174 @@ #include // runtime_error #include // cout -#include // ostrstream, istrstream +#include // ostrstream, istrstream #include #include // the general include for eo #include -#include +// declaration of the namespace using namespace std; //----------------------------------------------------------------------------- -// fitness function -#include -#include -#include -#include +// representation of solutions, and neighbors +#include // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp +#include // neighbor of bit string -#include -#include -#include -#include -#include -#include -#include - -// REPRESENTATION //----------------------------------------------------------------------------- -typedef eoBit Indi; -typedef moBitNeighbor Neighbor ; // incremental evaluation -typedef moOrderNeighborhood Neighborhood ; +// fitness function, and evaluation of neighbors +#include +#include +#include +//----------------------------------------------------------------------------- +// neighborhood description +#include // visit all neighbors in increasing order of bit index + +//----------------------------------------------------------------------------- +// the Hill-Climbing local search which randomly one of the best solution +#include + +// Declaration of types +//----------------------------------------------------------------------------- +// Indi is the typedef of the solution type like in paradisEO-eo +typedef eoBit Indi; // bit string with unsigned fitness type +// Neighbor is the typedef of the neighbor type, +// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness) +// all classes from paradisEO-mo use this template type +typedef moBitNeighbor Neighbor ; // bit string neighbor with unsigned fitness type + + +// Main function +//----------------------------------------------------------------------------- void main_function(int argc, char **argv) { - /* ========================================================= - * - * Parameters - * - * ========================================================= */ + /* ========================================================= + * + * Parameters from parser + * + * ========================================================= */ + // more information on the input parameters: see EO tutorial lesson 3 + // but don't care at first it just read the parameters of the bit string size and the random seed. - // First define a parser from the command-line arguments - eoParser parser(argc, argv); + // 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 + // 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(); + // random seed parameter + 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(); + // length of the bit string + eoValueParam vecSizeParam(20, "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" ); + // 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 - } + // 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 - * - * ========================================================= */ + /* ========================================================= + * + * 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); + // reproducible random seed: if you don't change SEED above, + // you'll aways get the same result, NOT a random run + // more information: see EO tutorial lesson 1 (FirstBitGA.cpp) + rng.reseed(seed); + /* ========================================================= + * + * Initialization of the solution + * + * ========================================================= */ - /* ========================================================= - * - * Eval fitness function - * - * ========================================================= */ + // a Indi random initializer: each bit is random + // more information: see EO tutorial lesson 1 (FirstBitGA.cpp) + eoUniformGenerator uGen; + eoInitFixedLength random(vecSize, uGen); - oneMaxFullEval eval; + /* ========================================================= + * + * Eval fitness function (full evaluation) + * + * ========================================================= */ + // the fitness function is just the number of 1 in the bit string + oneMaxFullEval fullEval; - /* ========================================================= - * - * Initilisation of the solution - * - * ========================================================= */ + /* ========================================================= + * + * evaluation of a neighbor solution + * + * ========================================================= */ - // a Indi random initializer - eoUniformGenerator uGen; - eoInitFixedLength random(vecSize, uGen); + // Use it if there is no incremental evaluation: a neighbor is evaluated by the full evaluation of a solution + // moFullEvalByModif neighborEval(fullEval); + // Incremental evaluation of the neighbor: fitness is modified by +/- 1 + moOneMaxIncrEval neighborEval; - /* ========================================================= - * - * evaluation of a neighbor solution - * - * ========================================================= */ + /* ========================================================= + * + * the neighborhood of a solution + * + * ========================================================= */ - moFullEvalByModif fulleval(eval); + // Exploration of the neighborhood in increasing order of the neigbor's index: + // bit-flip from bit 0 to bit (vecSize - 1) + moOrderNeighborhood neighborhood(vecSize); - //An eval by copy can be used instead of the eval by modif - //moFullEvalByCopy fulleval(eval); + /* ========================================================= + * + * the local search algorithm + * + * ========================================================= */ + moRandomBestHC hc(neighborhood, fullEval, neighborEval); - /* ========================================================= - * - * Comparator of neighbors - * - * ========================================================= */ + /* ========================================================= + * + * executes the local search from a random solution + * + * ========================================================= */ - moNeighborComparator comparator; - moSolNeighborComparator solComparator; + // The current solution + Indi solution; + // Apply random initialization + random(solution); - /* ========================================================= - * - * the neighborhood of a solution - * - * ========================================================= */ + // Evaluation of the initial solution: + // can be evaluated here, or else it will be done at the beginning of the local search + fullEval(solution); - Neighborhood neighborhood(vecSize); + // Output: the intial solution + std::cout << "initial: " << solution << std::endl ; + // Apply the local search on the solution ! + hc(solution); - /* ========================================================= - * - * a neighborhood explorer solution - * - * ========================================================= */ - - moRandomBestHCExplorer explorer(neighborhood, fulleval, comparator, solComparator); - - - /* ========================================================= - * - * the local search algorithm - * - * ========================================================= */ - - moTrueContinuator continuator;//always continue - - moLocalSearch localSearch(explorer, continuator, 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 ; + // Output: the final solution + std::cout << "final: " << solution << std::endl ; } @@ -187,11 +186,11 @@ void main_function(int argc, char **argv) int main(int argc, char **argv) { - try { - main_function(argc, argv); - } - catch (exception& e) { - cout << "Exception: " << e.what() << '\n'; - } - return 1; + try { + main_function(argc, argv); + } + catch (exception& e) { + cout << "Exception: " << e.what() << '\n'; + } + return 1; } diff --git a/trunk/paradiseo-mo/tutorial/Lesson1/lesson1_simpleHC.cpp b/trunk/paradiseo-mo/tutorial/Lesson1/lesson1_simpleHC.cpp index 47514a83d..7fd88507d 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson1/lesson1_simpleHC.cpp +++ b/trunk/paradiseo-mo/tutorial/Lesson1/lesson1_simpleHC.cpp @@ -11,13 +11,12 @@ #include // runtime_error #include // cout -#include // ostrstream, istrstream +#include // ostrstream, istrstream #include #include // the general include for eo #include -#include // declaration of the namespace using namespace std; @@ -47,7 +46,7 @@ using namespace std; typedef eoBit Indi; // bit string with unsigned fitness type // Neighbor is the typedef of the neighbor type, // Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness) -// all classes from paradisEO-mo use this template +// all classes from paradisEO-mo use this template type typedef moBitNeighbor Neighbor ; // bit string neighbor with unsigned fitness type @@ -75,7 +74,7 @@ void main_function(int argc, char **argv) unsigned seed = seedParam.value(); // length of the bit string - eoValueParam vecSizeParam(8, "vecSize", "Genotype size", 'V'); + eoValueParam vecSizeParam(20, "vecSize", "Genotype size", 'V'); parser.processParam( vecSizeParam, "Representation" ); unsigned vecSize = vecSizeParam.value(); @@ -108,7 +107,7 @@ void main_function(int argc, char **argv) /* ========================================================= * - * Initilisation of the solution + * Initialization of the solution * * ========================================================= */ @@ -165,19 +164,20 @@ void main_function(int argc, char **argv) // The current solution Indi solution; - // random intiialization + // Apply random initialization random(solution); - // Can be evaluated here, else it will be done at the beginning of the localSearch + // Evaluation of the initial solution: + // can be evaluated here, or else it will be done at the beginning of the local search fullEval(solution); - // output: the intial solution + // Output: the intial solution std::cout << "initial: " << solution << std::endl ; - // apply the local search on the solution ! + // Apply the local search on the solution ! hc(solution); - // output: the final solution + // Output: the final solution std::cout << "final: " << solution << std::endl ; }