From 403afc982337cc9f50089fe059b94c47458c360f Mon Sep 17 00:00:00 2001 From: jhumeau Date: Tue, 22 Jun 2010 16:04:02 +0000 Subject: [PATCH] =?UTF-8?q?pop=20base=20ls=20modifi=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1863 331e1502-861f-0410-8da2-ba01fb791d7f --- .../src/problems/bitString/moPopSol.h | 2 +- trunk/paradiseo-mo/tutorial/CMakeLists.txt | 1 + .../tutorial/Lesson8/testPopLS.cpp | 128 +++++++++++++++--- 3 files changed, 110 insertions(+), 21 deletions(-) diff --git a/trunk/paradiseo-mo/src/problems/bitString/moPopSol.h b/trunk/paradiseo-mo/src/problems/bitString/moPopSol.h index 9b6688a27..adb70a859 100644 --- a/trunk/paradiseo-mo/src/problems/bitString/moPopSol.h +++ b/trunk/paradiseo-mo/src/problems/bitString/moPopSol.h @@ -34,7 +34,7 @@ Contact: paradiseo-help@lists.gforge.inria.fr #include template -class moPopSol : public EO, public eoPop{ +class moPopSol : public EO, public std::vector{ public: typedef typename EO::Fitness Fitness; using EO::invalidate; diff --git a/trunk/paradiseo-mo/tutorial/CMakeLists.txt b/trunk/paradiseo-mo/tutorial/CMakeLists.txt index 5b622393e..bbf9bfcaf 100644 --- a/trunk/paradiseo-mo/tutorial/CMakeLists.txt +++ b/trunk/paradiseo-mo/tutorial/CMakeLists.txt @@ -395,6 +395,7 @@ ADD_SUBDIRECTORY(Lesson4) ADD_SUBDIRECTORY(Lesson5) ADD_SUBDIRECTORY(Lesson6) ADD_SUBDIRECTORY(Lesson7) +ADD_SUBDIRECTORY(Lesson8) IF(NOT CMAKE_GENERATOR STREQUAL "Visual Studio 8 2005" AND NOT CMAKE_GENERATOR STREQUAL "Visual Studio 9 2008" OR CMAKE_GENERATOR STREQUAL "Visual Studio 10") ADD_COMMANDS_NEWMO() diff --git a/trunk/paradiseo-mo/tutorial/Lesson8/testPopLS.cpp b/trunk/paradiseo-mo/tutorial/Lesson8/testPopLS.cpp index 979bf73e5..b026f86c7 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson8/testPopLS.cpp +++ b/trunk/paradiseo-mo/tutorial/Lesson8/testPopLS.cpp @@ -74,6 +74,8 @@ using namespace std; #include "moPopFitContinuator.h" +#include + //----------------------------------------------------------------------------- // Define types of the representation solution, different neighbors and neighborhoods @@ -81,7 +83,32 @@ using namespace std; typedef moPopSol > Solution; //Permutation (Queen's problem representation) typedef moPopBitNeighbor Neighbor; //shift Neighbor -typedef moOrderNeighborhood Neighborhood; //rnd shift Neighborhood (Indexed) +typedef moRndWithReplNeighborhood Neighborhood; //rnd shift Neighborhood (Indexed) + + +class popInit: public eoInit{ + +public: + + popInit( eoInitFixedLength > & _rnd, unsigned int _popSize):rnd(_rnd), popSize(_popSize){} + + void operator()(Solution & _sol){ + + + + eoBit tmp; + + for(unsigned int i=0; i >& rnd; + unsigned int popSize; + +}; void main_function(int argc, char **argv) { @@ -101,13 +128,18 @@ void main_function(int argc, char **argv) parser.processParam( seedParam ); unsigned seed = seedParam.value(); + // the number of steps of the random walk + eoValueParam stepParam(100, "nbStep", "Number of steps of the random walk", 'n'); + parser.processParam( stepParam, "Representation" ); + unsigned nbStep = stepParam.value(); + // description of genotype eoValueParam vecSizeParam(8, "vecSize", "Genotype size", 'V'); parser.processParam( vecSizeParam, "Representation" ); unsigned vecSize = vecSizeParam.value(); // description of genotype - eoValueParam popSizeParam(10, "popSize", "population size", 'V'); + eoValueParam popSizeParam(10, "popSize", "population size", 'P'); parser.processParam( popSizeParam, "Representation" ); unsigned popSize = popSizeParam.value(); @@ -116,6 +148,16 @@ void main_function(int argc, char **argv) eoValueParam statusParam(str_status.c_str(), "status", "Status file"); parser.processParam( statusParam, "Persistence" ); + // the name of the output file + string str_out = "out.dat"; // default value + eoValueParam outParam(str_out.c_str(), "out", "Output file of the sampling", 'o'); + parser.processParam(outParam, "Persistence" ); + + // description of genotype + eoValueParam pparam(10, "p", "p", 'p'); + parser.processParam( pparam, "Representation" ); + unsigned p = pparam.value(); + // do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED // i.e. in case you need parameters somewhere else, postpone these if (parser.userNeedsHelp()) { @@ -145,7 +187,7 @@ void main_function(int argc, char **argv) * ========================================================= */ oneMaxEval< eoBit > eval; - oneMaxPopEval< eoBit > popEval(eval, 2); + oneMaxPopEval< eoBit > popEval(eval, p); /* ========================================================= @@ -153,21 +195,13 @@ void main_function(int argc, char **argv) * Initilisation of the solution * * ========================================================= */ + eoUniformGenerator uGen; - eoInitFixedLength > random(vecSize, uGen); + eoInitFixedLength > rnd(vecSize, uGen); - moPopSol > sol; - eoBit tmp(vecSize); + popInit random(rnd, popSize); - for(unsigned int i=0; i evalNeighbor(eval,2); + moPopBitEval evalNeighbor(eval,p); // Neighbor n; // @@ -206,7 +240,7 @@ void main_function(int argc, char **argv) * * ========================================================= */ - moSimpleHC ls(neighborhood, popEval, evalNeighbor, cont); + //moSimpleHC ls(neighborhood, popEval, evalNeighbor, cont); /* ========================================================= * @@ -214,11 +248,65 @@ void main_function(int argc, char **argv) * * ========================================================= */ - ls(sol); +// ls(sol); +// +// std::cout << "final solution:" << std::endl; +// sol.printOn(std::cout); +// std::cout << std::endl; - std::cout << "final solution:" << std::endl; - sol.printOn(std::cout); - std::cout << std::endl; + + + + /* ========================================================= + * + * The sampling of the search space + * + * ========================================================= */ + + // sampling object : + // - random initialization + // - neighborhood to compute the next step + // - fitness function + // - neighbor evaluation + // - number of steps of the walk + moAutocorrelationSampling sampling(random, neighborhood, popEval, evalNeighbor, nbStep); + + /* ========================================================= + * + * execute the sampling + * + * ========================================================= */ + + sampling(); + + /* ========================================================= + * + * export the sampling + * + * ========================================================= */ + + // to export the statistics into file + sampling.fileExport(str_out); + + // to get the values of statistics + // so, you can compute some statistics in c++ from the data + const std::vector & fitnessValues = sampling.getValues(0); + + std::cout << "First values:" << std::endl; + std::cout << "Fitness " << fitnessValues[0] << std::endl; + + std::cout << "Last values:" << std::endl; + std::cout << "Fitness " << fitnessValues[fitnessValues.size() - 1] << std::endl; + + // more basic statistics on the distribution: + moStatistics statistics; + + vector rho, phi; + + statistics.autocorrelation(fitnessValues, 20, rho, phi); + + for (unsigned s = 0; s < rho.size(); s++) + std::cout << s << " " << "rho=" << rho[s] << ", phi=" << phi[s] << std::endl; // Queen solution1, solution2; //