From eec9e9fd5633e25bb431c2aca26df64f302859e4 Mon Sep 17 00:00:00 2001 From: LPTK Date: Fri, 19 Jul 2013 13:49:02 +0200 Subject: [PATCH] updated all mo tests --- mo/test/t-moFitnessVarianceStat.cpp | 51 ++++++- mo/test/t-moMetropolisHastings.cpp | 19 ++- mo/test/t-moMetropolisHastingsExplorer.cpp | 16 +- mo/test/t-moSA.cpp | 4 +- mo/test/t-moSAExplorer.cpp | 12 +- mo/test/t-moStdDevEstimator.cpp | 86 +++++++++++ mo/test/t-moTriki.cpp | 164 ++++++++++++++++++++ mo/test/t-moTrikiReal.cpp | 168 +++++++++++++++++++++ 8 files changed, 494 insertions(+), 26 deletions(-) diff --git a/mo/test/t-moFitnessVarianceStat.cpp b/mo/test/t-moFitnessVarianceStat.cpp index 3975338cc..66c66a015 100644 --- a/mo/test/t-moFitnessVarianceStat.cpp +++ b/mo/test/t-moFitnessVarianceStat.cpp @@ -1,5 +1,30 @@ +/* + +(c) Thales group, 2010 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; + version 2 of the License. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +Contact: http://eodev.sourceforge.net + +Authors: +Lionel Parreaux + +*/ + //----------------------------------------------------------------------------- -// t-moFitnessNeighborStat.cpp +// t-moFitnessVarianceStat.cpp //----------------------------------------------------------------------------- #include @@ -13,12 +38,30 @@ typedef eoReal< eoMinimizingFitness > EOT; typedef moRealNeighbor< EOT > Neighbor; +double objective_function(const EOT & sol) +{ + double sum = 0; + + for ( size_t i = 0; i < sol.size(); ++i ) + { + sum += sol[i] * sol[i]; + } + + return sum; +} + int main(int ac, char** av) { - //moNeighborhoodStat nhStat moFitnessVarianceStat stat; + eoEvalFuncPtr< EOT, double > eval( objective_function ); EOT solution(2, 5); + eval(solution); stat(solution); - //assert(stat.value() == 1); - std::cout << "ok " << stat.value() << std::endl; + solution[0] = solution[1] = 0; + solution.invalidate(); + eval(solution); + stat(solution); + std::cout << "var: " << stat.value() << std::endl; + assert(stat.value() == 625); + std::cout << "[t-moFitnessNeighborStat] => OK" << std::endl; } diff --git a/mo/test/t-moMetropolisHastings.cpp b/mo/test/t-moMetropolisHastings.cpp index 638c64bd8..48585780e 100644 --- a/mo/test/t-moMetropolisHastings.cpp +++ b/mo/test/t-moMetropolisHastings.cpp @@ -1,5 +1,5 @@ /* - + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau @@ -27,11 +27,15 @@ ParadisEO WebSite : http://paradiseo.gforge.inria.fr Contact: paradiseo-help@lists.gforge.inria.fr */ +//----------------------------------------------------------------------------- +// t-moMetropolisHastings.cpp +//----------------------------------------------------------------------------- + #include #include #include -#include +#include #include "moTestClass.h" #include #include @@ -40,25 +44,24 @@ Contact: paradiseo-help@lists.gforge.inria.fr int main() { - std::cout << "[t-moMetropolisHasting] => START" << std::endl; + std::cout << "[t-moMetropolisHastings] => START" << std::endl; bitNeighborhood nh(4); oneMaxEval fullEval; evalOneMax eval(4); moTrueContinuator cont; moSolNeighborComparator sncomp; - moNeighborComparator ncomp; //test du 1er constructeur - moMetropolisHasting test1(nh, fullEval, eval, 3); + moMetropolisHastings test1(nh, fullEval, eval, 3); //test du 2eme constructeur - moMetropolisHasting test2(nh, fullEval, eval, 3, cont); + moMetropolisHastings test2(nh, fullEval, eval, 3, cont); //test du 3eme constructeur - moMetropolisHasting test3(nh, fullEval, eval, 3, cont, ncomp, sncomp); + moMetropolisHastings test3(nh, fullEval, eval, 3, cont, sncomp); - std::cout << "[t-moMetropolisHasting] => OK" << std::endl; + std::cout << "[t-moMetropolisHastings] => OK" << std::endl; return EXIT_SUCCESS; } diff --git a/mo/test/t-moMetropolisHastingsExplorer.cpp b/mo/test/t-moMetropolisHastingsExplorer.cpp index 6214465a1..993106460 100644 --- a/mo/test/t-moMetropolisHastingsExplorer.cpp +++ b/mo/test/t-moMetropolisHastingsExplorer.cpp @@ -1,5 +1,5 @@ /* - + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau @@ -27,7 +27,11 @@ ParadisEO WebSite : http://paradiseo.gforge.inria.fr Contact: paradiseo-help@lists.gforge.inria.fr */ -#include +//----------------------------------------------------------------------------- +// t-moSimpleMetropolisHastingsExplorer.cpp +//----------------------------------------------------------------------------- + +#include #include "moTestClass.h" #include @@ -36,7 +40,7 @@ Contact: paradiseo-help@lists.gforge.inria.fr int main() { - std::cout << "[t-moMetropolisHastingExplorer] => START" << std::endl; + std::cout << "[t-moMetropolisHastingsExplorer] => START" << std::endl; //Instanciation eoBit sol(4, true); @@ -45,8 +49,8 @@ int main() { evalOneMax eval(4); moNeighborComparator ncomp; moSolNeighborComparator sncomp; - - moMetropolisHastingExplorer test(nh, eval, ncomp, sncomp, 3); + + moSimpleMetropolisHastingsExplorer test(nh, eval, 3, sncomp); //test de l'acceptation d'un voisin améliorant test.initParam(sol); @@ -90,7 +94,7 @@ int main() { test(sol); assert(!test.accept(sol)); - std::cout << "[t-moMetropolisHastingExplorer] => OK" << std::endl; + std::cout << "[t-moMetropolisHastingsExplorer] => OK" << std::endl; return EXIT_SUCCESS; } diff --git a/mo/test/t-moSA.cpp b/mo/test/t-moSA.cpp index b1cdfeb67..3c67865cb 100644 --- a/mo/test/t-moSA.cpp +++ b/mo/test/t-moSA.cpp @@ -51,12 +51,12 @@ int main() { //test second constructor moSimpleCoolingSchedule cool(10, 0.9, 100, 0.01); - moSA test2(nh, fullEval, eval, cool); + moSA test2(nh, fullEval, cool, eval); //test third constructor moTrueContinuator cont; moSolNeighborComparator comp; - moSA test3(nh, fullEval, eval, cool, comp, cont); + moSA test3(nh, fullEval, cool, eval, cont, comp); std::cout << "[t-moSA] => OK" << std::endl; diff --git a/mo/test/t-moSAExplorer.cpp b/mo/test/t-moSAExplorer.cpp index 4c619ff88..873dbf080 100644 --- a/mo/test/t-moSAExplorer.cpp +++ b/mo/test/t-moSAExplorer.cpp @@ -1,5 +1,5 @@ /* - + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau @@ -32,12 +32,12 @@ Contact: paradiseo-help@lists.gforge.inria.fr #include #include "moTestClass.h" -#include +#include #include int main() { - std::cout << "[t-moSAexplorer] => START" << std::endl; + std::cout << "[t-moSAExplorer] => START" << std::endl; eoBit sol(4, true); sol.fitness(4); @@ -47,8 +47,8 @@ int main() { moSolNeighborComparator sncomp; moSimpleCoolingSchedule cool(10,0.1,2,0.1); - moSAexplorer test1(emptyNH, eval, sncomp, cool); - moSAexplorer test2(nh, eval, sncomp, cool); + moSAExplorer test1(emptyNH, eval, cool, sncomp); + moSAExplorer test2(nh, eval, cool, sncomp); //test d'un voisinage vide test1.initParam(sol); @@ -80,7 +80,7 @@ int main() { - std::cout << "[t-moSAexplorer] => OK" << std::endl; + std::cout << "[t-moSAExplorer] => OK" << std::endl; return EXIT_SUCCESS; } diff --git a/mo/test/t-moStdDevEstimator.cpp b/mo/test/t-moStdDevEstimator.cpp index e69de29bb..9cfbe0930 100644 --- a/mo/test/t-moStdDevEstimator.cpp +++ b/mo/test/t-moStdDevEstimator.cpp @@ -0,0 +1,86 @@ +/* + +(c) Thales group, 2010 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; + version 2 of the License. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +Contact: http://eodev.sourceforge.net + +Authors: +Lionel Parreaux + +*/ + +//----------------------------------------------------------------------------- +// t-moStdDevEstimator.cpp +//----------------------------------------------------------------------------- + +#include +#include "es/eoReal.h" +#include "neighborhood/moRealNeighbor.h" + +//Representation and initializer +#include +//#include +#include + +// fitness function +#include + +//Neighbors and Neighborhoods +#include +#include + +//Sampling +#include + + +//----------------------------------------------------------------------------- +// 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) + +//----------------------------------------------------------------------------- + +typedef eoReal< eoMinimizingFitness > EOT; +typedef moRealNeighbor< EOT > Neighbor; + +int main(int ac, char** av) +{ + unsigned vecSize = 8; + + queenEval fullEval; + + eoInitPermutation init(vecSize); + + rndShiftNeighborhood rndShiftNH((vecSize-1) * (vecSize-1)); + + Queen solution; + + init(solution); + + fullEval(solution); + + moStdDevEstimator initTemp (500, rndShiftNH, fullEval); + + double temp = initTemp(solution); + + std::cout << "temp: " << temp << std::endl; + + assert(temp >= 0); +} diff --git a/mo/test/t-moTriki.cpp b/mo/test/t-moTriki.cpp index e69de29bb..14f4b0198 100644 --- a/mo/test/t-moTriki.cpp +++ b/mo/test/t-moTriki.cpp @@ -0,0 +1,164 @@ +//============================================================================ +// Name : Trikitest.cpp +// Author : +// Version : +// Copyright : Your copyright notice +// Description : Hello World in C++, Ansi-style +//============================================================================ + +// FIXME proper header + + +//#define HAVE_GNUPLOT + +#include +using namespace std; + +#include +#include + +//Representation and initializer +#include +#include +#include +#include + +/* +// fitness function +#include +#include +#include + +//Neighbors and Neighborhoods +#include +#include + +//Algorithm and its components +#include +#include + +//comparator +#include + +//continuators +#include +#include +#include +#include +#include +*/ + +//----------------------------------------------------------------------------- +// 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) + + + + +int main() { + //cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!! + //return 0; + + unsigned vecSize = 8; + + + queenEval fullEval; + + + /* ========================================================= + * + * Initilization of the solution + * + * ========================================================= */ + + eoInitPermutation init(vecSize); + + /* ========================================================= + * + * evaluation of a neighbor solution + * + * ========================================================= */ + + moFullEvalByCopy shiftEval(fullEval); /// by default + + /* ========================================================= + * + * the neighborhood of a solution + * + * ========================================================= */ + + rndShiftNeighborhood rndShiftNH((vecSize-1) * (vecSize-1)); + + /* ========================================================= + * + * the cooling schedule of the process + * + * ========================================================= */ + + /* ========================================================= + * + * the local search algorithm + * + * ========================================================= */ + + /* ========================================================= + * + * execute the local search from random solution + * + * ========================================================= */ + + Queen solution; + + init(solution); + + fullEval(solution); + + //moStdDevEstimator stdDevEst (500, rndShiftNH, fullEval); + double stdDevEst = moStdDevEstimator(500, rndShiftNH, fullEval)(solution); + + moTrueContinuator continuator; + moCheckpoint checkpoint(continuator); + moFitnessStat fitStat; + checkpoint.add(fitStat); + eoFileMonitor monitor("triki.out", ""); + eoGnuplot1DMonitor monitor2("trikignu.out", true); + moCounterMonitorSaver countMon(100, monitor); + checkpoint.add(countMon); + moCounterMonitorSaver gnuMon (10, monitor2); + checkpoint.add(gnuMon); + monitor.add(fitStat); + monitor2.add(fitStat); + //#ifdef HAVE_GNUPLOT + + + //moTrikiCoolingSchedule coolingSchedule(rndShiftNH, shiftEval, initTemp(solution1)); + //moTrikiCoolingSchedule coolingSchedule(initTemp(solution)); + moTrikiCoolingSchedule coolingSchedule(stdDevEst, stdDevEst); + moSA localSearch(rndShiftNH, fullEval, coolingSchedule, shiftEval, checkpoint); + //moSA localSearch(rndShiftNH, fullEval, shiftEval); + //moSA localSearch(rndShiftNH, fullEval, coolingSchedule); + + + std::cout << "#########################################" << std::endl; + std::cout << "initial solution: " << solution << std::endl ; + + localSearch(solution); + + std::cout << "final solution: " << solution << std::endl ; + std::cout << "#########################################" << std::endl; + + + +} + + + + + + + + diff --git a/mo/test/t-moTrikiReal.cpp b/mo/test/t-moTrikiReal.cpp index e69de29bb..a158b3250 100644 --- a/mo/test/t-moTrikiReal.cpp +++ b/mo/test/t-moTrikiReal.cpp @@ -0,0 +1,168 @@ +/* + +(c) Thales group, 2010 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; + version 2 of the License. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +Contact: http://eodev.sourceforge.net + +Authors: +Lionel Parreaux + +*/ + +#include +#include +#include +#include + +#include + +#include "neighborhood/moRealNeighbor.h" +#include "neighborhood/moRealNeighborhood.h" + +#include "sampling/moStdDevEstimator.h" + +#include "coolingSchedule/moTrikiCoolingSchedule.h" + + +typedef eoReal< eoMinimizingFitness > EOT; +typedef moRealNeighbor< EOT > Neighbor; + +double objective_function(const EOT & sol) +{ + double sum = 0; + + for ( size_t i = 0; i < sol.size(); ++i ) + { + sum += sol[i] * sol[i]; + } + + return sum; +} + +int main( int ac, char** av ) +{ + eoParser parser( ac, av ); + + eoState state; + + eoEvalFuncPtr< EOT, double > eval( objective_function ); + moFullEvalByCopy< Neighbor > neval( eval ); + + int dimSize = 15; + + //------------------------------------------------------- + // Parameters + //------------------------------------------------------- + + std::string section( "Temperature initialization paramaters" ); + + unsigned int dimension_size = parser.getORcreateParam( (unsigned int)dimSize, "dimension-size", "Dimension size", 'd', section ).value(); + double jump_bound = parser.getORcreateParam( (double)1, "jump-bound", "Bound of jump", '\0', section ).value(); + unsigned int maxiter = parser.getORcreateParam( (unsigned int)10, "max-iter", "Maximum number of iterations", '\0', section ).value(); + + //------------------------------------------------------- + + + //------------------------------------------------------- + // Instanciate needed classes + //------------------------------------------------------- + + edoUniform< EOT > distrib( EOT(dimension_size, -1 * jump_bound), EOT(dimension_size, 1 * jump_bound) ); + + edoBounder< EOT > bounder_search( EOT(dimension_size, -10), EOT(dimension_size, 10) ); + + edoSamplerUniform< EOT > sampler( bounder_search ); + + moRealNeighborhood< edoUniform< EOT >, Neighbor > neighborhood( distrib, sampler, bounder_search ); + + moStdDevEstimator< EOT, Neighbor > init( maxiter, neighborhood, eval ); + + //------------------------------------------------------- + + + + + //------------------------------------------------------- + // Help + Verbose routines + //------------------------------------------------------- + + if (parser.userNeedsHelp()) + { + parser.printHelp(std::cout); + exit(1); + } + + make_help(parser); + + //------------------------------------------------------- + + + EOT solution(dimSize, 5); + + std::cout << "init temp: " + << init( solution ) + << std::endl; + + + + + moTrueContinuator continuator; + moCheckpoint checkpoint(continuator); + + + moFitnessStat fitStat; + checkpoint.add(fitStat); + eoFileMonitor monitor("triki.out", ""); + //eoGnuplot1DMonitor monitor2("trikignu.out", true); + moCounterMonitorSaver countMon(100, monitor); + checkpoint.add(countMon); + //moCounterMonitorSaver gnuMon (1, monitor2); + //checkpoint.add(gnuMon); + monitor.add(fitStat); + //monitor2.add(fitStat); + + + double stdDevEst = init( solution ); + + moTrikiCoolingSchedule coolingSchedule ( + stdDevEst, + stdDevEst, + //50, + 150,//200, //150, + //100 + 250//350 // 250 + ); + //moSA localSearch(neighborhood, eval, neval, coolingSchedule, checkpoint); + //moSA localSearch(neighborhood, eval, neval); + //moSA localSearch(neighborhood, eval, coolingSchedule, neval, checkpoint); + //moSA localSearch(neighborhood, eval, coolingSchedule); + moSA localSearch(neighborhood, eval, coolingSchedule, neval, checkpoint); + + std::cout << "#########################################" << std::endl; + std::cout << "initial solution1: " << solution << std::endl ; + + localSearch(solution); + + std::cout << "final solution1: " << solution << std::endl ; + std::cout << "#########################################" << std::endl; + + + return 0; +} + + +