From 070fdd01ccad34bad0c9d0b7d8cc4a02217c6a90 Mon Sep 17 00:00:00 2001 From: jhumeau Date: Wed, 21 Apr 2010 08:57:26 +0000 Subject: [PATCH] Tutos updated git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1739 331e1502-861f-0410-8da2-ba01fb791d7f --- .../tutorial/Lesson2/testNeighborhood.cpp | 4 +- .../Lesson3/testSimulatedAnnealing.cpp | 141 +++++++++++------- 2 files changed, 87 insertions(+), 58 deletions(-) diff --git a/trunk/paradiseo-mo/tutorial/Lesson2/testNeighborhood.cpp b/trunk/paradiseo-mo/tutorial/Lesson2/testNeighborhood.cpp index d5983c166..21aef332e 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson2/testNeighborhood.cpp +++ b/trunk/paradiseo-mo/tutorial/Lesson2/testNeighborhood.cpp @@ -44,9 +44,11 @@ using namespace std; // Define types of the representation solution, different neighbors and neighborhoods //----------------------------------------------------------------------------- typedef eoInt Queen; //Permutation (Queen's problem representation) + typedef moSwapNeighbor swapNeighbor ; //swap Neighbor -typedef moShiftNeighbor shiftNeighbor; //shift Neighbor typedef moSwapNeighborhood swapNeighborhood; //classical swap Neighborhood + +typedef moShiftNeighbor shiftNeighbor; //shift Neighbor typedef moOrderNeighborhood orderShiftNeighborhood; //order shift Neighborhood (Indexed) typedef moRndWithoutReplNeighborhood rndWithoutReplShiftNeighborhood; //random without replacement shift Neighborhood (Indexed) typedef moRndWithReplNeighborhood rndWithReplShiftNeighborhood; //random with replacement shift Neighborhood (Indexed) diff --git a/trunk/paradiseo-mo/tutorial/Lesson3/testSimulatedAnnealing.cpp b/trunk/paradiseo-mo/tutorial/Lesson3/testSimulatedAnnealing.cpp index 31ccd7352..c69b2baf7 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson3/testSimulatedAnnealing.cpp +++ b/trunk/paradiseo-mo/tutorial/Lesson3/testSimulatedAnnealing.cpp @@ -2,7 +2,7 @@ /** testSimulatedAnnealing.cpp * * SV - 29/03/10 - * + * JH - 20/04/10 */ //----------------------------------------------------------------------------- @@ -22,26 +22,42 @@ using namespace std; //----------------------------------------------------------------------------- -// fitness function -#include -#include +//Representation and initializer #include -#include +#include +#include +// fitness function +#include #include #include -#include -#include -#include -#include -#include -#include -// REPRESENTATION +//Neighbors and Neighborhoods +#include +#include + +//Algorithm and its components +#include +#include + +//comparator +#include + +//continuators +#include +#include +#include +#include +#include + + //----------------------------------------------------------------------------- -typedef eoBit Indi; -typedef moBitNeighbor Neighbor ; // incremental evaluation -typedef moRndWithReplNeighborhood Neighborhood ; +// Define types of the representation solution, different neighbors and neighborhoods +//----------------------------------------------------------------------------- +typedef eoInt Queen; //Permutation (Queen's problem representation) + +typedef moShiftNeighbor shiftNeighbor; //shift Neighbor +typedef moRndWithReplNeighborhood rndShiftNeighborhood; //rnd shift Neighborhood (Indexed) void main_function(int argc, char **argv) { @@ -89,7 +105,7 @@ void main_function(int argc, char **argv) * ========================================================= */ //reproducible random seed: if you don't change SEED above, - // you'll aways get the same result, NOT a random run + // you'll always get the same result, NOT a random run rng.reseed(seed); @@ -99,7 +115,7 @@ void main_function(int argc, char **argv) * * ========================================================= */ - oneMaxFullEval eval; + queenFullEval fullEval; /* ========================================================= @@ -108,10 +124,7 @@ void main_function(int argc, char **argv) * * ========================================================= */ - // a Indi random initializer - eoUniformGenerator uGen; - eoInitFixedLength random(vecSize, uGen); - + eoInitPermutation init(vecSize); /* ========================================================= * @@ -119,20 +132,7 @@ void main_function(int argc, char **argv) * * ========================================================= */ - moFullEvalByModif fulleval(eval); - - //An eval by copy can be used instead of the eval by modif - //moFullEvalByCopy fulleval(eval); - - - /* ========================================================= - * - * Comparator of neighbors - * - * ========================================================= */ - - moSolNeighborComparator solComparator; - + moFullEvalByCopy shiftEval(fullEval); /* ========================================================= * @@ -140,7 +140,35 @@ void main_function(int argc, char **argv) * * ========================================================= */ - Neighborhood neighborhood(vecSize); + rndShiftNeighborhood rndShiftNH(pow(vecSize-1, 2)); + + /* ========================================================= + * + * the local search algorithm + * + * ========================================================= */ + + moSA localSearch1(rndShiftNH, fullEval, shiftEval); + + /* ========================================================= + * + * execute the local search from random solution + * + * ========================================================= */ + + Queen solution1, solution2; + + init(solution1); + + fullEval(solution1); + + std::cout << "#########################################" << std::endl; + std::cout << "initial solution1: " << solution1 << std::endl ; + + localSearch1(solution1); + + std::cout << "final solution1: " << solution1 << std::endl ; + std::cout << "#########################################" << std::endl; /* ========================================================= @@ -150,45 +178,44 @@ void main_function(int argc, char **argv) * ========================================================= */ // initial temp, factor of decrease, number of steps without decrease, final temp. - moSimpleCoolingSchedule coolingSchedule(10, 0.9, 1, 0.01); + moSimpleCoolingSchedule coolingSchedule(1, 0.9, 100, 0.01); /* ========================================================= * - * a neighborhood explorer solution + * Comparator of neighbors * * ========================================================= */ - moSAexplorer explorer(neighborhood, fulleval, solComparator, coolingSchedule); + moSolNeighborComparator solComparator; /* ========================================================= * - * the local search algorithm + * Example of Checkpointing * * ========================================================= */ - moTrueContinuator continuator;//always continue + moTrueContinuator continuator;//always continue + moCheckpoint checkpoint(continuator); + moFitnessStat fitStat; + checkpoint.add(fitStat); + eoFileMonitor monitor("fitness.out", ""); + moCounterMonitorSaver countMon(100, monitor); + checkpoint.add(countMon); + monitor.add(fitStat); - moLocalSearch localSearch(explorer, continuator, eval); + moSA localSearch2(rndShiftNH, fullEval, shiftEval, coolingSchedule, solComparator, checkpoint); - /* ========================================================= - * - * execute the local search from random sollution - * - * ========================================================= */ + init(solution2); - Indi solution; + fullEval(solution2); - random(solution); + std::cout << "#########################################" << std::endl; + std::cout << "initial solution2: " << solution2 << std::endl ; - //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 ; + localSearch2(solution2); + std::cout << "final solution2: " << solution2 << std::endl ; + std::cout << "#########################################" << std::endl; } // A main that catches the exceptions